2007-02-08 20:26:01 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
## tar archive frontend for git-fast-import
|
|
|
|
##
|
|
|
|
## For example:
|
|
|
|
##
|
|
|
|
## mkdir project; cd project; git init
|
|
|
|
## perl import-tars.perl *.tar.bz2
|
|
|
|
## git whatchanged import-tars
|
|
|
|
##
|
2009-09-03 12:15:00 +00:00
|
|
|
## Use --metainfo to specify the extension for a meta data file, where
|
|
|
|
## import-tars can read the commit message and optionally author and
|
|
|
|
## committer information.
|
|
|
|
##
|
|
|
|
## echo 'This is the commit message' > myfile.tar.bz2.msg
|
|
|
|
## perl import-tars.perl --metainfo=msg myfile.tar.bz2
|
2007-02-08 20:26:01 +00:00
|
|
|
|
|
|
|
use strict;
|
2009-09-03 12:15:00 +00:00
|
|
|
use Getopt::Long;
|
|
|
|
|
|
|
|
my $metaext = '';
|
|
|
|
|
2009-10-20 10:29:32 +00:00
|
|
|
die "usage: import-tars [--metainfo=extension] *.tar.{gz,bz2,lzma,xz,Z}\n"
|
2009-09-03 12:15:00 +00:00
|
|
|
unless GetOptions('metainfo=s' => \$metaext) && @ARGV;
|
2007-02-08 20:26:01 +00:00
|
|
|
|
|
|
|
my $branch_name = 'import-tars';
|
|
|
|
my $branch_ref = "refs/heads/$branch_name";
|
2009-03-20 09:57:50 +00:00
|
|
|
my $author_name = $ENV{'GIT_AUTHOR_NAME'} || 'T Ar Creator';
|
|
|
|
my $author_email = $ENV{'GIT_AUTHOR_EMAIL'} || 'tar@example.com';
|
|
|
|
my $committer_name = $ENV{'GIT_COMMITTER_NAME'} || `git config --get user.name`;
|
|
|
|
my $committer_email = $ENV{'GIT_COMMITTER_EMAIL'} || `git config --get user.email`;
|
|
|
|
|
|
|
|
chomp($committer_name, $committer_email);
|
2007-02-08 20:26:01 +00:00
|
|
|
|
|
|
|
open(FI, '|-', 'git', 'fast-import', '--quiet')
|
|
|
|
or die "Unable to start git fast-import: $!\n";
|
|
|
|
foreach my $tar_file (@ARGV)
|
|
|
|
{
|
2009-03-20 09:57:50 +00:00
|
|
|
my $commit_time = time;
|
2007-02-08 20:26:01 +00:00
|
|
|
$tar_file =~ m,([^/]+)$,;
|
|
|
|
my $tar_name = $1;
|
|
|
|
|
|
|
|
if ($tar_name =~ s/\.(tar\.gz|tgz)$//) {
|
2007-02-14 16:03:12 +00:00
|
|
|
open(I, '-|', 'gunzip', '-c', $tar_file)
|
|
|
|
or die "Unable to gunzip -c $tar_file: $!\n";
|
2007-02-08 20:26:01 +00:00
|
|
|
} elsif ($tar_name =~ s/\.(tar\.bz2|tbz2)$//) {
|
2007-02-14 16:03:12 +00:00
|
|
|
open(I, '-|', 'bunzip2', '-c', $tar_file)
|
|
|
|
or die "Unable to bunzip2 -c $tar_file: $!\n";
|
2007-02-08 20:26:01 +00:00
|
|
|
} elsif ($tar_name =~ s/\.tar\.Z$//) {
|
2007-02-14 16:03:12 +00:00
|
|
|
open(I, '-|', 'uncompress', '-c', $tar_file)
|
|
|
|
or die "Unable to uncompress -c $tar_file: $!\n";
|
2009-10-20 10:29:32 +00:00
|
|
|
} elsif ($tar_name =~ s/\.(tar\.(lzma|xz)|(tlz|txz))$//) {
|
|
|
|
open(I, '-|', 'xz', '-dc', $tar_file)
|
|
|
|
or die "Unable to xz -dc $tar_file: $!\n";
|
2007-02-08 20:26:01 +00:00
|
|
|
} elsif ($tar_name =~ s/\.tar$//) {
|
|
|
|
open(I, $tar_file) or die "Unable to open $tar_file: $!\n";
|
|
|
|
} else {
|
|
|
|
die "Unrecognized compression format: $tar_file\n";
|
|
|
|
}
|
|
|
|
|
2009-03-20 09:57:50 +00:00
|
|
|
my $author_time = 0;
|
2007-02-08 20:26:01 +00:00
|
|
|
my $next_mark = 1;
|
|
|
|
my $have_top_dir = 1;
|
|
|
|
my ($top_dir, %files);
|
|
|
|
|
2018-05-23 22:54:17 +00:00
|
|
|
my $next_path = '';
|
|
|
|
|
2007-02-08 20:26:01 +00:00
|
|
|
while (read(I, $_, 512) == 512) {
|
|
|
|
my ($name, $mode, $uid, $gid, $size, $mtime,
|
|
|
|
$chksum, $typeflag, $linkname, $magic,
|
|
|
|
$version, $uname, $gname, $devmajor, $devminor,
|
|
|
|
$prefix) = unpack 'Z100 Z8 Z8 Z8 Z12 Z12
|
|
|
|
Z8 Z1 Z100 Z6
|
|
|
|
Z2 Z32 Z32 Z8 Z8 Z*', $_;
|
2018-05-23 22:54:17 +00:00
|
|
|
|
|
|
|
unless ($next_path eq '') {
|
|
|
|
# Recover name from previous extended header
|
|
|
|
$name = $next_path;
|
|
|
|
$next_path = '';
|
|
|
|
}
|
|
|
|
|
2007-05-08 01:13:40 +00:00
|
|
|
last unless length($name);
|
2007-05-01 21:42:44 +00:00
|
|
|
if ($name eq '././@LongLink') {
|
|
|
|
# GNU tar extension
|
|
|
|
if (read(I, $_, 512) != 512) {
|
|
|
|
die ('Short archive');
|
|
|
|
}
|
|
|
|
$name = unpack 'Z257', $_;
|
|
|
|
next unless $name;
|
|
|
|
|
|
|
|
my $dummy;
|
|
|
|
if (read(I, $_, 512) != 512) {
|
|
|
|
die ('Short archive');
|
|
|
|
}
|
|
|
|
($dummy, $mode, $uid, $gid, $size, $mtime,
|
|
|
|
$chksum, $typeflag, $linkname, $magic,
|
|
|
|
$version, $uname, $gname, $devmajor, $devminor,
|
|
|
|
$prefix) = unpack 'Z100 Z8 Z8 Z8 Z12 Z12
|
|
|
|
Z8 Z1 Z100 Z6
|
|
|
|
Z2 Z32 Z32 Z8 Z8 Z*', $_;
|
|
|
|
}
|
2007-02-08 20:26:01 +00:00
|
|
|
$mode = oct $mode;
|
|
|
|
$size = oct $size;
|
|
|
|
$mtime = oct $mtime;
|
2007-05-16 16:22:26 +00:00
|
|
|
next if $typeflag == 5; # directory
|
2007-02-08 20:26:01 +00:00
|
|
|
|
2018-05-23 22:54:17 +00:00
|
|
|
if ($typeflag eq 'x') { # extended header
|
|
|
|
# If extended header, check for path
|
|
|
|
my $pax_header = '';
|
|
|
|
while ($size > 0 && read(I, $_, 512) == 512) {
|
|
|
|
$pax_header = $pax_header . substr($_, 0, $size);
|
|
|
|
$size -= 512;
|
|
|
|
}
|
|
|
|
|
|
|
|
my @lines = split /\n/, $pax_header;
|
|
|
|
foreach my $line (@lines) {
|
|
|
|
my ($len, $entry) = split / /, $line;
|
|
|
|
my ($key, $value) = split /=/, $entry;
|
|
|
|
if ($key eq 'path') {
|
|
|
|
$next_path = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next;
|
|
|
|
} elsif ($name =~ m{/\z}) { # directory
|
|
|
|
next;
|
|
|
|
} elsif ($typeflag != 1) { # handle hard links later
|
2016-08-03 13:30:20 +00:00
|
|
|
print FI "blob\n", "mark :$next_mark\n";
|
|
|
|
if ($typeflag == 2) { # symbolic link
|
|
|
|
print FI "data ", length($linkname), "\n",
|
|
|
|
$linkname;
|
|
|
|
$mode = 0120000;
|
|
|
|
} else {
|
|
|
|
print FI "data $size\n";
|
|
|
|
while ($size > 0 && read(I, $_, 512) == 512) {
|
|
|
|
print FI substr($_, 0, $size);
|
|
|
|
$size -= 512;
|
|
|
|
}
|
2009-06-17 12:49:39 +00:00
|
|
|
}
|
2016-08-03 13:30:20 +00:00
|
|
|
print FI "\n";
|
2007-02-08 20:26:01 +00:00
|
|
|
}
|
|
|
|
|
import-tars: ignore the global PAX header
The tar importer in `contrib/fast-import/import-tars.perl` has a very
convenient feature: if _all_ paths stored in the imported `.tar` start
with a common prefix, e.g. `git-2.26.0/` in the tar at
https://github.com/git/git/archive/v2.26.0.tar.gz, then this prefix is
stripped.
This feature makes a ton of sense because it is relatively common to
import two or more revisions of the same project into Git, and obviously
we don't want all files to live in a tree whose name changes from
revision to revision.
Now, the problem with that feature is that it breaks down if there is a
`pax_global_header` "file" located outside of said prefix, at the top of
the tree. This is the case for `.tar` files generated by Git's very own
`git archive` command: it inserts that header, and `git archive` allows
specifying a common prefix (that the header does _not_ share with the
other files contained in the archive) via `--prefix=my-project-1.0.0/`.
Let's just skip any global header when importing `.tar` files into Git.
Note: this global header might contain useful information. For example,
in the output of `git archive`, it lists the original commit, which _is_
useful information. A future improvement to the `import-tars.perl`
script might be to include that information in the commit message, or do
other things with the information (e.g. use `mtime` information
contained in the global header as date of the commit). This patch does
not prevent any future patch from making that happen, it only prevents
the header from being treated as if it was a regular file.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-03-24 19:35:45 +00:00
|
|
|
next if ($typeflag eq 'g'); # ignore global header
|
|
|
|
|
2007-04-24 11:51:04 +00:00
|
|
|
my $path;
|
|
|
|
if ($prefix) {
|
|
|
|
$path = "$prefix/$name";
|
|
|
|
} else {
|
|
|
|
$path = "$name";
|
|
|
|
}
|
2016-08-03 13:30:20 +00:00
|
|
|
|
|
|
|
if ($typeflag == 1) { # hard link
|
|
|
|
$linkname = "$prefix/$linkname" if $prefix;
|
|
|
|
$files{$path} = [ $files{$linkname}->[0], $mode ];
|
|
|
|
} else {
|
|
|
|
$files{$path} = [$next_mark++, $mode];
|
|
|
|
}
|
2007-02-08 20:26:01 +00:00
|
|
|
|
2009-03-20 09:57:50 +00:00
|
|
|
$author_time = $mtime if $mtime > $author_time;
|
2007-02-08 20:26:01 +00:00
|
|
|
$path =~ m,^([^/]+)/,;
|
|
|
|
$top_dir = $1 unless $top_dir;
|
|
|
|
$have_top_dir = 0 if $top_dir ne $1;
|
|
|
|
}
|
|
|
|
|
2009-09-03 12:15:00 +00:00
|
|
|
my $commit_msg = "Imported from $tar_file.";
|
|
|
|
my $this_committer_name = $committer_name;
|
|
|
|
my $this_committer_email = $committer_email;
|
|
|
|
my $this_author_name = $author_name;
|
|
|
|
my $this_author_email = $author_email;
|
|
|
|
if ($metaext ne '') {
|
|
|
|
# Optionally read a commit message from <filename.tar>.msg
|
|
|
|
# Add a line on the form "Committer: name <e-mail>" to override
|
|
|
|
# the committer and "Author: name <e-mail>" to override the
|
|
|
|
# author for this tar ball.
|
|
|
|
if (open MSG, '<', "${tar_file}.${metaext}") {
|
|
|
|
my $header_done = 0;
|
|
|
|
$commit_msg = '';
|
|
|
|
while (<MSG>) {
|
|
|
|
if (!$header_done && /^Committer:\s+([^<>]*)\s+<(.*)>\s*$/i) {
|
|
|
|
$this_committer_name = $1;
|
|
|
|
$this_committer_email = $2;
|
|
|
|
} elsif (!$header_done && /^Author:\s+([^<>]*)\s+<(.*)>\s*$/i) {
|
|
|
|
$this_author_name = $1;
|
|
|
|
$this_author_email = $2;
|
2009-10-09 12:08:31 +00:00
|
|
|
} elsif (!$header_done && /^$/) { # empty line ends header.
|
2009-09-03 12:15:00 +00:00
|
|
|
$header_done = 1;
|
|
|
|
} else {
|
|
|
|
$commit_msg .= $_;
|
|
|
|
$header_done = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close MSG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-08 20:26:01 +00:00
|
|
|
print FI <<EOF;
|
|
|
|
commit $branch_ref
|
2009-09-03 12:15:00 +00:00
|
|
|
author $this_author_name <$this_author_email> $author_time +0000
|
|
|
|
committer $this_committer_name <$this_committer_email> $commit_time +0000
|
2007-02-08 20:26:01 +00:00
|
|
|
data <<END_OF_COMMIT_MESSAGE
|
2009-09-03 12:15:00 +00:00
|
|
|
$commit_msg
|
2007-02-08 20:26:01 +00:00
|
|
|
END_OF_COMMIT_MESSAGE
|
|
|
|
|
|
|
|
deleteall
|
|
|
|
EOF
|
|
|
|
|
|
|
|
foreach my $path (keys %files)
|
|
|
|
{
|
|
|
|
my ($mark, $mode) = @{$files{$path}};
|
|
|
|
$path =~ s,^([^/]+)/,, if $have_top_dir;
|
2009-06-17 12:49:39 +00:00
|
|
|
$mode = $mode & 0111 ? 0755 : 0644 unless $mode == 0120000;
|
|
|
|
printf FI "M %o :%i %s\n", $mode, $mark, $path;
|
2007-02-08 20:26:01 +00:00
|
|
|
}
|
|
|
|
print FI "\n";
|
|
|
|
|
|
|
|
print FI <<EOF;
|
|
|
|
tag $tar_name
|
|
|
|
from $branch_ref
|
2009-03-20 09:57:50 +00:00
|
|
|
tagger $author_name <$author_email> $author_time +0000
|
2007-02-08 20:26:01 +00:00
|
|
|
data <<END_OF_TAG_MESSAGE
|
|
|
|
Package $tar_name
|
|
|
|
END_OF_TAG_MESSAGE
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
close I;
|
|
|
|
}
|
|
|
|
close FI;
|