summaryrefslogtreecommitdiff
path: root/lorry.tar-importer
blob: 7ffdfa99f6fc99441a9f3cbb6f484a4e03bdacd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/perl

## Copyright 2005-2012 Linus Torvalds and others
## Copyright 2012, 2015, 2018 Codethink Limited
##
## Based on git's contrib/fast-import/import-tars.perl; modified for Lorry.

## 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
##
## 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

use strict;
use Getopt::Long;

my $metaext = '';

die "usage: import-tars [--metainfo=extension] *.tar.{gz,bz2,lzma,xz,Z}\n"
	unless GetOptions('metainfo=s' => \$metaext) && @ARGV;

my $branch_name = 'master';
my $branch_ref = "refs/heads/$branch_name";
my $old_sha = `git show-ref $branch_ref 2>/dev/null`;
my $author_name = 'Lorry Tar Creator';
my $author_email = 'lorry-tar-importer@lorry';
my $committer_name = $author_name;
my $committer_email = $author_email;

chomp($committer_name, $committer_email, $old_sha);

if ($old_sha ne '') {
   $old_sha = ($old_sha =~ /^([a-f0-9]+)/)[0];
}
open(FI, '|-', 'git', 'fast-import', '--quiet')
	or die "Unable to start git fast-import: $!\n";
foreach my $tar_file (@ARGV)
{
	$tar_file =~ m,([^/]+)$,;
	my $tar_name = $1;

	if ($tar_name =~ s/\.(tar\.gz|tgz)$//) {
		open(I, '-|', 'gunzip', '-c', $tar_file)
			or die "Unable to gunzip -c $tar_file: $!\n";
	} elsif ($tar_name =~ s/\.(tar\.bz2|tbz2)$//) {
		open(I, '-|', 'bunzip2', '-c', $tar_file)
			or die "Unable to bunzip2 -c $tar_file: $!\n";
	} elsif ($tar_name =~ s/\.tar\.Z$//) {
		open(I, '-|', 'uncompress', '-c', $tar_file)
			or die "Unable to uncompress -c $tar_file: $!\n";
	} elsif ($tar_name =~ s/\.(tar\.(lzma|xz)|(tlz|txz))$//) {
		open(I, '-|', 'xz', '-dc', $tar_file)
			or die "Unable to xz -dc $tar_file: $!\n";
	} 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";
	}

	my $author_time = 0;
	my $next_mark = 1;
	my $have_top_dir = 1;
	my ($top_dir, %files);

	my $next_path = '';

	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*', $_;

		unless ($next_path eq '') {
			# Recover name from previous extended header
			$name = $next_path;
			$next_path = '';
		}

		last unless length($name);
		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*', $_;
		}
		$mode = oct $mode;
		$size = oct $size;
		$mtime = oct $mtime;
		next if $typeflag == 5; # directory

		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}) {
			# If it's a folder, ignore
			next;
		} elsif ($typeflag != 1) { # handle hard links later
			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;
				}
			}
			print FI "\n";
		}

		next if ($typeflag eq 'g'); # ignore global header

		my $path;
		if ($prefix) {
			$path = "$prefix/$name";
		} else {
			$path = "$name";
		}

		if ($typeflag == 1) { # hard link
			$linkname = "$prefix/$linkname" if $prefix;
			$files{$path} = [ $files{$linkname}->[0], $mode ];
		} else {
			$files{$path} = [$next_mark++, $mode];
		}

		$author_time = $mtime if $mtime > $author_time;
		$path =~ m,^([^/]+)/,;
		$top_dir = $1 unless $top_dir;
		$have_top_dir = 0 if $top_dir ne $1;
	}

	my $commit_msg = $tar_name;
	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;
				} elsif (!$header_done && /^$/) { # empty line ends header.
					$header_done = 1;
				} else {
					$commit_msg .= $_;
					$header_done = 1;
				}
			}
			close MSG;
		}
	}

	print FI <<EOF;
commit $branch_ref
author $this_author_name <$this_author_email> $author_time +0000
committer $this_committer_name <$this_committer_email> $author_time +0000
data <<END_OF_COMMIT_MESSAGE
$commit_msg
END_OF_COMMIT_MESSAGE
EOF

        if ($old_sha ne '') {
           print FI <<EOF;
from $old_sha
EOF
        }

        print FI <<EOF;
deleteall
EOF

	foreach my $path (keys %files)
	{
		my ($mark, $mode) = @{$files{$path}};
		$path =~ s,^([^/]+)/,, if $have_top_dir;
		$mode = $mode & 0111 ? 0755 : 0644 unless $mode == 0120000;
		printf FI "M %o :%i %s\n", $mode, $mark, $path;
	}
	print FI "\n";


	print FI <<EOF;
tag $tar_name
from $branch_ref
tagger $author_name <$author_email> $author_time +0000
data <<END_OF_TAG_MESSAGE
Package $tar_name
END_OF_TAG_MESSAGE

EOF

	close I;
}
close FI;