sync with girocco/v2.11.4+
[git/gitweb.git] / contrib / fast-import / import-tars.perl
blobd60b4315ed60ad10e849408c6986d1ea5b47b32b
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
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.
15 ## echo 'This is the commit message' > myfile.tar.bz2.msg
16 ## perl import-tars.perl --metainfo=msg myfile.tar.bz2
18 use strict;
19 use Getopt::Long;
21 my $metaext = '';
23 die "usage: import-tars [--metainfo=extension] *.tar.{gz,bz2,lzma,xz,Z}\n"
24 unless GetOptions('metainfo=s' => \$metaext) && @ARGV;
26 my $branch_name = 'import-tars';
27 my $branch_ref = "refs/heads/$branch_name";
28 my $author_name = $ENV{'GIT_AUTHOR_NAME'} || 'T Ar Creator';
29 my $author_email = $ENV{'GIT_AUTHOR_EMAIL'} || 'tar@example.com';
30 my $committer_name = $ENV{'GIT_COMMITTER_NAME'} || `git config --get user.name`;
31 my $committer_email = $ENV{'GIT_COMMITTER_EMAIL'} || `git config --get user.email`;
33 chomp($committer_name, $committer_email);
35 open(FI, '|-', 'git', 'fast-import', '--quiet')
36 or die "Unable to start git fast-import: $!\n";
37 foreach my $tar_file (@ARGV)
39 my $commit_time = time;
40 $tar_file =~ m,([^/]+)$,;
41 my $tar_name = $1;
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";
61 my $author_time = 0;
62 my $next_mark = 1;
63 my $have_top_dir = 1;
64 my ($top_dir, %files);
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');
79 $name = unpack 'Z257', $_;
80 next unless $name;
82 my $dummy;
83 if (read(I, $_, 512) != 512) {
84 die ('Short archive');
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*', $_;
93 next if $name =~ m{/\z};
94 $mode = oct $mode;
95 $size = oct $size;
96 $mtime = oct $mtime;
97 next if $typeflag == 5; # directory
99 if ($typeflag != 1) { # handle hard links later
100 print FI "blob\n", "mark :$next_mark\n";
101 if ($typeflag == 2) { # symbolic link
102 print FI "data ", length($linkname), "\n",
103 $linkname;
104 $mode = 0120000;
105 } else {
106 print FI "data $size\n";
107 while ($size > 0 && read(I, $_, 512) == 512) {
108 print FI substr($_, 0, $size);
109 $size -= 512;
112 print FI "\n";
115 my $path;
116 if ($prefix) {
117 $path = "$prefix/$name";
118 } else {
119 $path = "$name";
122 if ($typeflag == 1) { # hard link
123 $linkname = "$prefix/$linkname" if $prefix;
124 $files{$path} = [ $files{$linkname}->[0], $mode ];
125 } else {
126 $files{$path} = [$next_mark++, $mode];
129 $author_time = $mtime if $mtime > $author_time;
130 $path =~ m,^([^/]+)/,;
131 $top_dir = $1 unless $top_dir;
132 $have_top_dir = 0 if $top_dir ne $1;
135 my $commit_msg = "Imported from $tar_file.";
136 my $this_committer_name = $committer_name;
137 my $this_committer_email = $committer_email;
138 my $this_author_name = $author_name;
139 my $this_author_email = $author_email;
140 if ($metaext ne '') {
141 # Optionally read a commit message from <filename.tar>.msg
142 # Add a line on the form "Committer: name <e-mail>" to override
143 # the committer and "Author: name <e-mail>" to override the
144 # author for this tar ball.
145 if (open MSG, '<', "${tar_file}.${metaext}") {
146 my $header_done = 0;
147 $commit_msg = '';
148 while (<MSG>) {
149 if (!$header_done && /^Committer:\s+([^<>]*)\s+<(.*)>\s*$/i) {
150 $this_committer_name = $1;
151 $this_committer_email = $2;
152 } elsif (!$header_done && /^Author:\s+([^<>]*)\s+<(.*)>\s*$/i) {
153 $this_author_name = $1;
154 $this_author_email = $2;
155 } elsif (!$header_done && /^$/) { # empty line ends header.
156 $header_done = 1;
157 } else {
158 $commit_msg .= $_;
159 $header_done = 1;
162 close MSG;
166 print FI <<EOF;
167 commit $branch_ref
168 author $this_author_name <$this_author_email> $author_time +0000
169 committer $this_committer_name <$this_committer_email> $commit_time +0000
170 data <<END_OF_COMMIT_MESSAGE
171 $commit_msg
172 END_OF_COMMIT_MESSAGE
174 deleteall
177 foreach my $path (keys %files)
179 my ($mark, $mode) = @{$files{$path}};
180 $path =~ s,^([^/]+)/,, if $have_top_dir;
181 $mode = $mode & 0111 ? 0755 : 0644 unless $mode == 0120000;
182 printf FI "M %o :%i %s\n", $mode, $mark, $path;
184 print FI "\n";
186 print FI <<EOF;
187 tag $tar_name
188 from $branch_ref
189 tagger $author_name <$author_email> $author_time +0000
190 data <<END_OF_TAG_MESSAGE
191 Package $tar_name
192 END_OF_TAG_MESSAGE
196 close I;
198 close FI;