tar archive frontend for fast-import.
This is an example fast-import frontend, in less than 100 lines of Perl. It accepts one or more tar archives on the command line, passes them through gzcat/bzcat/zcat if necessary, parses out the individual file headers and feeds all contained data to fast-import. No temporary files are involved. Each tar is treated as one commit, with the commit timestamp coming from the oldest file modification date found within the tar. Each tar is also tagged with an annotated tag, using the basename of the tar file as the name of the tag. Currently symbolic links and hard links are not handled by the importer. The file checksums are also not verified. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Shawn O. Pearce committed
Feb 8, 2007 at 15:26 UTC
590dd4bfd25b3c7592004258384f7541d9315712
1 file changed
+105
contrib/fast-import/import-tars.perl
new
+105
@@ -0,0 +1,105 @@
1
+#!/usr/bin/perl
2
+
3
+## tar archive frontend for git-fast-import
4
+##
5
+## For example:
6
+##
7
+## mkdir project; cd project; git init
8
+## perl import-tars.perl *.tar.bz2
9
+## git whatchanged import-tars
10
+##
11
+
12
+use strict;
13
+die "usage: import-tars *.tar.{gz,bz2,Z}\n" unless @ARGV;
14
+
15
+my $branch_name = 'import-tars';
16
+my $branch_ref = "refs/heads/$branch_name";
17
+my $committer_name = 'T Ar Creator';
18
+my $committer_email = 'tar@example.com';
19
+
20
+open(FI, '|-', 'git', 'fast-import', '--quiet')
21
+ or die "Unable to start git fast-import: $!\n";
22
+foreach my $tar_file (@ARGV)
23
+{
24
+ $tar_file =~ m,([^/]+)$,;
25
+ my $tar_name = $1;
26
+
27
+ if ($tar_name =~ s/\.(tar\.gz|tgz)$//) {
28
+ open(I, '-|', 'gzcat', $tar_file) or die "Unable to gzcat $tar_file: $!\n";
29
+ } elsif ($tar_name =~ s/\.(tar\.bz2|tbz2)$//) {
30
+ open(I, '-|', 'bzcat', $tar_file) or die "Unable to bzcat $tar_file: $!\n";
31
+ } elsif ($tar_name =~ s/\.tar\.Z$//) {
32
+ open(I, '-|', 'zcat', $tar_file) or die "Unable to zcat $tar_file: $!\n";
33
+ } elsif ($tar_name =~ s/\.tar$//) {
34
+ open(I, $tar_file) or die "Unable to open $tar_file: $!\n";
35
+ } else {
36
+ die "Unrecognized compression format: $tar_file\n";
37
+ }
38
+
39
+ my $commit_time = 0;
40
+ my $next_mark = 1;
41
+ my $have_top_dir = 1;
42
+ my ($top_dir, %files);
43
+
44
+ while (read(I, $_, 512) == 512) {
45
+ my ($name, $mode, $uid, $gid, $size, $mtime,
46
+ $chksum, $typeflag, $linkname, $magic,
47
+ $version, $uname, $gname, $devmajor, $devminor,
48
+ $prefix) = unpack 'Z100 Z8 Z8 Z8 Z12 Z12
49
+ Z8 Z1 Z100 Z6
50
+ Z2 Z32 Z32 Z8 Z8 Z*', $_;
51
+ last unless $name;
52
+ $mode = oct $mode;
53
+ $size = oct $size;
54
+ $mtime = oct $mtime;
55
+ next if $mode & 0040000;
56
+
57
+ print FI "blob\n", "mark :$next_mark\n", "data $size\n";
58
+ while ($size > 0 && read(I, $_, 512) == 512) {
59
+ print FI substr($_, 0, $size);
60
+ $size -= 512;
61
+ }
62
+ print FI "\n";
63
+
64
+ my $path = "$prefix$name";
65
+ $files{$path} = [$next_mark++, $mode];
66
+
67
+ $commit_time = $mtime if $mtime > $commit_time;
68
+ $path =~ m,^([^/]+)/,;
69
+ $top_dir = $1 unless $top_dir;
70
+ $have_top_dir = 0 if $top_dir ne $1;
71
+ }
72
+
73
+ print FI <<EOF;
74
+commit $branch_ref
75
+committer $committer_name <$committer_email> $commit_time +0000
76
+data <<END_OF_COMMIT_MESSAGE
77
+Imported from $tar_file.
78
+END_OF_COMMIT_MESSAGE
79
+
80
+deleteall
81
+EOF
82
+
83
+ foreach my $path (keys %files)
84
+ {
85
+ my ($mark, $mode) = @{$files{$path}};
86
+ my $git_mode = 0644;
87
+ $git_mode |= 0700 if $mode & 0111;
88
+ $path =~ s,^([^/]+)/,, if $have_top_dir;
89
+ printf FI "M %o :%i %s\n", $git_mode, $mark, $path;
90
+ }
91
+ print FI "\n";
92
+
93
+ print FI <<EOF;
94
+tag $tar_name
95
+from $branch_ref
96
+tagger $committer_name <$committer_email> $commit_time +0000
97
+data <<END_OF_TAG_MESSAGE
98
+Package $tar_name
99
+END_OF_TAG_MESSAGE
100
+
101
+EOF
102
+
103
+ close I;
104
+}
105
+close FI;