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