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

git-svn: fix several small bugs, enable branch optimization

Share the repack counter between branches when doing multi-fetch. Pass the -d flag to git repack by default. That's the main reason we will want automatic pack generation, to save space and improve disk cache performance. I won't add -a by default since it can generate extremely large packs that make RAM-starved systems unhappy. We no longer generate the .git/svn/$GIT_SVN_ID/info/uuid file, either. It was never read in the first place. Check for and create .rev_db if we need to during fetch (in case somebody manually blew away their .rev_db and wanted to start over. Mainly makes debugging easier). Croak with $? instead of $! if there's an error closing pipes Quiet down some of the chatter, too. Signed-off-by: Eric Wong <normalperson@yhbt.net>

Eric Wong committed Jun 15, 2006 at 12:50 UTC cf7424b021f99fbc5dc1127725e57ca3d48e981c
1 file changed +81 -65
contrib/git-svn/git-svn.perl
+81 -65
@@ -368,7 +368,6 @@ sub fetch_lib {
368 defined(my $pid = fork) or croak $!;
369 if (!$pid) {
370 $SVN::Error::handler = \&libsvn_skip_unknown_revs;
371 - print "Fetching revisions $min .. $max\n";
371
372 # Yes I'm perfectly aware that the fourth argument
373 # below is the limit revisions number. Unfortunately
@@ -391,7 +390,6 @@ sub fetch_lib {
390 $log_msg, @parents);
391 }
392 });
394 - $SVN::Error::handler = sub { 'quiet warnings' };
393 exit 0;
394 }
395 waitpid $pid, 0;
@@ -463,7 +461,7 @@ sub commit_lib {
461 my (@revs) = @_;
462 my ($r_last, $cmt_last) = svn_grab_base_rev();
463 defined $r_last or die "Must have an existing revision to commit\n";
466 - my $fetched = fetch_lib();
464 + my $fetched = fetch();
465 if ($r_last != $fetched->{revision}) {
466 print STDERR "There are new revisions that were fetched ",
467 "and need to be merged (or acknowledged) ",
@@ -523,7 +521,7 @@ sub commit_lib {
521 $no = 1;
522 }
523 }
526 - close $fh or croak $!;
524 + close $fh or croak $?;
525 if (! defined $r_new && ! defined $cmt_new) {
526 unless ($no) {
527 die "Failed to parse revision information\n";
@@ -633,17 +631,8 @@ sub multi_init {
631 sub multi_fetch {
632 # try to do trunk first, since branches/tags
633 # may be descended from it.
636 - if (-d "$GIT_DIR/svn/trunk") {
637 - print "Fetching trunk\n";
638 - defined(my $pid = fork) or croak $!;
639 - if (!$pid) {
640 - $GIT_SVN = $ENV{GIT_SVN_ID} = 'trunk';
641 - init_vars();
642 - fetch(@_);
643 - exit 0;
644 - }
645 - waitpid $pid, 0;
646 - croak $? if $?;
634 + if (-e "$GIT_DIR/svn/trunk/info/url") {
635 + fetch_child_id('trunk', @_);
636 }
637 rec_fetch('', "$GIT_DIR/svn", @_);
638 }
@@ -725,6 +714,41 @@ out:
714
715 ########################### utility functions #########################
716
717 +sub fetch_child_id {
718 + my $id = shift;
719 + print "Fetching $id\n";
720 + my $ref = "$GIT_DIR/refs/remotes/$id";
721 + my $ca = file_to_s($ref) if (-r $ref);
722 + defined(my $pid = fork) or croak $!;
723 + if (!$pid) {
724 + $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
725 + init_vars();
726 + fetch(@_);
727 + exit 0;
728 + }
729 + waitpid $pid, 0;
730 + croak $? if $?;
731 + return unless $_repack || -r $ref;
732 +
733 + my $cb = file_to_s($ref);
734 +
735 + defined($pid = open my $fh, '-|') or croak $!;
736 + my $url = file_to_s("$GIT_DIR/svn/$id/info/url");
737 + $url = qr/\Q$url\E/;
738 + if (!$pid) {
739 + exec qw/git-rev-list --pretty=raw/,
740 + $ca ? "$ca..$cb" : $cb or croak $!;
741 + }
742 + while (<$fh>) {
743 + if (/^ git-svn-id: $url\@\d+ [a-f0-9\-]+$/) {
744 + check_repack();
745 + } elsif (/^ git-svn-id: \S+\@\d+ [a-f0-9\-]+$/) {
746 + last;
747 + }
748 + }
749 + close $fh;
750 +}
751 +
752 sub rec_fetch {
753 my ($pfx, $p, @args) = @_;
754 my @dir;
@@ -733,16 +757,7 @@ sub rec_fetch {
757 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
758 my $id = $pfx . basename $_;
759 next if $id eq 'trunk';
736 - print "Fetching $id\n";
737 - defined(my $pid = fork) or croak $!;
738 - if (!$pid) {
739 - $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
740 - init_vars();
741 - fetch(@args);
742 - exit 0;
743 - }
744 - waitpid $pid, 0;
745 - croak $? if $?;
760 + fetch_child_id($id, @args);
761 } elsif (-d $_) {
762 push @dir, $_;
763 }
@@ -943,7 +958,6 @@ sub read_uuid {
958 $SVN_UUID = $info->{'Repository UUID'} or
959 croak "Repository UUID unreadable\n";
960 }
946 - s_to_file($SVN_UUID,"$GIT_SVN_DIR/info/uuid");
961 }
962
963 sub quiet_run {
@@ -1107,7 +1121,7 @@ sub parse_diff_tree {
1121 croak "Error parsing $_\n";
1122 }
1123 }
1110 - close $diff_fh or croak $!;
1124 + close $diff_fh or croak $?;
1125
1126 return \@mods;
1127 }
@@ -1348,7 +1362,7 @@ sub get_commit_message {
1362 print $msg $_ or croak $!;
1363 }
1364 }
1351 - close $msg_fh or croak $!;
1365 + close $msg_fh or croak $?;
1366 }
1367 close $msg or croak $!;
1368
@@ -1562,7 +1576,7 @@ sub svn_info {
1576 push @{$ret->{-order}}, $1;
1577 }
1578 }
1565 - close $info_fh or croak $!;
1579 + close $info_fh or croak $?;
1580 return $ret;
1581 }
1582
@@ -1638,7 +1652,7 @@ sub do_update_index {
1652 }
1653 print $ui $x,"\0";
1654 }
1641 - close $ui or croak $!;
1655 + close $ui or croak $?;
1656 }
1657
1658 sub index_changes {
@@ -1765,11 +1779,15 @@ sub git_commit {
1779
1780 # this output is read via pipe, do not change:
1781 print "r$log_msg->{revision} = $commit\n";
1782 + check_repack();
1783 + return $commit;
1784 +}
1785 +
1786 +sub check_repack {
1787 if ($_repack && (--$_repack_nr == 0)) {
1788 $_repack_nr = $_repack;
1789 sys("git repack $_repack_flags");
1790 }
1772 - return $commit;
1791 }
1792
1793 sub set_commit_env {
@@ -1877,6 +1895,10 @@ sub svn_cmd_checkout {
1895 }
1896
1897 sub check_upgrade_needed {
1898 + if (!-r $REVDB) {
1899 + open my $fh, '>>',$REVDB or croak $!;
1900 + close $fh;
1901 + }
1902 my $old = eval {
1903 my $pid = open my $child, '-|';
1904 defined $pid or croak $!;
@@ -2026,7 +2048,8 @@ sub migration_check {
2048 sub find_rev_before {
2049 my ($r, $id, $eq_ok) = @_;
2050 my $f = "$GIT_DIR/svn/$id/.rev_db";
2029 - # --$r unless $eq_ok;
2051 + return (undef,undef) unless -r $f;
2052 + --$r unless $eq_ok;
2053 while ($r > 0) {
2054 if (my $c = revdb_get($f, $r)) {
2055 return ($r, $c);
@@ -2072,7 +2095,7 @@ sub set_default_vals {
2095 if (defined $_repack) {
2096 $_repack = 1000 if ($_repack <= 0);
2097 $_repack_nr = $_repack;
2075 - $_repack_flags ||= '';
2098 + $_repack_flags ||= '-d';
2099 }
2100 }
2101
@@ -2352,7 +2375,7 @@ sub libsvn_get_file {
2375 close $ho or croak $?;
2376 $hash =~ /^$sha1$/o or die "not a sha1: $hash\n";
2377 print $gui $mode,' ',$hash,"\t",$p,"\0" or croak $!;
2355 - close $fd or croak $!;
2378 + close $fd or croak $?;
2379 }
2380
2381 sub libsvn_log_entry {
@@ -2381,7 +2404,7 @@ sub process_rm {
2404 while (<$ls>) {
2405 print $gui '0 ',0 x 40,"\t",$_ or croak $!;
2406 }
2384 - close $ls or croak $!;
2407 + close $ls or croak $?;
2408 } else {
2409 print $gui '0 ',0 x 40,"\t",$f,"\0" or croak $!;
2410 }
@@ -2411,7 +2434,7 @@ sub libsvn_fetch {
2434 $pool->clear;
2435 }
2436 libsvn_get_file($gui, $_, $rev) foreach (@amr);
2414 - close $gui or croak $!;
2437 + close $gui or croak $?;
2438 return libsvn_log_entry($rev, $author, $date, $msg, [$last_commit]);
2439 }
2440
@@ -2514,36 +2537,30 @@ sub revisions_eq {
2537 }
2538
2539 sub libsvn_find_parent_branch {
2517 - return undef; # XXX this function is disabled atm (not tested enough)
2540 my ($paths, $rev, $author, $date, $msg) = @_;
2541 my $svn_path = '/'.$SVN_PATH;
2542
2543 # look for a parent from another branch:
2522 - foreach (keys %$paths) {
2523 - next if ($_ ne $svn_path);
2524 - my $i = $paths->{$_};
2525 - my $branch_from = $i->copyfrom_path or next;
2526 - my $r = $i->copyfrom_rev;
2527 - print STDERR "Found possible branch point: ",
2528 - "$branch_from => $svn_path, $r\n";
2529 - $branch_from =~ s#^/##;
2530 - my $l_map = read_url_paths();
2531 - my $url = $SVN->{url};
2532 - defined $l_map->{$url} or next;
2533 - my $id = $l_map->{$url}->{$branch_from} or next;
2534 - my ($r0, $parent) = find_rev_before($r,$id,1);
2535 - if (defined $r0 && defined $parent &&
2536 - revisions_eq($branch_from, $r0, $r)) {
2537 - unlink $GIT_SVN_INDEX;
2538 - print STDERR "Found branch parent: $parent\n";
2539 - sys(qw/git-read-tree/, $parent);
2540 - return libsvn_fetch($parent, $paths, $rev,
2541 - $author, $date, $msg);
2542 - } else {
2543 - print STDERR
2544 - "Nope, branch point not imported or unknown\n";
2545 - }
2546 - }
2544 + my $i = $paths->{$svn_path} or return;
2545 + my $branch_from = $i->copyfrom_path or return;
2546 + my $r = $i->copyfrom_rev;
2547 + print STDERR "Found possible branch point: ",
2548 + "$branch_from => $svn_path, $r\n";
2549 + $branch_from =~ s#^/##;
2550 + my $l_map = read_url_paths();
2551 + my $url = $SVN->{url};
2552 + defined $l_map->{$url} or return;
2553 + my $id = $l_map->{$url}->{$branch_from} or return;
2554 + my ($r0, $parent) = find_rev_before($r,$id,1);
2555 + return unless (defined $r0 && defined $parent);
2556 + if (revisions_eq($branch_from, $r0, $r)) {
2557 + unlink $GIT_SVN_INDEX;
2558 + print STDERR "Found branch parent: $parent\n";
2559 + sys(qw/git-read-tree/, $parent);
2560 + return libsvn_fetch($parent, $paths, $rev,
2561 + $author, $date, $msg);
2562 + }
2563 + print STDERR "Nope, branch point not imported or unknown\n";
2564 return undef;
2565 }
2566
@@ -2556,7 +2573,7 @@ sub libsvn_new_tree {
2573 my $pool = SVN::Pool->new;
2574 libsvn_traverse($gui, '', $SVN_PATH, $rev, $pool);
2575 $pool->clear;
2559 - close $gui or croak $!;
2576 + close $gui or croak $?;
2577 return libsvn_log_entry($rev, $author, $date, $msg);
2578 }
2579
@@ -2630,7 +2647,7 @@ sub libsvn_commit_cb {
2647 exit 1;
2648 }
2649 } else {
2633 - fetch_lib("$rev=$c");
2650 + fetch("$rev=$c");
2651 }
2652 }
2653
@@ -2664,7 +2681,6 @@ sub libsvn_skip_unknown_revs {
2681 # 175002 - http(s)://
2682 # More codes may be discovered later...
2683 if ($errno == 175002 || $errno == 160013) {
2667 - print STDERR "directory non-existent\n";
2684 return;
2685 }
2686 croak "Error from SVN, ($errno): ", $err->expanded_message,"\n";