git-svn and branches
I'm using git-svn to track a rather large subversion repository with a non-standard layout. In the past, I've only cared about trunk, but now I need to occasionally use branches, too. By adding a second git-svn remote with the branch URL, I can fetch the branch, and git-svn is even intelligent enough to notice that the branch was copied off of trunk.
However, git-svn also does a complete checkout for the first revision of
the branch. By this, I mean it goes through shows "A file" for every
file in the repository. This takes quite a while, and seems rather unnecessary given that git-svn already noticed that the branch shares a history with trunk, which is already fetched.
Knowing just enough of what git-svn is doing to be dangerous, I whipped up a short little patch. This patch seems to work for the common case, and avoids fetching every file from subversion. It does break sometimes, however, and I don't understand why.
Maybe someone with a better grasps of the code can see what I did wrong, or suggest a better means to my end?
git-svn.perl +2 -1
@@ -1848,9 +1848,10 @@ sub find_parent_branch { $self->full_url, $ed) or die "SVN connection failed somewhere...\n"; } else { + $self->assert_index_clean($parent); print STDERR "Following parent with do_update\n"; $ed = SVN::Git::Fetcher->new($self); - $self->ra->gs_do_update($rev, $rev, $self, $ed) + $self->ra->gs_do_update($rev, $r0, $self, $ed) or die "SVN connection failed somewhere...\n"; } print STDERR "Successfully followed parent\n";
Signature · 8 lines
-Steven Walter <[email protected]> "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects." -Robert Heinlein
Quoted 7 lines · Steven Walter
Knowing just enough of what git-svn is doing to be dangerous, I whipped up a short little patch. This patch seems to work for the common case, and avoids fetching every file from subversion. It does break sometimes, however, and I don't understand why.
Maybe someone with a better grasps of the code can see what I did wrong, or suggest a better means to my end?
Try also with the SVN trunk - the do_switch API has recently been added and it also avoids this excess checkout. I'm not sure why a solution like you post isn't used, perhaps Eric can comment further.
Sam.
Quoted 16 lines
git-svn.perl +2
-1@@ -1848,9 +1848,10 @@ sub find_parent_branch { $self->full_url, $ed) or die "SVN connection failed somewhere...\n"; } else { + $self->assert_index_clean($parent); print STDERR "Following parent with do_update\n"; $ed = SVN::Git::Fetcher->new($self); - $self->ra->gs_do_update($rev, $rev, $self, $ed) + $self->ra->gs_do_update($rev, $r0, $self, $ed) or die "SVN connection failed somewhere...\n"; } print STDERR "Successfully followed parent\n";
Quoted 12 lines · Steven Walter
I'm using git-svn to track a rather large subversion repository with a non-standard layout. In the past, I've only cared about trunk, but now I need to occasionally use branches, too. By adding a second git-svn remote with the branch URL, I can fetch the branch, and git-svn is even intelligent enough to notice that the branch was copied off of trunk.
However, git-svn also does a complete checkout for the first revision of
the branch. By this, I mean it goes through shows "A file" for every
file in the repository. This takes quite a while, and seems rather unnecessary given that git-svn already noticed that the branch shares a history with trunk, which is already fetched.
Quoted 7 lines
Knowing just enough of what git-svn is doing to be dangerous, I whipped up a short little patch. This patch seems to work for the common case, and avoids fetching every file from subversion. It does break sometimes, however, and I don't understand why.
Maybe someone with a better grasps of the code can see what I did wrong, or suggest a better means to my end?
I believe your case handles where a branch is created directly from a trunk copy with no file modifications in the branch, but not when a branch is created and files are modified in the trunk (or branch) within the same revision. Is this what's happening?
Additionally, I think this breaks when an entire trunk or branch is moved around because the original directory has moved or gone away:
/trunk => /project-a/trunk
Anyways, as Sam said, newer SVN (1.4.4+) has a working do_switch() function and that code path will never be hit at all.
Quoted 16 lines
git-svn.perl +2
-1@@ -1848,9 +1848,10 @@ sub find_parent_branch { $self->full_url, $ed) or die "SVN connection failed somewhere...\n"; } else { + $self->assert_index_clean($parent); print STDERR "Following parent with do_update\n"; $ed = SVN::Git::Fetcher->new($self); - $self->ra->gs_do_update($rev, $rev, $self, $ed) + $self->ra->gs_do_update($rev, $r0, $self, $ed) or die "SVN connection failed somewhere...\n"; } print STDERR "Successfully followed parent\n";
Signature · 1 line
Eric Wong
Quoted 5 lines · Eric Wong
I believe your case handles where a branch is created directly from a trunk copy with no file modifications in the branch, but not when a branch is created and files are modified in the trunk (or branch) within the same revision. Is this what's happening?
Quoted 7 lines
Additionally, I think this breaks when an entire trunk or branch is moved around because the original directory has moved or gone away:
/trunk => /project-a/trunk
Anyways, as Sam said, newer SVN (1.4.4+) has a working
do_switch()function and that code path will never be hit at all.
I think you're right that my code would only handle a verbatim copy. I made a few changes and I think I have something that is more generally useful. Using a newer version of subversion isn't practical for me, as this is a work situation where I don't have control of the workstations.
One criticism of the patch: the trees_match function probably needs to be re-written. My SVN::Perl-foo is weak.
Patch to follow
Signature · 8 lines
-Steven Walter <[email protected]> "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects." -Robert Heinlein
PATCH 1/1 Don't checkout the full tree if avoidable
In most cases of branching, the tree is copied unmodified from the trunk to the branch. When that is done, we can simply start with the parent's index and apply the changes on the branch as usual.
--- git-svn.perl | 18 ++++++++++++++++++ 1 files changed, 18 insertions(+), 0 deletions(-)
git-svn.perl +18 -0
@@ -1847,6 +1847,13 @@ sub find_parent_branch { $gs->ra->gs_do_switch($r0, $rev, $gs, $self->full_url, $ed) or die "SVN connection failed somewhere...\n"; + } elsif ($self->trees_match($new_url, $r0, + $self->full_url, $rev)) { + $self->tmp_index_do(sub { + command_noisy('read-tree', $parent); + }); + $self->{last_commit} = $parent; + # Assume copy with no changes } else { print STDERR "Following parent with do_update\n"; $ed = SVN::Git::Fetcher->new($self);
@@ -1859,6 +1866,17 @@ sub find_parent_branch { return undef; } +sub trees_match { + my ($self, $url1, $rev1, $url2, $rev2) = @_; + + my $ret=1; + open(my $fh, "svn diff $url1\@$rev1 $url2\@$rev2 |"); + $ret=0 if (<$fh>); + close($fh); + + return $ret; +} + sub do_fetch { my ($self, $paths, $rev) = @_; my $ed;
Signature · 1 line
1.5.3.1
Re: [PATCH] Don't checkout the full tree if avoidable
Quoted 5 lines · Steven Walter
In most cases of branching, the tree is copied unmodified from the trunk to the branch. When that is done, we can simply start with the parent's index and apply the changes on the branch as usual.
Signed-off-by Steven Walter <[email protected]>
Eric, do you like this one?
Quoted 42 lines
--- git-svn.perl | 18 ++++++++++++++++++ 1 files changed, 18 insertions(+), 0 deletions(-)git-svn.perl +18
-0@@ -1847,6 +1847,13 @@ sub find_parent_branch { $gs->ra->gs_do_switch($r0, $rev, $gs, $self->full_url, $ed) or die "SVN connection failed somewhere...\n"; + } elsif ($self->trees_match($new_url, $r0, + $self->full_url, $rev)) { + $self->tmp_index_do(sub { + command_noisy('read-tree', $parent); + }); + $self->{last_commit} = $parent; + # Assume copy with no changes } else { print STDERR "Following parent with do_update\n"; $ed = SVN::Git::Fetcher->new($self);@@ -1859,6 +1866,17 @@ sub find_parent_branch { return undef; } +sub trees_match { + my ($self, $url1, $rev1, $url2, $rev2) = @_; + + my $ret=1; + open(my $fh, "svn diff $url1\@$rev1 $url2\@$rev2 |"); + $ret=0 if (<$fh>); + close($fh); + + return $ret; +} + sub do_fetch { my ($self, $paths, $rev) = @_; my $ed;Signature · 1 line
1.5.3.1
Re: [PATCH] Don't checkout the full tree if avoidable
Quoted 9 lines · Junio C Hamano
Quoted 5 lines · Steven Walter
In most cases of branching, the tree is copied unmodified from the trunk to the branch. When that is done, we can simply start with the parent's index and apply the changes on the branch as usual.
Signed-off-by Steven Walter <[email protected]>Eric, do you like this one?
Junio, thanks for pinging me about it, I haven't been following the ML very closely and forgot about this issue.
Quoted 2 lines · Steven Walter
One criticism of the patch: the
trees_matchfunction probably needs to be re-written. My SVN::Perl-foo is weak.
Yep :)
Steven:
How does the following work for you? Which version of SVN do you have, by the way? I just found a bug with the way SVN::Client::diff() is exported for SVN 1.1.4, hence the SVN::Pool->new_default_sub usage.
From: Steven Walter <[email protected]>
Date: Fri, 28 Sep 2007 13:24:19 -0400
Subject: [PATCH] Don't checkout the full tree if avoidable
In most cases of branching, the tree is copied unmodified from the trunk to the branch. When that is done, we can simply start with the parent's index and apply the changes on the branch as usual.
[ew: rewritten from Steven's original to use SVN::Client instead
of the command-line svn client.
Since SVN::Client connects separately, we'll share our authentication providers array between our usages of SVN::Client and SVN::Ra, too. Bypassing the high-level SVN::Client library can avoid this, but the code will be much more complex. Regardless, any implementation of this seems to require restarting a connection to the remote server.
Also of note is that SVN 1.4 and later allows a more efficient diff_summary to be done instead of a full diff, but since this code is only to support SVN < 1.4.4, we'll ignore it for now.]
--- git-svn.perl | 64 +++++++++++++++++++++++++++++++++++++++++++-------------- 1 files changed, 48 insertions(+), 16 deletions(-)
git-svn.perl +48 -16
@@ -1847,6 +1847,16 @@ sub find_parent_branch { $gs->ra->gs_do_switch($r0, $rev, $gs, $self->full_url, $ed) or die "SVN connection failed somewhere...\n"; + } elsif ($self->ra->trees_match($new_url, $r0, + $self->full_url, $rev)) { + print STDERR "Trees match:\n", + " $new_url\@$r0\n", + " ${\$self->full_url}\@$rev\n", + "Following parent with no changes\n"; + $self->tmp_index_do(sub { + command_noisy('read-tree', $parent); + }); + $self->{last_commit} = $parent; } else { print STDERR "Following parent with do_update\n"; $ed = SVN::Git::Fetcher->new($self);
@@ -3027,28 +3037,32 @@ BEGIN { } } +sub _auth_providers () { + [ + SVN::Client::get_simple_provider(), + SVN::Client::get_ssl_server_trust_file_provider(), + SVN::Client::get_simple_prompt_provider( + \&Git::SVN::Prompt::simple, 2), + SVN::Client::get_ssl_client_cert_file_provider(), + SVN::Client::get_ssl_client_cert_prompt_provider( + \&Git::SVN::Prompt::ssl_client_cert, 2), + SVN::Client::get_ssl_client_cert_pw_prompt_provider( + \&Git::SVN::Prompt::ssl_client_cert_pw, 2), + SVN::Client::get_username_provider(), + SVN::Client::get_ssl_server_trust_prompt_provider( + \&Git::SVN::Prompt::ssl_server_trust), + SVN::Client::get_username_prompt_provider( + \&Git::SVN::Prompt::username, 2) + ] +} + sub new { my ($class, $url) = @_; $url =~ s!/+$!!; return $RA if ($RA && $RA->{url} eq $url); SVN::_Core::svn_config_ensure($config_dir, undef); - my ($baton, $callbacks) = SVN::Core::auth_open_helper([ - SVN::Client::get_simple_provider(), - SVN::Client::get_ssl_server_trust_file_provider(), - SVN::Client::get_simple_prompt_provider( - \&Git::SVN::Prompt::simple, 2), - SVN::Client::get_ssl_client_cert_file_provider(), - SVN::Client::get_ssl_client_cert_prompt_provider( - \&Git::SVN::Prompt::ssl_client_cert, 2), - SVN::Client::get_ssl_client_cert_pw_prompt_provider( - \&Git::SVN::Prompt::ssl_client_cert_pw, 2), - SVN::Client::get_username_provider(), - SVN::Client::get_ssl_server_trust_prompt_provider( - \&Git::SVN::Prompt::ssl_server_trust), - SVN::Client::get_username_prompt_provider( - \&Git::SVN::Prompt::username, 2), - ]); + my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers); my $config = SVN::Core::config_get_config($config_dir); $RA = undef; my $self = SVN::Ra->new(url => $url, auth => $baton,
@@ -3112,6 +3126,24 @@ sub get_log { $ret; } +sub trees_match { + my ($self, $url1, $rev1, $url2, $rev2) = @_; + my $ctx = SVN::Client->new(auth => _auth_providers); + my $out = IO::File->new_tmpfile; + + # older SVN (1.1.x) doesn't take $pool as the last parameter for + # $ctx->diff(), so we'll create a default one + my $pool = SVN::Pool->new_default_sub; + + $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1 + $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out); + $out->flush; + my $ret = (($out->stat)[7] == 0); + close $out or croak $!; + + $ret; +} + sub get_commit_editor { my ($self, $log, $cb, $pool) = @_; my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
Signature · 1 line
Eric Wong
Re: [PATCH] Don't checkout the full tree if avoidable
Quoted 11 lines · Eric Wong
Quoted 2 lines · Steven Walter
One criticism of the patch: the
trees_matchfunction probably needs to be re-written. My SVN::Perl-foo is weak.Yep :)
Steven:
How does the following work for you? Which version of SVN do you have, by the way? I just found a bug with the way
SVN::Client::diff()is exported for SVN 1.1.4, hence theSVN::Pool->new_default_subusage.
swalter@sentra:~% svn --version
svn, version 1.3.2 (r19776)
This version works great; seems to have exactly the same behavior as my patch. Verified that it still falls back to the do_update code when trees_match fails.
Signature · 8 lines
-Steven Walter <[email protected]> "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects." -Robert Heinlein
Re: [PATCH] Don't checkout the full tree if avoidable
Quoted 13 lines · Steven Walter
Quoted 11 lines · Eric Wong
Quoted 2 lines · Steven Walter
One criticism of the patch: the
trees_matchfunction probably needs to be re-written. My SVN::Perl-foo is weak.Yep :)
Steven:
How does the following work for you? Which version of SVN do you have, by the way? I just found a bug with the way
SVN::Client::diff()is exported for SVN 1.1.4, hence theSVN::Pool->new_default_subusage.
Quoted 6 lines
swalter@sentra:~% svn --version
svn, version 1.3.2 (r19776)This version works great; seems to have exactly the same behavior as my patch. Verified that it still falls back to the
do_updatecode whentrees_matchfails.
Thanks Steven.
Junio: can you please apply my version of Steven's patch? Thanks.
Signature · 1 line
Eric Wong