import-tars: be nice to wrong directory modes
[git.git] / contrib / fast-import / import-tars.perl
blobe84647770abbac3090bd92d256edf23565a0bae6
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 $committer_name = 'T Ar Creator';
18 my $committer_email = 'tar@example.com';
20 open(FI, '|-', 'git', 'fast-import', '--quiet')
21 or die "Unable to start git fast-import: $!\n";
22 foreach my $tar_file (@ARGV)
24 $tar_file =~ m,([^/]+)$,;
25 my $tar_name = $1;
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";
42 my $commit_time = 0;
43 my $next_mark = 1;
44 my $have_top_dir = 1;
45 my ($top_dir, %files);
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 $name;
55 next if $name =~ '/$';
56 $mode = oct $mode;
57 $size = oct $size;
58 $mtime = oct $mtime;
59 next if $mode & 0040000;
61 print FI "blob\n", "mark :$next_mark\n", "data $size\n";
62 while ($size > 0 && read(I, $_, 512) == 512) {
63 print FI substr($_, 0, $size);
64 $size -= 512;
66 print FI "\n";
68 my $path = "$prefix$name";
69 $files{$path} = [$next_mark++, $mode];
71 $commit_time = $mtime if $mtime > $commit_time;
72 $path =~ m,^([^/]+)/,;
73 $top_dir = $1 unless $top_dir;
74 $have_top_dir = 0 if $top_dir ne $1;
77 print FI <<EOF;
78 commit $branch_ref
79 committer $committer_name <$committer_email> $commit_time +0000
80 data <<END_OF_COMMIT_MESSAGE
81 Imported from $tar_file.
82 END_OF_COMMIT_MESSAGE
84 deleteall
85 EOF
87 foreach my $path (keys %files)
89 my ($mark, $mode) = @{$files{$path}};
90 $path =~ s,^([^/]+)/,, if $have_top_dir;
91 printf FI "M %o :%i %s\n", $mode & 0111 ? 0755 : 0644, $mark, $path;
93 print FI "\n";
95 print FI <<EOF;
96 tag $tar_name
97 from $branch_ref
98 tagger $committer_name <$committer_email> $commit_time +0000
99 data <<END_OF_TAG_MESSAGE
100 Package $tar_name
101 END_OF_TAG_MESSAGE
105 close I;
107 close FI;