contrib/git-svn: use refs/remotes/git-svn instead of git-svn-HEAD
[git.git] / contrib / git-svn / git-svn.perl
1 #!/usr/bin/env perl
2 # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3 # License: GPL v2 or later
4 use warnings;
5 use strict;
6 use vars qw/    $AUTHOR $VERSION
7                 $SVN_URL $SVN_INFO $SVN_WC
8                 $GIT_SVN_INDEX $GIT_SVN
9                 $GIT_DIR $REV_DIR/;
10 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
11 $VERSION = '0.10.0';
12 $GIT_DIR = $ENV{GIT_DIR} || "$ENV{PWD}/.git";
13 $GIT_SVN = $ENV{GIT_SVN_ID} || 'git-svn';
14 $GIT_SVN_INDEX = "$GIT_DIR/$GIT_SVN/index";
15 $ENV{GIT_DIR} ||= $GIT_DIR;
16 $SVN_URL = undef;
17 $REV_DIR = "$GIT_DIR/$GIT_SVN/revs";
18 $SVN_WC = "$GIT_DIR/$GIT_SVN/tree";
19
20 # make sure the svn binary gives consistent output between locales and TZs:
21 $ENV{TZ} = 'UTC';
22 $ENV{LC_ALL} = 'C';
23
24 # If SVN:: library support is added, please make the dependencies
25 # optional and preserve the capability to use the command-line client.
26 # use eval { require SVN::... } to make it lazy load
27 use Carp qw/croak/;
28 use IO::File qw//;
29 use File::Basename qw/dirname basename/;
30 use File::Path qw/mkpath/;
31 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
32 use File::Spec qw//;
33 use POSIX qw/strftime/;
34 my $sha1 = qr/[a-f\d]{40}/;
35 my $sha1_short = qr/[a-f\d]{6,40}/;
36 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
37         $_find_copies_harder, $_l, $_version, $_upgrade);
38
39 GetOptions(     'revision|r=s' => \$_revision,
40                 'no-ignore-externals' => \$_no_ignore_ext,
41                 'stdin|' => \$_stdin,
42                 'edit|e' => \$_edit,
43                 'rmdir' => \$_rmdir,
44                 'upgrade' => \$_upgrade,
45                 'help|H|h' => \$_help,
46                 'find-copies-harder' => \$_find_copies_harder,
47                 'l=i' => \$_l,
48                 'version|V' => \$_version,
49                 'no-stop-on-copy' => \$_no_stop_copy );
50 my %cmd = (
51         fetch => [ \&fetch, "Download new revisions from SVN" ],
52         init => [ \&init, "Initialize and fetch (import)"],
53         commit => [ \&commit, "Commit git revisions to SVN" ],
54         'show-ignore' => [ \&show_ignore, "Show svn:ignore listings" ],
55         rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)" ],
56         help => [ \&usage, "Show help" ],
57 );
58 my $cmd;
59 for (my $i = 0; $i < @ARGV; $i++) {
60         if (defined $cmd{$ARGV[$i]}) {
61                 $cmd = $ARGV[$i];
62                 splice @ARGV, $i, 1;
63                 last;
64         }
65 };
66
67 # we may be called as git-svn-(command), or git-svn(command).
68 foreach (keys %cmd) {
69         if (/git\-svn\-?($_)(?:\.\w+)?$/) {
70                 $cmd = $1;
71                 last;
72         }
73 }
74 usage(0) if $_help;
75 version() if $_version;
76 usage(1) unless (defined $cmd);
77 svn_check_ignore_externals();
78 $cmd{$cmd}->[0]->(@ARGV);
79 exit 0;
80
81 ####################### primary functions ######################
82 sub usage {
83         my $exit = shift || 0;
84         my $fd = $exit ? \*STDERR : \*STDOUT;
85         print $fd <<"";
86 git-svn - bidirectional operations between a single Subversion tree and git
87 Usage: $0 <command> [options] [arguments]\n
88 Available commands:
89
90         foreach (sort keys %cmd) {
91                 print $fd '  ',pack('A10',$_),$cmd{$_}->[1],"\n";
92         }
93         print $fd <<"";
94 \nGIT_SVN_ID may be set in the environment to an arbitrary identifier if
95 you're tracking multiple SVN branches/repositories in one git repository
96 and want to keep them separate.
97
98         exit $exit;
99 }
100
101 sub version {
102         print "git-svn version $VERSION\n";
103         exit 0;
104 }
105
106 sub rebuild {
107         $SVN_URL = shift or undef;
108         my $repo_uuid;
109         my $newest_rev = 0;
110         if ($_upgrade) {
111                 sys('git-update-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
112         } else {
113                 check_upgrade_needed();
114         }
115
116         my $pid = open(my $rev_list,'-|');
117         defined $pid or croak $!;
118         if ($pid == 0) {
119                 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
120         }
121         my $latest;
122         while (<$rev_list>) {
123                 chomp;
124                 my $c = $_;
125                 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
126                 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
127                 next if (!@commit); # skip merges
128                 my $id = $commit[$#commit];
129                 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
130                                                 \s([a-f\d\-]+)$/x);
131                 if (!$rev || !$uuid || !$url) {
132                         # some of the original repositories I made had
133                         # indentifiers like this:
134                         ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)
135                                                         \@([a-f\d\-]+)/x);
136                         if (!$rev || !$uuid) {
137                                 croak "Unable to extract revision or UUID from ",
138                                         "$c, $id\n";
139                         }
140                 }
141
142                 # if we merged or otherwise started elsewhere, this is
143                 # how we break out of it
144                 next if (defined $repo_uuid && ($uuid ne $repo_uuid));
145                 next if (defined $SVN_URL && ($url ne $SVN_URL));
146
147                 print "r$rev = $c\n";
148                 unless (defined $latest) {
149                         if (!$SVN_URL && !$url) {
150                                 croak "SVN repository location required: $url\n";
151                         }
152                         $SVN_URL ||= $url;
153                         $repo_uuid ||= setup_git_svn();
154                         $latest = $rev;
155                 }
156                 assert_revision_eq_or_unknown($rev, $c);
157                 sys('git-update-ref',"$GIT_SVN/revs/$rev",$c);
158                 $newest_rev = $rev if ($rev > $newest_rev);
159         }
160         close $rev_list or croak $?;
161         if (!chdir $SVN_WC) {
162                 my @svn_co = ('svn','co',"-r$latest");
163                 push @svn_co, '--ignore-externals' unless $_no_ignore_ext;
164                 sys(@svn_co, $SVN_URL, $SVN_WC);
165                 chdir $SVN_WC or croak $!;
166         }
167
168         $pid = fork;
169         defined $pid or croak $!;
170         if ($pid == 0) {
171                 my @svn_up = qw(svn up);
172                 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
173                 sys(@svn_up,"-r$newest_rev");
174                 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
175                 git_addremove();
176                 exec('git-write-tree');
177         }
178         waitpid $pid, 0;
179
180         if ($_upgrade) {
181                 print STDERR <<"";
182 Keeping deprecated refs/head/$GIT_SVN-HEAD for now.  Please remove it
183 when you have upgraded your tools and habits to use refs/remotes/$GIT_SVN
184
185         }
186 }
187
188 sub init {
189         $SVN_URL = shift or croak "SVN repository location required\n";
190         unless (-d $GIT_DIR) {
191                 sys('git-init-db');
192         }
193         setup_git_svn();
194 }
195
196 sub fetch {
197         my (@parents) = @_;
198         check_upgrade_needed();
199         $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
200         my @log_args = -d $SVN_WC ? ($SVN_WC) : ($SVN_URL);
201         unless ($_revision) {
202                 $_revision = -d $SVN_WC ? 'BASE:HEAD' : '0:HEAD';
203         }
204         push @log_args, "-r$_revision";
205         push @log_args, '--stop-on-copy' unless $_no_stop_copy;
206
207         my $svn_log = svn_log_raw(@log_args);
208         @$svn_log = sort { $a->{revision} <=> $b->{revision} } @$svn_log;
209
210         my $base = shift @$svn_log or croak "No base revision!\n";
211         my $last_commit = undef;
212         unless (-d $SVN_WC) {
213                 my @svn_co = ('svn','co',"-r$base->{revision}");
214                 push @svn_co,'--ignore-externals' unless $_no_ignore_ext;
215                 sys(@svn_co, $SVN_URL, $SVN_WC);
216                 chdir $SVN_WC or croak $!;
217                 $last_commit = git_commit($base, @parents);
218                 unless (-f "$GIT_DIR/refs/heads/master") {
219                         sys(qw(git-update-ref refs/heads/master),$last_commit);
220                 }
221                 assert_svn_wc_clean($base->{revision}, $last_commit);
222         } else {
223                 chdir $SVN_WC or croak $!;
224                 $last_commit = file_to_s("$REV_DIR/$base->{revision}");
225         }
226         my @svn_up = qw(svn up);
227         push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
228         my $last_rev = $base->{revision};
229         foreach my $log_msg (@$svn_log) {
230                 assert_svn_wc_clean($last_rev, $last_commit);
231                 $last_rev = $log_msg->{revision};
232                 sys(@svn_up,"-r$last_rev");
233                 $last_commit = git_commit($log_msg, $last_commit, @parents);
234         }
235         assert_svn_wc_clean($last_rev, $last_commit);
236         return pop @$svn_log;
237 }
238
239 sub commit {
240         my (@commits) = @_;
241         check_upgrade_needed();
242         if ($_stdin || !@commits) {
243                 print "Reading from stdin...\n";
244                 @commits = ();
245                 while (<STDIN>) {
246                         if (/\b([a-f\d]{6,40})\b/) {
247                                 unshift @commits, $1;
248                         }
249                 }
250         }
251         my @revs;
252         foreach my $c (@commits) {
253                 chomp(my @tmp = safe_qx('git-rev-parse',$c));
254                 if (scalar @tmp == 1) {
255                         push @revs, $tmp[0];
256                 } elsif (scalar @tmp > 1) {
257                         push @revs, reverse (safe_qx('git-rev-list',@tmp));
258                 } else {
259                         die "Failed to rev-parse $c\n";
260                 }
261         }
262         chomp @revs;
263
264         fetch();
265         chdir $SVN_WC or croak $!;
266         my $svn_current_rev =  svn_info('.')->{'Last Changed Rev'};
267         foreach my $c (@revs) {
268                 print "Committing $c\n";
269                 my $mods = svn_checkout_tree($svn_current_rev, $c);
270                 if (scalar @$mods == 0) {
271                         print "Skipping, no changes detected\n";
272                         next;
273                 }
274                 $svn_current_rev = svn_commit_tree($svn_current_rev, $c);
275         }
276         print "Done committing ",scalar @revs," revisions to SVN\n";
277
278 }
279
280 sub show_ignore {
281         require File::Find or die $!;
282         my $exclude_file = "$GIT_DIR/info/exclude";
283         open my $fh, '<', $exclude_file or croak $!;
284         chomp(my @excludes = (<$fh>));
285         close $fh or croak $!;
286
287         $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
288         chdir $SVN_WC or croak $!;
289         my %ign;
290         File::Find::find({wanted=>sub{if(lstat $_ && -d _ && -d "$_/.svn"){
291                 s#^\./##;
292                 @{$ign{$_}} = safe_qx(qw(svn propget svn:ignore),$_);
293                 }}, no_chdir=>1},'.');
294
295         print "\n# /\n";
296         foreach (@{$ign{'.'}}) { print '/',$_ if /\S/ }
297         delete $ign{'.'};
298         foreach my $i (sort keys %ign) {
299                 print "\n# ",$i,"\n";
300                 foreach (@{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
301         }
302 }
303
304 ########################### utility functions #########################
305
306 sub setup_git_svn {
307         defined $SVN_URL or croak "SVN repository location required\n";
308         unless (-d $GIT_DIR) {
309                 croak "GIT_DIR=$GIT_DIR does not exist!\n";
310         }
311         mkpath(["$GIT_DIR/$GIT_SVN"]);
312         mkpath(["$GIT_DIR/$GIT_SVN/info"]);
313         mkpath([$REV_DIR]);
314         s_to_file($SVN_URL,"$GIT_DIR/$GIT_SVN/info/url");
315         my $uuid = svn_info($SVN_URL)->{'Repository UUID'} or
316                                         croak "Repository UUID unreadable\n";
317         s_to_file($uuid,"$GIT_DIR/$GIT_SVN/info/uuid");
318
319         open my $fd, '>>', "$GIT_DIR/$GIT_SVN/info/exclude" or croak $!;
320         print $fd '.svn',"\n";
321         close $fd or croak $!;
322         return $uuid;
323 }
324
325 sub assert_svn_wc_clean {
326         my ($svn_rev, $treeish) = @_;
327         croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
328         croak "$treeish is not a sha1!\n" unless ($treeish =~ /^$sha1$/o);
329         my $svn_info = svn_info('.');
330         if ($svn_rev != $svn_info->{'Last Changed Rev'}) {
331                 croak "Expected r$svn_rev, got r",
332                                 $svn_info->{'Last Changed Rev'},"\n";
333         }
334         my @status = grep(!/^Performing status on external/,(`svn status`));
335         @status = grep(!/^\s*$/,@status);
336         if (scalar @status) {
337                 print STDERR "Tree ($SVN_WC) is not clean:\n";
338                 print STDERR $_ foreach @status;
339                 croak;
340         }
341         assert_tree($treeish);
342 }
343
344 sub assert_tree {
345         my ($treeish) = @_;
346         croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
347         chomp(my $type = `git-cat-file -t $treeish`);
348         my $expected;
349         while ($type eq 'tag') {
350                 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
351         }
352         if ($type eq 'commit') {
353                 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
354                 ($expected) = ($expected =~ /^tree ($sha1)$/);
355                 die "Unable to get tree from $treeish\n" unless $expected;
356         } elsif ($type eq 'tree') {
357                 $expected = $treeish;
358         } else {
359                 die "$treeish is a $type, expected tree, tag or commit\n";
360         }
361
362         my $old_index = $ENV{GIT_INDEX_FILE};
363         my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
364         if (-e $tmpindex) {
365                 unlink $tmpindex or croak $!;
366         }
367         $ENV{GIT_INDEX_FILE} = $tmpindex;
368         git_addremove();
369         chomp(my $tree = `git-write-tree`);
370         if ($old_index) {
371                 $ENV{GIT_INDEX_FILE} = $old_index;
372         } else {
373                 delete $ENV{GIT_INDEX_FILE};
374         }
375         if ($tree ne $expected) {
376                 croak "Tree mismatch, Got: $tree, Expected: $expected\n";
377         }
378 }
379
380 sub parse_diff_tree {
381         my $diff_fh = shift;
382         local $/ = "\0";
383         my $state = 'meta';
384         my @mods;
385         while (<$diff_fh>) {
386                 chomp $_; # this gets rid of the trailing "\0"
387                 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
388                                         $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
389                         push @mods, {   mode_a => $1, mode_b => $2,
390                                         sha1_b => $3, chg => $4 };
391                         if ($4 =~ /^(?:C|R)$/) {
392                                 $state = 'file_a';
393                         } else {
394                                 $state = 'file_b';
395                         }
396                 } elsif ($state eq 'file_a') {
397                         my $x = $mods[$#mods] or croak "Empty array\n";
398                         if ($x->{chg} !~ /^(?:C|R)$/) {
399                                 croak "Error parsing $_, $x->{chg}\n";
400                         }
401                         $x->{file_a} = $_;
402                         $state = 'file_b';
403                 } elsif ($state eq 'file_b') {
404                         my $x = $mods[$#mods] or croak "Empty array\n";
405                         if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
406                                 croak "Error parsing $_, $x->{chg}\n";
407                         }
408                         if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
409                                 croak "Error parsing $_, $x->{chg}\n";
410                         }
411                         $x->{file_b} = $_;
412                         $state = 'meta';
413                 } else {
414                         croak "Error parsing $_\n";
415                 }
416         }
417         close $diff_fh or croak $!;
418
419         return \@mods;
420 }
421
422 sub svn_check_prop_executable {
423         my $m = shift;
424         return if -l $m->{file_b};
425         if ($m->{mode_b} =~ /755$/) {
426                 chmod((0755 &~ umask),$m->{file_b}) or croak $!;
427                 if ($m->{mode_a} !~ /755$/) {
428                         sys(qw(svn propset svn:executable 1), $m->{file_b});
429                 }
430                 -x $m->{file_b} or croak "$m->{file_b} is not executable!\n";
431         } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
432                 sys(qw(svn propdel svn:executable), $m->{file_b});
433                 chmod((0644 &~ umask),$m->{file_b}) or croak $!;
434                 -x $m->{file_b} and croak "$m->{file_b} is executable!\n";
435         }
436 }
437
438 sub svn_ensure_parent_path {
439         my $dir_b = dirname(shift);
440         svn_ensure_parent_path($dir_b) if ($dir_b ne File::Spec->curdir);
441         mkpath([$dir_b]) unless (-d $dir_b);
442         sys(qw(svn add -N), $dir_b) unless (-d "$dir_b/.svn");
443 }
444
445 sub precommit_check {
446         my $mods = shift;
447         my (%rm_file, %rmdir_check, %added_check);
448
449         my %o = ( D => 0, R => 1, C => 2, A => 3, M => 3, T => 3 );
450         foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
451                 if ($m->{chg} eq 'R') {
452                         if (-d $m->{file_b}) {
453                                 err_dir_to_file("$m->{file_a} => $m->{file_b}");
454                         }
455                         # dir/$file => dir/file/$file
456                         my $dirname = dirname($m->{file_b});
457                         while ($dirname ne File::Spec->curdir) {
458                                 if ($dirname ne $m->{file_a}) {
459                                         $dirname = dirname($dirname);
460                                         next;
461                                 }
462                                 err_file_to_dir("$m->{file_a} => $m->{file_b}");
463                         }
464                         # baz/zzz => baz (baz is a file)
465                         $dirname = dirname($m->{file_a});
466                         while ($dirname ne File::Spec->curdir) {
467                                 if ($dirname ne $m->{file_b}) {
468                                         $dirname = dirname($dirname);
469                                         next;
470                                 }
471                                 err_dir_to_file("$m->{file_a} => $m->{file_b}");
472                         }
473                 }
474                 if ($m->{chg} =~ /^(D|R)$/) {
475                         my $t = $1 eq 'D' ? 'file_b' : 'file_a';
476                         $rm_file{ $m->{$t} } = 1;
477                         my $dirname = dirname( $m->{$t} );
478                         my $basename = basename( $m->{$t} );
479                         $rmdir_check{$dirname}->{$basename} = 1;
480                 } elsif ($m->{chg} =~ /^(?:A|C)$/) {
481                         if (-d $m->{file_b}) {
482                                 err_dir_to_file($m->{file_b});
483                         }
484                         my $dirname = dirname( $m->{file_b} );
485                         my $basename = basename( $m->{file_b} );
486                         $added_check{$dirname}->{$basename} = 1;
487                         while ($dirname ne File::Spec->curdir) {
488                                 if ($rm_file{$dirname}) {
489                                         err_file_to_dir($m->{file_b});
490                                 }
491                                 $dirname = dirname $dirname;
492                         }
493                 }
494         }
495         return (\%rmdir_check, \%added_check);
496
497         sub err_dir_to_file {
498                 my $file = shift;
499                 print STDERR "Node change from directory to file ",
500                                 "is not supported by Subversion: ",$file,"\n";
501                 exit 1;
502         }
503         sub err_file_to_dir {
504                 my $file = shift;
505                 print STDERR "Node change from file to directory ",
506                                 "is not supported by Subversion: ",$file,"\n";
507                 exit 1;
508         }
509 }
510
511 sub svn_checkout_tree {
512         my ($svn_rev, $treeish) = @_;
513         my $from = file_to_s("$REV_DIR/$svn_rev");
514         assert_svn_wc_clean($svn_rev,$from);
515         print "diff-tree '$from' '$treeish'\n";
516         my $pid = open my $diff_fh, '-|';
517         defined $pid or croak $!;
518         if ($pid == 0) {
519                 my @diff_tree = qw(git-diff-tree -z -r -C);
520                 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
521                 push @diff_tree, "-l$_l" if defined $_l;
522                 exec(@diff_tree, $from, $treeish) or croak $!;
523         }
524         my $mods = parse_diff_tree($diff_fh);
525         unless (@$mods) {
526                 # git can do empty commits, SVN doesn't allow it...
527                 return $mods;
528         }
529         my ($rm, $add) = precommit_check($mods);
530
531         my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
532         foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
533                 if ($m->{chg} eq 'C') {
534                         svn_ensure_parent_path( $m->{file_b} );
535                         sys(qw(svn cp),         $m->{file_a}, $m->{file_b});
536                         apply_mod_line_blob($m);
537                         svn_check_prop_executable($m);
538                 } elsif ($m->{chg} eq 'D') {
539                         sys(qw(svn rm --force), $m->{file_b});
540                 } elsif ($m->{chg} eq 'R') {
541                         svn_ensure_parent_path( $m->{file_b} );
542                         sys(qw(svn mv --force), $m->{file_a}, $m->{file_b});
543                         apply_mod_line_blob($m);
544                         svn_check_prop_executable($m);
545                 } elsif ($m->{chg} eq 'M') {
546                         apply_mod_line_blob($m);
547                         svn_check_prop_executable($m);
548                 } elsif ($m->{chg} eq 'T') {
549                         sys(qw(svn rm --force),$m->{file_b});
550                         apply_mod_line_blob($m);
551                         sys(qw(svn add --force), $m->{file_b});
552                         svn_check_prop_executable($m);
553                 } elsif ($m->{chg} eq 'A') {
554                         svn_ensure_parent_path( $m->{file_b} );
555                         apply_mod_line_blob($m);
556                         sys(qw(svn add --force), $m->{file_b});
557                         svn_check_prop_executable($m);
558                 } else {
559                         croak "Invalid chg: $m->{chg}\n";
560                 }
561         }
562
563         assert_tree($treeish);
564         if ($_rmdir) { # remove empty directories
565                 handle_rmdir($rm, $add);
566         }
567         assert_tree($treeish);
568         return $mods;
569 }
570
571 # svn ls doesn't work with respect to the current working tree, but what's
572 # in the repository.  There's not even an option for it... *sigh*
573 # (added files don't show up and removed files remain in the ls listing)
574 sub svn_ls_current {
575         my ($dir, $rm, $add) = @_;
576         chomp(my @ls = safe_qx('svn','ls',$dir));
577         my @ret = ();
578         foreach (@ls) {
579                 s#/$##; # trailing slashes are evil
580                 push @ret, $_ unless $rm->{$dir}->{$_};
581         }
582         if (exists $add->{$dir}) {
583                 push @ret, keys %{$add->{$dir}};
584         }
585         return \@ret;
586 }
587
588 sub handle_rmdir {
589         my ($rm, $add) = @_;
590
591         foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
592                 my $ls = svn_ls_current($dir, $rm, $add);
593                 next if (scalar @$ls);
594                 sys(qw(svn rm --force),$dir);
595
596                 my $dn = dirname $dir;
597                 $rm->{ $dn }->{ basename $dir } = 1;
598                 $ls = svn_ls_current($dn, $rm, $add);
599                 while (scalar @$ls == 0 && $dn ne File::Spec->curdir) {
600                         sys(qw(svn rm --force),$dn);
601                         $dir = basename $dn;
602                         $dn = dirname $dn;
603                         $rm->{ $dn }->{ $dir } = 1;
604                         $ls = svn_ls_current($dn, $rm, $add);
605                 }
606         }
607 }
608
609 sub svn_commit_tree {
610         my ($svn_rev, $commit) = @_;
611         my $commit_msg = "$GIT_DIR/$GIT_SVN/.svn-commit.tmp.$$";
612         my %log_msg = ( msg => '' );
613         open my $msg, '>', $commit_msg  or croak $!;
614
615         chomp(my $type = `git-cat-file -t $commit`);
616         if ($type eq 'commit') {
617                 my $pid = open my $msg_fh, '-|';
618                 defined $pid or croak $!;
619
620                 if ($pid == 0) {
621                         exec(qw(git-cat-file commit), $commit) or croak $!;
622                 }
623                 my $in_msg = 0;
624                 while (<$msg_fh>) {
625                         if (!$in_msg) {
626                                 $in_msg = 1 if (/^\s*$/);
627                         } else {
628                                 $log_msg{msg} .= $_;
629                                 print $msg $_ or croak $!;
630                         }
631                 }
632                 close $msg_fh or croak $!;
633         }
634         close $msg or croak $!;
635
636         if ($_edit || ($type eq 'tree')) {
637                 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
638                 system($editor, $commit_msg);
639         }
640         my @ci_output = safe_qx(qw(svn commit -F),$commit_msg);
641         my ($committed) = grep(/^Committed revision \d+\./,@ci_output);
642         unlink $commit_msg;
643         defined $committed or croak
644                         "Commit output failed to parse committed revision!\n",
645                         join("\n",@ci_output),"\n";
646         my ($rev_committed) = ($committed =~ /^Committed revision (\d+)\./);
647
648         my @svn_up = qw(svn up);
649         push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
650         if ($rev_committed == ($svn_rev + 1)) {
651                 push @svn_up, "-r$rev_committed";
652                 sys(@svn_up);
653                 my $info = svn_info('.');
654                 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
655                 if ($info->{'Last Changed Rev'} != $rev_committed) {
656                         croak "$info->{'Last Changed Rev'} != $rev_committed\n"
657                 }
658                 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
659                                         /(\d{4})\-(\d\d)\-(\d\d)\s
660                                          (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
661                                          or croak "Failed to parse date: $date\n";
662                 $log_msg{date} = "$tz $Y-$m-$d $H:$M:$S";
663                 $log_msg{author} = $info->{'Last Changed Author'};
664                 $log_msg{revision} = $rev_committed;
665                 $log_msg{msg} .= "\n";
666                 my $parent = file_to_s("$REV_DIR/$svn_rev");
667                 git_commit(\%log_msg, $parent, $commit);
668                 return $rev_committed;
669         }
670         # resync immediately
671         push @svn_up, "-r$svn_rev";
672         sys(@svn_up);
673         return fetch("$rev_committed=$commit")->{revision};
674 }
675
676 sub svn_log_raw {
677         my (@log_args) = @_;
678         my $pid = open my $log_fh,'-|';
679         defined $pid or croak $!;
680
681         if ($pid == 0) {
682                 exec (qw(svn log), @log_args) or croak $!
683         }
684
685         my @svn_log;
686         my $state = 'sep';
687         while (<$log_fh>) {
688                 chomp;
689                 if (/^\-{72}$/) {
690                         if ($state eq 'msg') {
691                                 if ($svn_log[$#svn_log]->{lines}) {
692                                         $svn_log[$#svn_log]->{msg} .= $_."\n";
693                                         unless(--$svn_log[$#svn_log]->{lines}) {
694                                                 $state = 'sep';
695                                         }
696                                 } else {
697                                         croak "Log parse error at: $_\n",
698                                                 $svn_log[$#svn_log]->{revision},
699                                                 "\n";
700                                 }
701                                 next;
702                         }
703                         if ($state ne 'sep') {
704                                 croak "Log parse error at: $_\n",
705                                         "state: $state\n",
706                                         $svn_log[$#svn_log]->{revision},
707                                         "\n";
708                         }
709                         $state = 'rev';
710
711                         # if we have an empty log message, put something there:
712                         if (@svn_log) {
713                                 $svn_log[$#svn_log]->{msg} ||= "\n";
714                                 delete $svn_log[$#svn_log]->{lines};
715                         }
716                         next;
717                 }
718                 if ($state eq 'rev' && s/^r(\d+)\s*\|\s*//) {
719                         my $rev = $1;
720                         my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
721                         ($lines) = ($lines =~ /(\d+)/);
722                         my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
723                                         /(\d{4})\-(\d\d)\-(\d\d)\s
724                                          (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
725                                          or croak "Failed to parse date: $date\n";
726                         my %log_msg = ( revision => $rev,
727                                         date => "$tz $Y-$m-$d $H:$M:$S",
728                                         author => $author,
729                                         lines => $lines,
730                                         msg => '' );
731                         push @svn_log, \%log_msg;
732                         $state = 'msg_start';
733                         next;
734                 }
735                 # skip the first blank line of the message:
736                 if ($state eq 'msg_start' && /^$/) {
737                         $state = 'msg';
738                 } elsif ($state eq 'msg') {
739                         if ($svn_log[$#svn_log]->{lines}) {
740                                 $svn_log[$#svn_log]->{msg} .= $_."\n";
741                                 unless (--$svn_log[$#svn_log]->{lines}) {
742                                         $state = 'sep';
743                                 }
744                         } else {
745                                 croak "Log parse error at: $_\n",
746                                         $svn_log[$#svn_log]->{revision},"\n";
747                         }
748                 }
749         }
750         close $log_fh or croak $?;
751         return \@svn_log;
752 }
753
754 sub svn_info {
755         my $url = shift || $SVN_URL;
756
757         my $pid = open my $info_fh, '-|';
758         defined $pid or croak $!;
759
760         if ($pid == 0) {
761                 exec(qw(svn info),$url) or croak $!;
762         }
763
764         my $ret = {};
765         # only single-lines seem to exist in svn info output
766         while (<$info_fh>) {
767                 chomp $_;
768                 if (m#^([^:]+)\s*:\s*(\S.*)$#) {
769                         $ret->{$1} = $2;
770                         push @{$ret->{-order}}, $1;
771                 }
772         }
773         close $info_fh or croak $!;
774         return $ret;
775 }
776
777 sub sys { system(@_) == 0 or croak $? }
778
779 sub git_addremove {
780         system( "git-diff-files --name-only -z ".
781                                 " | git-update-index --remove -z --stdin && ".
782                 "git-ls-files -z --others ".
783                         "'--exclude-from=$GIT_DIR/$GIT_SVN/info/exclude'".
784                                 " | git-update-index --add -z --stdin"
785                 ) == 0 or croak $?
786 }
787
788 sub s_to_file {
789         my ($str, $file, $mode) = @_;
790         open my $fd,'>',$file or croak $!;
791         print $fd $str,"\n" or croak $!;
792         close $fd or croak $!;
793         chmod ($mode &~ umask, $file) if (defined $mode);
794 }
795
796 sub file_to_s {
797         my $file = shift;
798         open my $fd,'<',$file or croak "$!: file: $file\n";
799         local $/;
800         my $ret = <$fd>;
801         close $fd or croak $!;
802         $ret =~ s/\s*$//s;
803         return $ret;
804 }
805
806 sub assert_revision_unknown {
807         my $revno = shift;
808         if (-f "$REV_DIR/$revno") {
809                 croak "$REV_DIR/$revno already exists! ",
810                                 "Why are we refetching it?";
811         }
812 }
813
814 sub assert_revision_eq_or_unknown {
815         my ($revno, $commit) = @_;
816         if (-f "$REV_DIR/$revno") {
817                 my $current = file_to_s("$REV_DIR/$revno");
818                 if ($commit ne $current) {
819                         croak "$REV_DIR/$revno already exists!\n",
820                                 "current: $current\nexpected: $commit\n";
821                 }
822                 return;
823         }
824 }
825
826 sub git_commit {
827         my ($log_msg, @parents) = @_;
828         assert_revision_unknown($log_msg->{revision});
829         my $out_fh = IO::File->new_tmpfile or croak $!;
830         my $info = svn_info('.');
831         my $uuid = $info->{'Repository UUID'};
832         defined $uuid or croak "Unable to get Repository UUID\n";
833
834         # commit parents can be conditionally bound to a particular
835         # svn revision via: "svn_revno=commit_sha1", filter them out here:
836         my @exec_parents;
837         foreach my $p (@parents) {
838                 next unless defined $p;
839                 if ($p =~ /^(\d+)=($sha1_short)$/o) {
840                         if ($1 == $log_msg->{revision}) {
841                                 push @exec_parents, $2;
842                         }
843                 } else {
844                         push @exec_parents, $p if $p =~ /$sha1_short/o;
845                 }
846         }
847
848         my $pid = fork;
849         defined $pid or croak $!;
850         if ($pid == 0) {
851                 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
852                 git_addremove();
853                 chomp(my $tree = `git-write-tree`);
854                 croak if $?;
855                 my $msg_fh = IO::File->new_tmpfile or croak $!;
856                 print $msg_fh $log_msg->{msg}, "\ngit-svn-id: ",
857                                         "$SVN_URL\@$log_msg->{revision}",
858                                         " $uuid\n" or croak $!;
859                 $msg_fh->flush == 0 or croak $!;
860                 seek $msg_fh, 0, 0 or croak $!;
861
862                 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} =
863                                                 $log_msg->{author};
864                 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} =
865                                                 $log_msg->{author}."\@$uuid";
866                 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} =
867                                                 $log_msg->{date};
868                 my @exec = ('git-commit-tree',$tree);
869                 push @exec, '-p', $_  foreach @exec_parents;
870                 open STDIN, '<&', $msg_fh or croak $!;
871                 open STDOUT, '>&', $out_fh or croak $!;
872                 exec @exec or croak $!;
873         }
874         waitpid($pid,0);
875         croak if $?;
876
877         $out_fh->flush == 0 or croak $!;
878         seek $out_fh, 0, 0 or croak $!;
879         chomp(my $commit = do { local $/; <$out_fh> });
880         if ($commit !~ /^$sha1$/o) {
881                 croak "Failed to commit, invalid sha1: $commit\n";
882         }
883         my @update_ref = ('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
884         if (my $primary_parent = shift @exec_parents) {
885                 push @update_ref, $primary_parent;
886         }
887         sys(@update_ref);
888         sys('git-update-ref',"$GIT_SVN/revs/$log_msg->{revision}",$commit);
889         print "r$log_msg->{revision} = $commit\n";
890         return $commit;
891 }
892
893 sub apply_mod_line_blob {
894         my $m = shift;
895         if ($m->{mode_b} =~ /^120/) {
896                 blob_to_symlink($m->{sha1_b}, $m->{file_b});
897         } else {
898                 blob_to_file($m->{sha1_b}, $m->{file_b});
899         }
900 }
901
902 sub blob_to_symlink {
903         my ($blob, $link) = @_;
904         defined $link or croak "\$link not defined!\n";
905         croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
906         if (-l $link || -f _) {
907                 unlink $link or croak $!;
908         }
909
910         my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
911         symlink $dest, $link or croak $!;
912 }
913
914 sub blob_to_file {
915         my ($blob, $file) = @_;
916         defined $file or croak "\$file not defined!\n";
917         croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
918         if (-l $file || -f _) {
919                 unlink $file or croak $!;
920         }
921
922         open my $blob_fh, '>', $file or croak "$!: $file\n";
923         my $pid = fork;
924         defined $pid or croak $!;
925
926         if ($pid == 0) {
927                 open STDOUT, '>&', $blob_fh or croak $!;
928                 exec('git-cat-file','blob',$blob);
929         }
930         waitpid $pid, 0;
931         croak $? if $?;
932
933         close $blob_fh or croak $!;
934 }
935
936 sub safe_qx {
937         my $pid = open my $child, '-|';
938         defined $pid or croak $!;
939         if ($pid == 0) {
940                 exec(@_) or croak $?;
941         }
942         my @ret = (<$child>);
943         close $child or croak $?;
944         die $? if $?; # just in case close didn't error out
945         return wantarray ? @ret : join('',@ret);
946 }
947
948 sub svn_check_ignore_externals {
949         return if $_no_ignore_ext;
950         unless (grep /ignore-externals/,(safe_qx(qw(svn co -h)))) {
951                 print STDERR "W: Installed svn version does not support ",
952                                 "--ignore-externals\n";
953                 $_no_ignore_ext = 1;
954         }
955 }
956
957 sub check_upgrade_needed {
958         my $old = eval {
959                 my $pid = open my $child, '-|';
960                 defined $pid or croak $!;
961                 if ($pid == 0) {
962                         close STDERR;
963                         exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $?;
964                 }
965                 my @ret = (<$child>);
966                 close $child or croak $?;
967                 die $? if $?; # just in case close didn't error out
968                 return wantarray ? @ret : join('',@ret);
969         };
970         return unless $old;
971         my $head = eval { safe_qx('git-rev-parse',"refs/remotes/$GIT_SVN") };
972         if ($@ || !$head) {
973                 print STDERR "Please run: $0 rebuild --upgrade\n";
974                 exit 1;
975         }
976 }
977
978 __END__
979
980 Data structures:
981
982 @svn_log = array of log_msg hashes
983
984 $log_msg hash
985 {
986         msg => 'whitespace-formatted log entry
987 ',                                              # trailing newline is preserved
988         revision => '8',                        # integer
989         date => '2004-02-24T17:01:44.108345Z',  # commit date
990         author => 'committer name'
991 };
992
993
994 @mods = array of diff-index line hashes, each element represents one line
995         of diff-index output
996
997 diff-index line ($m hash)
998 {
999         mode_a => first column of diff-index output, no leading ':',
1000         mode_b => second column of diff-index output,
1001         sha1_b => sha1sum of the final blob,
1002         chg => change type [MCRAD],
1003         file_a => original file name of a file (iff chg is 'C' or 'R')
1004         file_b => new/current file name of a file (any chg)
1005 }
1006 ;