import-tars: support symlinks
[git/kirr.git] / contrib / fast-import / import-tars.perl
blob78e40d2a13e5c754b10a8e68641236bf58394e05
1 #!/usr/bin/perl
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
12 use strict;
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 $author_name = $ENV{'GIT_AUTHOR_NAME'} || 'T Ar Creator';
18 my $author_email = $ENV{'GIT_AUTHOR_EMAIL'} || 'tar@example.com';
19 my $committer_name = $ENV{'GIT_COMMITTER_NAME'} || `git config --get user.name`;
20 my $committer_email = $ENV{'GIT_COMMITTER_EMAIL'} || `git config --get user.email`;
22 chomp($committer_name, $committer_email);
24 open(FI, '|-', 'git', 'fast-import', '--quiet')
25 or die "Unable to start git fast-import: $!\n";
26 foreach my $tar_file (@ARGV)
28 my $commit_time = time;
29 $tar_file =~ m,([^/]+)$,;
30 my $tar_name = $1;
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";
47 my $author_time = 0;
48 my $next_mark = 1;
49 my $have_top_dir = 1;
50 my ($top_dir, %files);
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');
65 $name = unpack 'Z257', $_;
66 next unless $name;
68 my $dummy;
69 if (read(I, $_, 512) != 512) {
70 die ('Short archive');
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*', $_;
79 next if $name =~ m{/\z};
80 $mode = oct $mode;
81 $size = oct $size;
82 $mtime = oct $mtime;
83 next if $typeflag == 5; # directory
85 print FI "blob\n", "mark :$next_mark\n";
86 if ($typeflag == 2) { # symbolic link
87 print FI "data ", length($linkname), "\n", $linkname;
88 $mode = 0120000;
89 } else {
90 print FI "data $size\n";
91 while ($size > 0 && read(I, $_, 512) == 512) {
92 print FI substr($_, 0, $size);
93 $size -= 512;
96 print FI "\n";
98 my $path;
99 if ($prefix) {
100 $path = "$prefix/$name";
101 } else {
102 $path = "$name";
104 $files{$path} = [$next_mark++, $mode];
106 $author_time = $mtime if $mtime > $author_time;
107 $path =~ m,^([^/]+)/,;
108 $top_dir = $1 unless $top_dir;
109 $have_top_dir = 0 if $top_dir ne $1;
112 print FI <<EOF;
113 commit $branch_ref
114 author $author_name <$author_email> $author_time +0000
115 committer $committer_name <$committer_email> $commit_time +0000
116 data <<END_OF_COMMIT_MESSAGE
117 Imported from $tar_file.
118 END_OF_COMMIT_MESSAGE
120 deleteall
123 foreach my $path (keys %files)
125 my ($mark, $mode) = @{$files{$path}};
126 $path =~ s,^([^/]+)/,, if $have_top_dir;
127 $mode = $mode & 0111 ? 0755 : 0644 unless $mode == 0120000;
128 printf FI "M %o :%i %s\n", $mode, $mark, $path;
130 print FI "\n";
132 print FI <<EOF;
133 tag $tar_name
134 from $branch_ref
135 tagger $author_name <$author_email> $author_time +0000
136 data <<END_OF_TAG_MESSAGE
137 Package $tar_name
138 END_OF_TAG_MESSAGE
142 close I;
144 close FI;