@matheusbsbs / matheus-bagliano / commits / 706587fc6d
Fork 0

git-svn: add support for metadata in .git/config

Of course, we handle metadata migrations from previous versions and we have added unit tests. The new .git/config remotes resemble non-SVN remotes. Below is an example with comments: [svn-remote "git-svn"] ; like non-svn remotes, we have one URL per-remote url = http://foo.bar.org/svn ; 'fetch' keys are done in the same way as non-svn ; remotes, too. With the left-hand-side of the ':' ; being the remote (SVN) repository path relative to the ; above 'url' key; and the right-hand-side being a ; remote ref in git (refs/remotes/*). ; An empty left-hand-side means that it will fetch ; the entire contents of the 'url' key. ; old-style (migrated from previous versions of git-svn) ; are like this: fetch = :refs/remotes/git-svn ; this is created by a current version of git-svn ; using the multi-init command with an explicit ; url (specified above). This allows multi-init ; to reuse SVN::Ra connections. fetch = trunk:refs/remotes/trunk fetch = branches/a:refs/remotes/a fetch = branches/b:refs/remotes/b fetch = tags/0.1:refs/remotes/tags/0.1 fetch = tags/0.2:refs/remotes/tags/0.2 fetch = tags/0.3:refs/remotes/tags/0.3 [svn-remote "alt"] ; this is another old-style remote migrated over ; to the new config format url = http://foo.bar.org/alt fetch = :refs/remotes/alt Signed-off-by: Eric Wong <normalperson@yhbt.net>

