3 ## tar archive frontend for git-fast-import
7 ## mkdir project; cd project; git init
8 ## perl import-tars.perl *.tar.bz2
9 ## git whatchanged import-tars
13 die "usage: import-tars *.tar.{gz,bz2,Z}\n" unless @ARGV;
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';
20 open(FI, '|-', 'git', 'fast-import', '--quiet')
21 or die "Unable to start git fast-import: $!\n";
22 foreach my $tar_file (@ARGV)
24 $tar_file =~ m,([^/]+)$,;
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";
36 die "Unrecognized compression format: $tar_file\n";
42 my ($top_dir, %files);
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
50 Z2 Z32 Z32 Z8 Z8 Z*', $_;
55 next if $mode & 0040000;
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);
64 my $path = "$prefix$name";
65 $files{$path} = [$next_mark++, $mode];
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;
75 committer $committer_name <$committer_email> $commit_time +0000
76 data <<END_OF_COMMIT_MESSAGE
77 Imported from $tar_file.
83 foreach my $path (keys %files)
85 my ($mark, $mode) = @{$files{$path}};
86 $path =~ s,^([^/]+)/,, if $have_top_dir;
87 printf FI "M %o :%i %s\n", $mode & 0111 ? 0755 : 0644, $mark, $path;
94 tagger $committer_name <$committer_email> $commit_time +0000
95 data <<END_OF_TAG_MESSAGE