]> git.ipfire.org Git - thirdparty/git.git/blame - contrib/git-svn/git-svn.perl
git-svn: fix handling of filenames with embedded '@'
[thirdparty/git.git] / contrib / git-svn / git-svn.perl
CommitLineData
3397f9df 1#!/usr/bin/env perl
551ce28f
EW
2# Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3# License: GPL v2 or later
3397f9df
EW
4use warnings;
5use strict;
6use vars qw/ $AUTHOR $VERSION
1ca72aef 7 $SVN_URL $SVN_INFO $SVN_WC $SVN_UUID
3397f9df
EW
8 $GIT_SVN_INDEX $GIT_SVN
9 $GIT_DIR $REV_DIR/;
10$AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
304dac15 11$VERSION = '1.1.0-pre';
13ccd6d4
EW
12
13use Cwd qw/abs_path/;
14$GIT_DIR = abs_path($ENV{GIT_DIR} || '.git');
15$ENV{GIT_DIR} = $GIT_DIR;
16
3397f9df
EW
17# make sure the svn binary gives consistent output between locales and TZs:
18$ENV{TZ} = 'UTC';
19$ENV{LC_ALL} = 'C';
20
21# If SVN:: library support is added, please make the dependencies
22# optional and preserve the capability to use the command-line client.
ce6f3519 23# use eval { require SVN::... } to make it lazy load
eeb0abe0 24# We don't use any modules not in the standard Perl distribution:
3397f9df
EW
25use Carp qw/croak/;
26use IO::File qw//;
27use File::Basename qw/dirname basename/;
28use File::Path qw/mkpath/;
29use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
30use File::Spec qw//;
e17512f3 31use POSIX qw/strftime/;
3397f9df 32my $sha1 = qr/[a-f\d]{40}/;
ac8e0b91 33my $sha1_short = qr/[a-f\d]{4,40}/;
72942938 34my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
a9612be2
EW
35 $_find_copies_harder, $_l, $_version, $_upgrade, $_authors);
36my (@_branch_from, %tree_map, %users);
8a97e368 37my ($_svn_co_url_revs, $_svn_pg_peg_revs);
3397f9df 38
eeb0abe0 39my %fc_opts = ( 'no-ignore-externals' => \$_no_ignore_ext,
69f0d91e 40 'branch|b=s' => \@_branch_from,
eeb0abe0 41 'authors-file|A=s' => \$_authors );
36f5b1f0
EW
42
43# yes, 'native' sets "\n". Patches to fix this for non-*nix systems welcome:
44my %EOL = ( CR => "\015", LF => "\012", CRLF => "\015\012", native => "\012" );
45
3397f9df 46my %cmd = (
eeb0abe0
EW
47 fetch => [ \&fetch, "Download new revisions from SVN",
48 { 'revision|r=s' => \$_revision, %fc_opts } ],
81c5a0e6
EW
49 init => [ \&init, "Initialize a repo for tracking" .
50 " (requires URL argument)", { } ],
eeb0abe0
EW
51 commit => [ \&commit, "Commit git revisions to SVN",
52 { 'stdin|' => \$_stdin,
53 'edit|e' => \$_edit,
54 'rmdir' => \$_rmdir,
55 'find-copies-harder' => \$_find_copies_harder,
56 'l=i' => \$_l,
57 %fc_opts,
58 } ],
59 'show-ignore' => [ \&show_ignore, "Show svn:ignore listings", { } ],
60 rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)",
61 { 'no-ignore-externals' => \$_no_ignore_ext,
62 'upgrade' => \$_upgrade } ],
3397f9df
EW
63);
64my $cmd;
65for (my $i = 0; $i < @ARGV; $i++) {
66 if (defined $cmd{$ARGV[$i]}) {
67 $cmd = $ARGV[$i];
68 splice @ARGV, $i, 1;
69 last;
70 }
71};
72
448c81b4 73my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
a9612be2 74
53909056
EW
75# convert GetOpt::Long specs for use by git-repo-config
76foreach my $o (keys %opts) {
77 my $v = $opts{$o};
78 my ($key) = ($o =~ /^([a-z\-]+)/);
79 $key =~ s/-//g;
80 my $arg = 'git-repo-config';
81 $arg .= ' --int' if ($o =~ /=i$/);
82 $arg .= ' --bool' if ($o !~ /=[sfi]$/);
53909056 83 if (ref $v eq 'ARRAY') {
fc9957b0 84 chomp(my @tmp = `$arg --get-all svn.$key`);
5941a9e9 85 @$v = @tmp if @tmp;
53909056 86 } else {
fc9957b0 87 chomp(my $tmp = `$arg --get svn.$key`);
5941a9e9
EW
88 if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
89 $$v = $tmp;
90 }
53909056
EW
91 }
92}
93
6f0783cf
EW
94GetOptions(%opts, 'help|H|h' => \$_help,
95 'version|V' => \$_version,
96 'id|i=s' => \$GIT_SVN) or exit 1;
97
98$GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn';
99$GIT_SVN_INDEX = "$GIT_DIR/$GIT_SVN/index";
6f0783cf
EW
100$SVN_URL = undef;
101$REV_DIR = "$GIT_DIR/$GIT_SVN/revs";
102$SVN_WC = "$GIT_DIR/$GIT_SVN/tree";
103
3397f9df 104usage(0) if $_help;
551ce28f 105version() if $_version;
eeb0abe0
EW
106usage(1) unless defined $cmd;
107load_authors() if $_authors;
1d52aba8 108svn_compat_check();
3397f9df
EW
109$cmd{$cmd}->[0]->(@ARGV);
110exit 0;
111
112####################### primary functions ######################
113sub usage {
114 my $exit = shift || 0;
115 my $fd = $exit ? \*STDERR : \*STDOUT;
116 print $fd <<"";
117git-svn - bidirectional operations between a single Subversion tree and git
118Usage: $0 <command> [options] [arguments]\n
448c81b4
EW
119
120 print $fd "Available commands:\n" unless $cmd;
3397f9df
EW
121
122 foreach (sort keys %cmd) {
448c81b4 123 next if $cmd && $cmd ne $_;
ac8e0b91 124 print $fd ' ',pack('A13',$_),$cmd{$_}->[1],"\n";
448c81b4
EW
125 foreach (keys %{$cmd{$_}->[2]}) {
126 # prints out arguments as they should be passed:
127 my $x = s#=s$## ? '<arg>' : s#=i$## ? '<num>' : '';
128 print $fd ' ' x 17, join(', ', map { length $_ > 1 ?
129 "--$_" : "-$_" }
130 split /\|/,$_)," $x\n";
131 }
3397f9df
EW
132 }
133 print $fd <<"";
448c81b4
EW
134\nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
135arbitrary identifier if you're tracking multiple SVN branches/repositories in
136one git repository and want to keep them separate. See git-svn(1) for more
137information.
3397f9df
EW
138
139 exit $exit;
140}
141
551ce28f
EW
142sub version {
143 print "git-svn version $VERSION\n";
144 exit 0;
145}
146
3397f9df
EW
147sub rebuild {
148 $SVN_URL = shift or undef;
3397f9df 149 my $newest_rev = 0;
2beb3cdd
EW
150 if ($_upgrade) {
151 sys('git-update-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
152 } else {
153 check_upgrade_needed();
154 }
3397f9df
EW
155
156 my $pid = open(my $rev_list,'-|');
157 defined $pid or croak $!;
158 if ($pid == 0) {
2beb3cdd 159 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
3397f9df 160 }
2beb3cdd 161 my $latest;
3397f9df
EW
162 while (<$rev_list>) {
163 chomp;
164 my $c = $_;
165 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
166 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
167 next if (!@commit); # skip merges
168 my $id = $commit[$#commit];
169 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
170 \s([a-f\d\-]+)$/x);
171 if (!$rev || !$uuid || !$url) {
172 # some of the original repositories I made had
173 # indentifiers like this:
174 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)
175 \@([a-f\d\-]+)/x);
176 if (!$rev || !$uuid) {
177 croak "Unable to extract revision or UUID from ",
178 "$c, $id\n";
179 }
180 }
2beb3cdd
EW
181
182 # if we merged or otherwise started elsewhere, this is
183 # how we break out of it
1ca72aef 184 next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
779b1446 185 next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
2beb3cdd 186
3397f9df 187 print "r$rev = $c\n";
2beb3cdd 188 unless (defined $latest) {
3397f9df
EW
189 if (!$SVN_URL && !$url) {
190 croak "SVN repository location required: $url\n";
191 }
192 $SVN_URL ||= $url;
1d52aba8
EW
193 $SVN_UUID ||= $uuid;
194 setup_git_svn();
2beb3cdd 195 $latest = $rev;
3397f9df
EW
196 }
197 assert_revision_eq_or_unknown($rev, $c);
198 sys('git-update-ref',"$GIT_SVN/revs/$rev",$c);
199 $newest_rev = $rev if ($rev > $newest_rev);
200 }
201 close $rev_list or croak $?;
202 if (!chdir $SVN_WC) {
1d52aba8 203 svn_cmd_checkout($SVN_URL, $latest, $SVN_WC);
3397f9df
EW
204 chdir $SVN_WC or croak $!;
205 }
206
207 $pid = fork;
208 defined $pid or croak $!;
209 if ($pid == 0) {
210 my @svn_up = qw(svn up);
211 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
212 sys(@svn_up,"-r$newest_rev");
213 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
36f5b1f0 214 index_changes();
3397f9df
EW
215 exec('git-write-tree');
216 }
217 waitpid $pid, 0;
2beb3cdd
EW
218
219 if ($_upgrade) {
220 print STDERR <<"";
221Keeping deprecated refs/head/$GIT_SVN-HEAD for now. Please remove it
222when you have upgraded your tools and habits to use refs/remotes/$GIT_SVN
223
224 }
3397f9df
EW
225}
226
227sub init {
81c5a0e6
EW
228 $SVN_URL = shift or die "SVN repository location required " .
229 "as a command-line argument\n";
3397f9df
EW
230 unless (-d $GIT_DIR) {
231 sys('git-init-db');
232 }
233 setup_git_svn();
234}
235
236sub fetch {
237 my (@parents) = @_;
2beb3cdd 238 check_upgrade_needed();
3397f9df
EW
239 $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
240 my @log_args = -d $SVN_WC ? ($SVN_WC) : ($SVN_URL);
defc6492
EW
241 unless ($_revision) {
242 $_revision = -d $SVN_WC ? 'BASE:HEAD' : '0:HEAD';
3397f9df 243 }
defc6492 244 push @log_args, "-r$_revision";
3397f9df
EW
245 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
246
ce6f3519 247 my $svn_log = svn_log_raw(@log_args);
3397f9df 248
03823184 249 my $base = next_log_entry($svn_log) or croak "No base revision!\n";
3397f9df
EW
250 my $last_commit = undef;
251 unless (-d $SVN_WC) {
1d52aba8 252 svn_cmd_checkout($SVN_URL,$base->{revision},$SVN_WC);
3397f9df 253 chdir $SVN_WC or croak $!;
1d52aba8 254 read_uuid();
3397f9df 255 $last_commit = git_commit($base, @parents);
36f5b1f0 256 assert_tree($last_commit);
3397f9df
EW
257 } else {
258 chdir $SVN_WC or croak $!;
1d52aba8 259 read_uuid();
3397f9df
EW
260 $last_commit = file_to_s("$REV_DIR/$base->{revision}");
261 }
262 my @svn_up = qw(svn up);
263 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
03823184
EW
264 my $last = $base;
265 while (my $log_msg = next_log_entry($svn_log)) {
36f5b1f0 266 assert_tree($last_commit);
03823184
EW
267 if ($last->{revision} >= $log_msg->{revision}) {
268 croak "Out of order: last >= current: ",
269 "$last->{revision} >= $log_msg->{revision}\n";
270 }
36f5b1f0
EW
271 # Revert is needed for cases like:
272 # https://svn.musicpd.org/Jamming/trunk (r166:167), but
273 # I can't seem to reproduce something like that on a test...
274 sys(qw/svn revert -R ./);
275 assert_svn_wc_clean($last->{revision});
03823184 276 sys(@svn_up,"-r$log_msg->{revision}");
3397f9df 277 $last_commit = git_commit($log_msg, $last_commit, @parents);
03823184 278 $last = $log_msg;
3397f9df 279 }
7f60b228
EW
280 unless (-e "$GIT_DIR/refs/heads/master") {
281 sys(qw(git-update-ref refs/heads/master),$last_commit);
282 }
03823184 283 return $last;
3397f9df
EW
284}
285
286sub commit {
287 my (@commits) = @_;
2beb3cdd 288 check_upgrade_needed();
3397f9df
EW
289 if ($_stdin || !@commits) {
290 print "Reading from stdin...\n";
291 @commits = ();
292 while (<STDIN>) {
1ca72aef 293 if (/\b($sha1_short)\b/o) {
3397f9df
EW
294 unshift @commits, $1;
295 }
296 }
297 }
298 my @revs;
8de010ad
EW
299 foreach my $c (@commits) {
300 chomp(my @tmp = safe_qx('git-rev-parse',$c));
301 if (scalar @tmp == 1) {
302 push @revs, $tmp[0];
303 } elsif (scalar @tmp > 1) {
304 push @revs, reverse (safe_qx('git-rev-list',@tmp));
305 } else {
306 die "Failed to rev-parse $c\n";
307 }
3397f9df
EW
308 }
309 chomp @revs;
310
311 fetch();
312 chdir $SVN_WC or croak $!;
1d52aba8
EW
313 my $info = svn_info('.');
314 read_uuid($info);
315 my $svn_current_rev = $info->{'Last Changed Rev'};
3397f9df 316 foreach my $c (@revs) {
cf52b8f0
EW
317 my $mods = svn_checkout_tree($svn_current_rev, $c);
318 if (scalar @$mods == 0) {
319 print "Skipping, no changes detected\n";
320 next;
321 }
3397f9df
EW
322 $svn_current_rev = svn_commit_tree($svn_current_rev, $c);
323 }
324 print "Done committing ",scalar @revs," revisions to SVN\n";
3397f9df
EW
325}
326
8f22562c
EW
327sub show_ignore {
328 require File::Find or die $!;
329 my $exclude_file = "$GIT_DIR/info/exclude";
330 open my $fh, '<', $exclude_file or croak $!;
331 chomp(my @excludes = (<$fh>));
332 close $fh or croak $!;
333
334 $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
335 chdir $SVN_WC or croak $!;
336 my %ign;
337 File::Find::find({wanted=>sub{if(lstat $_ && -d _ && -d "$_/.svn"){
338 s#^\./##;
8a97e368 339 @{$ign{$_}} = svn_propget_base('svn:ignore', $_);
8f22562c
EW
340 }}, no_chdir=>1},'.');
341
342 print "\n# /\n";
343 foreach (@{$ign{'.'}}) { print '/',$_ if /\S/ }
344 delete $ign{'.'};
345 foreach my $i (sort keys %ign) {
346 print "\n# ",$i,"\n";
347 foreach (@{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
348 }
349}
350
3397f9df
EW
351########################### utility functions #########################
352
1d52aba8
EW
353sub read_uuid {
354 return if $SVN_UUID;
355 my $info = shift || svn_info('.');
356 $SVN_UUID = $info->{'Repository UUID'} or
357 croak "Repository UUID unreadable\n";
358 s_to_file($SVN_UUID,"$GIT_DIR/$GIT_SVN/info/uuid");
359}
360
3397f9df
EW
361sub setup_git_svn {
362 defined $SVN_URL or croak "SVN repository location required\n";
363 unless (-d $GIT_DIR) {
364 croak "GIT_DIR=$GIT_DIR does not exist!\n";
365 }
366 mkpath(["$GIT_DIR/$GIT_SVN"]);
367 mkpath(["$GIT_DIR/$GIT_SVN/info"]);
368 mkpath([$REV_DIR]);
369 s_to_file($SVN_URL,"$GIT_DIR/$GIT_SVN/info/url");
3397f9df
EW
370
371 open my $fd, '>>', "$GIT_DIR/$GIT_SVN/info/exclude" or croak $!;
372 print $fd '.svn',"\n";
373 close $fd or croak $!;
3397f9df
EW
374}
375
376sub assert_svn_wc_clean {
36f5b1f0 377 my ($svn_rev) = @_;
3397f9df 378 croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
ce4c8b24
EW
379 my $lcr = svn_info('.')->{'Last Changed Rev'};
380 if ($svn_rev != $lcr) {
381 print STDERR "Checking for copy-tree ... ";
ce4c8b24
EW
382 my @diff = grep(/^Index: /,(safe_qx(qw(svn diff),
383 "-r$lcr:$svn_rev")));
384 if (@diff) {
385 croak "Nope! Expected r$svn_rev, got r$lcr\n";
386 } else {
387 print STDERR "OK!\n";
388 }
3397f9df
EW
389 }
390 my @status = grep(!/^Performing status on external/,(`svn status`));
391 @status = grep(!/^\s*$/,@status);
392 if (scalar @status) {
393 print STDERR "Tree ($SVN_WC) is not clean:\n";
394 print STDERR $_ foreach @status;
395 croak;
396 }
cf52b8f0
EW
397}
398
399sub assert_tree {
400 my ($treeish) = @_;
401 croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
402 chomp(my $type = `git-cat-file -t $treeish`);
403 my $expected;
404 while ($type eq 'tag') {
405 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
406 }
407 if ($type eq 'commit') {
408 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
409 ($expected) = ($expected =~ /^tree ($sha1)$/);
410 die "Unable to get tree from $treeish\n" unless $expected;
411 } elsif ($type eq 'tree') {
412 $expected = $treeish;
413 } else {
414 die "$treeish is a $type, expected tree, tag or commit\n";
415 }
416
417 my $old_index = $ENV{GIT_INDEX_FILE};
418 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
419 if (-e $tmpindex) {
420 unlink $tmpindex or croak $!;
421 }
422 $ENV{GIT_INDEX_FILE} = $tmpindex;
36f5b1f0 423 index_changes(1);
cf52b8f0
EW
424 chomp(my $tree = `git-write-tree`);
425 if ($old_index) {
426 $ENV{GIT_INDEX_FILE} = $old_index;
427 } else {
428 delete $ENV{GIT_INDEX_FILE};
429 }
430 if ($tree ne $expected) {
431 croak "Tree mismatch, Got: $tree, Expected: $expected\n";
3397f9df 432 }
36f5b1f0 433 unlink $tmpindex;
3397f9df
EW
434}
435
436sub parse_diff_tree {
437 my $diff_fh = shift;
438 local $/ = "\0";
439 my $state = 'meta';
440 my @mods;
441 while (<$diff_fh>) {
442 chomp $_; # this gets rid of the trailing "\0"
3397f9df
EW
443 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
444 $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
445 push @mods, { mode_a => $1, mode_b => $2,
446 sha1_b => $3, chg => $4 };
447 if ($4 =~ /^(?:C|R)$/) {
448 $state = 'file_a';
449 } else {
450 $state = 'file_b';
451 }
452 } elsif ($state eq 'file_a') {
cf52b8f0 453 my $x = $mods[$#mods] or croak "Empty array\n";
3397f9df 454 if ($x->{chg} !~ /^(?:C|R)$/) {
cf52b8f0 455 croak "Error parsing $_, $x->{chg}\n";
3397f9df
EW
456 }
457 $x->{file_a} = $_;
458 $state = 'file_b';
459 } elsif ($state eq 'file_b') {
cf52b8f0 460 my $x = $mods[$#mods] or croak "Empty array\n";
3397f9df 461 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
cf52b8f0 462 croak "Error parsing $_, $x->{chg}\n";
3397f9df
EW
463 }
464 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
cf52b8f0 465 croak "Error parsing $_, $x->{chg}\n";
3397f9df
EW
466 }
467 $x->{file_b} = $_;
468 $state = 'meta';
469 } else {
cf52b8f0 470 croak "Error parsing $_\n";
3397f9df
EW
471 }
472 }
473 close $diff_fh or croak $!;
cf52b8f0 474
3397f9df
EW
475 return \@mods;
476}
477
478sub svn_check_prop_executable {
479 my $m = shift;
cf52b8f0
EW
480 return if -l $m->{file_b};
481 if ($m->{mode_b} =~ /755$/) {
482 chmod((0755 &~ umask),$m->{file_b}) or croak $!;
483 if ($m->{mode_a} !~ /755$/) {
484 sys(qw(svn propset svn:executable 1), $m->{file_b});
485 }
486 -x $m->{file_b} or croak "$m->{file_b} is not executable!\n";
3397f9df
EW
487 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
488 sys(qw(svn propdel svn:executable), $m->{file_b});
cf52b8f0
EW
489 chmod((0644 &~ umask),$m->{file_b}) or croak $!;
490 -x $m->{file_b} and croak "$m->{file_b} is executable!\n";
3397f9df
EW
491 }
492}
493
494sub svn_ensure_parent_path {
495 my $dir_b = dirname(shift);
496 svn_ensure_parent_path($dir_b) if ($dir_b ne File::Spec->curdir);
497 mkpath([$dir_b]) unless (-d $dir_b);
498 sys(qw(svn add -N), $dir_b) unless (-d "$dir_b/.svn");
499}
500
cf52b8f0
EW
501sub precommit_check {
502 my $mods = shift;
503 my (%rm_file, %rmdir_check, %added_check);
504
505 my %o = ( D => 0, R => 1, C => 2, A => 3, M => 3, T => 3 );
506 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
507 if ($m->{chg} eq 'R') {
508 if (-d $m->{file_b}) {
509 err_dir_to_file("$m->{file_a} => $m->{file_b}");
510 }
511 # dir/$file => dir/file/$file
512 my $dirname = dirname($m->{file_b});
513 while ($dirname ne File::Spec->curdir) {
514 if ($dirname ne $m->{file_a}) {
515 $dirname = dirname($dirname);
516 next;
517 }
518 err_file_to_dir("$m->{file_a} => $m->{file_b}");
519 }
520 # baz/zzz => baz (baz is a file)
521 $dirname = dirname($m->{file_a});
522 while ($dirname ne File::Spec->curdir) {
523 if ($dirname ne $m->{file_b}) {
524 $dirname = dirname($dirname);
525 next;
526 }
527 err_dir_to_file("$m->{file_a} => $m->{file_b}");
528 }
529 }
530 if ($m->{chg} =~ /^(D|R)$/) {
531 my $t = $1 eq 'D' ? 'file_b' : 'file_a';
532 $rm_file{ $m->{$t} } = 1;
533 my $dirname = dirname( $m->{$t} );
534 my $basename = basename( $m->{$t} );
535 $rmdir_check{$dirname}->{$basename} = 1;
536 } elsif ($m->{chg} =~ /^(?:A|C)$/) {
537 if (-d $m->{file_b}) {
538 err_dir_to_file($m->{file_b});
539 }
540 my $dirname = dirname( $m->{file_b} );
541 my $basename = basename( $m->{file_b} );
542 $added_check{$dirname}->{$basename} = 1;
543 while ($dirname ne File::Spec->curdir) {
544 if ($rm_file{$dirname}) {
545 err_file_to_dir($m->{file_b});
546 }
547 $dirname = dirname $dirname;
548 }
549 }
550 }
551 return (\%rmdir_check, \%added_check);
552
553 sub err_dir_to_file {
554 my $file = shift;
555 print STDERR "Node change from directory to file ",
556 "is not supported by Subversion: ",$file,"\n";
557 exit 1;
558 }
559 sub err_file_to_dir {
560 my $file = shift;
561 print STDERR "Node change from file to directory ",
562 "is not supported by Subversion: ",$file,"\n";
563 exit 1;
564 }
565}
566
3397f9df 567sub svn_checkout_tree {
cf52b8f0 568 my ($svn_rev, $treeish) = @_;
3397f9df 569 my $from = file_to_s("$REV_DIR/$svn_rev");
36f5b1f0 570 assert_tree($from);
ac8e0b91 571 print "diff-tree $from $treeish\n";
3397f9df
EW
572 my $pid = open my $diff_fh, '-|';
573 defined $pid or croak $!;
574 if ($pid == 0) {
72942938
EW
575 my @diff_tree = qw(git-diff-tree -z -r -C);
576 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
577 push @diff_tree, "-l$_l" if defined $_l;
cf52b8f0 578 exec(@diff_tree, $from, $treeish) or croak $!;
3397f9df
EW
579 }
580 my $mods = parse_diff_tree($diff_fh);
581 unless (@$mods) {
ac8e0b91 582 # git can do empty commits, but SVN doesn't allow it...
cf52b8f0 583 return $mods;
3397f9df 584 }
cf52b8f0
EW
585 my ($rm, $add) = precommit_check($mods);
586
587 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
588 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
3397f9df
EW
589 if ($m->{chg} eq 'C') {
590 svn_ensure_parent_path( $m->{file_b} );
591 sys(qw(svn cp), $m->{file_a}, $m->{file_b});
cf52b8f0 592 apply_mod_line_blob($m);
3397f9df
EW
593 svn_check_prop_executable($m);
594 } elsif ($m->{chg} eq 'D') {
3397f9df
EW
595 sys(qw(svn rm --force), $m->{file_b});
596 } elsif ($m->{chg} eq 'R') {
597 svn_ensure_parent_path( $m->{file_b} );
598 sys(qw(svn mv --force), $m->{file_a}, $m->{file_b});
cf52b8f0 599 apply_mod_line_blob($m);
3397f9df 600 svn_check_prop_executable($m);
3397f9df 601 } elsif ($m->{chg} eq 'M') {
cf52b8f0 602 apply_mod_line_blob($m);
3397f9df
EW
603 svn_check_prop_executable($m);
604 } elsif ($m->{chg} eq 'T') {
605 sys(qw(svn rm --force),$m->{file_b});
cf52b8f0 606 apply_mod_line_blob($m);
3397f9df 607 sys(qw(svn add --force), $m->{file_b});
cf52b8f0 608 svn_check_prop_executable($m);
3397f9df
EW
609 } elsif ($m->{chg} eq 'A') {
610 svn_ensure_parent_path( $m->{file_b} );
cf52b8f0 611 apply_mod_line_blob($m);
3397f9df 612 sys(qw(svn add --force), $m->{file_b});
cf52b8f0 613 svn_check_prop_executable($m);
3397f9df
EW
614 } else {
615 croak "Invalid chg: $m->{chg}\n";
616 }
617 }
cf52b8f0
EW
618
619 assert_tree($treeish);
620 if ($_rmdir) { # remove empty directories
621 handle_rmdir($rm, $add);
622 }
623 assert_tree($treeish);
624 return $mods;
625}
626
627# svn ls doesn't work with respect to the current working tree, but what's
628# in the repository. There's not even an option for it... *sigh*
629# (added files don't show up and removed files remain in the ls listing)
630sub svn_ls_current {
631 my ($dir, $rm, $add) = @_;
632 chomp(my @ls = safe_qx('svn','ls',$dir));
633 my @ret = ();
634 foreach (@ls) {
635 s#/$##; # trailing slashes are evil
636 push @ret, $_ unless $rm->{$dir}->{$_};
637 }
638 if (exists $add->{$dir}) {
639 push @ret, keys %{$add->{$dir}};
640 }
641 return \@ret;
642}
643
644sub handle_rmdir {
645 my ($rm, $add) = @_;
646
647 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
648 my $ls = svn_ls_current($dir, $rm, $add);
649 next if (scalar @$ls);
650 sys(qw(svn rm --force),$dir);
651
652 my $dn = dirname $dir;
653 $rm->{ $dn }->{ basename $dir } = 1;
654 $ls = svn_ls_current($dn, $rm, $add);
655 while (scalar @$ls == 0 && $dn ne File::Spec->curdir) {
656 sys(qw(svn rm --force),$dn);
657 $dir = basename $dn;
658 $dn = dirname $dn;
659 $rm->{ $dn }->{ $dir } = 1;
660 $ls = svn_ls_current($dn, $rm, $add);
3397f9df
EW
661 }
662 }
663}
664
665sub svn_commit_tree {
666 my ($svn_rev, $commit) = @_;
667 my $commit_msg = "$GIT_DIR/$GIT_SVN/.svn-commit.tmp.$$";
e17512f3 668 my %log_msg = ( msg => '' );
ac8e0b91 669 open my $msg, '>', $commit_msg or croak $!;
3397f9df
EW
670
671 chomp(my $type = `git-cat-file -t $commit`);
672 if ($type eq 'commit') {
673 my $pid = open my $msg_fh, '-|';
674 defined $pid or croak $!;
675
676 if ($pid == 0) {
677 exec(qw(git-cat-file commit), $commit) or croak $!;
678 }
679 my $in_msg = 0;
680 while (<$msg_fh>) {
681 if (!$in_msg) {
682 $in_msg = 1 if (/^\s*$/);
df746c5a
EW
683 } elsif (/^git-svn-id: /) {
684 # skip this, we regenerate the correct one
685 # on re-fetch anyways
3397f9df
EW
686 } else {
687 print $msg $_ or croak $!;
688 }
689 }
690 close $msg_fh or croak $!;
691 }
692 close $msg or croak $!;
693
694 if ($_edit || ($type eq 'tree')) {
695 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
696 system($editor, $commit_msg);
697 }
ac8e0b91
EW
698
699 # file_to_s removes all trailing newlines, so just use chomp() here:
700 open $msg, '<', $commit_msg or croak $!;
701 { local $/; chomp($log_msg{msg} = <$msg>); }
702 close $msg or croak $!;
703
704 my ($oneline) = ($log_msg{msg} =~ /([^\n\r]+)/);
705 print "Committing $commit: $oneline\n";
706
3397f9df
EW
707 my @ci_output = safe_qx(qw(svn commit -F),$commit_msg);
708 my ($committed) = grep(/^Committed revision \d+\./,@ci_output);
709 unlink $commit_msg;
710 defined $committed or croak
711 "Commit output failed to parse committed revision!\n",
712 join("\n",@ci_output),"\n";
713 my ($rev_committed) = ($committed =~ /^Committed revision (\d+)\./);
714
e17512f3 715 my @svn_up = qw(svn up);
3397f9df 716 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
e17512f3
EW
717 if ($rev_committed == ($svn_rev + 1)) {
718 push @svn_up, "-r$rev_committed";
719 sys(@svn_up);
720 my $info = svn_info('.');
721 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
722 if ($info->{'Last Changed Rev'} != $rev_committed) {
723 croak "$info->{'Last Changed Rev'} != $rev_committed\n"
724 }
725 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
726 /(\d{4})\-(\d\d)\-(\d\d)\s
727 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
728 or croak "Failed to parse date: $date\n";
729 $log_msg{date} = "$tz $Y-$m-$d $H:$M:$S";
730 $log_msg{author} = $info->{'Last Changed Author'};
731 $log_msg{revision} = $rev_committed;
732 $log_msg{msg} .= "\n";
733 my $parent = file_to_s("$REV_DIR/$svn_rev");
734 git_commit(\%log_msg, $parent, $commit);
735 return $rev_committed;
736 }
737 # resync immediately
738 push @svn_up, "-r$svn_rev";
3397f9df
EW
739 sys(@svn_up);
740 return fetch("$rev_committed=$commit")->{revision};
741}
742
03823184
EW
743# read the entire log into a temporary file (which is removed ASAP)
744# and store the file handle + parser state
3397f9df
EW
745sub svn_log_raw {
746 my (@log_args) = @_;
03823184
EW
747 my $log_fh = IO::File->new_tmpfile or croak $!;
748 my $pid = fork;
3397f9df 749 defined $pid or croak $!;
03823184
EW
750 if (!$pid) {
751 open STDOUT, '>&', $log_fh or croak $!;
3397f9df
EW
752 exec (qw(svn log), @log_args) or croak $!
753 }
03823184
EW
754 waitpid $pid, 0;
755 croak if $?;
756 seek $log_fh, 0, 0 or croak $!;
757 return { state => 'sep', fh => $log_fh };
758}
759
760sub next_log_entry {
761 my $log = shift; # retval of svn_log_raw()
762 my $ret = undef;
763 my $fh = $log->{fh};
3397f9df 764
03823184 765 while (<$fh>) {
3397f9df
EW
766 chomp;
767 if (/^\-{72}$/) {
03823184
EW
768 if ($log->{state} eq 'msg') {
769 if ($ret->{lines}) {
770 $ret->{msg} .= $_."\n";
771 unless(--$ret->{lines}) {
772 $log->{state} = 'sep';
ce6f3519
EW
773 }
774 } else {
775 croak "Log parse error at: $_\n",
03823184 776 $ret->{revision},
ce6f3519
EW
777 "\n";
778 }
779 next;
780 }
03823184 781 if ($log->{state} ne 'sep') {
ce6f3519 782 croak "Log parse error at: $_\n",
03823184
EW
783 "state: $log->{state}\n",
784 $ret->{revision},
ce6f3519
EW
785 "\n";
786 }
03823184 787 $log->{state} = 'rev';
3397f9df
EW
788
789 # if we have an empty log message, put something there:
03823184
EW
790 if ($ret) {
791 $ret->{msg} ||= "\n";
792 delete $ret->{lines};
793 return $ret;
3397f9df
EW
794 }
795 next;
796 }
03823184 797 if ($log->{state} eq 'rev' && s/^r(\d+)\s*\|\s*//) {
3397f9df 798 my $rev = $1;
ce6f3519
EW
799 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
800 ($lines) = ($lines =~ /(\d+)/);
3397f9df
EW
801 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
802 /(\d{4})\-(\d\d)\-(\d\d)\s
803 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
804 or croak "Failed to parse date: $date\n";
03823184 805 $ret = { revision => $rev,
3397f9df
EW
806 date => "$tz $Y-$m-$d $H:$M:$S",
807 author => $author,
ce6f3519 808 lines => $lines,
03823184 809 msg => '' };
a9612be2
EW
810 if (defined $_authors && ! defined $users{$author}) {
811 die "Author: $author not defined in ",
812 "$_authors file\n";
813 }
03823184 814 $log->{state} = 'msg_start';
3397f9df
EW
815 next;
816 }
817 # skip the first blank line of the message:
03823184
EW
818 if ($log->{state} eq 'msg_start' && /^$/) {
819 $log->{state} = 'msg';
820 } elsif ($log->{state} eq 'msg') {
821 if ($ret->{lines}) {
822 $ret->{msg} .= $_."\n";
823 unless (--$ret->{lines}) {
824 $log->{state} = 'sep';
ce6f3519
EW
825 }
826 } else {
827 croak "Log parse error at: $_\n",
03823184 828 $ret->{revision},"\n";
ce6f3519 829 }
3397f9df
EW
830 }
831 }
03823184 832 return $ret;
3397f9df
EW
833}
834
835sub svn_info {
836 my $url = shift || $SVN_URL;
837
838 my $pid = open my $info_fh, '-|';
839 defined $pid or croak $!;
840
841 if ($pid == 0) {
842 exec(qw(svn info),$url) or croak $!;
843 }
844
845 my $ret = {};
846 # only single-lines seem to exist in svn info output
847 while (<$info_fh>) {
848 chomp $_;
e17512f3 849 if (m#^([^:]+)\s*:\s*(\S.*)$#) {
3397f9df
EW
850 $ret->{$1} = $2;
851 push @{$ret->{-order}}, $1;
852 }
853 }
854 close $info_fh or croak $!;
855 return $ret;
856}
857
858sub sys { system(@_) == 0 or croak $? }
859
36f5b1f0
EW
860sub eol_cp {
861 my ($from, $to) = @_;
8a97e368 862 my $es = svn_propget_base('svn:eol-style', $to);
36f5b1f0
EW
863 open my $rfd, '<', $from or croak $!;
864 binmode $rfd or croak $!;
865 open my $wfd, '>', $to or croak $!;
866 binmode $wfd or croak $!;
867
868 my $eol = $EOL{$es} or undef;
869 if ($eol) {
870 print "$eol: $from => $to\n";
871 }
872 my $buf;
873 while (1) {
874 my ($r, $w, $t);
875 defined($r = sysread($rfd, $buf, 4096)) or croak $!;
876 return unless $r;
877 $buf =~ s/(?:\015|\012|\015\012)/$eol/gs if $eol;
878 for ($w = 0; $w < $r; $w += $t) {
879 $t = syswrite($wfd, $buf, $r - $w, $w) or croak $!;
880 }
881 }
882}
883
884sub do_update_index {
885 my ($z_cmd, $cmd, $no_text_base) = @_;
886
887 my $z = open my $p, '-|';
888 defined $z or croak $!;
889 unless ($z) { exec @$z_cmd or croak $! }
890
891 my $pid = open my $ui, '|-';
892 defined $pid or croak $!;
893 unless ($pid) {
894 exec('git-update-index',"--$cmd",'-z','--stdin') or croak $!;
895 }
896 local $/ = "\0";
897 while (my $x = <$p>) {
898 chomp $x;
899 if (!$no_text_base && lstat $x && ! -l _ &&
8a97e368 900 svn_propget_base('svn:keywords', $x)) {
36f5b1f0
EW
901 my $mode = -x _ ? 0755 : 0644;
902 my ($v,$d,$f) = File::Spec->splitpath($x);
903 my $tb = File::Spec->catfile($d, '.svn', 'tmp',
904 'text-base',"$f.svn-base");
905 $tb =~ s#^/##;
906 unless (-f $tb) {
907 $tb = File::Spec->catfile($d, '.svn',
908 'text-base',"$f.svn-base");
909 $tb =~ s#^/##;
910 }
911 unlink $x or croak $!;
912 eol_cp($tb, $x);
913 chmod(($mode &~ umask), $x) or croak $!;
914 }
915 print $ui $x,"\0";
916 }
917 close $ui or croak $!;
918}
919
920sub index_changes {
921 my $no_text_base = shift;
922 do_update_index([qw/git-diff-files --name-only -z/],
923 'remove',
924 $no_text_base);
925 do_update_index([qw/git-ls-files -z --others/,
926 "--exclude-from=$GIT_DIR/$GIT_SVN/info/exclude"],
927 'add',
928 $no_text_base);
3397f9df
EW
929}
930
931sub s_to_file {
932 my ($str, $file, $mode) = @_;
933 open my $fd,'>',$file or croak $!;
934 print $fd $str,"\n" or croak $!;
935 close $fd or croak $!;
936 chmod ($mode &~ umask, $file) if (defined $mode);
937}
938
939sub file_to_s {
940 my $file = shift;
941 open my $fd,'<',$file or croak "$!: file: $file\n";
942 local $/;
943 my $ret = <$fd>;
944 close $fd or croak $!;
945 $ret =~ s/\s*$//s;
946 return $ret;
947}
948
949sub assert_revision_unknown {
950 my $revno = shift;
951 if (-f "$REV_DIR/$revno") {
952 croak "$REV_DIR/$revno already exists! ",
953 "Why are we refetching it?";
954 }
955}
956
ac749050
EW
957sub trees_eq {
958 my ($x, $y) = @_;
959 my @x = safe_qx('git-cat-file','commit',$x);
960 my @y = safe_qx('git-cat-file','commit',$y);
961 if (($y[0] ne $x[0]) || $x[0] !~ /^tree $sha1\n$/
962 || $y[0] !~ /^tree $sha1\n$/) {
963 print STDERR "Trees not equal: $y[0] != $x[0]\n";
964 return 0
965 }
966 return 1;
967}
968
3397f9df
EW
969sub assert_revision_eq_or_unknown {
970 my ($revno, $commit) = @_;
971 if (-f "$REV_DIR/$revno") {
972 my $current = file_to_s("$REV_DIR/$revno");
ac749050 973 if (($commit ne $current) && !trees_eq($commit, $current)) {
3397f9df
EW
974 croak "$REV_DIR/$revno already exists!\n",
975 "current: $current\nexpected: $commit\n";
976 }
977 return;
978 }
979}
980
981sub git_commit {
982 my ($log_msg, @parents) = @_;
983 assert_revision_unknown($log_msg->{revision});
984 my $out_fh = IO::File->new_tmpfile or croak $!;
3397f9df 985
69f0d91e
EW
986 map_tree_joins() if (@_branch_from && !%tree_map);
987
3397f9df
EW
988 # commit parents can be conditionally bound to a particular
989 # svn revision via: "svn_revno=commit_sha1", filter them out here:
990 my @exec_parents;
991 foreach my $p (@parents) {
992 next unless defined $p;
993 if ($p =~ /^(\d+)=($sha1_short)$/o) {
994 if ($1 == $log_msg->{revision}) {
995 push @exec_parents, $2;
996 }
997 } else {
998 push @exec_parents, $p if $p =~ /$sha1_short/o;
999 }
1000 }
1001
1002 my $pid = fork;
1003 defined $pid or croak $!;
1004 if ($pid == 0) {
1005 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
36f5b1f0 1006 index_changes();
3397f9df
EW
1007 chomp(my $tree = `git-write-tree`);
1008 croak if $?;
69f0d91e
EW
1009 if (exists $tree_map{$tree}) {
1010 my %seen_parent = map { $_ => 1 } @exec_parents;
1011 foreach (@{$tree_map{$tree}}) {
1012 # MAXPARENT is defined to 16 in commit-tree.c:
1013 if ($seen_parent{$_} || @exec_parents > 16) {
1014 next;
1015 }
1016 push @exec_parents, $_;
1017 $seen_parent{$_} = 1;
1018 }
1019 }
3397f9df
EW
1020 my $msg_fh = IO::File->new_tmpfile or croak $!;
1021 print $msg_fh $log_msg->{msg}, "\ngit-svn-id: ",
1022 "$SVN_URL\@$log_msg->{revision}",
1ca72aef 1023 " $SVN_UUID\n" or croak $!;
3397f9df
EW
1024 $msg_fh->flush == 0 or croak $!;
1025 seek $msg_fh, 0, 0 or croak $!;
1026
1ca72aef 1027 set_commit_env($log_msg);
a9612be2 1028
3397f9df
EW
1029 my @exec = ('git-commit-tree',$tree);
1030 push @exec, '-p', $_ foreach @exec_parents;
1031 open STDIN, '<&', $msg_fh or croak $!;
1032 open STDOUT, '>&', $out_fh or croak $!;
1033 exec @exec or croak $!;
1034 }
1035 waitpid($pid,0);
1036 croak if $?;
1037
1038 $out_fh->flush == 0 or croak $!;
1039 seek $out_fh, 0, 0 or croak $!;
1040 chomp(my $commit = do { local $/; <$out_fh> });
1041 if ($commit !~ /^$sha1$/o) {
1042 croak "Failed to commit, invalid sha1: $commit\n";
1043 }
2beb3cdd 1044 my @update_ref = ('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
3397f9df 1045 if (my $primary_parent = shift @exec_parents) {
1d52aba8
EW
1046 $pid = fork;
1047 defined $pid or croak $!;
1048 if (!$pid) {
1049 close STDERR;
1050 close STDOUT;
1051 exec 'git-rev-parse','--verify',
1052 "refs/remotes/$GIT_SVN^0";
1053 }
1054 waitpid $pid, 0;
1055 push @update_ref, $primary_parent unless $?;
3397f9df
EW
1056 }
1057 sys(@update_ref);
1058 sys('git-update-ref',"$GIT_SVN/revs/$log_msg->{revision}",$commit);
1059 print "r$log_msg->{revision} = $commit\n";
1060 return $commit;
1061}
1062
a9612be2 1063sub set_commit_env {
1ca72aef 1064 my ($log_msg) = @_;
a9612be2
EW
1065 my $author = $log_msg->{author};
1066 my ($name,$email) = defined $users{$author} ? @{$users{$author}}
1ca72aef 1067 : ($author,"$author\@$SVN_UUID");
a9612be2
EW
1068 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name;
1069 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = $email;
1070 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_msg->{date};
1071}
1072
cf52b8f0
EW
1073sub apply_mod_line_blob {
1074 my $m = shift;
1075 if ($m->{mode_b} =~ /^120/) {
1076 blob_to_symlink($m->{sha1_b}, $m->{file_b});
1077 } else {
1078 blob_to_file($m->{sha1_b}, $m->{file_b});
1079 }
1080}
1081
3397f9df
EW
1082sub blob_to_symlink {
1083 my ($blob, $link) = @_;
1084 defined $link or croak "\$link not defined!\n";
1085 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
cf52b8f0
EW
1086 if (-l $link || -f _) {
1087 unlink $link or croak $!;
1088 }
1089
3397f9df
EW
1090 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
1091 symlink $dest, $link or croak $!;
1092}
1093
1094sub blob_to_file {
1095 my ($blob, $file) = @_;
1096 defined $file or croak "\$file not defined!\n";
1097 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
cf52b8f0
EW
1098 if (-l $file || -f _) {
1099 unlink $file or croak $!;
1100 }
1101
3397f9df
EW
1102 open my $blob_fh, '>', $file or croak "$!: $file\n";
1103 my $pid = fork;
1104 defined $pid or croak $!;
1105
1106 if ($pid == 0) {
1107 open STDOUT, '>&', $blob_fh or croak $!;
1108 exec('git-cat-file','blob',$blob);
1109 }
1110 waitpid $pid, 0;
1111 croak $? if $?;
1112
1113 close $blob_fh or croak $!;
1114}
1115
1116sub safe_qx {
1117 my $pid = open my $child, '-|';
1118 defined $pid or croak $!;
1119 if ($pid == 0) {
1120 exec(@_) or croak $?;
1121 }
1122 my @ret = (<$child>);
1123 close $child or croak $?;
1124 die $? if $?; # just in case close didn't error out
1125 return wantarray ? @ret : join('',@ret);
1126}
1127
1d52aba8
EW
1128sub svn_compat_check {
1129 my @co_help = safe_qx(qw(svn co -h));
1130 unless (grep /ignore-externals/,@co_help) {
3397f9df
EW
1131 print STDERR "W: Installed svn version does not support ",
1132 "--ignore-externals\n";
1133 $_no_ignore_ext = 1;
1134 }
1d52aba8
EW
1135 if (grep /usage: checkout URL\[\@REV\]/,@co_help) {
1136 $_svn_co_url_revs = 1;
1137 }
8a97e368
EW
1138 if (grep /\[TARGET\[\@REV\]\.\.\.\]/, `svn propget -h`) {
1139 $_svn_pg_peg_revs = 1;
1140 }
7317ed90
EW
1141
1142 # I really, really hope nobody hits this...
1143 unless (grep /stop-on-copy/, (safe_qx(qw(svn log -h)))) {
1144 print STDERR <<'';
1145W: The installed svn version does not support the --stop-on-copy flag in
1146 the log command.
1147 Lets hope the directory you're tracking is not a branch or tag
1148 and was never moved within the repository...
1149
1150 $_no_stop_copy = 1;
1151 }
1d52aba8
EW
1152}
1153
1154# *sigh*, new versions of svn won't honor -r<rev> without URL@<rev>,
1155# (and they won't honor URL@<rev> without -r<rev>, too!)
1156sub svn_cmd_checkout {
1157 my ($url, $rev, $dir) = @_;
1158 my @cmd = ('svn','co', "-r$rev");
1159 push @cmd, '--ignore-externals' unless $_no_ignore_ext;
1160 $url .= "\@$rev" if $_svn_co_url_revs;
1161 sys(@cmd, $url, $dir);
3397f9df 1162}
2beb3cdd
EW
1163
1164sub check_upgrade_needed {
1165 my $old = eval {
1166 my $pid = open my $child, '-|';
1167 defined $pid or croak $!;
1168 if ($pid == 0) {
1169 close STDERR;
1170 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $?;
1171 }
1172 my @ret = (<$child>);
1173 close $child or croak $?;
1174 die $? if $?; # just in case close didn't error out
1175 return wantarray ? @ret : join('',@ret);
1176 };
1177 return unless $old;
1178 my $head = eval { safe_qx('git-rev-parse',"refs/remotes/$GIT_SVN") };
1179 if ($@ || !$head) {
1180 print STDERR "Please run: $0 rebuild --upgrade\n";
1181 exit 1;
1182 }
1183}
1184
69f0d91e
EW
1185# fills %tree_map with a reverse mapping of trees to commits. Useful
1186# for finding parents to commit on.
1187sub map_tree_joins {
1188 foreach my $br (@_branch_from) {
1189 my $pid = open my $pipe, '-|';
1190 defined $pid or croak $!;
1191 if ($pid == 0) {
1192 exec(qw(git-rev-list --pretty=raw), $br) or croak $?;
1193 }
1194 while (<$pipe>) {
1195 if (/^commit ($sha1)$/o) {
1196 my $commit = $1;
1197 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
1198 unless (defined $tree) {
1199 die "Failed to parse commit $commit\n";
1200 }
1201 push @{$tree_map{$tree}}, $commit;
1202 }
1203 }
1204 close $pipe or croak $?;
1205 }
1206}
1207
eeb0abe0
EW
1208# '<svn username> = real-name <email address>' mapping based on git-svnimport:
1209sub load_authors {
1210 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1211 while (<$authors>) {
1212 chomp;
1213 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
1214 my ($user, $name, $email) = ($1, $2, $3);
1215 $users{$user} = [$name, $email];
1216 }
1217 close $authors or croak $!;
1218}
1219
8a97e368
EW
1220sub svn_propget_base {
1221 my ($p, $f) = @_;
1222 $f .= '@BASE' if $_svn_pg_peg_revs;
1223 return safe_qx(qw/svn propget/, $p, $f);
1224}
1225
3397f9df
EW
1226__END__
1227
1228Data structures:
1229
03823184
EW
1230$svn_log hashref (as returned by svn_log_raw)
1231{
1232 fh => file handle of the log file,
1233 state => state of the log file parser (sep/msg/rev/msg_start...)
1234}
3397f9df 1235
03823184 1236$log_msg hashref as returned by next_log_entry($svn_log)
3397f9df
EW
1237{
1238 msg => 'whitespace-formatted log entry
1239', # trailing newline is preserved
1240 revision => '8', # integer
1241 date => '2004-02-24T17:01:44.108345Z', # commit date
1242 author => 'committer name'
1243};
1244
1245
1246@mods = array of diff-index line hashes, each element represents one line
1247 of diff-index output
1248
1249diff-index line ($m hash)
1250{
1251 mode_a => first column of diff-index output, no leading ':',
1252 mode_b => second column of diff-index output,
1253 sha1_b => sha1sum of the final blob,
ac8e0b91 1254 chg => change type [MCRADT],
3397f9df
EW
1255 file_a => original file name of a file (iff chg is 'C' or 'R')
1256 file_b => new/current file name of a file (any chg)
1257}
1258;