Eric Wong committed Jan 18, 2007 at 17:50 UTC 706587fc6d56db1ba6c7207d4c0c456bac6f77c2
2 files changed +367 -279
git-svn.perl
+304 -279
@@ -13,9 +13,8 @@ use vars qw/ $AUTHOR $VERSION
13 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
14 $VERSION = '@@GIT_VERSION@@';
15
16 -use Cwd qw/abs_path/;
17 -$GIT_DIR = abs_path($ENV{GIT_DIR} || '.git');
18 -$ENV{GIT_DIR} = $GIT_DIR;
16 +$ENV{GIT_DIR} ||= '.git';
17 +$Git::SVN::default_repo_id = $ENV{GIT_SVN_ID} || 'git-svn';
18
19 my $LC_ALL = $ENV{LC_ALL};
20 $Git::SVN::Log::TZ = $ENV{TZ};
@@ -47,6 +46,7 @@ BEGIN {
46 foreach (qw/command command_oneline command_noisy command_output_pipe
47 command_input_pipe command_close_pipe/) {
48 $s .= "*SVN::Git::Editor::$_ = *SVN::Git::Fetcher::$_ = ".
49 + "*Git::SVN::Migration::$_ = ".
50 "*Git::SVN::Log::$_ = *Git::SVN::$_ = *$_ = *Git::$_; ";
51 }
52 eval $s;
@@ -64,17 +64,17 @@ my ($_stdin, $_help, $_edit,
64 $_version, $_upgrade,
65 $_merge, $_strategy, $_dry_run,
66 $_prefix);
67 -my @repo_path_split_cache;
67
68 +my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
69 + 'config-dir=s' => \$Git::SVN::Ra::config_dir,
70 + 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache );
71 my %fc_opts = ( 'follow-parent|follow' => \$_follow_parent,
72 'authors-file|A=s' => \$_authors,
73 'repack:i' => \$_repack,
74 'no-metadata' => \$_no_metadata,
75 'quiet|q' => \$_q,
74 - 'username=s' => \$Git::SVN::Prompt::_username,
75 - 'config-dir=s' => \$Git::SVN::Ra::config_dir,
76 - 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache,
77 - 'repack-flags|repack-args|repack-opts=s' => \$_repack_flags);
76 + 'repack-flags|repack-args|repack-opts=s' => \$_repack_flags,
77 + %remote_opts );
78
79 my ($_trunk, $_tags, $_branches);
80 my %multi_opts = ( 'trunk|T=s' => \$_trunk,
@@ -110,16 +110,20 @@ my %cmd = (
110 'upgrade' => \$_upgrade } ],
111 'multi-init' => [ \&cmd_multi_init,
112 'Initialize multiple trees (like git-svnimport)',
113 - { %multi_opts, %init_opts,
113 + { %multi_opts, %init_opts, %remote_opts,
114 'revision|r=i' => \$_revision,
115 - 'username=s' => \$Git::SVN::Prompt::_username,
116 - 'config-dir=s' => \$Git::SVN::Ra::config_dir,
117 - 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache,
115 'prefix=s' => \$_prefix,
116 } ],
117 'multi-fetch' => [ \&cmd_multi_fetch,
118 'Fetch multiple trees (like git-svnimport)',
119 \%fc_opts ],
120 + 'migrate' => [ sub { },
121 + # no-op, we automatically run this anyways,
122 + # we may add a flag to automatically optimize the
123 + # configuration to avoid reconnects in the future
124 + 'Migrate configuration/metadata/layout from
125 + previous versions of git-svn',
126 + \%remote_opts ],
127 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs',
128 { 'limit=i' => \$Git::SVN::Log::limit,
129 'revision|r=s' => \$_revision,
@@ -154,15 +158,16 @@ my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
158 read_repo_config(\%opts);
159 my $rv = GetOptions(%opts, 'help|H|h' => \$_help,
160 'version|V' => \$_version,
157 - 'id|i=s' => \$GIT_SVN);
161 + 'id|i=s' => \$Git::SVN::default_repo_id);
162 exit 1 if (!$rv && $cmd ne 'log');
163
164 usage(0) if $_help;
165 version() if $_version;
166 usage(1) unless defined $cmd;
163 -init_vars();
167 load_authors() if $_authors;
165 -migration_check() unless $cmd =~ /^(?:init|rebuild|multi-init|commit-diff)$/;
168 +unless ($cmd =~ /^(?:init|rebuild|multi-init|commit-diff)$/) {
169 + Git::SVN::Migration::migration_check();
170 +}
171 $cmd{$cmd}->[0]->(@ARGV);
172 exit 0;
173
@@ -203,17 +208,12 @@ sub version {
208
209 sub cmd_rebuild {
210 my $url = shift;
206 - my $gs = $url ? Git::SVN->init(undef, $url)
211 + my $gs = $url ? Git::SVN->init($url)
212 : eval { Git::SVN->new };
213 $gs ||= Git::SVN->_new;
214 if (!verify_ref($gs->refname.'^0')) {
215 $gs->copy_remote_ref;
216 }
212 - if ($_upgrade) {
213 - command_noisy('update-ref',$gs->refname, $gs->{id}.'-HEAD');
214 - } else {
215 - $gs->check_upgrade_needed;
216 - }
217
218 my ($rev_list, $ctx) = command_output_pipe("rev-list", $gs->refname);
219 my $latest;
@@ -238,7 +238,7 @@ sub cmd_rebuild {
238 if (!$gs->{url} && !$url) {
239 fatal "SVN repository location required\n";
240 }
241 - $gs = Git::SVN->init(undef, $url);
241 + $gs = Git::SVN->init($url);
242 $latest = $rev;
243 }
244 $gs->rev_db_set($rev, $c);
@@ -268,7 +268,7 @@ sub cmd_init {
268 }
269 do_git_init_db();
270
271 - Git::SVN->init(undef, $url);
271 + Git::SVN->init($url);
272 }
273
274 sub cmd_fetch {
@@ -389,28 +389,35 @@ sub cmd_multi_init {
389 }
390 do_git_init_db();
391 $_prefix = '' unless defined $_prefix;
392 + $url =~ s#/+$## if defined $url;
393 if (defined $_trunk) {
393 - my $gs_trunk = eval { Git::SVN->new($_prefix . 'trunk') };
394 + my $trunk_ref = $_prefix . 'trunk';
395 + # try both old-style and new-style lookups:
396 + my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
397 unless ($gs_trunk) {
395 - my $trunk_url = complete_svn_url($url, $_trunk);
396 - $gs_trunk = Git::SVN->init($_prefix . 'trunk',
397 - $trunk_url);
398 - command_noisy('config', 'svn.trunk', $trunk_url);
398 + my ($trunk_url, $trunk_path) =
399 + complete_svn_url($url, $_trunk);
400 + $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
401 + undef, $trunk_ref);
402 }
403 }
404 + return unless defined $_branches || defined $_tags;
405 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
406 complete_url_ls_init($ra, $_branches, '--branches/-b', $_prefix);
407 complete_url_ls_init($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
408 }
409
410 sub cmd_multi_fetch {
407 - # try to do trunk first, since branches/tags
408 - # may be descended from it.
409 - if (-e "$ENV{GIT_DIR}/svn/trunk/info/url") {
410 - my $gs = Git::SVN->new('trunk');
411 - $gs->fetch(@_);
411 + my @gs;
412 + foreach (command(qw/config -l/)) {
413 + next unless m!^svn-remote\.(.+)\.fetch=
414 + \s*(.*)\s*:\s*refs/remotes/(.+)\s*$!x;
415 + my ($repo_id, $path, $ref_id) = ($1, $2, $3);
416 + push @gs, Git::SVN->new($ref_id, $repo_id, $path);
417 + }
418 + foreach (@gs) {
419 + $_->fetch;
420 }
413 - rec_fetch('', "$ENV{GIT_DIR}/svn", @_);
421 }
422
423 # this command is special because it requires no metadata
@@ -464,95 +471,50 @@ sub cmd_commit_diff {
471
472 ########################### utility functions #########################
473
467 -sub rec_fetch {
468 - my ($pfx, $p, @args) = @_;
469 - my @dir;
470 - foreach (sort <$p/*>) {
471 - if (-r "$_/info/url") {
472 - $pfx .= '/' if $pfx && $pfx !~ m!/$!;
473 - my $id = $pfx . basename $_;
474 - next if $id eq 'trunk';
475 - my $gs = Git::SVN->new($id);
476 - $gs->fetch(@args);
477 - } elsif (-d $_) {
478 - push @dir, $_;
479 - }
480 - }
481 - foreach (@dir) {
482 - my $x = $_;
483 - $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
484 - rec_fetch($x, $_, @args);
485 - }
486 -}
487 -
474 sub complete_svn_url {
475 my ($url, $path) = @_;
476 $path =~ s#/+$##;
491 - $url =~ s#/+$## if $url;
477 if ($path !~ m#^[a-z\+]+://#) {
493 - $path = '/' . $path if ($path !~ m#^/#);
478 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
479 fatal("E: '$path' is not a complete URL ",
480 "and a separate URL is not specified\n");
481 }
498 - $path = $url . $path;
482 + return ($url, $path);
483 }
500 - return $path;
484 + return ($path, '');
485 }
486
487 sub complete_url_ls_init {
504 - my ($ra, $path, $switch, $pfx) = @_;
505 - unless ($path) {
488 + my ($ra, $repo_path, $switch, $pfx) = @_;
489 + unless ($repo_path) {
490 print STDERR "W: $switch not specified\n";
491 return;
492 }
509 - $path =~ s#/+$##;
510 - if ($path =~ m#^[a-z\+]+://#) {
511 - $ra = Git::SVN::Ra->new($path);
512 - $path = '';
493 + $repo_path =~ s#/+$##;
494 + if ($repo_path =~ m#^[a-z\+]+://#) {
495 + $ra = Git::SVN::Ra->new($repo_path);
496 + $repo_path = '';
497 } else {
514 - $path =~ s#^/+##;
498 + $repo_path =~ s#^/+##;
499 unless ($ra) {
516 - fatal("E: '$path' is not a complete URL ",
500 + fatal("E: '$repo_path' is not a complete URL ",
501 "and a separate URL is not specified\n");
502 }
503 }
504 my $r = defined $_revision ? $_revision : $ra->get_latest_revnum;
521 - my ($dirent, undef, undef) = $ra->get_dir($path, $r);
522 - my $url = $ra->{url} . (length $path ? "/$path" : '');
505 + my ($dirent, undef, undef) = $ra->get_dir($repo_path, $r);
506 + my $url = $ra->{url};
507 foreach my $d (sort keys %$dirent) {
508 next if ($dirent->{$d}->kind != $SVN::Node::dir);
525 - my $u = "$url/$d";
526 - my $id = "$pfx$d";
527 - my $gs = eval { Git::SVN->new($id) };
509 + my $path = "$repo_path/$d";
510 + my $ref = "$pfx$d";
511 + my $gs = eval { Git::SVN->new($ref) };
512 # don't try to init already existing refs
513 unless ($gs) {
530 - print "init $u => $id\n";
531 - Git::SVN->init($id, $u);
514 + print "init $url/$path => $ref\n";
515 + Git::SVN->init($url, $path, undef, $ref);
516 }
517 }
534 - my ($n) = ($switch =~ /^--(\w+)/);
535 - command_noisy('config', "svn.$n", $url);
536 -}
537 -
538 -sub common_prefix {
539 - my $paths = shift;
540 - my %common;
541 - foreach (@$paths) {
542 - my @tmp = split m#/#, $_;
543 - my $p = '';
544 - while (my $x = shift @tmp) {
545 - $p .= "/$x";
546 - $common{$p} ||= 0;
547 - $common{$p}++;
548 - }
549 - }
550 - foreach (sort {length $b <=> length $a} keys %common) {
551 - if ($common{$_} == @$paths) {
552 - return $_;
553 - }
554 - }
555 - return '';
518 }
519
520 sub verify_ref {
@@ -561,34 +523,6 @@ sub verify_ref {
523 { STDERR => 0 }); };
524 }
525
564 -sub repo_path_split {
565 - my $full_url = shift;
566 - $full_url =~ s#/+$##;
567 -
568 - foreach (@repo_path_split_cache) {
569 - if ($full_url =~ s#$_##) {
570 - my $u = $1;
571 - $full_url =~ s#^/+##;
572 - return ($u, $full_url);
573 - }
574 - }
575 - my $tmp = Git::SVN::Ra->new($full_url);
576 - return ($tmp->{repos_root}, $tmp->{svn_path});
577 -}
578 -
579 -sub setup_git_svn {
580 - defined $SVN_URL or croak "SVN repository location required\n";
581 - unless (-d $GIT_DIR) {
582 - croak "GIT_DIR=$GIT_DIR does not exist!\n";
583 - }
584 - mkpath([$GIT_SVN_DIR]);
585 - mkpath(["$GIT_SVN_DIR/info"]);
586 - open my $fh, '>>',$REVDB or croak $!;
587 - close $fh;
588 - s_to_file($SVN_URL,"$GIT_SVN_DIR/info/url");
589 -
590 -}
591 -
526 sub get_tree_from_treeish {
527 my ($treeish) = @_;
528 # $treeish can be a symbolic ref, too:
@@ -668,23 +602,6 @@ sub file_to_s {
602 return $ret;
603 }
604
671 -sub check_upgrade_needed {
672 - if (!-r $REVDB) {
673 - -d $GIT_SVN_DIR or mkpath([$GIT_SVN_DIR]);
674 - open my $fh, '>>',$REVDB or croak $!;
675 - close $fh;
676 - }
677 - return unless eval {
678 - command([qw/rev-parse --verify/,"$GIT_SVN-HEAD^0"],
679 - {STDERR => 0});
680 - };
681 - my $head = eval { command('rev-parse',"refs/remotes/$GIT_SVN") };
682 - if ($@ || !$head) {
683 - print STDERR "Please run: $0 rebuild --upgrade\n";
684 - exit 1;
685 - }
686 -}
687 -
605 # '<svn username> = real-name <email address>' mapping based on git-svnimport:
606 sub load_authors {
607 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
@@ -702,75 +619,9 @@ sub load_authors {
619 close $authors or croak $!;
620 }
621
705 -sub git_svn_each {
706 - my $sub = shift;
707 - foreach (command(qw/rev-parse --symbolic --all/)) {
708 - next unless s#^refs/remotes/##;
709 - chomp $_;
710 - next unless -f "$GIT_DIR/svn/$_/info/url";
711 - &$sub($_);
712 - }
713 -}
714 -
715 -sub migrate_revdb {
716 - git_svn_each(sub {
717 - my $id = shift;
718 - defined(my $pid = fork) or croak $!;
719 - if (!$pid) {
720 - $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
721 - init_vars();
722 - exit 0 if -r $REVDB;
723 - print "Upgrading svn => git mapping...\n";
724 - -d $GIT_SVN_DIR or mkpath([$GIT_SVN_DIR]);
725 - open my $fh, '>>',$REVDB or croak $!;
726 - close $fh;
727 - rebuild();
728 - print "Done upgrading. You may now delete the ",
729 - "deprecated $GIT_SVN_DIR/revs directory\n";
730 - exit 0;
731 - }
732 - waitpid $pid, 0;
733 - croak $? if $?;
734 - });
735 -}
736 -
737 -sub migration_check {
738 - migrate_revdb() unless (-e $REVDB);
739 - return if (-d "$GIT_DIR/svn" || !-d $GIT_DIR);
740 - print "Upgrading repository...\n";
741 - unless (-d "$GIT_DIR/svn") {
742 - mkdir "$GIT_DIR/svn" or croak $!;
743 - }
744 - print "Data from a previous version of git-svn exists, but\n\t",
745 - "$GIT_SVN_DIR\n\t(required for this version ",
746 - "($VERSION) of git-svn) does not.\n";
747 -
748 - foreach my $x (command(qw/rev-parse --symbolic --all/)) {
749 - next unless $x =~ s#^refs/remotes/##;
750 - chomp $x;
751 - next unless -f "$GIT_DIR/$x/info/url";
752 - my $u = eval { file_to_s("$GIT_DIR/$x/info/url") };
753 - next unless $u;
754 - my $dn = dirname("$GIT_DIR/svn/$x");
755 - mkpath([$dn]) unless -d $dn;
756 - rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak "$!: $x";
757 - }
758 - migrate_revdb() if (-d $GIT_SVN_DIR && !-w $REVDB);
759 - print "Done upgrading.\n";
760 -}
761 -
762 -sub init_vars {
763 - $GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn';
764 - $Git::SVN::default = $GIT_SVN;
765 - $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
766 - $REVDB = "$GIT_SVN_DIR/.rev_db";
767 - $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
768 - $SVN_URL = undef;
769 -}
770 -
622 # convert GetOpt::Long specs for use by git-config
623 sub read_repo_config {
773 - return unless -d $GIT_DIR;
624 + return unless -d $ENV{GIT_DIR};
625 my $opts = shift;
626 foreach my $o (keys %$opts) {
627 my $v = $opts->{$o};
@@ -791,38 +642,6 @@ sub read_repo_config {
642 }
643 }
644
794 -sub read_url_paths_all {
795 - my ($l_map, $pfx, $p) = @_;
796 - my @dir;
797 - foreach (<$p/*>) {
798 - if (-r "$_/info/url") {
799 - $pfx .= '/' if $pfx && $pfx !~ m!/$!;
800 - my $id = $pfx . basename $_;
801 - my $url = file_to_s("$_/info/url");
802 - my ($u, $p) = repo_path_split($url);
803 - $l_map->{$u}->{$p} = $id;
804 - } elsif (-d $_) {
805 - push @dir, $_;
806 - }
807 - }
808 - foreach (@dir) {
809 - my $x = $_;
810 - $x =~ s!^\Q$GIT_DIR\E/svn/!!o;
811 - read_url_paths_all($l_map, $x, $_);
812 - }
813 -}
814 -
815 -# this one only gets ids that have been imported, not new ones
816 -sub read_url_paths {
817 - my $l_map = {};
818 - git_svn_each(sub { my $x = shift;
819 - my $url = file_to_s("$GIT_DIR/svn/$x/info/url");
820 - my ($u, $p) = repo_path_split($url);
821 - $l_map->{$u}->{$p} = $x;
822 - });
823 - return $l_map;
824 -}
825 -
645 sub extract_metadata {
646 my $id = shift or return (undef, undef, undef);
647 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
@@ -866,7 +685,7 @@ sub tz_to_s_offset {
685 package Git::SVN;
686 use strict;
687 use warnings;
869 -use vars qw/$default/;
688 +use vars qw/$default_repo_id/;
689 use Carp qw/croak/;
690 use File::Path qw/mkpath/;
691 use IPC::Open3;
@@ -882,28 +701,76 @@ BEGIN {
701 svn:entry:committed-date/;
702 }
703
704 +# we allow dashes, unlike remotes2config.sh
705 +sub sanitize_remote_name {
706 + my ($name) = @_;
707 + $name =~ tr/A-Za-z0-9-/./c;
708 + $name;
709 +}
710 +
711 sub init {
886 - my ($class, $id, $url) = @_;
887 - my $self = _new($class, $id);
888 - mkpath(["$self->{dir}/info"]);
712 + my ($class, $url, $path, $repo_id, $ref_id) = @_;
713 + my $self = _new($class, $repo_id, $ref_id, $path);
714 + mkpath([$self->{dir}]);
715 if (defined $url) {
716 $url =~ s!/+$!!; # strip trailing slash
891 - ::s_to_file($url, "$self->{dir}/info/url");
717 + my $orig_url = eval {
718 + command_oneline('config', '--get',
719 + "svn-remote.$repo_id.url")
720 + };
721 + if ($orig_url) {
722 + if ($orig_url ne $url) {
723 + die "svn-remote.$repo_id.url already set: ",
724 + "$orig_url\nwanted to set to: $url\n";
725 + }
726 + } else {
727 + command_noisy('config',
728 + "svn-remote.$repo_id.url", $url);
729 + }
730 + command_noisy('config', '--add',
731 + "svn-remote.$repo_id.fetch",
732 + "$path:".$self->refname);
733 }
734 $self->{url} = $url;
894 - open my $fh, '>>', $self->{db_path} or croak $!;
895 - close $fh or croak $!;
735 + unless (-f $self->{db_path}) {
736 + open my $fh, '>>', $self->{db_path} or croak $!;
737 + close $fh or croak $!;
738 + }
739 $self;
740 }
741
742 +sub find_ref {
743 + my ($ref_id) = @_;
744 + foreach (command(qw/config -l/)) {
745 + next unless m!^svn-remote\.(.+)\.fetch=
746 + \s*(.*)\s*:\s*refs/remotes/(.+)\s*$!x;
747 + my ($repo_id, $path, $ref) = ($1, $2, $3);
748 + if ($ref eq $ref_id) {
749 + $path = '' if ($path =~ m#^\./?#);
750 + return ($repo_id, $path);
751 + }
752 + }
753 + (undef, undef, undef);
754 +}
755 +
756 sub new {
900 - my ($class, $id) = @_;
901 - my $self = _new($class, $id);
902 - $self->{url} = ::file_to_s("$self->{dir}/info/url");
757 + my ($class, $ref_id, $repo_id, $path) = @_;
758 + if (defined $ref_id && !defined $repo_id && !defined $path) {
759 + ($repo_id, $path) = find_ref($ref_id);
760 + if (!defined $repo_id) {
761 + die "Could not find a \"svn-remote.*.fetch\" key ",
762 + "in the repository configuration matching: ",
763 + "refs/remotes/$ref_id\n";
764 + }
765 + }
766 + my $self = _new($class, $repo_id, $ref_id, $path);
767 + $self->{url} = command_oneline('config', '--get',
768 + "svn-remote.$repo_id.url") or
769 + die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
770 $self;
771 }
772
906 -sub refname { "refs/remotes/$_[0]->{id}" }
773 +sub refname { "refs/remotes/$_[0]->{ref_id}" }
774
775 sub ra {
776 my ($self) = shift;
@@ -952,7 +819,7 @@ sub last_rev_commit {
819 return ($self->{last_rev}, $self->{last_commit});
820 }
821 my $c = ::verify_ref($self->refname.'^0');
955 - if (defined $c && length $c) {
822 + if ($c) {
823 my $rev = (::cmt_metadata($c))[1];
824 if (defined $rev) {
825 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
@@ -1064,18 +931,9 @@ sub get_commit_parents {
931 @ret;
932 }
933
1067 -sub check_upgrade_needed {
934 +sub full_url {
935 my ($self) = @_;
1069 - if (!-r $self->{db_path}) {
1070 - -d $self->{dir} or mkpath([$self->{dir}]);
1071 - open my $fh, '>>', $self->{db_path} or croak $!;
1072 - close $fh;
1073 - }
1074 - return unless ::verify_ref($self->{id}.'-HEAD^0');
1075 - my $head = ::verify_ref($self->refname.'^0');
1076 - if ($@ || !$head) {
1077 - ::fatal("Please run: $0 rebuild --upgrade\n");
1078 - }
936 + $self->ra->{url} . (length $self->{path} ? '/' . $self->{path} : '');
937 }
938
939 sub do_git_commit {
@@ -1105,7 +963,7 @@ sub do_git_commit {
963 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
964 or croak $!;
965 print $msg_fh $log_entry->{log} or croak $!;
1108 - print $msg_fh "\ngit-svn-id: ", $self->ra->{url}, '@',
966 + print $msg_fh "\ngit-svn-id: ", $self->full_url, '@',
967 $log_entry->{revision}, ' ',
968 $self->ra->uuid, "\n" or croak $!;
969 $msg_fh->flush == 0 or croak $!;
@@ -1128,7 +986,7 @@ sub do_git_commit {
986 }
987
988 sub do_fetch {
1131 - my ($self, $paths, $rev) = @_; #, $author, $date, $log) = @_;
989 + my ($self, $paths, $rev) = @_;
990 my $ed = SVN::Git::Fetcher->new($self);
991 my ($last_rev, @parents);
992 if ($self->{last_commit}) {
@@ -1138,7 +996,8 @@ sub do_fetch {
996 } else {
997 $last_rev = $rev;
998 }
1141 - unless ($self->ra->gs_do_update($last_rev, $rev, '', 1, $ed)) {
999 + unless ($self->ra->gs_do_update($last_rev, $rev,
1000 + $self->{path}, 1, $ed)) {
1001 die "SVN connection failed somewhere...\n";
1002 }
1003 $self->make_log_entry($rev, \@parents, $ed);
@@ -1361,11 +1220,19 @@ sub rev_db_get {
1220 }
1221
1222 sub _new {
1364 - my ($class, $id) = @_;
1365 - $id ||= $Git::SVN::default;
1366 - my $dir = "$ENV{GIT_DIR}/svn/$id";
1367 - bless { id => $id, dir => $dir, index => "$dir/index",
1368 - db_path => "$dir/.rev_db" }, $class;
1223 + my ($class, $repo_id, $ref_id, $path) = @_;
1224 + unless (defined $repo_id && length $repo_id) {
1225 + $repo_id = $Git::SVN::default_repo_id;
1226 + }
1227 + unless (defined $ref_id && length $ref_id) {
1228 + $_[2] = $ref_id = $repo_id;
1229 + }
1230 + $_[1] = $repo_id = sanitize_remote_name($repo_id);
1231 + my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
1232 + $_[3] = $path = '' unless (defined $path);
1233 + bless { ref_id => $ref_id, dir => $dir, index => "$dir/index",
1234 + path => $path,
1235 + db_path => "$dir/.rev_db", repo_id => $repo_id }, $class;
1236 }
1237
1238 sub uri_encode {
@@ -1630,6 +1497,9 @@ sub new {
1497 my $self = SVN::Delta::Editor->new;
1498 bless $self, $class;
1499 $self->{c} = $git_svn->{last_commit} if exists $git_svn->{last_commit};
1500 + if (length $git_svn->{path}) {
1501 + $self->{path_strip} = qr/\Q$git_svn->{path}\E\/?/;
1502 + }
1503 $self->{empty} = {};
1504 $self->{dir_prop} = {};
1505 $self->{file_prop} = {};
@@ -1650,33 +1520,41 @@ sub open_directory {
1520 { path => $path };
1521 }
1522
1523 +sub git_path {
1524 + my ($self, $path) = @_;
1525 + $path =~ s!$self->{path_strip}!! if $self->{path_strip};
1526 + $path;
1527 +}
1528 +
1529 sub delete_entry {
1530 my ($self, $path, $rev, $pb) = @_;
1531 my $gui = $self->{gui};
1532
1533 + my $gpath = $self->git_path($path);
1534 # remove entire directories.
1658 - if (command('ls-tree', $self->{c}, '--', $path) =~ /^040000 tree/) {
1535 + if (command('ls-tree', $self->{c}, '--', $gpath) =~ /^040000 tree/) {
1536 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
1537 -r --name-only -z/,
1661 - $self->{c}, '--', $path);
1538 + $self->{c}, '--', $gpath);
1539 local $/ = "\0";
1540 while (<$ls>) {
1541 print $gui '0 ',0 x 40,"\t",$_ or croak $!;
1542 print "\tD\t$_\n" unless $self->{q};
1543 }
1667 - print "\tD\t$path/\n" unless $self->{q};
1544 + print "\tD\t$gpath/\n" unless $self->{q};
1545 command_close_pipe($ls, $ctx);
1546 $self->{empty}->{$path} = 0
1547 } else {
1671 - print $gui '0 ',0 x 40,"\t",$path,"\0" or croak $!;
1672 - print "\tD\t$path\n" unless $self->{q};
1548 + print $gui '0 ',0 x 40,"\t",$gpath,"\0" or croak $!;
1549 + print "\tD\t$gpath\n" unless $self->{q};
1550 }
1551 undef;
1552 }
1553
1554 sub open_file {
1555 my ($self, $path, $pb, $rev) = @_;
1679 - my ($mode, $blob) = (command('ls-tree', $self->{c}, '--',$path)
1556 + my $gpath = $self->git_path($path);
1557 + my ($mode, $blob) = (command('ls-tree', $self->{c}, '--', $gpath)
1558 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
1559 unless (defined $mode && defined $blob) {
1560 die "$path was not found in commit $self->{c} (r$rev)\n";
@@ -1775,7 +1653,7 @@ sub apply_textdelta {
1653 sub close_file {
1654 my ($self, $fb, $exp) = @_;
1655 my $hash;
1778 - my $path = $fb->{path};
1656 + my $path = $self->git_path($fb->{path});
1657 if (my $fh = $fb->{fh}) {
1658 seek($fh, 0, 0) or croak $!;
1659 my $md5 = Digest::MD5->new;
@@ -2223,7 +2101,7 @@ sub gs_do_update {
2101 $editor, $pool);
2102 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
2103 my $new = ($rev_a == $rev_b);
2226 - $reporter->set_path($path, $rev_a, $new, @lock, $pool);
2104 + $reporter->set_path('', $rev_a, $new, @lock, $pool);
2105 $reporter->finish_report($pool);
2106 $pool->clear;
2107 $editor->{git_commit_ok};
@@ -2561,6 +2439,153 @@ out:
2439 print '-' x72,"\n" unless $incremental || $oneline;
2440 }
2441
2442 +package Git::SVN::Migration;
2443 +# these version numbers do NOT correspond to actual version numbers
2444 +# of git nor git-svn. They are just relative.
2445 +#
2446 +# v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
2447 +#
2448 +# v1 layout: .git/$id/info/url, refs/remotes/$id
2449 +#
2450 +# v2 layout: .git/svn/$id/info/url, refs/remotes/$id
2451 +#
2452 +# v3 layout: .git/svn/$id, refs/remotes/$id
2453 +# - info/url may remain for backwards compatibility
2454 +# - this is what we migrate up to this layout automatically,
2455 +# - this will be used by git svn init on single branches
2456 +#
2457 +# v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
2458 +# - this is only created for newly multi-init-ed
2459 +# repositories. Similar in spirit to the
2460 +# --use-separate-remotes option in git-clone (now default)
2461 +# - we do not automatically migrate to this (following
2462 +# the example set by core git)
2463 +use strict;
2464 +use warnings;
2465 +use Carp qw/croak/;
2466 +use File::Path qw/mkpath/;
2467 +use File::Basename qw/dirname/;
2468 +
2469 +sub migrate_from_v0 {
2470 + my $git_dir = $ENV{GIT_DIR};
2471 + return undef unless -d $git_dir;
2472 + my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
2473 + my $migrated = 0;
2474 + while (<$fh>) {
2475 + chomp;
2476 + my ($id, $orig_ref) = ($_, $_);
2477 + next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#;
2478 + next unless -f "$git_dir/$id/info/url";
2479 + my $new_ref = "refs/remotes/$id";
2480 + if (::verify_ref("$new_ref^0")) {
2481 + print STDERR "W: $orig_ref is probably an old ",
2482 + "branch used by an ancient version of ",
2483 + "git-svn.\n",
2484 + "However, $new_ref also exists.\n",
2485 + "We will not be able ",
2486 + "to use this branch until this ",
2487 + "ambiguity is resolved.\n";
2488 + next;
2489 + }
2490 + print STDERR "Migrating from v0 layout...\n" if !$migrated;
2491 + print STDERR "Renaming ref: $orig_ref => $new_ref\n";
2492 + command_noisy('update-ref', $new_ref, $orig_ref);
2493 + command_noisy('update-ref', '-d', $orig_ref, $orig_ref);
2494 + $migrated++;
2495 + }
2496 + command_close_pipe($fh, $ctx);
2497 + print STDERR "Done migrating from v0 layout...\n" if $migrated;
2498 + $migrated;
2499 +}
2500 +
2501 +sub migrate_from_v1 {
2502 + my $git_dir = $ENV{GIT_DIR};
2503 + my $migrated = 0;
2504 + return $migrated unless -d $git_dir;
2505 + my $svn_dir = "$git_dir/svn";
2506 +
2507 + # just in case somebody used 'svn' as their $id at some point...
2508 + return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url";
2509 +
2510 + print STDERR "Migrating from a git-svn v1 layout...\n";
2511 + mkpath([$svn_dir]);
2512 + print STDERR "Data from a previous version of git-svn exists, but\n\t",
2513 + "$svn_dir\n\t(required for this version ",
2514 + "($::VERSION) of git-svn) does not. exist\n";
2515 + my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
2516 + while (<$fh>) {
2517 + my $x = $_;
2518 + next unless $x =~ s#^refs/remotes/##;
2519 + chomp $x;
2520 + next unless -f "$git_dir/$x/info/url";
2521 + my $u = eval { ::file_to_s("$git_dir/$x/info/url") };
2522 + next unless $u;
2523 + my $dn = dirname("$git_dir/svn/$x");
2524 + mkpath([$dn]) unless -d $dn;
2525 + if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
2526 + mkpath(["$git_dir/svn/svn"]);
2527 + print STDERR " - $git_dir/$x/info => ",
2528 + "$git_dir/svn/$x/info\n";
2529 + rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
2530 + croak "$!: $x";
2531 + # don't worry too much about these, they probably
2532 + # don't exist with repos this old (save for index,
2533 + # and we can easily regenerate that)
2534 + foreach my $f (qw/unhandled.log index .rev_db/) {
2535 + rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
2536 + }
2537 + } else {
2538 + print STDERR " - $git_dir/$x => $git_dir/svn/$x\n";
2539 + rename "$git_dir/$x", "$git_dir/svn/$x" or
2540 + croak "$!: $x";
2541 + }
2542 + $migrated++;
2543 + }
2544 + command_close_pipe($fh, $ctx);
2545 + print STDERR "Done migrating from a git-svn v1 layout\n";
2546 + $migrated;
2547 +}
2548 +
2549 +sub read_old_urls {
2550 + my ($l_map, $pfx, $path) = @_;
2551 + my @dir;
2552 + foreach (<$path/*>) {
2553 + if (-r "$_/info/url") {
2554 + $pfx .= '/' if $pfx && $pfx !~ m!/$!;
2555 + my $ref_id = $pfx . basename $_;
2556 + my $url = ::file_to_s("$_/info/url");
2557 + $l_map->{$ref_id} = $url;
2558 + } elsif (-d $_) {
2559 + push @dir, $_;
2560 + }
2561 + }
2562 + foreach (@dir) {
2563 + my $x = $_;
2564 + $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
2565 + read_old_urls($l_map, $x, $_);
2566 + }
2567 +}
2568 +
2569 +sub migrate_from_v2 {
2570 + my @cfg = command(qw/config -l/);
2571 + return if grep /^svn-remote\..+\.url=/, @cfg;
2572 + my %l_map;
2573 + read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn");
2574 + my $migrated = 0;
2575 +
2576 + foreach my $ref_id (sort keys %l_map) {
2577 + Git::SVN->init($l_map{$ref_id}, $ref_id);
2578 + $migrated++;
2579 + }
2580 + $migrated;
2581 +}
2582 +
2583 +sub migration_check {
2584 + migrate_from_v0();
2585 + migrate_from_v1();
2586 + migrate_from_v2();
2587 +}
2588 +
2589 __END__
2590
2591 Data structures:
t/t9107-git-svn-migrate.sh new
+63
@@ -0,0 +1,63 @@
1 +#!/bin/sh
2 +# Copyright (c) 2006 Eric Wong
3 +test_description='git-svn metadata migrations from previous versions'
4 +. ./lib-git-svn.sh
5 +
6 +test_expect_success 'setup old-looking metadata' "
7 + cp $GIT_DIR/config $GIT_DIR/config-old-git-svn &&
8 + git-svn init $svnrepo &&
9 + git-svn fetch &&
10 + for i in trunk branches/a branches/b tags/0.1 tags/0.2 tags/0.3; do
11 + mkdir -p \$i && echo hello >> \$i/README || exit 1; done &&
12 + git ls-files -o trunk branches tags | git update-index --add --stdin &&
13 + git commit -m 'test' &&
14 + git-svn dcommit &&
15 + mv $GIT_DIR/svn/* $GIT_DIR/ &&
16 + rmdir $GIT_DIR/svn &&
17 + git-update-ref refs/heads/git-svn-HEAD refs/remotes/git-svn &&
18 + git-update-ref refs/heads/svn-HEAD refs/remotes/git-svn &&
19 + git-update-ref -d refs/remotes/git-svn refs/remotes/git-svn
20 + "
21 +
22 +head=`git rev-parse --verify refs/heads/git-svn-HEAD^0`
23 +test_expect_success 'git-svn-HEAD is a real HEAD' "test -n '$head'"
24 +
25 +test_expect_success 'initialize old-style (v0) git-svn layout' "
26 + mkdir -p $GIT_DIR/git-svn/info $GIT_DIR/svn/info &&
27 + echo $svnrepo > $GIT_DIR/git-svn/info/url &&
28 + echo $svnrepo > $GIT_DIR/svn/info/url &&
29 + git-svn migrate &&
30 + ! test -d $GIT_DIR/git-svn &&
31 + git-rev-parse --verify refs/remotes/git-svn^0 &&
32 + git-rev-parse --verify refs/remotes/svn^0 &&
33 + test \`git repo-config --get svn-remote.git-svn.url\` = '$svnrepo' &&
34 + test \`git repo-config --get svn-remote.git-svn.fetch\` = \
35 + ':refs/remotes/git-svn'
36 + "
37 +
38 +test_expect_success 'initialize a multi-repository repo' "
39 + git-svn multi-init $svnrepo -T trunk -t tags -b branches &&
40 + git-repo-config --get-all svn-remote.git-svn.fetch > fetch.out &&
41 + grep '^trunk:refs/remotes/trunk$' fetch.out &&
42 + grep '^branches/a:refs/remotes/a$' fetch.out &&
43 + grep '^branches/b:refs/remotes/b$' fetch.out &&
44 + grep '^tags/0\.1:refs/remotes/tags/0\.1$' fetch.out &&
45 + grep '^tags/0\.2:refs/remotes/tags/0\.2$' fetch.out &&
46 + grep '^tags/0\.3:refs/remotes/tags/0\.3$' fetch.out
47 + "
48 +
49 +test_expect_success 'multi-fetch works on partial urls + paths' "
50 + git-svn multi-fetch &&
51 + for i in trunk a b tags/0.1 tags/0.2 tags/0.3; do
52 + git rev-parse --verify refs/remotes/\$i^0 >> refs.out || exit 1;
53 + done &&
54 + test -z \"\`sort < refs.out | uniq -d\`\" &&
55 + for i in trunk a b tags/0.1 tags/0.2 tags/0.3; do
56 + for j in trunk a b tags/0.1 tags/0.2 tags/0.3; do
57 + if test \$j != \$i; then continue; fi
58 + test -z \"\`git diff refs/remotes/\$i \
59 + refs/remotes/\$j\`\" ||exit 1; done; done
60 + "
61 +
62 +test_done
63 +