contrib / fast-import / import-tars.perlon commit Merge branch 'os/gitweb-highlight-uncaptured' (fd6db67)
   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## Use --metainfo to specify the extension for a meta data file, where
  12## import-tars can read the commit message and optionally author and
  13## committer information.
  14##
  15##  echo 'This is the commit message' > myfile.tar.bz2.msg
  16##  perl import-tars.perl --metainfo=msg myfile.tar.bz2
  17
  18use strict;
  19use Getopt::Long;
  20
  21my $metaext = '';
  22
  23die "usage: import-tars [--metainfo=extension] *.tar.{gz,bz2,lzma,xz,Z}\n"
  24        unless GetOptions('metainfo=s' => \$metaext) && @ARGV;
  25
  26my $branch_name = 'import-tars';
  27my $branch_ref = "refs/heads/$branch_name";
  28my $author_name = $ENV{'GIT_AUTHOR_NAME'} || 'T Ar Creator';
  29my $author_email = $ENV{'GIT_AUTHOR_EMAIL'} || 'tar@example.com';
  30my $committer_name = $ENV{'GIT_COMMITTER_NAME'} || `git config --get user.name`;
  31my $committer_email = $ENV{'GIT_COMMITTER_EMAIL'} || `git config --get user.email`;
  32
  33chomp($committer_name, $committer_email);
  34
  35open(FI, '|-', 'git', 'fast-import', '--quiet')
  36        or die "Unable to start git fast-import: $!\n";
  37foreach my $tar_file (@ARGV)
  38{
  39        my $commit_time = time;
  40        $tar_file =~ m,([^/]+)$,;
  41        my $tar_name = $1;
  42
  43        if ($tar_name =~ s/\.(tar\.gz|tgz)$//) {
  44                open(I, '-|', 'gunzip', '-c', $tar_file)
  45                        or die "Unable to gunzip -c $tar_file: $!\n";
  46        } elsif ($tar_name =~ s/\.(tar\.bz2|tbz2)$//) {
  47                open(I, '-|', 'bunzip2', '-c', $tar_file)
  48                        or die "Unable to bunzip2 -c $tar_file: $!\n";
  49        } elsif ($tar_name =~ s/\.tar\.Z$//) {
  50                open(I, '-|', 'uncompress', '-c', $tar_file)
  51                        or die "Unable to uncompress -c $tar_file: $!\n";
  52        } elsif ($tar_name =~ s/\.(tar\.(lzma|xz)|(tlz|txz))$//) {
  53                open(I, '-|', 'xz', '-dc', $tar_file)
  54                        or die "Unable to xz -dc $tar_file: $!\n";
  55        } elsif ($tar_name =~ s/\.tar$//) {
  56                open(I, $tar_file) or die "Unable to open $tar_file: $!\n";
  57        } else {
  58                die "Unrecognized compression format: $tar_file\n";
  59        }
  60
  61        my $author_time = 0;
  62        my $next_mark = 1;
  63        my $have_top_dir = 1;
  64        my ($top_dir, %files);
  65
  66        while (read(I, $_, 512) == 512) {
  67                my ($name, $mode, $uid, $gid, $size, $mtime,
  68                        $chksum, $typeflag, $linkname, $magic,
  69                        $version, $uname, $gname, $devmajor, $devminor,
  70                        $prefix) = unpack 'Z100 Z8 Z8 Z8 Z12 Z12
  71                        Z8 Z1 Z100 Z6
  72                        Z2 Z32 Z32 Z8 Z8 Z*', $_;
  73                last unless length($name);
  74                if ($name eq '././@LongLink') {
  75                        # GNU tar extension
  76                        if (read(I, $_, 512) != 512) {
  77                                die ('Short archive');
  78                        }
  79                        $name = unpack 'Z257', $_;
  80                        next unless $name;
  81
  82                        my $dummy;
  83                        if (read(I, $_, 512) != 512) {
  84                                die ('Short archive');
  85                        }
  86                        ($dummy, $mode, $uid, $gid, $size, $mtime,
  87                        $chksum, $typeflag, $linkname, $magic,
  88                        $version, $uname, $gname, $devmajor, $devminor,
  89                        $prefix) = unpack 'Z100 Z8 Z8 Z8 Z12 Z12
  90                        Z8 Z1 Z100 Z6
  91                        Z2 Z32 Z32 Z8 Z8 Z*', $_;
  92                }
  93                next if $name =~ m{/\z};
  94                $mode = oct $mode;
  95                $size = oct $size;
  96                $mtime = oct $mtime;
  97                next if $typeflag == 5; # directory
  98
  99                print FI "blob\n", "mark :$next_mark\n";
 100                if ($typeflag == 2) { # symbolic link
 101                        print FI "data ", length($linkname), "\n", $linkname;
 102                        $mode = 0120000;
 103                } else {
 104                        print FI "data $size\n";
 105                        while ($size > 0 && read(I, $_, 512) == 512) {
 106                                print FI substr($_, 0, $size);
 107                                $size -= 512;
 108                        }
 109                }
 110                print FI "\n";
 111
 112                my $path;
 113                if ($prefix) {
 114                        $path = "$prefix/$name";
 115                } else {
 116                        $path = "$name";
 117                }
 118                $files{$path} = [$next_mark++, $mode];
 119
 120                $author_time = $mtime if $mtime > $author_time;
 121                $path =~ m,^([^/]+)/,;
 122                $top_dir = $1 unless $top_dir;
 123                $have_top_dir = 0 if $top_dir ne $1;
 124        }
 125
 126        my $commit_msg = "Imported from $tar_file.";
 127        my $this_committer_name = $committer_name;
 128        my $this_committer_email = $committer_email;
 129        my $this_author_name = $author_name;
 130        my $this_author_email = $author_email;
 131        if ($metaext ne '') {
 132                # Optionally read a commit message from <filename.tar>.msg
 133                # Add a line on the form "Committer: name <e-mail>" to override
 134                # the committer and "Author: name <e-mail>" to override the
 135                # author for this tar ball.
 136                if (open MSG, '<', "${tar_file}.${metaext}") {
 137                        my $header_done = 0;
 138                        $commit_msg = '';
 139                        while (<MSG>) {
 140                                if (!$header_done && /^Committer:\s+([^<>]*)\s+<(.*)>\s*$/i) {
 141                                        $this_committer_name = $1;
 142                                        $this_committer_email = $2;
 143                                } elsif (!$header_done && /^Author:\s+([^<>]*)\s+<(.*)>\s*$/i) {
 144                                        $this_author_name = $1;
 145                                        $this_author_email = $2;
 146                                } elsif (!$header_done && /^$/) { # empty line ends header.
 147                                        $header_done = 1;
 148                                } else {
 149                                        $commit_msg .= $_;
 150                                        $header_done = 1;
 151                                }
 152                        }
 153                        close MSG;
 154                }
 155        }
 156
 157        print FI <<EOF;
 158commit $branch_ref
 159author $this_author_name <$this_author_email> $author_time +0000
 160committer $this_committer_name <$this_committer_email> $commit_time +0000
 161data <<END_OF_COMMIT_MESSAGE
 162$commit_msg
 163END_OF_COMMIT_MESSAGE
 164
 165deleteall
 166EOF
 167
 168        foreach my $path (keys %files)
 169        {
 170                my ($mark, $mode) = @{$files{$path}};
 171                $path =~ s,^([^/]+)/,, if $have_top_dir;
 172                $mode = $mode & 0111 ? 0755 : 0644 unless $mode == 0120000;
 173                printf FI "M %o :%i %s\n", $mode, $mark, $path;
 174        }
 175        print FI "\n";
 176
 177        print FI <<EOF;
 178tag $tar_name
 179from $branch_ref
 180tagger $author_name <$author_email> $author_time +0000
 181data <<END_OF_TAG_MESSAGE
 182Package $tar_name
 183END_OF_TAG_MESSAGE
 184
 185EOF
 186
 187        close I;
 188}
 189close FI;