contrib / fast-import / import-tars.perlon commit Sync with 1.6.2.2 (8130949)
   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
  12use strict;
  13die "usage: import-tars *.tar.{gz,bz2,Z}\n" unless @ARGV;
  14
  15my $branch_name = 'import-tars';
  16my $branch_ref = "refs/heads/$branch_name";
  17my $author_name = $ENV{'GIT_AUTHOR_NAME'} || 'T Ar Creator';
  18my $author_email = $ENV{'GIT_AUTHOR_EMAIL'} || 'tar@example.com';
  19my $committer_name = $ENV{'GIT_COMMITTER_NAME'} || `git config --get user.name`;
  20my $committer_email = $ENV{'GIT_COMMITTER_EMAIL'} || `git config --get user.email`;
  21
  22chomp($committer_name, $committer_email);
  23
  24open(FI, '|-', 'git', 'fast-import', '--quiet')
  25        or die "Unable to start git fast-import: $!\n";
  26foreach my $tar_file (@ARGV)
  27{
  28        my $commit_time = time;
  29        $tar_file =~ m,([^/]+)$,;
  30        my $tar_name = $1;
  31
  32        if ($tar_name =~ s/\.(tar\.gz|tgz)$//) {
  33                open(I, '-|', 'gunzip', '-c', $tar_file)
  34                        or die "Unable to gunzip -c $tar_file: $!\n";
  35        } elsif ($tar_name =~ s/\.(tar\.bz2|tbz2)$//) {
  36                open(I, '-|', 'bunzip2', '-c', $tar_file)
  37                        or die "Unable to bunzip2 -c $tar_file: $!\n";
  38        } elsif ($tar_name =~ s/\.tar\.Z$//) {
  39                open(I, '-|', 'uncompress', '-c', $tar_file)
  40                        or die "Unable to uncompress -c $tar_file: $!\n";
  41        } elsif ($tar_name =~ s/\.tar$//) {
  42                open(I, $tar_file) or die "Unable to open $tar_file: $!\n";
  43        } else {
  44                die "Unrecognized compression format: $tar_file\n";
  45        }
  46
  47        my $author_time = 0;
  48        my $next_mark = 1;
  49        my $have_top_dir = 1;
  50        my ($top_dir, %files);
  51
  52        while (read(I, $_, 512) == 512) {
  53                my ($name, $mode, $uid, $gid, $size, $mtime,
  54                        $chksum, $typeflag, $linkname, $magic,
  55                        $version, $uname, $gname, $devmajor, $devminor,
  56                        $prefix) = unpack 'Z100 Z8 Z8 Z8 Z12 Z12
  57                        Z8 Z1 Z100 Z6
  58                        Z2 Z32 Z32 Z8 Z8 Z*', $_;
  59                last unless length($name);
  60                if ($name eq '././@LongLink') {
  61                        # GNU tar extension
  62                        if (read(I, $_, 512) != 512) {
  63                                die ('Short archive');
  64                        }
  65                        $name = unpack 'Z257', $_;
  66                        next unless $name;
  67
  68                        my $dummy;
  69                        if (read(I, $_, 512) != 512) {
  70                                die ('Short archive');
  71                        }
  72                        ($dummy, $mode, $uid, $gid, $size, $mtime,
  73                        $chksum, $typeflag, $linkname, $magic,
  74                        $version, $uname, $gname, $devmajor, $devminor,
  75                        $prefix) = unpack 'Z100 Z8 Z8 Z8 Z12 Z12
  76                        Z8 Z1 Z100 Z6
  77                        Z2 Z32 Z32 Z8 Z8 Z*', $_;
  78                }
  79                next if $name =~ m{/\z};
  80                $mode = oct $mode;
  81                $size = oct $size;
  82                $mtime = oct $mtime;
  83                next if $typeflag == 5; # directory
  84
  85                print FI "blob\n", "mark :$next_mark\n", "data $size\n";
  86                while ($size > 0 && read(I, $_, 512) == 512) {
  87                        print FI substr($_, 0, $size);
  88                        $size -= 512;
  89                }
  90                print FI "\n";
  91
  92                my $path;
  93                if ($prefix) {
  94                        $path = "$prefix/$name";
  95                } else {
  96                        $path = "$name";
  97                }
  98                $files{$path} = [$next_mark++, $mode];
  99
 100                $author_time = $mtime if $mtime > $author_time;
 101                $path =~ m,^([^/]+)/,;
 102                $top_dir = $1 unless $top_dir;
 103                $have_top_dir = 0 if $top_dir ne $1;
 104        }
 105
 106        print FI <<EOF;
 107commit $branch_ref
 108author $author_name <$author_email> $author_time +0000
 109committer $committer_name <$committer_email> $commit_time +0000
 110data <<END_OF_COMMIT_MESSAGE
 111Imported from $tar_file.
 112END_OF_COMMIT_MESSAGE
 113
 114deleteall
 115EOF
 116
 117        foreach my $path (keys %files)
 118        {
 119                my ($mark, $mode) = @{$files{$path}};
 120                $path =~ s,^([^/]+)/,, if $have_top_dir;
 121                printf FI "M %o :%i %s\n", $mode & 0111 ? 0755 : 0644, $mark, $path;
 122        }
 123        print FI "\n";
 124
 125        print FI <<EOF;
 126tag $tar_name
 127from $branch_ref
 128tagger $author_name <$author_email> $author_time +0000
 129data <<END_OF_TAG_MESSAGE
 130Package $tar_name
 131END_OF_TAG_MESSAGE
 132
 133EOF
 134
 135        close I;
 136}
 137close FI;