]>
Commit | Line | Data |
---|---|---|
590dd4bf SP |
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 | ## | |
7e787953 PK |
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. | |
14 | ## | |
15 | ## echo 'This is the commit message' > myfile.tar.bz2.msg | |
16 | ## perl import-tars.perl --metainfo=msg myfile.tar.bz2 | |
590dd4bf SP |
17 | |
18 | use strict; | |
7e787953 PK |
19 | use Getopt::Long; |
20 | ||
21 | my $metaext = ''; | |
22 | ||
23 | die "usage: import-tars [--metainfo=extension] *.tar.{gz,bz2,Z}\n" | |
24 | unless GetOptions('metainfo=s' => \$metaext) && @ARGV; | |
590dd4bf SP |
25 | |
26 | my $branch_name = 'import-tars'; | |
27 | my $branch_ref = "refs/heads/$branch_name"; | |
6872f606 GB |
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`; | |
32 | ||
33 | chomp($committer_name, $committer_email); | |
590dd4bf SP |
34 | |
35 | open(FI, '|-', 'git', 'fast-import', '--quiet') | |
36 | or die "Unable to start git fast-import: $!\n"; | |
37 | foreach my $tar_file (@ARGV) | |
38 | { | |
6872f606 | 39 | my $commit_time = time; |
590dd4bf SP |
40 | $tar_file =~ m,([^/]+)$,; |
41 | my $tar_name = $1; | |
42 | ||
43 | if ($tar_name =~ s/\.(tar\.gz|tgz)$//) { | |
c750da25 ML |
44 | open(I, '-|', 'gunzip', '-c', $tar_file) |
45 | or die "Unable to gunzip -c $tar_file: $!\n"; | |
590dd4bf | 46 | } elsif ($tar_name =~ s/\.(tar\.bz2|tbz2)$//) { |
c750da25 ML |
47 | open(I, '-|', 'bunzip2', '-c', $tar_file) |
48 | or die "Unable to bunzip2 -c $tar_file: $!\n"; | |
590dd4bf | 49 | } elsif ($tar_name =~ s/\.tar\.Z$//) { |
c750da25 ML |
50 | open(I, '-|', 'uncompress', '-c', $tar_file) |
51 | or die "Unable to uncompress -c $tar_file: $!\n"; | |
590dd4bf SP |
52 | } elsif ($tar_name =~ s/\.tar$//) { |
53 | open(I, $tar_file) or die "Unable to open $tar_file: $!\n"; | |
54 | } else { | |
55 | die "Unrecognized compression format: $tar_file\n"; | |
56 | } | |
57 | ||
6872f606 | 58 | my $author_time = 0; |
590dd4bf SP |
59 | my $next_mark = 1; |
60 | my $have_top_dir = 1; | |
61 | my ($top_dir, %files); | |
62 | ||
63 | while (read(I, $_, 512) == 512) { | |
64 | my ($name, $mode, $uid, $gid, $size, $mtime, | |
65 | $chksum, $typeflag, $linkname, $magic, | |
66 | $version, $uname, $gname, $devmajor, $devminor, | |
67 | $prefix) = unpack 'Z100 Z8 Z8 Z8 Z12 Z12 | |
68 | Z8 Z1 Z100 Z6 | |
69 | Z2 Z32 Z32 Z8 Z8 Z*', $_; | |
d966e6aa | 70 | last unless length($name); |
775477aa JS |
71 | if ($name eq '././@LongLink') { |
72 | # GNU tar extension | |
73 | if (read(I, $_, 512) != 512) { | |
74 | die ('Short archive'); | |
75 | } | |
76 | $name = unpack 'Z257', $_; | |
77 | next unless $name; | |
78 | ||
79 | my $dummy; | |
80 | if (read(I, $_, 512) != 512) { | |
81 | die ('Short archive'); | |
82 | } | |
83 | ($dummy, $mode, $uid, $gid, $size, $mtime, | |
84 | $chksum, $typeflag, $linkname, $magic, | |
85 | $version, $uname, $gname, $devmajor, $devminor, | |
86 | $prefix) = unpack 'Z100 Z8 Z8 Z8 Z12 Z12 | |
87 | Z8 Z1 Z100 Z6 | |
88 | Z2 Z32 Z32 Z8 Z8 Z*', $_; | |
89 | } | |
d0c32b63 | 90 | next if $name =~ m{/\z}; |
590dd4bf SP |
91 | $mode = oct $mode; |
92 | $size = oct $size; | |
93 | $mtime = oct $mtime; | |
df8cfac8 | 94 | next if $typeflag == 5; # directory |
590dd4bf | 95 | |
6fb37f86 JS |
96 | print FI "blob\n", "mark :$next_mark\n"; |
97 | if ($typeflag == 2) { # symbolic link | |
98 | print FI "data ", length($linkname), "\n", $linkname; | |
99 | $mode = 0120000; | |
100 | } else { | |
101 | print FI "data $size\n"; | |
102 | while ($size > 0 && read(I, $_, 512) == 512) { | |
103 | print FI substr($_, 0, $size); | |
104 | $size -= 512; | |
105 | } | |
590dd4bf SP |
106 | } |
107 | print FI "\n"; | |
108 | ||
46f6178a UKK |
109 | my $path; |
110 | if ($prefix) { | |
111 | $path = "$prefix/$name"; | |
112 | } else { | |
113 | $path = "$name"; | |
114 | } | |
590dd4bf SP |
115 | $files{$path} = [$next_mark++, $mode]; |
116 | ||
6872f606 | 117 | $author_time = $mtime if $mtime > $author_time; |
590dd4bf SP |
118 | $path =~ m,^([^/]+)/,; |
119 | $top_dir = $1 unless $top_dir; | |
120 | $have_top_dir = 0 if $top_dir ne $1; | |
121 | } | |
122 | ||
7e787953 PK |
123 | my $commit_msg = "Imported from $tar_file."; |
124 | my $this_committer_name = $committer_name; | |
125 | my $this_committer_email = $committer_email; | |
126 | my $this_author_name = $author_name; | |
127 | my $this_author_email = $author_email; | |
128 | if ($metaext ne '') { | |
129 | # Optionally read a commit message from <filename.tar>.msg | |
130 | # Add a line on the form "Committer: name <e-mail>" to override | |
131 | # the committer and "Author: name <e-mail>" to override the | |
132 | # author for this tar ball. | |
133 | if (open MSG, '<', "${tar_file}.${metaext}") { | |
134 | my $header_done = 0; | |
135 | $commit_msg = ''; | |
136 | while (<MSG>) { | |
137 | if (!$header_done && /^Committer:\s+([^<>]*)\s+<(.*)>\s*$/i) { | |
138 | $this_committer_name = $1; | |
139 | $this_committer_email = $2; | |
140 | } elsif (!$header_done && /^Author:\s+([^<>]*)\s+<(.*)>\s*$/i) { | |
141 | $this_author_name = $1; | |
142 | $this_author_email = $2; | |
143 | } elsif (!$header_done && /^$/ { # empty line ends header. | |
144 | $header_done = 1; | |
145 | } else { | |
146 | $commit_msg .= $_; | |
147 | $header_done = 1; | |
148 | } | |
149 | } | |
150 | close MSG; | |
151 | } | |
152 | } | |
153 | ||
590dd4bf SP |
154 | print FI <<EOF; |
155 | commit $branch_ref | |
7e787953 PK |
156 | author $this_author_name <$this_author_email> $author_time +0000 |
157 | committer $this_committer_name <$this_committer_email> $commit_time +0000 | |
590dd4bf | 158 | data <<END_OF_COMMIT_MESSAGE |
7e787953 | 159 | $commit_msg |
590dd4bf SP |
160 | END_OF_COMMIT_MESSAGE |
161 | ||
162 | deleteall | |
163 | EOF | |
164 | ||
165 | foreach my $path (keys %files) | |
166 | { | |
167 | my ($mark, $mode) = @{$files{$path}}; | |
590dd4bf | 168 | $path =~ s,^([^/]+)/,, if $have_top_dir; |
6fb37f86 JS |
169 | $mode = $mode & 0111 ? 0755 : 0644 unless $mode == 0120000; |
170 | printf FI "M %o :%i %s\n", $mode, $mark, $path; | |
590dd4bf SP |
171 | } |
172 | print FI "\n"; | |
173 | ||
174 | print FI <<EOF; | |
175 | tag $tar_name | |
176 | from $branch_ref | |
6872f606 | 177 | tagger $author_name <$author_email> $author_time +0000 |
590dd4bf SP |
178 | data <<END_OF_TAG_MESSAGE |
179 | Package $tar_name | |
180 | END_OF_TAG_MESSAGE | |
181 | ||
182 | EOF | |
183 | ||
184 | close I; | |
185 | } | |
186 | close FI; |