@matheusbsbs / matheus-bagliano / commits / 8a49ee9759
Fork 0

git-svn: add support for SVN::Mirror/svk using revprops for metadata

Pass --use-svm-props or set the svn.usesvmprops key with git-config to enable using properties set by SVN::Mirror when it mirrored the upstream URL. This is heavily based on work from Sam Vilain: > From: Sam Vilain <sam@vilain.net> > Date: Sun, 11 Feb 2007 12:34:45 +1300 > Subject: [PATCH] git-svn: re-map repository URLs and UUIDs on SVK mirror paths > > If an SVN revision has a property, "svm:headrev", it is likely that > the revision was created by SVN::Mirror (a part of SVK). The property > contains a repository UUID and a revision. We want to make it look > like we are mirroring the original URL, so introduce a helper function > that returns the original identity URL and UUID, and use it when > generating commit messages. Signed-off-by: Eric Wong <normalperson@yhbt.net>

Eric Wong committed Feb 10, 2007 at 20:46 UTC 8a49ee9759f72ba7c61e035a2ca4b10d8a51c94e
1 file changed +105 -10
git-svn.perl
+105 -10
@@ -64,6 +64,7 @@ my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent,
64 'authors-file|A=s' => \$_authors,
65 'repack:i' => \$Git::SVN::_repack,
66 'no-metadata' => \$Git::SVN::_no_metadata,
67 + 'use-svm-props|svm-props' => \$Git::SVN::_use_svm_props,
68 'quiet|q' => \$_q,
69 'repack-flags|repack-args|repack-opts=s' =>
70 \$Git::SVN::_repack_flags,
@@ -645,7 +646,7 @@ package Git::SVN;
646 use strict;
647 use warnings;
648 use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
648 - $_repack $_repack_flags/;
649 + $_repack $_repack_flags $_use_svm_props/;
650 use Carp qw/croak/;
651 use File::Path qw/mkpath/;
652 use File::Copy qw/copy/;
@@ -920,9 +921,54 @@ sub new {
921
922 sub refname { "refs/remotes/$_[0]->{ref_id}" }
923
924 +sub set_svm_vars {
925 + my ($self, $ra) = @_;
926 + my $section = "svn-remote.$self->{repo_id}";
927 +
928 + # see if we have it in our config, first:
929 + eval {
930 + $self->{svm} = {
931 + source => $self->tmp_config('--get', "$section.svm-source"),
932 + uuid => $self->tmp_config('--get', "$section.svm-uuid"),
933 + }
934 + };
935 + return $ra if ($self->{svm}->{source} && $self->{svm}->{uuid});
936 +
937 + # nope, make sure we're connected to the repository root:
938 + if ($ra->{repos_root} ne $self->{url}) {
939 + $ra = Git::SVN::Ra->new($ra->{repos_root});
940 + }
941 + my $r = $ra->get_latest_revnum;
942 + my ($props) = ($ra->get_dir('', $r))[2];
943 + if (my $src = $props->{'svm:source'}) {
944 + # don't know what a '!' is there for, also the
945 + # username is of no interest
946 + $src =~ s{!$}{};
947 + $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
948 + $self->tmp_config('--add', "$section.svm-source", $src);
949 +
950 + my $uuid = $props->{'svm:uuid'};
951 + $uuid =~ m{^[0-9a-f\-]{30,}$}
952 + or die "doesn't look right - svm:uuid is '$uuid'\n";
953 + $self->tmp_config('--add', "$section.svm-uuid", $uuid);
954 +
955 + $self->{svm} = { source => $src , uuid => $uuid };
956 + }
957 + if ($ra->{repos_root} ne $self->{url}) {
958 + $ra = Git::SVN::Ra->new($self->{url});
959 + }
960 + $ra;
961 +}
962 +
963 sub ra {
964 my ($self) = shift;
925 - Git::SVN::Ra->new($self->{url});
965 + my $ra = Git::SVN::Ra->new($self->{url});
966 + $self->{-use_svm_props} = $Git::SVN::_use_svm_props;
967 + if ($self->{-use_svm_props} && !$self->{svm}) {
968 + $ra = $self->set_svm_vars($ra);
969 + $self->{-want_revprops} = 1;
970 + }
971 + $ra;
972 }
973
974 sub rel_path {
@@ -1006,16 +1052,44 @@ sub get_fetch_range {
1052 (++$min, $max);
1053 }
1054
1055 +sub tmp_config {
1056 + my ($self, @args) = @_;
1057 + unless (-f $self->{config}) {
1058 + open my $fh, '>', $self->{config} or
1059 + die "Can't open $self->{config}: $!\n";
1060 + print $fh "; This file is used internally by git-svn\n" or
1061 + die "Couldn't write to $self->{config}: $!\n";
1062 + print $fh "; You should not have to edit it\n" or
1063 + die "Couldn't write to $self->{config}: $!\n";
1064 + close $fh or die "Couldn't close $self->{config}: $!\n";
1065 + }
1066 + my $old_config = $ENV{GIT_CONFIG};
1067 + $ENV{GIT_CONFIG} = $self->{config};
1068 + $@ = undef;
1069 + my @ret = eval { command('config', @args) };
1070 + my $err = $@;
1071 + if (defined $old_config) {
1072 + $ENV{GIT_CONFIG} = $old_config;
1073 + } else {
1074 + delete $ENV{GIT_CONFIG};
1075 + }
1076 + die $err if $err;
1077 + wantarray ? @ret : $ret[0];
1078 +}
1079 +
1080 sub tmp_index_do {
1081 my ($self, $sub) = @_;
1082 my $old_index = $ENV{GIT_INDEX_FILE};
1083 $ENV{GIT_INDEX_FILE} = $self->{index};
1013 - my @ret = &$sub;
1014 - if ($old_index) {
1084 + $@ = undef;
1085 + my @ret = eval { &$sub };
1086 + my $err = $@;
1087 + if (defined $old_index) {
1088 $ENV{GIT_INDEX_FILE} = $old_index;
1089 } else {
1090 delete $ENV{GIT_INDEX_FILE};
1091 }
1092 + die $err if $err;
1093 wantarray ? @ret : $ret[0];
1094 }
1095
@@ -1105,9 +1179,8 @@ sub do_git_commit {
1179 or croak $!;
1180 print $msg_fh $log_entry->{log} or croak $!;
1181 unless ($_no_metadata) {
1108 - print $msg_fh "\ngit-svn-id: ", $self->full_url, '@',
1109 - $log_entry->{revision}, ' ',
1110 - $self->ra->uuid, "\n" or croak $!;
1182 + print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
1183 + or croak $!;
1184 }
1185 $msg_fh->flush == 0 or croak $!;
1186 close $msg_fh or croak $!;
@@ -1123,7 +1196,11 @@ sub do_git_commit {
1196
1197 $self->{last_rev} = $log_entry->{revision};
1198 $self->{last_commit} = $commit;
1126 - print "r$log_entry->{revision} = $commit ($self->{ref_id})\n";
1199 + print "r$log_entry->{revision}";
1200 + if (defined $log_entry->{svm_revision}) {
1201 + print " (\@$log_entry->{svm_revision})";
1202 + }
1203 + print " = $commit ($self->{ref_id})\n";
1204 if (defined $_repack && (--$_repack_nr == 0)) {
1205 $_repack_nr = $_repack;
1206 # repack doesn't use any arguments with spaces in them, does it?
@@ -1351,13 +1428,16 @@ sub make_log_entry {
1428 my %log_entry = ( parents => $parents || [], revision => $rev,
1429 log => '');
1430
1431 + my $headrev;
1432 my $logged = delete $self->{logged_rev_props};
1355 - if (!$logged || $self->{-want_extra_revprops}) {
1433 + if (!$logged || $self->{-want_revprops}) {
1434 my $rp = $self->ra->rev_proplist($rev);
1435 foreach (sort keys %$rp) {
1436 my $v = $rp->{$_};
1437 if (/^svn:(author|date|log)$/) {
1438 $log_entry{$1} = $v;
1439 + } elsif ($_ eq 'svm:headrev') {
1440 + $headrev = $v;
1441 } else {
1442 print $un " rev_prop: ", uri_encode($_), ' ',
1443 uri_encode($v), "\n";
@@ -1371,6 +1451,21 @@ sub make_log_entry {
1451 $log_entry{date} = parse_svn_date($log_entry{date});
1452 $log_entry{author} = check_author($log_entry{author});
1453 $log_entry{log} .= "\n";
1454 + if (defined $headrev && $self->{-use_svm_props}) {
1455 + my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$};
1456 + if ($uuid ne $self->{svm}->{uuid}) {
1457 + die "UUID mismatch on SVM path:\n",
1458 + "expected: $self->{svm}->{uuid}\n",
1459 + " got: $uuid\n";
1460 + }
1461 + my $full_url = $self->{svm}->{source};
1462 + $full_url .= "/$self->{path}" if length $self->{path};
1463 + $log_entry{metadata} = "$full_url\@$r $uuid";
1464 + $log_entry{svm_revision} = $r;
1465 + } else {
1466 + $log_entry{metadata} = $self->full_url . "\@$rev " .
1467 + $self->ra->uuid;
1468 + }
1469 \%log_entry;
1470 }
1471
@@ -1549,7 +1644,7 @@ sub _new {
1644 close $fh or croak $!;
1645 }
1646 bless { ref_id => $ref_id, dir => $dir, index => "$dir/index",
1552 - path => $path,
1647 + path => $path, config => "$ENV{GIT_DIR}/svn/config",
1648 db_path => "$dir/.rev_db", repo_id => $repo_id }, $class;
1649 }
1650