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

git-svn: implement auto-discovery of branches/tags

This is similar to the way git proper handles refs, except we use the keys 'branches' and 'tags' to distinguish when we want to use wildcards. The left-hand side of the ':' contains the remote path, and must have one asterisk ('*') in it for the branch name. The asterisk may be in any component of the path as long as is it on its own directory level. The right-hand side contains the refname and must have the asterisk as the last path component. branches = branches/*:refs/remotes/* tags = tags/*:refs/remotes/tags/* Signed-off-by: Eric Wong <normalperson@yhbt.net>

Eric Wong committed Feb 8, 2007 at 12:53 UTC e518192f3be92097ba550098dbb69d769ca18429
1 file changed +120 -28
git-svn.perl
+120 -28
@@ -701,14 +701,32 @@ sub resolve_local_globs {
701
702 sub fetch_all {
703 my ($repo_id, $remotes) = @_;
704 - my $fetch = $remotes->{$repo_id}->{fetch};
705 - my $url = $remotes->{$repo_id}->{url};
706 - my @gs;
707 - resolve_local_globs($url, $fetch, $remotes->{$repo_id}->{branches});
708 - resolve_local_globs($url, $fetch, $remotes->{$repo_id}->{tags});
704 + my $remote = $remotes->{$repo_id};
705 + my $fetch = $remote->{fetch};
706 + my $url = $remote->{url};
707 + my (@gs, @globs);
708 my $ra = Git::SVN::Ra->new($url);
709 + my $uuid = $ra->uuid;
710 my $head = $ra->get_latest_revnum;
711 my $base = $head;
712 +
713 + # read the max revs for wildcard expansion (branches/*, tags/*)
714 + foreach my $t (qw/branches tags/) {
715 + defined $remote->{$t} or next;
716 + push @globs, $remote->{$t};
717 + my $f = "$ENV{GIT_DIR}/svn/.$uuid.$t";
718 + if (open my $fh, '<', $f) {
719 + chomp(my $max_rev = <$fh>);
720 + close $fh or die "Error closing $f: $!\n";
721 +
722 + if ($max_rev !~ /^\d+$/) {
723 + die "$max_rev (in $f) is not an integer!\n";
724 + }
725 + $remote->{$t}->{max_rev} = $max_rev;
726 + $base = $max_rev if ($max_rev < $base);
727 + }
728 + }
729 +
730 foreach my $p (sort keys %$fetch) {
731 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
732 my $lr = $gs->rev_db_max;
@@ -717,8 +735,7 @@ sub fetch_all {
735 }
736 push @gs, $gs;
737 }
720 - return if (++$base > $head);
721 - $ra->gs_fetch_loop_common($base, $head, @gs);
738 + $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
739 }
740
741 sub read_all_remotes {
@@ -732,6 +749,7 @@ sub read_all_remotes {
749 (.*):refs/remotes/(.+)\s*$/!x) {
750 my ($p, $g) = ($3, $4);
751 my $rs = $r->{$1}->{$2} = {
752 + t => $2,
753 path => Git::SVN::GlobSpec->new($p),
754 ref => Git::SVN::GlobSpec->new($g) };
755 if (length($rs->{ref}->{right}) != 0) {
@@ -793,20 +811,26 @@ sub init_remote_config {
811 my $r = read_all_remotes();
812 my $existing = find_existing_remote($url, $r);
813 if ($existing) {
796 - print STDERR "Using existing ",
797 - "[svn-remote \"$existing\"]\n";
814 + unless ($no_write) {
815 + print STDERR "Using existing ",
816 + "[svn-remote \"$existing\"]\n";
817 + }
818 $self->{repo_id} = $existing;
819 } else {
820 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
821 $existing = find_existing_remote($min_url, $r);
822 if ($existing) {
803 - print STDERR "Using existing ",
804 - "[svn-remote \"$existing\"]\n";
823 + unless ($no_write) {
824 + print STDERR "Using existing ",
825 + "[svn-remote \"$existing\"]\n";
826 + }
827 $self->{repo_id} = $existing;
828 }
829 if ($min_url ne $url) {
808 - print STDERR "Using higher level of URL: ",
809 - "$url => $min_url\n";
830 + unless ($no_write) {
831 + print STDERR "Using higher level of URL: ",
832 + "$url => $min_url\n";
833 + }
834 my $old_path = $self->{path};
835 $self->{path} = $url;
836 $self->{path} =~ s!^\Q$min_url\E/*!!;
@@ -1122,8 +1146,8 @@ sub match_paths {
1146 foreach (split m#/#, $self->{path}) {
1147 $c .= "/$_";
1148 next unless ($paths->{$c} && ($paths->{$c}->{action} eq 'A'));
1125 - my @x = eval { $self->ra->get_dir($self->{path}, $r) };
1126 - if (scalar @x == 3) {
1149 + if ($self->ra->check_path($self->{path}, $r) ==
1150 + $SVN::Node::dir) {
1151 return 1;
1152 }
1153 }
@@ -1172,6 +1196,10 @@ sub find_parent_branch {
1196 my $u = $remotes->{$repo_id}->{url} or next;
1197 next if $url ne $u;
1198 my $fetch = $remotes->{$repo_id}->{fetch};
1199 + foreach (qw/branches tags/) {
1200 + resolve_local_globs($url, $fetch,
1201 + $remotes->{$repo_id}->{$_});
1202 + }
1203 foreach my $f (keys %$fetch) {
1204 next if $f ne $branch_from;
1205 $gs = Git::SVN->new($fetch->{$f}, $repo_id, $f);
@@ -1238,7 +1266,7 @@ sub do_fetch {
1266 my ($self, $paths, $rev) = @_;
1267 my $ed;
1268 my ($last_rev, @parents);
1241 - if ($self->{last_commit}) {
1269 + if ($self->last_commit) {
1270 $ed = SVN::Git::Fetcher->new($self);
1271 $last_rev = $self->{last_rev};
1272 $ed->{c} = $self->{last_commit};
@@ -1354,8 +1382,7 @@ sub fetch {
1382 my ($self, $min_rev, $max_rev, @parents) = @_;
1383 my ($last_rev, $last_commit) = $self->last_rev_commit;
1384 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
1357 - return if ($base > $head);
1358 - $self->ra->gs_fetch_loop_common($base, $head, $self);
1385 + $self->ra->gs_fetch_loop_common($base, $head, [$self]);
1386 }
1387
1388 sub set_tree_cb {
@@ -2430,12 +2457,14 @@ sub gs_do_switch {
2457 }
2458
2459 sub gs_fetch_loop_common {
2433 - my ($self, $base, $head, @gs) = @_;
2460 + my ($self, $base, $head, $gsv, $globs) = @_;
2461 + return if ($base > $head);
2462 my $inc = 1000;
2463 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
2436 -
2464 my %common;
2438 - foreach my $gs (@gs) {
2465 + my $common_max = scalar @$gsv;
2466 +
2467 + foreach my $gs (@$gsv) {
2468 if (my $last_commit = $gs->last_commit) {
2469 $gs->assert_index_clean($last_commit);
2470 }
@@ -2447,9 +2476,21 @@ sub gs_fetch_loop_common {
2476 $common{$p}++;
2477 }
2478 }
2479 + $globs ||= [];
2480 + $common_max += scalar @$globs;
2481 + foreach my $glob (@$globs) {
2482 + my @tmp = split m#/#, $glob->{path}->{left};
2483 + my $p = '';
2484 + foreach (@tmp) {
2485 + $p .= length($p) ? "/$_" : $_;
2486 + $common{$p} ||= 0;
2487 + $common{$p}++;
2488 + }
2489 + }
2490 +
2491 my $longest_path = '';
2492 foreach (sort {length $b <=> length $a} keys %common) {
2452 - if ($common{$_} == @gs) {
2493 + if ($common{$_} == $common_max) {
2494 $longest_path = $_;
2495 last;
2496 }
@@ -2491,9 +2532,12 @@ sub gs_fetch_loop_common {
2532 }
2533 $SVN::Error::handler = $err_handler;
2534
2535 + my %exists = map { $_->{path} => $_ } @$gsv;
2536 foreach my $r (sort {$a <=> $b} keys %revs) {
2537 my ($paths, $logged) = @{$revs{$r}};
2496 - foreach my $gs (@gs) {
2538 +
2539 + foreach my $gs ($self->match_globs(\%exists, $paths,
2540 + $globs, $r)) {
2541 if ($gs->rev_db_max >= $r) {
2542 next;
2543 }
@@ -2504,10 +2548,22 @@ sub gs_fetch_loop_common {
2548 $gs->do_git_commit($log_entry);
2549 }
2550 }
2551 + foreach my $g (@$globs) {
2552 + my $f = "$ENV{GIT_DIR}/svn/." .
2553 + $self->uuid . ".$g->{t}";
2554 + open my $fh, '>', "$f.tmp" or
2555 + die "Can't open $f.tmp for writing: $!";
2556 + print $fh "$r\n" or
2557 + die "Couldn't write to $f: $!\n";
2558 + close $fh or die "Error closing $f: $!\n";
2559 + rename "$f.tmp", $f or
2560 + die "Couldn't rename ",
2561 + "$f.tmp => $f: $!\n";
2562 + }
2563 }
2564 # pre-fill the .rev_db since it'll eventually get filled in
2565 # with '0' x40 if something new gets committed
2510 - foreach my $gs (@gs) {
2566 + foreach my $gs (@$gsv) {
2567 next if defined $gs->rev_db_get($max);
2568 $gs->rev_db_set($max, 0 x40);
2569 }
@@ -2518,6 +2574,43 @@ sub gs_fetch_loop_common {
2574 }
2575 }
2576
2577 +sub match_globs {
2578 + my ($self, $exists, $paths, $globs, $r) = @_;
2579 + foreach my $g (@$globs) {
2580 + foreach (keys %$paths) {
2581 + next unless /$g->{path}->{regex}/;
2582 + my $p = $1;
2583 + my $pathname = $g->{path}->full_path($p);
2584 + next if $exists->{$pathname};
2585 + $exists->{$pathname} = Git::SVN->init(
2586 + $self->{url}, $pathname, undef,
2587 + $g->{ref}->full_path($p), 1);
2588 + }
2589 + my $c = '';
2590 + foreach (split m#/#, $g->{path}->{left}) {
2591 + $c .= "/$_";
2592 + next unless ($paths->{$c} &&
2593 + ($paths->{$c}->{action} eq 'A'));
2594 + my @x = eval { $self->get_dir($g->{path}->{left}, $r) };
2595 + next unless scalar @x == 3;
2596 + my $dirents = $x[0];
2597 + foreach my $de (keys %$dirents) {
2598 + next if $dirents->{$de}->kind !=
2599 + $SVN::Node::dir;
2600 + my $p = $g->{path}->full_path($de);
2601 + next if $exists->{$p};
2602 + next if (length $g->{path}->{right} &&
2603 + ($self->check_path($p, $r) !=
2604 + $SVN::Node::dir));
2605 + $exists->{$p} = Git::SVN->init($self->{url},
2606 + $p, undef,
2607 + $g->{ref}->full_path($de), 1);
2608 + }
2609 + }
2610 + }
2611 + values %$exists;
2612 +}
2613 +
2614 sub minimize_url {
2615 my ($self) = @_;
2616 return $self->{url} if ($self->{url} eq $self->{repos_root});
@@ -3167,16 +3260,15 @@ use warnings;
3260
3261 sub new {
3262 my ($class, $glob) = @_;
3170 - warn "glob: $glob\n";
3263 my $re = $glob;
3264 $re =~ s!/+$!!g; # no need for trailing slashes
3265 my $nr = ($re =~ s!^(.*/?)\*(/?.*)$!\(\[^/\]+\)!g);
3266 my ($left, $right) = ($1, $2);
3267 if ($nr > 1) {
3176 - warn "Only one '*' wildcard expansion ",
3177 - "is supported (got $nr): '$glob'\n";
3268 + die "Only one '*' wildcard expansion ",
3269 + "is supported (got $nr): '$glob'\n";
3270 } elsif ($nr == 0) {
3179 - warn "One '*' is needed for glob: '$glob'\n";
3271 + die "One '*' is needed for glob: '$glob'\n";
3272 }
3273 $re = quotemeta($left) . $re . quotemeta($right);
3274 $left =~ s!/+$!!g;