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

git-svn: add --follow-parent and --no-metadata options to fetch

--follow-parent: This is especially helpful when we're tracking a directory that has been moved around within the repository, or if we started tracking a branch and never tracked the trunk it was descended from. This relies on the SVN::* libraries to work. We can't reliably parse path info from the svn command-line client without relying on XML, so it's better just to have the SVN::* libs installed. This also removes oldvalue verification when calling update-ref In SVN, branches can be deleted, and then recreated under the same path as the original one with different ancestry information, causing parent information to be mismatched / misordered. Also force the current ref, if existing, to be a parent, regardless of whether or not it was specified. --no-metadata: This gets rid of the git-svn-id: lines at the end of every commit. With this, you lose the ability to use the rebuild command. If you ever lose your .git/svn/git-svn/.rev_db file, you won't be able to fetch again, either. This is fine for one-shot imports. Also fix some issues with multi-fetch --follow-parent that were exposed while testing this. Additionally, repack checking is simplified greatly. git-svn log will not work on repositories using this, either. Signed-off-by: Eric Wong <normalperson@yhbt.net> Signed-off-by: Junio C Hamano <junkio@cox.net>

Eric Wong committed Jun 27, 2006 at 19:39 UTC a00439acd209142167bf891dcf646a6501a814e5
2 files changed +167 -46
contrib/git-svn/git-svn.perl
+123 -46
@@ -19,6 +19,7 @@ my $TZ = $ENV{TZ};
19 # make sure the svn binary gives consistent output between locales and TZs:
20 $ENV{TZ} = 'UTC';
21 $ENV{LC_ALL} = 'C';
22 +$| = 1; # unbuffer STDOUT
23
24 # If SVN:: library support is added, please make the dependencies
25 # optional and preserve the capability to use the command-line client.
@@ -46,7 +47,7 @@ my $sha1_short = qr/[a-f\d]{4,40}/;
47 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
48 $_find_copies_harder, $_l, $_cp_similarity, $_cp_remote,
49 $_repack, $_repack_nr, $_repack_flags,
49 - $_message, $_file,
50 + $_message, $_file, $_follow_parent, $_no_metadata,
51 $_template, $_shared, $_no_default_regex, $_no_graft_copy,
52 $_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
53 $_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m);
@@ -56,9 +57,11 @@ my @repo_path_split_cache;
57
58 my %fc_opts = ( 'no-ignore-externals' => \$_no_ignore_ext,
59 'branch|b=s' => \@_branch_from,
60 + 'follow-parent|follow' => \$_follow_parent,
61 'branch-all-refs|B' => \$_branch_all_refs,
62 'authors-file|A=s' => \$_authors,
63 'repack:i' => \$_repack,
64 + 'no-metadata' => \$_no_metadata,
65 'repack-flags|repack-args|repack-opts=s' => \$_repack_flags);
66
67 my ($_trunk, $_tags, $_branches);
@@ -824,35 +827,19 @@ sub fetch_child_id {
827 my $id = shift;
828 print "Fetching $id\n";
829 my $ref = "$GIT_DIR/refs/remotes/$id";
827 - my $ca = file_to_s($ref) if (-r $ref);
828 - defined(my $pid = fork) or croak $!;
830 + defined(my $pid = open my $fh, '-|') or croak $!;
831 if (!$pid) {
832 + $_repack = undef;
833 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
834 init_vars();
835 fetch(@_);
836 exit 0;
837 }
835 - waitpid $pid, 0;
836 - croak $? if $?;
837 - return unless $_repack || -r $ref;
838 -
839 - my $cb = file_to_s($ref);
840 -
841 - defined($pid = open my $fh, '-|') or croak $!;
842 - my $url = file_to_s("$GIT_DIR/svn/$id/info/url");
843 - $url = qr/\Q$url\E/;
844 - if (!$pid) {
845 - exec qw/git-rev-list --pretty=raw/,
846 - $ca ? "$ca..$cb" : $cb or croak $!;
847 - }
838 while (<$fh>) {
849 - if (/^ git-svn-id: $url\@\d+ [a-f0-9\-]+$/) {
850 - check_repack();
851 - } elsif (/^ git-svn-id: \S+\@\d+ [a-f0-9\-]+$/) {
852 - last;
853 - }
839 + print $_;
840 + check_repack() if (/^r\d+ = $sha1/);
841 }
855 - close $fh;
842 + close $fh or croak $?;
843 }
844
845 sub rec_fetch {
@@ -1919,6 +1906,13 @@ sub git_commit {
1906 croak $? if $?;
1907 restore_index($index);
1908 }
1909 +
1910 + # just in case we clobber the existing ref, we still want that ref
1911 + # as our parent:
1912 + if (my $cur = eval { file_to_s("$GIT_DIR/refs/remotes/$GIT_SVN") }) {
1913 + push @tmp_parents, $cur;
1914 + }
1915 +
1916 if (exists $tree_map{$tree}) {
1917 foreach my $p (@{$tree_map{$tree}}) {
1918 my $skip;
@@ -1949,31 +1943,26 @@ sub git_commit {
1943 last if @exec_parents > 16;
1944 }
1945
1952 - defined(my $pid = open my $out_fh, '-|') or croak $!;
1953 - if ($pid == 0) {
1954 - my $msg_fh = IO::File->new_tmpfile or croak $!;
1955 - print $msg_fh $log_msg->{msg}, "\ngit-svn-id: ",
1956 - "$SVN_URL\@$log_msg->{revision}",
1946 + set_commit_env($log_msg);
1947 + my @exec = ('git-commit-tree', $tree);
1948 + push @exec, '-p', $_ foreach @exec_parents;
1949 + defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1950 + or croak $!;
1951 + print $msg_fh $log_msg->{msg} or croak $!;
1952 + unless ($_no_metadata) {
1953 + print $msg_fh "\ngit-svn-id: $SVN_URL\@$log_msg->{revision}",
1954 " $SVN_UUID\n" or croak $!;
1958 - $msg_fh->flush == 0 or croak $!;
1959 - seek $msg_fh, 0, 0 or croak $!;
1960 - set_commit_env($log_msg);
1961 - my @exec = ('git-commit-tree',$tree);
1962 - push @exec, '-p', $_ foreach @exec_parents;
1963 - open STDIN, '<&', $msg_fh or croak $!;
1964 - exec @exec or croak $!;
1955 }
1956 + $msg_fh->flush == 0 or croak $!;
1957 + close $msg_fh or croak $!;
1958 chomp(my $commit = do { local $/; <$out_fh> });
1967 - close $out_fh or croak $?;
1959 + close $out_fh or croak $!;
1960 + waitpid $pid, 0;
1961 + croak $? if $?;
1962 if ($commit !~ /^$sha1$/o) {
1969 - croak "Failed to commit, invalid sha1: $commit\n";
1963 + die "Failed to commit, invalid sha1: $commit\n";
1964 }
1971 - my @update_ref = ('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
1972 - if (my $primary_parent = shift @exec_parents) {
1973 - quiet_run(qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0");
1974 - push @update_ref, $primary_parent unless $?;
1975 - }
1976 - sys(@update_ref);
1965 + sys('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
1966 revdb_set($REVDB, $log_msg->{revision}, $commit);
1967
1968 # this output is read via pipe, do not change:
@@ -2058,6 +2047,11 @@ sub safe_qx {
2047 }
2048
2049 sub svn_compat_check {
2050 + if ($_follow_parent) {
2051 + print STDERR 'E: --follow-parent functionality is only ',
2052 + "available when SVN libraries are used\n";
2053 + exit 1;
2054 + }
2055 my @co_help = safe_qx(qw(svn co -h));
2056 unless (grep /ignore-externals/,@co_help) {
2057 print STDERR "W: Installed svn version does not support ",
@@ -2386,6 +2380,28 @@ sub write_grafts {
2380 close $fh or croak $!;
2381 }
2382
2383 +sub read_url_paths_all {
2384 + my ($l_map, $pfx, $p) = @_;
2385 + my @dir;
2386 + foreach (<$p/*>) {
2387 + if (-r "$_/info/url") {
2388 + $pfx .= '/' if $pfx && $pfx !~ m!/$!;
2389 + my $id = $pfx . basename $_;
2390 + my $url = file_to_s("$_/info/url");
2391 + my ($u, $p) = repo_path_split($url);
2392 + $l_map->{$u}->{$p} = $id;
2393 + } elsif (-d $_) {
2394 + push @dir, $_;
2395 + }
2396 + }
2397 + foreach (@dir) {
2398 + my $x = $_;
2399 + $x =~ s!^\Q$GIT_DIR\E/svn/!!o;
2400 + read_url_paths_all($l_map, $x, $_);
2401 + }
2402 +}
2403 +
2404 +# this one only gets ids that have been imported, not new ones
2405 sub read_url_paths {
2406 my $l_map = {};
2407 git_svn_each(sub { my $x = shift;
@@ -2599,7 +2615,6 @@ sub libsvn_get_file {
2615 # redirect STDOUT for SVN 1.1.x compatibility
2616 open my $stdout, '>&', \*STDOUT or croak $!;
2617 open STDOUT, '>&', $in or croak $!;
2602 - $| = 1; # not sure if this is necessary, better safe than sorry...
2618 my ($r, $props) = $SVN->get_file($f, $rev, \*STDOUT, $pool);
2619 $in->flush == 0 or croak $!;
2620 open STDOUT, '>&', $stdout or croak $!;
@@ -2702,6 +2717,28 @@ sub svn_grab_base_rev {
2717 close $fh;
2718 if (defined $c && length $c) {
2719 my ($url, $rev, $uuid) = cmt_metadata($c);
2720 + return ($rev, $c) if defined $rev;
2721 + }
2722 + if ($_no_metadata) {
2723 + my $offset = -41; # from tail
2724 + my $rl;
2725 + open my $fh, '<', $REVDB or
2726 + die "--no-metadata specified and $REVDB not readable\n";
2727 + seek $fh, $offset, 2;
2728 + $rl = readline $fh;
2729 + defined $rl or return (undef, undef);
2730 + chomp $rl;
2731 + while ($c ne $rl && tell $fh != 0) {
2732 + $offset -= 41;
2733 + seek $fh, $offset, 2;
2734 + $rl = readline $fh;
2735 + defined $rl or return (undef, undef);
2736 + chomp $rl;
2737 + }
2738 + my $rev = tell $fh;
2739 + croak $! if ($rev < -1);
2740 + $rev = ($rev - 41) / 41;
2741 + close $fh or croak $!;
2742 return ($rev, $c);
2743 }
2744 return (undef, undef);
@@ -2799,15 +2836,45 @@ sub libsvn_find_parent_branch {
2836 print STDERR "Found possible branch point: ",
2837 "$branch_from => $svn_path, $r\n";
2838 $branch_from =~ s#^/##;
2802 - my $l_map = read_url_paths();
2839 + my $l_map = {};
2840 + read_url_paths_all($l_map, '', "$GIT_DIR/svn");
2841 my $url = $SVN->{url};
2842 defined $l_map->{$url} or return;
2805 - my $id = $l_map->{$url}->{$branch_from} or return;
2843 + my $id = $l_map->{$url}->{$branch_from};
2844 + if (!defined $id && $_follow_parent) {
2845 + print STDERR "Following parent: $branch_from\@$r\n";
2846 + # auto create a new branch and follow it
2847 + $id = basename($branch_from);
2848 + $id .= '@'.$r if -r "$GIT_DIR/svn/$id";
2849 + while (-r "$GIT_DIR/svn/$id") {
2850 + # just grow a tail if we're not unique enough :x
2851 + $id .= '-';
2852 + }
2853 + }
2854 + return unless defined $id;
2855 +
2856 my ($r0, $parent) = find_rev_before($r,$id,1);
2857 + if ($_follow_parent && (!defined $r0 || !defined $parent)) {
2858 + defined(my $pid = fork) or croak $!;
2859 + if (!$pid) {
2860 + $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
2861 + init_vars();
2862 + $SVN_URL = "$url/$branch_from";
2863 + $SVN_LOG = $SVN = undef;
2864 + setup_git_svn();
2865 + # we can't assume SVN_URL exists at r+1:
2866 + $_revision = "0:$r";
2867 + fetch_lib();
2868 + exit 0;
2869 + }
2870 + waitpid $pid, 0;
2871 + croak $? if $?;
2872 + ($r0, $parent) = find_rev_before($r,$id,1);
2873 + }
2874 return unless (defined $r0 && defined $parent);
2875 if (revisions_eq($branch_from, $r0, $r)) {
2876 unlink $GIT_SVN_INDEX;
2810 - print STDERR "Found branch parent: $parent\n";
2877 + print STDERR "Found branch parent: ($GIT_SVN) $parent\n";
2878 sys(qw/git-read-tree/, $parent);
2879 return libsvn_fetch($parent, $paths, $rev,
2880 $author, $date, $msg);
@@ -3274,6 +3341,16 @@ diff-index line ($m hash)
3341 }
3342 ;
3343
3344 +# retval of read_url_paths{,_all}();
3345 +$l_map = {
3346 + # repository root url
3347 + 'https://svn.musicpd.org' => {
3348 + # repository path # GIT_SVN_ID
3349 + 'mpd/trunk' => 'trunk',
3350 + 'mpd/tags/0.11.5' => 'tags/0.11.5',
3351 + },
3352 +}
3353 +
3354 Notes:
3355 I don't trust the each() function on unless I created %hash myself
3356 because the internal iterator may not have started at base.
contrib/git-svn/t/t0004-follow-parent.sh new
+44
@@ -0,0 +1,44 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2006 Eric Wong
4 +#
5 +
6 +test_description='git-svn --follow-parent fetching'
7 +. ./lib-git-svn.sh
8 +
9 +if test -n "$GIT_SVN_NO_LIB" && test "$GIT_SVN_NO_LIB" -ne 0
10 +then
11 + echo 'Skipping: --follow-parent needs SVN libraries'
12 + test_done
13 + exit 0
14 +fi
15 +
16 +test_expect_success 'initialize repo' "
17 + mkdir import &&
18 + cd import &&
19 + mkdir -p trunk &&
20 + echo hello > trunk/readme &&
21 + svn import -m 'initial' . $svnrepo &&
22 + cd .. &&
23 + svn co $svnrepo wc &&
24 + cd wc &&
25 + echo world >> trunk/readme &&
26 + svn commit -m 'another commit' &&
27 + svn up &&
28 + svn mv -m 'rename to thunk' trunk thunk &&
29 + svn up &&
30 + echo goodbye >> thunk/readme &&
31 + svn commit -m 'bye now' &&
32 + cd ..
33 + "
34 +
35 +test_expect_success 'init and fetch --follow-parent a moved directory' "
36 + git-svn init -i thunk $svnrepo/thunk &&
37 + git-svn fetch --follow-parent -i thunk &&
38 + git-rev-parse --verify refs/remotes/trunk &&
39 + test '$?' -eq '0'
40 + "
41 +
42 +test_debug 'gitk --all &'
43 +
44 +test_done