@matheusbsbs / matheus-bagliano / commits / a5e0cedc0a
Fork 0

git-svn: add support for Perl SVN::* libraries

This means we no longer have to deal with having bloated SVN working copies around and we get a nice performance increase as well because we don't have to exec the SVN binary and start a new server connection each time. Of course we have to manually manage memory with SVN::Pool whenever we can, and hack around cases where SVN just eats memory despite pools (I blame Perl, too). I would like to keep memory usage as stable as possible during long fetch/commit processes since I still use computers with only 256-512M RAM. commit should always be faster with the SVN library code. The SVN::Delta interface is leaky (or I'm not using it with pools correctly), so I'm forking on every commit, but that doesn't seem to hurt performance too much (at least on normal Unix/Linux systems where fork() is pretty cheap). fetch should be faster in most common cases, but probably not all. fetches will be faster where client/server delta generation is the bottleneck and not bandwidth. Of course, full-files are generated server-side via deltas, too. Full files are always transferred when they're updated, just like git-svnimport and unlike command-line svn. I'm also hacking around memory leaks (see comments) here by using some more forks. I've tested fetch with http://, https://, file://, and svn:// repositories, so we should be reasonably covered in terms of error handling for fetching. Of course, we'll keep plain command-line svn compatibility as a fallback for people running SVN 1.1 (I'm looking into library support for 1.1.x SVN, too). If you want to force command-line SVN usage, set GIT_SVN_NO_LIB=1 in your environment. We also require two simultaneous connections (just like git-svnimport), but this shouldn't be a problem for most servers. Less important commands: show-ignore is slower because it requires repository access, but -r/--revision <num> can be specified. graft-branches may use more memory, but it's a short-term process and is funky-filename-safe. Signed-off-by: Eric Wong <normalperson@yhbt.net>

Eric Wong committed Jun 12, 2006 at 15:23 UTC a5e0cedc0a4d0018f3e7e4ba8ca54c91742dd859
3 files changed +974 -111
contrib/git-svn/git-svn.perl
+962 -106
@@ -31,6 +31,10 @@ use File::Path qw/mkpath/;
31 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
32 use File::Spec qw//;
33 use POSIX qw/strftime/;
34 +
35 +my ($SVN_PATH, $SVN, $SVN_LOG, $_use_lib);
36 +$_use_lib = 1 unless $ENV{GIT_SVN_NO_LIB};
37 +libsvn_load();
38 my $sha1 = qr/[a-f\d]{40}/;
39 my $sha1_short = qr/[a-f\d]{4,40}/;
40 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
@@ -74,7 +78,8 @@ my %cmd = (
78 'copy-similarity|C=i'=> \$_cp_similarity,
79 %fc_opts,
80 } ],
77 - 'show-ignore' => [ \&show_ignore, "Show svn:ignore listings", { } ],
81 + 'show-ignore' => [ \&show_ignore, "Show svn:ignore listings",
82 + { 'revision|r=i' => \$_revision } ],
83 rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)",
84 { 'no-ignore-externals' => \$_no_ignore_ext,
85 'upgrade' => \$_upgrade } ],
@@ -211,6 +216,8 @@ sub rebuild {
216 $newest_rev = $rev if ($rev > $newest_rev);
217 }
218 close $rev_list or croak $?;
219 +
220 + goto out if $_use_lib;
221 if (!chdir $SVN_WC) {
222 svn_cmd_checkout($SVN_URL, $latest, $SVN_WC);
223 chdir $SVN_WC or croak $!;
@@ -228,7 +235,7 @@ sub rebuild {
235 }
236 waitpid $pid, 0;
237 croak $? if $?;
231 -
238 +out:
239 if ($_upgrade) {
240 print STDERR <<"";
241 Keeping deprecated refs/head/$GIT_SVN-HEAD for now. Please remove it
@@ -251,9 +258,18 @@ sub init {
258 }
259
260 sub fetch {
254 - my (@parents) = @_;
261 check_upgrade_needed();
262 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
263 + my $ret = $_use_lib ? fetch_lib(@_) : fetch_cmd(@_);
264 + if ($ret->{commit} && quiet_run(qw(git-rev-parse --verify
265 + refs/heads/master^0))) {
266 + sys(qw(git-update-ref refs/heads/master),$ret->{commit});
267 + }
268 + return $ret;
269 +}
270 +
271 +sub fetch_cmd {
272 + my (@parents) = @_;
273 my @log_args = -d $SVN_WC ? ($SVN_WC) : ($SVN_URL);
274 unless ($_revision) {
275 $_revision = -d $SVN_WC ? 'BASE:HEAD' : '0:HEAD';
@@ -301,13 +317,91 @@ sub fetch {
317 $last_commit = git_commit($log_msg, $last_commit, @parents);
318 $last = $log_msg;
319 }
304 - unless (-e "$GIT_DIR/refs/heads/master") {
305 - sys(qw(git-update-ref refs/heads/master),$last_commit);
306 - }
320 close $svn_log->{fh};
321 + $last->{commit} = $last_commit;
322 return $last;
323 }
324
325 +sub fetch_lib {
326 + my (@parents) = @_;
327 + $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
328 + my $repo;
329 + ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
330 + $SVN_LOG ||= libsvn_connect($repo);
331 + $SVN ||= libsvn_connect($repo);
332 + my ($last_rev, $last_commit) = svn_grab_base_rev();
333 + my ($base, $head) = libsvn_parse_revision($last_rev);
334 + if ($base > $head) {
335 + return { revision => $last_rev, commit => $last_commit }
336 + }
337 + my $index = set_index($GIT_SVN_INDEX);
338 +
339 + # limit ourselves and also fork() since get_log won't release memory
340 + # after processing a revision and SVN stuff seems to leak
341 + my $inc = 1000;
342 + my ($min, $max) = ($base, $head < $base+$inc ? $head : $base+$inc);
343 + read_uuid();
344 + if (defined $last_commit) {
345 + unless (-e $GIT_SVN_INDEX) {
346 + sys(qw/git-read-tree/, $last_commit);
347 + }
348 + chomp (my $x = `git-write-tree`);
349 + my ($y) = (`git-cat-file commit $last_commit`
350 + =~ /^tree ($sha1)/m);
351 + if ($y ne $x) {
352 + unlink $GIT_SVN_INDEX or croak $!;
353 + sys(qw/git-read-tree/, $last_commit);
354 + }
355 + chomp ($x = `git-write-tree`);
356 + if ($y ne $x) {
357 + print STDERR "trees ($last_commit) $y != $x\n",
358 + "Something is seriously wrong...\n";
359 + }
360 + }
361 + while (1) {
362 + # fork, because using SVN::Pool with get_log() still doesn't
363 + # seem to help enough to keep memory usage down.
364 + defined(my $pid = fork) or croak $!;
365 + if (!$pid) {
366 + $SVN::Error::handler = \&libsvn_skip_unknown_revs;
367 + print "Fetching revisions $min .. $max\n";
368 +
369 + # Yes I'm perfectly aware that the fourth argument
370 + # below is the limit revisions number. Unfortunately
371 + # performance sucks with it enabled, so it's much
372 + # faster to fetch revision ranges instead of relying
373 + # on the limiter.
374 + $SVN_LOG->get_log( '/'.$SVN_PATH, $min, $max, 0, 1, 1,
375 + sub {
376 + my $log_msg;
377 + if ($last_commit) {
378 + $log_msg = libsvn_fetch(
379 + $last_commit, @_);
380 + $last_commit = git_commit(
381 + $log_msg,
382 + $last_commit,
383 + @parents);
384 + } else {
385 + $log_msg = libsvn_new_tree(@_);
386 + $last_commit = git_commit(
387 + $log_msg, @parents);
388 + }
389 + });
390 + $SVN::Error::handler = sub { 'quiet warnings' };
391 + exit 0;
392 + }
393 + waitpid $pid, 0;
394 + croak $? if $?;
395 + ($last_rev, $last_commit) = svn_grab_base_rev();
396 + last if ($max >= $head);
397 + $min = $max + 1;
398 + $max += $inc;
399 + $max = $head if ($max > $head);
400 + }
401 + restore_index($index);
402 + return { revision => $last_rev, commit => $last_commit };
403 +}
404 +
405 sub commit {
406 my (@commits) = @_;
407 check_upgrade_needed();
@@ -332,6 +426,12 @@ sub commit {
426 }
427 }
428 chomp @revs;
429 + $_use_lib ? commit_lib(@revs) : commit_cmd(@revs);
430 + print "Done committing ",scalar @revs," revisions to SVN\n";
431 +}
432 +
433 +sub commit_cmd {
434 + my (@revs) = @_;
435
436 chdir $SVN_WC or croak "Unable to chdir $SVN_WC: $!\n";
437 my $info = svn_info('.');
@@ -353,17 +453,95 @@ sub commit {
453 }
454 $svn_current_rev = svn_commit_tree($svn_current_rev, $c);
455 }
356 - print "Done committing ",scalar @revs," revisions to SVN\n";
456 }
457
359 -sub show_ignore {
360 - require File::Find or die $!;
361 - my $exclude_file = "$GIT_DIR/info/exclude";
362 - open my $fh, '<', $exclude_file or croak $!;
363 - chomp(my @excludes = (<$fh>));
364 - close $fh or croak $!;
458 +sub commit_lib {
459 + my (@revs) = @_;
460 + my ($r_last, $cmt_last) = svn_grab_base_rev();
461 + defined $r_last or die "Must have an existing revision to commit\n";
462 + my $fetched = fetch_lib();
463 + if ($r_last != $fetched->{revision}) {
464 + print STDERR "There are new revisions that were fetched ",
465 + "and need to be merged (or acknowledged) ",
466 + "before committing.\n",
467 + "last rev: $r_last\n",
468 + " current: $fetched->{revision}\n";
469 + exit 1;
470 + }
471 + read_uuid();
472 + my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
473 + my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
474 +
475 + foreach my $c (@revs) {
476 + # fork for each commit because there's a memory leak I
477 + # can't track down... (it's probably in the SVN code)
478 + defined(my $pid = open my $fh, '-|') or croak $!;
479 + if (!$pid) {
480 + if (defined $LC_ALL) {
481 + $ENV{LC_ALL} = $LC_ALL;
482 + } else {
483 + delete $ENV{LC_ALL};
484 + }
485 + my $log_msg = get_commit_message($c, $commit_msg);
486 + my $ed = SVN::Git::Editor->new(
487 + { r => $r_last,
488 + ra => $SVN,
489 + c => $c,
490 + svn_path => $SVN_PATH
491 + },
492 + $SVN->get_commit_editor(
493 + $log_msg->{msg},
494 + sub {
495 + libsvn_commit_cb(
496 + @_, $c,
497 + $log_msg->{msg},
498 + $r_last,
499 + $cmt_last)
500 + },
501 + @lock)
502 + );
503 + my $mods = libsvn_checkout_tree($r_last, $c, $ed);
504 + if (@$mods == 0) {
505 + print "No changes\nr$r_last = $cmt_last\n";
506 + $ed->abort_edit;
507 + } else {
508 + $ed->close_edit;
509 + }
510 + exit 0;
511 + }
512 + my ($r_new, $cmt_new, $no);
513 + while (<$fh>) {
514 + print $_;
515 + chomp;
516 + if (/^r(\d+) = ($sha1)$/o) {
517 + ($r_new, $cmt_new) = ($1, $2);
518 + } elsif ($_ eq 'No changes') {
519 + $no = 1;
520 + }
521 + }
522 + close $fh or croak $!;
523 + if (! defined $r_new && ! defined $cmt_new) {
524 + unless ($no) {
525 + die "Failed to parse revision information\n";
526 + }
527 + } else {
528 + ($r_last, $cmt_last) = ($r_new, $cmt_new);
529 + }
530 + }
531 + unlink $commit_msg;
532 +}
533
534 +sub show_ignore {
535 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
536 + $_use_lib ? show_ignore_lib() : show_ignore_cmd();
537 +}
538 +
539 +sub show_ignore_cmd {
540 + require File::Find or die $!;
541 + if (defined $_revision) {
542 + die "-r/--revision option doesn't work unless the Perl SVN ",
543 + "libraries are used\n";
544 + }
545 chdir $SVN_WC or croak $!;
546 my %ign;
547 File::Find::find({wanted=>sub{if(lstat $_ && -d _ && -d "$_/.svn"){
@@ -380,6 +558,14 @@ sub show_ignore {
558 }
559 }
560
561 +sub show_ignore_lib {
562 + my $repo;
563 + ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
564 + $SVN ||= libsvn_connect($repo);
565 + my $r = defined $_revision ? $_revision : $SVN->get_latest_revnum;
566 + libsvn_traverse_ignore(\*STDOUT, $SVN_PATH, $r);
567 +}
568 +
569 sub graft_branches {
570 my $gr_file = "$GIT_DIR/info/grafts";
571 my ($grafts, $comments) = read_grafts($gr_file);
@@ -403,7 +589,13 @@ sub graft_branches {
589 graft_merge_msg($grafts,$l_map,$u,$p);
590 }
591 }
406 - graft_file_copy($grafts,$l_map,$u) unless $_no_graft_copy;
592 + unless ($_no_graft_copy) {
593 + if ($_use_lib) {
594 + graft_file_copy_lib($grafts,$l_map,$u);
595 + } else {
596 + graft_file_copy_cmd($grafts,$l_map,$u);
597 + }
598 + }
599 }
600
601 write_grafts($grafts, $comments, $gr_file);
@@ -574,7 +766,8 @@ sub complete_url_ls_init {
766 }
767 $var = $url . $var;
768 }
577 - chomp(my @ls = safe_qx(qw/svn ls --non-interactive/, $var));
769 + chomp(my @ls = $_use_lib ? libsvn_ls_fullurl($var)
770 + : safe_qx(qw/svn ls --non-interactive/, $var));
771 my $old = $GIT_SVN;
772 defined(my $pid = fork) or croak $!;
773 if (!$pid) {
@@ -617,7 +810,7 @@ sub common_prefix {
810 }
811
812 # this isn't funky-filename safe, but good enough for now...
620 -sub graft_file_copy {
813 +sub graft_file_copy_cmd {
814 my ($grafts, $l_map, $u) = @_;
815 my $paths = $l_map->{$u};
816 my $pfx = common_prefix([keys %$paths]);
@@ -625,7 +818,9 @@ sub graft_file_copy {
818 my $pid = open my $fh, '-|';
819 defined $pid or croak $!;
820 unless ($pid) {
628 - exec(qw/svn log -v/, $u.$pfx) or croak $!;
821 + my @exec = qw/svn log -v/;
822 + push @exec, "-r$_revision" if defined $_revision;
823 + exec @exec, $u.$pfx or croak $!;
824 }
825 my ($r, $mp) = (undef, undef);
826 while (<$fh>) {
@@ -637,42 +832,40 @@ sub graft_file_copy {
832 } elsif (/^Changed paths:/) {
833 $mp = 1;
834 } elsif ($mp && m#^ [AR] /(\S.*?) \(from /(\S+?):(\d+)\)$#) {
640 - my $dbg = "r$r | $_";
835 my ($p1, $p0, $r0) = ($1, $2, $3);
642 - my $c;
643 - foreach my $x (keys %$paths) {
644 - next unless ($p1 =~ /^\Q$x\E/);
645 - my $i = $paths->{$x};
646 - my $f = "$GIT_DIR/svn/$i/revs/$r";
647 - unless (-r $f) {
648 - print STDERR "r$r of $i not imported,",
649 - " $dbg\n";
650 - next;
651 - }
652 - $c = file_to_s($f);
653 - }
836 + my $c = find_graft_path_commit($paths, $p1, $r);
837 next unless $c;
655 - foreach my $x (keys %$paths) {
656 - next unless ($p0 =~ /^\Q$x\E/);
657 - my $i = $paths->{$x};
658 - my $f = "$GIT_DIR/svn/$i/revs/$r0";
659 - while ($r0 && !-r $f) {
660 - # could be an older revision, too...
661 - $r0--;
662 - $f = "$GIT_DIR/svn/$i/revs/$r0";
663 - }
664 - unless (-r $f) {
665 - print STDERR "r$r0 of $i not imported,",
666 - " $dbg\n";
667 - next;
668 - }
669 - my $r1 = file_to_s($f);
670 - $grafts->{$c}->{$r1} = 1;
671 - }
838 + find_graft_path_parents($grafts, $paths, $c, $p0, $r0);
839 }
840 }
841 }
842
843 +sub graft_file_copy_lib {
844 + my ($grafts, $l_map, $u) = @_;
845 + my $tree_paths = $l_map->{$u};
846 + my $pfx = common_prefix([keys %$tree_paths]);
847 + my ($repo, $path) = repo_path_split($u.$pfx);
848 + $SVN_LOG ||= libsvn_connect($repo);
849 + $SVN ||= libsvn_connect($repo);
850 +
851 + my ($base, $head) = libsvn_parse_revision();
852 + my $inc = 1000;
853 + my ($min, $max) = ($base, $head < $base+$inc ? $head : $base+$inc);
854 + while (1) {
855 + my $pool = SVN::Pool->new;
856 + $SVN_LOG->get_log( "/$path", $min, $max, 0, 1, 1,
857 + sub {
858 + libsvn_graft_file_copies($grafts, $tree_paths,
859 + $path, @_);
860 + }, $pool);
861 + $pool->clear;
862 + last if ($max >= $head);
863 + $min = $max + 1;
864 + $max += $inc;
865 + $max = $head if ($max > $head);
866 + }
867 +}
868 +
869 sub process_merge_msg_matches {
870 my ($grafts, $l_map, $u, $p, $c, @matches) = @_;
871 my (@strong, @weak);
@@ -734,9 +927,15 @@ sub graft_merge_msg {
927
928 sub read_uuid {
929 return if $SVN_UUID;
737 - my $info = shift || svn_info('.');
738 - $SVN_UUID = $info->{'Repository UUID'} or
930 + if ($_use_lib) {
931 + my $pool = SVN::Pool->new;
932 + $SVN_UUID = $SVN->get_uuid($pool);
933 + $pool->clear;
934 + } else {
935 + my $info = shift || svn_info('.');
936 + $SVN_UUID = $info->{'Repository UUID'} or
937 croak "Repository UUID unreadable\n";
938 + }
939 s_to_file($SVN_UUID,"$GIT_SVN_DIR/info/uuid");
940 }
941
@@ -769,9 +968,19 @@ sub repo_path_split {
968 $path =~ s#^/+##;
969 my @paths = split(m#/+#, $path);
970
772 - while (quiet_run(qw/svn ls --non-interactive/, $url)) {
773 - my $n = shift @paths || last;
774 - $url .= "/$n";
971 + if ($_use_lib) {
972 + while (1) {
973 + $SVN = libsvn_connect($url);
974 + last if (defined $SVN &&
975 + defined eval { $SVN->get_latest_revnum });
976 + my $n = shift @paths || last;
977 + $url .= "/$n";
978 + }
979 + } else {
980 + while (quiet_run(qw/svn ls --non-interactive/, $url)) {
981 + my $n = shift @paths || last;
982 + $url .= "/$n";
983 + }
984 }
985 push @repo_path_split_cache, qr/^(\Q$url\E)/;
986 $path = join('/',@paths);
@@ -797,6 +1006,7 @@ sub setup_git_svn {
1006 }
1007
1008 sub assert_svn_wc_clean {
1009 + return if $_use_lib;
1010 my ($svn_rev) = @_;
1011 croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
1012 my $lcr = svn_info('.')->{'Last Changed Rev'};
@@ -819,7 +1029,7 @@ sub assert_svn_wc_clean {
1029 }
1030 }
1031
822 -sub assert_tree {
1032 +sub get_tree_from_treeish {
1033 my ($treeish) = @_;
1034 croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
1035 chomp(my $type = `git-cat-file -t $treeish`);
@@ -836,20 +1046,22 @@ sub assert_tree {
1046 } else {
1047 die "$treeish is a $type, expected tree, tag or commit\n";
1048 }
1049 + return $expected;
1050 +}
1051 +
1052 +sub assert_tree {
1053 + return if $_use_lib;
1054 + my ($treeish) = @_;
1055 + my $expected = get_tree_from_treeish($treeish);
1056
840 - my $old_index = $ENV{GIT_INDEX_FILE};
1057 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
1058 if (-e $tmpindex) {
1059 unlink $tmpindex or croak $!;
1060 }
845 - $ENV{GIT_INDEX_FILE} = $tmpindex;
1061 + my $old_index = set_index($tmpindex);
1062 index_changes(1);
1063 chomp(my $tree = `git-write-tree`);
848 - if ($old_index) {
849 - $ENV{GIT_INDEX_FILE} = $old_index;
850 - } else {
851 - delete $ENV{GIT_INDEX_FILE};
852 - }
1064 + restore_index($old_index);
1065 if ($tree ne $expected) {
1066 croak "Tree mismatch, Got: $tree, Expected: $expected\n";
1067 }
@@ -987,7 +1199,8 @@ sub precommit_check {
1199 }
1200 }
1201
990 -sub svn_checkout_tree {
1202 +
1203 +sub get_diff {
1204 my ($svn_rev, $treeish) = @_;
1205 my $from = file_to_s("$REV_DIR/$svn_rev");
1206 assert_tree($from);
@@ -1005,11 +1218,13 @@ sub svn_checkout_tree {
1218 push @diff_tree, "-l$_l" if defined $_l;
1219 exec(@diff_tree, $from, $treeish) or croak $!;
1220 }
1008 - my $mods = parse_diff_tree($diff_fh);
1009 - unless (@$mods) {
1010 - # git can do empty commits, but SVN doesn't allow it...
1011 - return $mods;
1012 - }
1221 + return parse_diff_tree($diff_fh);
1222 +}
1223 +
1224 +sub svn_checkout_tree {
1225 + my ($svn_rev, $treeish) = @_;
1226 + my $mods = get_diff($svn_rev, $treeish);
1227 + return $mods unless (scalar @$mods);
1228 my ($rm, $add) = precommit_check($mods);
1229
1230 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
@@ -1052,6 +1267,23 @@ sub svn_checkout_tree {
1267 return $mods;
1268 }
1269
1270 +sub libsvn_checkout_tree {
1271 + my ($svn_rev, $treeish, $ed) = @_;
1272 + my $mods = get_diff($svn_rev, $treeish);
1273 + return $mods unless (scalar @$mods);
1274 + my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
1275 + foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
1276 + my $f = $m->{chg};
1277 + if (defined $o{$f}) {
1278 + $ed->$f($m);
1279 + } else {
1280 + croak "Invalid change type: $f\n";
1281 + }
1282 + }
1283 + $ed->rmdirs if $_rmdir;
1284 + return $mods;
1285 +}
1286 +
1287 # svn ls doesn't work with respect to the current working tree, but what's
1288 # in the repository. There's not even an option for it... *sigh*
1289 # (added files don't show up and removed files remain in the ls listing)
@@ -1090,12 +1322,12 @@ sub handle_rmdir {
1322 }
1323 }
1324
1093 -sub svn_commit_tree {
1094 - my ($svn_rev, $commit) = @_;
1095 - my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
1325 +sub get_commit_message {
1326 + my ($commit, $commit_msg) = (@_);
1327 my %log_msg = ( msg => '' );
1328 open my $msg, '>', $commit_msg or croak $!;
1329
1330 + print "commit: $commit\n";
1331 chomp(my $type = `git-cat-file -t $commit`);
1332 if ($type eq 'commit') {
1333 my $pid = open my $msg_fh, '-|';
@@ -1129,7 +1361,14 @@ sub svn_commit_tree {
1361 { local $/; chomp($log_msg{msg} = <$msg>); }
1362 close $msg or croak $!;
1363
1132 - my ($oneline) = ($log_msg{msg} =~ /([^\n\r]+)/);
1364 + return \%log_msg;
1365 +}
1366 +
1367 +sub svn_commit_tree {
1368 + my ($svn_rev, $commit) = @_;
1369 + my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
1370 + my $log_msg = get_commit_message($commit, $commit_msg);
1371 + my ($oneline) = ($log_msg->{msg} =~ /([^\n\r]+)/);
1372 print "Committing $commit: $oneline\n";
1373
1374 if (defined $LC_ALL) {
@@ -1165,12 +1404,12 @@ sub svn_commit_tree {
1404 /(\d{4})\-(\d\d)\-(\d\d)\s
1405 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
1406 or croak "Failed to parse date: $date\n";
1168 - $log_msg{date} = "$tz $Y-$m-$d $H:$M:$S";
1169 - $log_msg{author} = $info->{'Last Changed Author'};
1170 - $log_msg{revision} = $committed;
1171 - $log_msg{msg} .= "\n";
1407 + $log_msg->{date} = "$tz $Y-$m-$d $H:$M:$S";
1408 + $log_msg->{author} = $info->{'Last Changed Author'};
1409 + $log_msg->{revision} = $committed;
1410 + $log_msg->{msg} .= "\n";
1411 my $parent = file_to_s("$REV_DIR/$svn_rev");
1173 - git_commit(\%log_msg, $parent, $commit);
1412 + git_commit($log_msg, $parent, $commit);
1413 return $committed;
1414 }
1415 # resync immediately
@@ -1335,8 +1574,14 @@ sub eol_cp {
1574 binmode $rfd or croak $!;
1575 open my $wfd, '>', $to or croak $!;
1576 binmode $wfd or croak $!;
1577 + eol_cp_fd($rfd, $wfd, $es);
1578 + close $rfd or croak $!;
1579 + close $wfd or croak $!;
1580 +}
1581
1339 - my $eol = $EOL{$es} or undef;
1582 +sub eol_cp_fd {
1583 + my ($rfd, $wfd, $es) = @_;
1584 + my $eol = defined $es ? $EOL{$es} : undef;
1585 my $buf;
1586 use bytes;
1587 while (1) {
@@ -1396,6 +1641,7 @@ sub do_update_index {
1641 }
1642
1643 sub index_changes {
1644 + return if $_use_lib;
1645 my $no_text_base = shift;
1646 do_update_index([qw/git-diff-files --name-only -z/],
1647 'remove',
@@ -1459,63 +1705,59 @@ sub assert_revision_eq_or_unknown {
1705 sub git_commit {
1706 my ($log_msg, @parents) = @_;
1707 assert_revision_unknown($log_msg->{revision});
1462 - my $out_fh = IO::File->new_tmpfile or croak $!;
1463 -
1708 map_tree_joins() if (@_branch_from && !%tree_map);
1709
1710 + my (@tmp_parents, @exec_parents, %seen_parent);
1711 + if (my $lparents = $log_msg->{parents}) {
1712 + @tmp_parents = @$lparents
1713 + }
1714 # commit parents can be conditionally bound to a particular
1715 # svn revision via: "svn_revno=commit_sha1", filter them out here:
1468 - my @exec_parents;
1716 foreach my $p (@parents) {
1717 next unless defined $p;
1718 if ($p =~ /^(\d+)=($sha1_short)$/o) {
1719 if ($1 == $log_msg->{revision}) {
1473 - push @exec_parents, $2;
1720 + push @tmp_parents, $2;
1721 }
1722 } else {
1476 - push @exec_parents, $p if $p =~ /$sha1_short/o;
1723 + push @tmp_parents, $p if $p =~ /$sha1_short/o;
1724 }
1725 }
1479 -
1480 - my $pid = fork;
1481 - defined $pid or croak $!;
1482 - if ($pid == 0) {
1483 - $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
1726 + my $tree = $log_msg->{tree};
1727 + if (!defined $tree) {
1728 + my $index = set_index($GIT_SVN_INDEX);
1729 index_changes();
1485 - chomp(my $tree = `git-write-tree`);
1730 + chomp($tree = `git-write-tree`);
1731 croak $? if $?;
1487 - if (exists $tree_map{$tree}) {
1488 - my %seen_parent = map { $_ => 1 } @exec_parents;
1489 - foreach (@{$tree_map{$tree}}) {
1490 - # MAXPARENT is defined to 16 in commit-tree.c:
1491 - if ($seen_parent{$_} || @exec_parents > 16) {
1492 - next;
1493 - }
1494 - push @exec_parents, $_;
1495 - $seen_parent{$_} = 1;
1496 - }
1497 - }
1732 + restore_index($index);
1733 + }
1734 + if (exists $tree_map{$tree}) {
1735 + push @tmp_parents, @{$tree_map{$tree}};
1736 + }
1737 + foreach (@tmp_parents) {
1738 + next if $seen_parent{$_};
1739 + $seen_parent{$_} = 1;
1740 + push @exec_parents, $_;
1741 + # MAXPARENT is defined to 16 in commit-tree.c:
1742 + last if @exec_parents > 16;
1743 + }
1744 +
1745 + defined(my $pid = open my $out_fh, '-|') or croak $!;
1746 + if ($pid == 0) {
1747 my $msg_fh = IO::File->new_tmpfile or croak $!;
1748 print $msg_fh $log_msg->{msg}, "\ngit-svn-id: ",
1749 "$SVN_URL\@$log_msg->{revision}",
1750 " $SVN_UUID\n" or croak $!;
1751 $msg_fh->flush == 0 or croak $!;
1752 seek $msg_fh, 0, 0 or croak $!;
1504 -
1753 set_commit_env($log_msg);
1506 -
1754 my @exec = ('git-commit-tree',$tree);
1755 push @exec, '-p', $_ foreach @exec_parents;
1756 open STDIN, '<&', $msg_fh or croak $!;
1510 - open STDOUT, '>&', $out_fh or croak $!;
1757 exec @exec or croak $!;
1758 }
1513 - waitpid($pid,0);
1514 - croak $? if $?;
1515 -
1516 - $out_fh->flush == 0 or croak $!;
1517 - seek $out_fh, 0, 0 or croak $!;
1759 chomp(my $commit = do { local $/; <$out_fh> });
1760 + close $out_fh or croak $?;
1761 if ($commit !~ /^$sha1$/o) {
1762 croak "Failed to commit, invalid sha1: $commit\n";
1763 }
@@ -1534,6 +1776,7 @@ sub git_commit {
1776 }
1777 sys(@update_ref);
1778 sys('git-update-ref',"svn/$GIT_SVN/revs/$log_msg->{revision}",$commit);
1779 + # this output is read via pipe, do not change:
1780 print "r$log_msg->{revision} = $commit\n";
1781 if ($_repack && (--$_repack_nr == 0)) {
1782 $_repack_nr = $_repack;
@@ -1545,6 +1788,9 @@ sub git_commit {
1788 sub set_commit_env {
1789 my ($log_msg) = @_;
1790 my $author = $log_msg->{author};
1791 + if (!defined $author || length $author == 0) {
1792 + $author = '(no author)';
1793 + }
1794 my ($name,$email) = defined $users{$author} ? @{$users{$author}}
1795 : ($author,"$author\@$SVN_UUID");
1796 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name;
@@ -2029,6 +2275,612 @@ sub show_commit_normal {
2275 }
2276 }
2277
2278 +sub libsvn_load {
2279 + return unless $_use_lib;
2280 + $_use_lib = eval {
2281 + require SVN::Core;
2282 + if ($SVN::Core::VERSION lt '1.2.1') {
2283 + die "Need SVN::Core 1.2.1 or better ",
2284 + "(got $SVN::Core::VERSION) ",
2285 + "Falling back to command-line svn\n";
2286 + }
2287 + require SVN::Ra;
2288 + require SVN::Delta;
2289 + push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
2290 + my $kill_stupid_warnings = $SVN::Node::none.$SVN::Node::file.
2291 + $SVN::Node::dir.$SVN::Node::unknown.
2292 + $SVN::Node::none.$SVN::Node::file.
2293 + $SVN::Node::dir.$SVN::Node::unknown;
2294 + 1;
2295 + };
2296 +}
2297 +
2298 +sub libsvn_connect {
2299 + my ($url) = @_;
2300 + my $auth = SVN::Core::auth_open([SVN::Client::get_simple_provider(),
2301 + SVN::Client::get_ssl_server_trust_file_provider(),
2302 + SVN::Client::get_username_provider()]);
2303 + my $s = eval { SVN::Ra->new(url => $url, auth => $auth) };
2304 + return $s;
2305 +}
2306 +
2307 +sub libsvn_get_file {
2308 + my ($gui, $f, $rev) = @_;
2309 + my $p = $f;
2310 + return unless ($p =~ s#^\Q$SVN_PATH\E/?##);
2311 +
2312 + my $fd = IO::File->new_tmpfile or croak $!;
2313 + my $pool = SVN::Pool->new;
2314 + my ($r, $props) = $SVN->get_file($f, $rev, $fd, $pool);
2315 + $pool->clear;
2316 + $fd->flush == 0 or croak $!;
2317 + seek $fd, 0, 0 or croak $!;
2318 + if (my $es = $props->{'svn:eol-style'}) {
2319 + my $new_fd = IO::File->new_tmpfile or croak $!;
2320 + eol_cp_fd($fd, $new_fd, $es);
2321 + close $fd or croak $!;
2322 + $fd = $new_fd;
2323 + seek $fd, 0, 0 or croak $!;
2324 + $fd->flush == 0 or croak $!;
2325 + }
2326 + my $mode = '100644';
2327 + if (exists $props->{'svn:executable'}) {
2328 + $mode = '100755';
2329 + }
2330 + if (exists $props->{'svn:special'}) {
2331 + $mode = '120000';
2332 + local $/;
2333 + my $link = <$fd>;
2334 + $link =~ s/^link // or die "svn:special file with contents: <",
2335 + $link, "> is not understood\n";
2336 + seek $fd, 0, 0 or croak $!;
2337 + truncate $fd, 0 or croak $!;
2338 + print $fd $link or croak $!;
2339 + seek $fd, 0, 0 or croak $!;
2340 + $fd->flush == 0 or croak $!;
2341 + }
2342 + my $pid = open my $ho, '-|';
2343 + defined $pid or croak $!;
2344 + if (!$pid) {
2345 + open STDIN, '<&', $fd or croak $!;
2346 + exec qw/git-hash-object -w --stdin/ or croak $!;
2347 + }
2348 + chomp(my $hash = do { local $/; <$ho> });
2349 + close $ho or croak $?;
2350 + $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2351 + print $gui $mode,' ',$hash,"\t",$p,"\0" or croak $!;
2352 + close $fd or croak $!;
2353 +}
2354 +
2355 +sub libsvn_log_entry {
2356 + my ($rev, $author, $date, $msg, $parents) = @_;
2357 + my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
2358 + (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x)
2359 + or die "Unable to parse date: $date\n";
2360 + if (defined $_authors && ! defined $users{$author}) {
2361 + die "Author: $author not defined in $_authors file\n";
2362 + }
2363 + return { revision => $rev, date => "+0000 $Y-$m-$d $H:$M:$S",
2364 + author => $author, msg => $msg."\n", parents => $parents || [] }
2365 +}
2366 +
2367 +sub process_rm {
2368 + my ($gui, $last_commit, $f) = @_;
2369 + $f =~ s#^\Q$SVN_PATH\E/?## or return;
2370 + # remove entire directories.
2371 + if (safe_qx('git-ls-tree',$last_commit,'--',$f) =~ /^040000 tree/) {
2372 + defined(my $pid = open my $ls, '-|') or croak $!;
2373 + if (!$pid) {
2374 + exec(qw/git-ls-tree -r --name-only -z/,
2375 + $last_commit,'--',$f) or croak $!;
2376 + }
2377 + local $/ = "\0";
2378 + while (<$ls>) {
2379 + print $gui '0 ',0 x 40,"\t",$_ or croak $!;
2380 + }
2381 + close $ls or croak $!;
2382 + } else {
2383 + print $gui '0 ',0 x 40,"\t",$f,"\0" or croak $!;
2384 + }
2385 +}
2386 +
2387 +sub libsvn_fetch {
2388 + my ($last_commit, $paths, $rev, $author, $date, $msg) = @_;
2389 + open my $gui, '| git-update-index -z --index-info' or croak $!;
2390 + my @amr;
2391 + foreach my $f (keys %$paths) {
2392 + my $m = $paths->{$f}->action();
2393 + $f =~ s#^/+##;
2394 + if ($m =~ /^[DR]$/) {
2395 + process_rm($gui, $last_commit, $f);
2396 + next if $m eq 'D';
2397 + # 'R' can be file replacements, too, right?
2398 + }
2399 + my $pool = SVN::Pool->new;
2400 + my $t = $SVN->check_path($f, $rev, $pool);
2401 + if ($t == $SVN::Node::file) {
2402 + if ($m =~ /^[AMR]$/) {
2403 + push @amr, $f;
2404 + } else {
2405 + die "Unrecognized action: $m, ($f r$rev)\n";
2406 + }
2407 + }
2408 + $pool->clear;
2409 + }
2410 + libsvn_get_file($gui, $_, $rev) foreach (@amr);
2411 + close $gui or croak $!;
2412 + return libsvn_log_entry($rev, $author, $date, $msg, [$last_commit]);
2413 +}
2414 +
2415 +sub svn_grab_base_rev {
2416 + defined(my $pid = open my $fh, '-|') or croak $!;
2417 + if (!$pid) {
2418 + open my $null, '>', '/dev/null' or croak $!;
2419 + open STDERR, '>&', $null or croak $!;
2420 + exec qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0"
2421 + or croak $!;
2422 + }
2423 + chomp(my $c = do { local $/; <$fh> });
2424 + close $fh;
2425 + if (defined $c && length $c) {
2426 + my ($url, $rev, $uuid) = extract_metadata((grep(/^git-svn-id: /,
2427 + safe_qx(qw/git-cat-file commit/, $c)))[0]);
2428 + return ($rev, $c);
2429 + }
2430 + return (undef, undef);
2431 +}
2432 +
2433 +sub libsvn_parse_revision {
2434 + my $base = shift;
2435 + my $head = $SVN->get_latest_revnum();
2436 + if (!defined $_revision || $_revision eq 'BASE:HEAD') {
2437 + return ($base + 1, $head) if (defined $base);
2438 + return (0, $head);
2439 + }
2440 + return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/);
2441 + return ($_revision, $_revision) if ($_revision =~ /^\d+$/);
2442 + if ($_revision =~ /^BASE:(\d+)$/) {
2443 + return ($base + 1, $1) if (defined $base);
2444 + return (0, $head);
2445 + }
2446 + return ($1, $head) if ($_revision =~ /^(\d+):HEAD$/);
2447 + die "revision argument: $_revision not understood by git-svn\n",
2448 + "Try using the command-line svn client instead\n";
2449 +}
2450 +
2451 +sub libsvn_traverse {
2452 + my ($gui, $pfx, $path, $rev) = @_;
2453 + my $cwd = "$pfx/$path";
2454 + my $pool = SVN::Pool->new;
2455 + $cwd =~ s#^/+##g;
2456 + my ($dirent, $r, $props) = $SVN->get_dir($cwd, $rev, $pool);
2457 + foreach my $d (keys %$dirent) {
2458 + my $t = $dirent->{$d}->kind;
2459 + if ($t == $SVN::Node::dir) {
2460 + libsvn_traverse($gui, $cwd, $d, $rev);
2461 + } elsif ($t == $SVN::Node::file) {
2462 + libsvn_get_file($gui, "$cwd/$d", $rev);
2463 + }
2464 + }
2465 + $pool->clear;
2466 +}
2467 +
2468 +sub libsvn_traverse_ignore {
2469 + my ($fh, $path, $r) = @_;
2470 + $path =~ s#^/+##g;
2471 + my $pool = SVN::Pool->new;
2472 + my ($dirent, undef, $props) = $SVN->get_dir($path, $r, $pool);
2473 + my $p = $path;
2474 + $p =~ s#^\Q$SVN_PATH\E/?##;
2475 + print $fh length $p ? "\n# $p\n" : "\n# /\n";
2476 + if (my $s = $props->{'svn:ignore'}) {
2477 + $s =~ s/[\r\n]+/\n/g;
2478 + chomp $s;
2479 + if (length $p == 0) {
2480 + $s =~ s#\n#\n/$p#g;
2481 + print $fh "/$s\n";
2482 + } else {
2483 + $s =~ s#\n#\n/$p/#g;
2484 + print $fh "/$p/$s\n";
2485 + }
2486 + }
2487 + foreach (sort keys %$dirent) {
2488 + next if $dirent->{$_}->kind != $SVN::Node::dir;
2489 + libsvn_traverse_ignore($fh, "$path/$_", $r);
2490 + }
2491 + $pool->clear;
2492 +}
2493 +
2494 +sub libsvn_new_tree {
2495 + my ($paths, $rev, $author, $date, $msg) = @_;
2496 + my $svn_path = '/'.$SVN_PATH;
2497 +
2498 + # look for a parent from another branch:
2499 + foreach (keys %$paths) {
2500 + next if ($_ ne $svn_path);
2501 + my $i = $paths->{$_};
2502 + my $branch_from = $i->copyfrom_path or next;
2503 + my $r = $i->copyfrom_rev;
2504 + print STDERR "Found possible branch point: ",
2505 + "$branch_from => $svn_path, $r\n";
2506 + $branch_from =~ s#^/##;
2507 + my $l_map = read_url_paths();
2508 + my $url = $SVN->{url};
2509 + defined $l_map->{$url} or next;
2510 + my $id = $l_map->{$url}->{$branch_from} or next;
2511 + my $f = "$GIT_DIR/svn/$id/revs/$r";
2512 + while ($r && !-r $f) {
2513 + $r--;
2514 + $f = "$GIT_DIR/svn/$id/revs/$r";
2515 + }
2516 + if (-r $f) {
2517 + my $parent = file_to_s($f);
2518 + unlink $GIT_SVN_INDEX;
2519 + print STDERR "Found branch parent: $parent\n";
2520 + sys(qw/git-read-tree/, $parent);
2521 + return libsvn_fetch($parent, $paths, $rev,
2522 + $author, $date, $msg);
2523 + }
2524 + print STDERR "Nope, branch point not imported or unknown\n";
2525 + }
2526 + open my $gui, '| git-update-index -z --index-info' or croak $!;
2527 + my $pool = SVN::Pool->new;
2528 + libsvn_traverse($gui, '', $SVN_PATH, $rev, $pool);
2529 + $pool->clear;
2530 + close $gui or croak $!;
2531 + return libsvn_log_entry($rev, $author, $date, $msg);
2532 +}
2533 +
2534 +sub find_graft_path_commit {
2535 + my ($tree_paths, $p1, $r1) = @_;
2536 + foreach my $x (keys %$tree_paths) {
2537 + next unless ($p1 =~ /^\Q$x\E/);
2538 + my $i = $tree_paths->{$x};
2539 + my $f = "$GIT_DIR/svn/$i/revs/$r1";
2540 +
2541 + return file_to_s($f) if (-r $f);
2542 +
2543 + print STDERR "r$r1 of $i not imported\n";
2544 + next;
2545 + }
2546 + return undef;
2547 +}
2548 +
2549 +sub find_graft_path_parents {
2550 + my ($grafts, $tree_paths, $c, $p0, $r0) = @_;
2551 + foreach my $x (keys %$tree_paths) {
2552 + next unless ($p0 =~ /^\Q$x\E/);
2553 + my $i = $tree_paths->{$x};
2554 + my $f = "$GIT_DIR/svn/$i/revs/$r0";
2555 + while ($r0 && !-r $f) {
2556 + # could be an older revision, too...
2557 + $r0--;
2558 + $f = "$GIT_DIR/svn/$i/revs/$r0";
2559 + }
2560 + unless (-r $f) {
2561 + print STDERR "r$r0 of $i not imported\n";
2562 + next;
2563 + }
2564 + my $parent = file_to_s($f);
2565 + $grafts->{$c}->{$parent} = 1;
2566 + }
2567 +}
2568 +
2569 +sub libsvn_graft_file_copies {
2570 + my ($grafts, $tree_paths, $path, $paths, $rev) = @_;
2571 + foreach (keys %$paths) {
2572 + my $i = $paths->{$_};
2573 + my ($m, $p0, $r0) = ($i->action, $i->copyfrom_path,
2574 + $i->copyfrom_rev);
2575 + next unless (defined $p0 && defined $r0);
2576 +
2577 + my $p1 = $_;
2578 + $p1 =~ s#^/##;
2579 + $p0 =~ s#^/##;
2580 + my $c = find_graft_path_commit($tree_paths, $p1, $rev);
2581 + next unless $c;
2582 + find_graft_path_parents($grafts, $tree_paths, $c, $p0, $r0);
2583 + }
2584 +}
2585 +
2586 +sub set_index {
2587 + my $old = $ENV{GIT_INDEX_FILE};
2588 + $ENV{GIT_INDEX_FILE} = shift;
2589 + return $old;
2590 +}
2591 +
2592 +sub restore_index {
2593 + my ($old) = @_;
2594 + if (defined $old) {
2595 + $ENV{GIT_INDEX_FILE} = $old;
2596 + } else {
2597 + delete $ENV{GIT_INDEX_FILE};
2598 + }
2599 +}
2600 +
2601 +sub libsvn_commit_cb {
2602 + my ($rev, $date, $committer, $c, $msg, $r_last, $cmt_last) = @_;
2603 + if ($rev == ($r_last + 1)) {
2604 + # optimized (avoid fetch)
2605 + my $log = libsvn_log_entry($rev,$committer,$date,$msg);
2606 + $log->{tree} = get_tree_from_treeish($c);
2607 + my $cmt = git_commit($log, $cmt_last, $c);
2608 + my @diff = safe_qx('git-diff-tree', $cmt, $c);
2609 + if (@diff) {
2610 + print STDERR "Trees differ: $cmt $c\n",
2611 + join('',@diff),"\n";
2612 + exit 1;
2613 + }
2614 + } else {
2615 + fetch_lib("$rev=$c");
2616 + }
2617 +}
2618 +
2619 +sub libsvn_ls_fullurl {
2620 + my $fullurl = shift;
2621 + my ($repo, $path) = repo_path_split($fullurl);
2622 + $SVN ||= libsvn_connect($repo);
2623 + my @ret;
2624 + my $pool = SVN::Pool->new;
2625 + my ($dirent, undef, undef) = $SVN->get_dir($path,
2626 + $SVN->get_latest_revnum, $pool);
2627 + foreach my $d (keys %$dirent) {
2628 + if ($dirent->{$d}->kind == $SVN::Node::dir) {
2629 + push @ret, "$d/"; # add '/' for compat with cli svn
2630 + }
2631 + }
2632 + $pool->clear;
2633 + return @ret;
2634 +}
2635 +
2636 +
2637 +sub libsvn_skip_unknown_revs {
2638 + my $err = shift;
2639 + my $errno = $err->apr_err();
2640 + # Maybe the branch we're tracking didn't
2641 + # exist when the repo started, so it's
2642 + # not an error if it doesn't, just continue
2643 + #
2644 + # Wonderfully consistent library, eh?
2645 + # 160013 - svn:// and file://
2646 + # 175002 - http(s)://
2647 + # More codes may be discovered later...
2648 + if ($errno == 175002 || $errno == 160013) {
2649 + print STDERR "directory non-existent\n";
2650 + return;
2651 + }
2652 + croak "Error from SVN, ($errno): ", $err->expanded_message,"\n";
2653 +};
2654 +
2655 +package SVN::Git::Editor;
2656 +use vars qw/@ISA/;
2657 +use strict;
2658 +use warnings;
2659 +use Carp qw/croak/;
2660 +use IO::File;
2661 +
2662 +sub new {
2663 + my $class = shift;
2664 + my $git_svn = shift;
2665 + my $self = SVN::Delta::Editor->new(@_);
2666 + bless $self, $class;
2667 + foreach (qw/svn_path c r ra /) {
2668 + die "$_ required!\n" unless (defined $git_svn->{$_});
2669 + $self->{$_} = $git_svn->{$_};
2670 + }
2671 + $self->{pool} = SVN::Pool->new;
2672 + $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
2673 + $self->{rm} = { };
2674 + require Digest::MD5;
2675 + return $self;
2676 +}
2677 +
2678 +sub split_path {
2679 + return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
2680 +}
2681 +
2682 +sub repo_path {
2683 + (defined $_[1] && length $_[1]) ? "$_[0]->{svn_path}/$_[1]"
2684 + : $_[0]->{svn_path}
2685 +}
2686 +
2687 +sub url_path {
2688 + my ($self, $path) = @_;
2689 + $self->{ra}->{url} . '/' . $self->repo_path($path);
2690 +}
2691 +
2692 +sub rmdirs {
2693 + my ($self) = @_;
2694 + my $rm = $self->{rm};
2695 + delete $rm->{''}; # we never delete the url we're tracking
2696 + return unless %$rm;
2697 +
2698 + foreach (keys %$rm) {
2699 + my @d = split m#/#, $_;
2700 + my $c = shift @d;
2701 + $rm->{$c} = 1;
2702 + while (@d) {
2703 + $c .= '/' . shift @d;
2704 + $rm->{$c} = 1;
2705 + }
2706 + }
2707 + delete $rm->{$self->{svn_path}};
2708 + delete $rm->{''}; # we never delete the url we're tracking
2709 + return unless %$rm;
2710 +
2711 + defined(my $pid = open my $fh,'-|') or croak $!;
2712 + if (!$pid) {
2713 + exec qw/git-ls-tree --name-only -r -z/, $self->{c} or croak $!;
2714 + }
2715 + local $/ = "\0";
2716 + while (<$fh>) {
2717 + chomp;
2718 + $_ = $self->{svn_path} . '/' . $_;
2719 + my ($dn) = ($_ =~ m#^(.*?)/?(?:[^/]+)$#);
2720 + delete $rm->{$dn};
2721 + last unless %$rm;
2722 + }
2723 + my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
2724 + foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
2725 + $self->close_directory($bat->{$d}, $p);
2726 + my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
2727 + $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
2728 + delete $bat->{$d};
2729 + }
2730 +}
2731 +
2732 +sub open_or_add_dir {
2733 + my ($self, $full_path, $baton) = @_;
2734 + my $p = SVN::Pool->new;
2735 + my $t = $self->{ra}->check_path($full_path, $self->{r}, $p);
2736 + $p->clear;
2737 + if ($t == $SVN::Node::none) {
2738 + return $self->add_directory($full_path, $baton,
2739 + undef, -1, $self->{pool});
2740 + } elsif ($t == $SVN::Node::dir) {
2741 + return $self->open_directory($full_path, $baton,
2742 + $self->{r}, $self->{pool});
2743 + }
2744 + print STDERR "$full_path already exists in repository at ",
2745 + "r$self->{r} and it is not a directory (",
2746 + ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
2747 + exit 1;
2748 +}
2749 +
2750 +sub ensure_path {
2751 + my ($self, $path) = @_;
2752 + my $bat = $self->{bat};
2753 + $path = $self->repo_path($path);
2754 + return $bat->{''} unless (length $path);
2755 + my @p = split m#/+#, $path;
2756 + my $c = shift @p;
2757 + $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
2758 + while (@p) {
2759 + my $c0 = $c;
2760 + $c .= '/' . shift @p;
2761 + $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
2762 + }
2763 + return $bat->{$c};
2764 +}
2765 +
2766 +sub A {
2767 + my ($self, $m) = @_;
2768 + my ($dir, $file) = split_path($m->{file_b});
2769 + my $pbat = $self->ensure_path($dir);
2770 + my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
2771 + undef, -1);
2772 + $self->chg_file($fbat, $m);
2773 + $self->close_file($fbat,undef,$self->{pool});
2774 +}
2775 +
2776 +sub C {
2777 + my ($self, $m) = @_;
2778 + my ($dir, $file) = split_path($m->{file_b});
2779 + my $pbat = $self->ensure_path($dir);
2780 + my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
2781 + $self->url_path($m->{file_a}), $self->{r});
2782 + $self->chg_file($fbat, $m);
2783 + $self->close_file($fbat,undef,$self->{pool});
2784 +}
2785 +
2786 +sub delete_entry {
2787 + my ($self, $path, $pbat) = @_;
2788 + my $rpath = $self->repo_path($path);
2789 + my ($dir, $file) = split_path($rpath);
2790 + $self->{rm}->{$dir} = 1;
2791 + $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
2792 +}
2793 +
2794 +sub R {
2795 + my ($self, $m) = @_;
2796 + my ($dir, $file) = split_path($m->{file_b});
2797 + my $pbat = $self->ensure_path($dir);
2798 + my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
2799 + $self->url_path($m->{file_a}), $self->{r});
2800 + $self->chg_file($fbat, $m);
2801 + $self->close_file($fbat,undef,$self->{pool});
2802 +
2803 + ($dir, $file) = split_path($m->{file_a});
2804 + $pbat = $self->ensure_path($dir);
2805 + $self->delete_entry($m->{file_a}, $pbat);
2806 +}
2807 +
2808 +sub M {
2809 + my ($self, $m) = @_;
2810 + my ($dir, $file) = split_path($m->{file_b});
2811 + my $pbat = $self->ensure_path($dir);
2812 + my $fbat = $self->open_file($self->repo_path($m->{file_b}),
2813 + $pbat,$self->{r},$self->{pool});
2814 + $self->chg_file($fbat, $m);
2815 + $self->close_file($fbat,undef,$self->{pool});
2816 +}
2817 +
2818 +sub T { shift->M(@_) }
2819 +
2820 +sub change_file_prop {
2821 + my ($self, $fbat, $pname, $pval) = @_;
2822 + $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
2823 +}
2824 +
2825 +sub chg_file {
2826 + my ($self, $fbat, $m) = @_;
2827 + if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
2828 + $self->change_file_prop($fbat,'svn:executable','*');
2829 + } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
2830 + $self->change_file_prop($fbat,'svn:executable',undef);
2831 + }
2832 + my $fh = IO::File->new_tmpfile or croak $!;
2833 + if ($m->{mode_b} =~ /^120/) {
2834 + print $fh 'link ' or croak $!;
2835 + $self->change_file_prop($fbat,'svn:special','*');
2836 + } elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) {
2837 + $self->change_file_prop($fbat,'svn:special',undef);
2838 + }
2839 + defined(my $pid = fork) or croak $!;
2840 + if (!$pid) {
2841 + open STDOUT, '>&', $fh or croak $!;
2842 + exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!;
2843 + }
2844 + waitpid $pid, 0;
2845 + croak $? if $?;
2846 + $fh->flush == 0 or croak $!;
2847 + seek $fh, 0, 0 or croak $!;
2848 +
2849 + my $md5 = Digest::MD5->new;
2850 + $md5->addfile($fh) or croak $!;
2851 + seek $fh, 0, 0 or croak $!;
2852 +
2853 + my $exp = $md5->hexdigest;
2854 + my $atd = $self->apply_textdelta($fbat, undef, $self->{pool});
2855 + my $got = SVN::TxDelta::send_stream($fh, @$atd, $self->{pool});
2856 + die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
2857 +
2858 + close $fh or croak $!;
2859 +}
2860 +
2861 +sub D {
2862 + my ($self, $m) = @_;
2863 + my ($dir, $file) = split_path($m->{file_b});
2864 + my $pbat = $self->ensure_path($dir);
2865 + $self->delete_entry($m->{file_b}, $pbat);
2866 +}
2867 +
2868 +sub close_edit {
2869 + my ($self) = @_;
2870 + my ($p,$bat) = ($self->{pool}, $self->{bat});
2871 + foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
2872 + $self->close_directory($bat->{$_}, $p);
2873 + }
2874 + $self->SUPER::close_edit($p);
2875 + $p->clear;
2876 +}
2877 +
2878 +sub abort_edit {
2879 + my ($self) = @_;
2880 + $self->SUPER::abort_edit($self->{pool});
2881 + $self->{pool}->clear;
2882 +}
2883 +
2884 __END__
2885
2886 Data structures:
@@ -2062,3 +2914,7 @@ diff-index line ($m hash)
2914 file_b => new/current file name of a file (any chg)
2915 }
2916 ;
2917 +
2918 +Notes:
2919 + I don't trust the each() function on unless I created %hash myself
2920 + because the internal iterator may not have started at base.
contrib/git-svn/t/lib-git-svn.sh
+1 -1
@@ -11,7 +11,7 @@ fi
11
12 GIT_DIR=$PWD/.git
13 GIT_SVN_DIR=$GIT_DIR/svn/git-svn
14 -SVN_TREE=$GIT_SVN_DIR/tree
14 +SVN_TREE=$GIT_SVN_DIR/svn-tree
15
16 svnadmin >/dev/null 2>&1
17 if test $? != 1
contrib/git-svn/t/t0000-contrib-git-svn.sh
+11 -4
@@ -31,6 +31,7 @@ test_expect_success \
31 'import an SVN revision into git' \
32 'git-svn fetch'
33
34 +test_expect_success "checkout from svn" "svn co $svnrepo $SVN_TREE"
35
36 name='try a deep --rmdir with a commit'
37 git checkout -f -b mybranch remotes/git-svn
@@ -41,6 +42,7 @@ git commit -m "$name"
42
43 test_expect_success "$name" \
44 "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch &&
45 + svn up $SVN_TREE &&
46 test -d $SVN_TREE/dir && test ! -d $SVN_TREE/dir/a"
47
48
@@ -52,7 +54,7 @@ git update-index --remove dir/file
54 git update-index --add dir/file/file
55 git commit -m "$name"
56
55 -test_expect_code 1 "$name" \
57 +test_expect_failure "$name" \
58 'git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch' \
59 || true
60
@@ -67,7 +69,7 @@ git update-index --remove -- bar/zzz
69 git update-index --add -- bar
70 git commit -m "$name"
71
70 -test_expect_code 1 "$name" \
72 +test_expect_failure "$name" \
73 'git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch2' \
74 || true
75
@@ -82,7 +84,7 @@ echo yyy > bar/zzz/yyy
84 git-update-index --add bar/zzz/yyy
85 git commit -m "$name"
86
85 -test_expect_code 1 "$name" \
87 +test_expect_failure "$name" \
88 'git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch3' \
89 || true
90
@@ -97,7 +99,7 @@ echo asdf > dir
99 git update-index --add -- dir
100 git commit -m "$name"
101
100 -test_expect_code 1 "$name" \
102 +test_expect_failure "$name" \
103 'git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch4' \
104 || true
105
@@ -111,6 +113,7 @@ git commit -m "$name"
113
114 test_expect_success "$name" \
115 "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch5 &&
116 + svn up $SVN_TREE &&
117 test ! -x $SVN_TREE/exec.sh"
118
119
@@ -121,6 +124,7 @@ git commit -m "$name"
124
125 test_expect_success "$name" \
126 "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch5 &&
127 + svn up $SVN_TREE &&
128 test -x $SVN_TREE/exec.sh"
129
130
@@ -133,6 +137,7 @@ git commit -m "$name"
137
138 test_expect_success "$name" \
139 "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch5 &&
140 + svn up $SVN_TREE &&
141 test -L $SVN_TREE/exec.sh"
142
143
@@ -145,6 +150,7 @@ git commit -m "$name"
150
151 test_expect_success "$name" \
152 "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch5 &&
153 + svn up $SVN_TREE &&
154 test -x $SVN_TREE/bar/zzz &&
155 test -L $SVN_TREE/exec-2.sh"
156
@@ -159,6 +165,7 @@ git commit -m "$name"
165
166 test_expect_success "$name" \
167 "git-svn commit --find-copies-harder --rmdir remotes/git-svn..mybranch5 &&
168 + svn up $SVN_TREE &&
169 test -f $SVN_TREE/exec-2.sh &&
170 test ! -L $SVN_TREE/exec-2.sh &&
171 diff -u help $SVN_TREE/exec-2.sh"