git-svn: avoid an extra svn_ra connection during commits
Before, we needed a separate svn_ra instance to run our check_path calls once the editor was active; but we can avoid that by running all the check_path calls before our editor is active. Signed-off-by: Eric Wong <normalperson@yhbt.net>
Eric Wong committed
Jan 27, 2007 at 01:32 UTC
6e8548cca888205a99773a18a73533b2b8dc651d
1 file changed
+123
-82
git-svn.perl
+123
-82
@@ -352,16 +352,22 @@ sub cmd_dcommit {
352
my $log = get_commit_entry($d)->{log};
353
my $ra = $gs->ra;
354
my $pool = SVN::Pool->new;
355
+ my $mods = generate_diff("$d~1", $d);
356
+ my $types = check_diff_paths($ra,
357
+ $gs->{path},
358
+ $last_rev,
359
+ $mods);
360
my %ed_opts = ( r => $last_rev,
356
- ra => $ra->dup,
361
+ mods => $mods,
362
+ url => $ra->{url},
363
+ types => $types,
364
svn_path => $gs->{path} );
365
my $ed = SVN::Git::Editor->new(\%ed_opts,
366
$ra->get_commit_editor($log,
367
sub { print "Committed r$_[0]\n";
368
$last_rev = $_[0]; }),
369
$pool);
363
- my $mods = $ed->apply_diff("$d~1", $d);
364
- if (@$mods == 0) {
370
+ if (!$ed->apply_diff($mods, $d)) {
371
print "No changes\n$d~1 == $d\n";
372
}
373
}
@@ -469,15 +475,15 @@ sub cmd_commit_diff {
475
die "revision argument: $r not understood by git-svn\n";
476
}
477
my $pool = SVN::Pool->new;
472
- my %ed_opts = ( r => $r,
473
- ra => $ra->dup,
474
- svn_path => $svn_path );
478
+ my $mods = generate_diff($ta, $tb);
479
+ my $types = check_diff_paths($ra, $svn_path, $r, $mods);
480
+ my %ed_opts = ( r => $r, url => $ra->{url}, svn_path => $svn_path,
481
+ mods => $mods, types => $types );
482
my $ed = SVN::Git::Editor->new(\%ed_opts,
483
$ra->get_commit_editor($_message,
484
sub { print "Committed r$_[0]\n" }),
485
$pool);
479
- my $mods = $ed->apply_diff($ta, $tb);
480
- if (@$mods == 0) {
486
+ if (!$ed->apply_diff($mods, $tb)) {
487
print "No changes\n$ta == $tb\n";
488
}
489
$pool->clear;
@@ -708,6 +714,92 @@ sub tz_to_s_offset {
714
return ($1 * 60) + ($tz * 3600);
715
}
716
717
+sub generate_diff {
718
+ my ($tree_a, $tree_b) = @_;
719
+ my @diff_tree = qw(diff-tree -z -r);
720
+ if ($_cp_similarity) {
721
+ push @diff_tree, "-C$_cp_similarity";
722
+ } else {
723
+ push @diff_tree, '-C';
724
+ }
725
+ push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
726
+ push @diff_tree, "-l$_l" if defined $_l;
727
+ push @diff_tree, $tree_a, $tree_b;
728
+ my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
729
+ local $/ = "\0";
730
+ my $state = 'meta';
731
+ my @mods;
732
+ while (<$diff_fh>) {
733
+ chomp $_; # this gets rid of the trailing "\0"
734
+ if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
735
+ $sha1\s($sha1)\s
736
+ ([MTCRAD])\d*$/xo) {
737
+ push @mods, { mode_a => $1, mode_b => $2,
738
+ sha1_b => $3, chg => $4 };
739
+ if ($4 =~ /^(?:C|R)$/) {
740
+ $state = 'file_a';
741
+ } else {
742
+ $state = 'file_b';
743
+ }
744
+ } elsif ($state eq 'file_a') {
745
+ my $x = $mods[$#mods] or croak "Empty array\n";
746
+ if ($x->{chg} !~ /^(?:C|R)$/) {
747
+ croak "Error parsing $_, $x->{chg}\n";
748
+ }
749
+ $x->{file_a} = $_;
750
+ $state = 'file_b';
751
+ } elsif ($state eq 'file_b') {
752
+ my $x = $mods[$#mods] or croak "Empty array\n";
753
+ if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
754
+ croak "Error parsing $_, $x->{chg}\n";
755
+ }
756
+ if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
757
+ croak "Error parsing $_, $x->{chg}\n";
758
+ }
759
+ $x->{file_b} = $_;
760
+ $state = 'meta';
761
+ } else {
762
+ croak "Error parsing $_\n";
763
+ }
764
+ }
765
+ command_close_pipe($diff_fh, $ctx);
766
+ \@mods;
767
+}
768
+
769
+sub check_diff_paths {
770
+ my ($ra, $pfx, $rev, $mods) = @_;
771
+ my %types;
772
+ $pfx .= '/' if length $pfx;
773
+
774
+ sub type_diff_paths {
775
+ my ($ra, $types, $path, $rev) = @_;
776
+ my @p = split m#/+#, $path;
777
+ my $c = shift @p;
778
+ unless (defined $types->{$c}) {
779
+ $types->{$c} = $ra->check_path($c, $rev);
780
+ }
781
+ while (@p) {
782
+ $c .= '/' . shift @p;
783
+ next if defined $types->{$c};
784
+ $types->{$c} = $ra->check_path($c, $rev);
785
+ }
786
+ }
787
+
788
+ foreach my $m (@$mods) {
789
+ foreach my $f (qw/file_a file_b/) {
790
+ next unless defined $m->{$f};
791
+ my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
792
+ if (length $pfx.$dir && ! defined $types{$dir}) {
793
+ type_diff_paths($ra, \%types, $pfx.$dir, $rev);
794
+ }
795
+ }
796
+ }
797
+ use Data::Dumper;
798
+ warn Dumper \%types;
799
+ warn Dumper $mods;
800
+ \%types;
801
+}
802
+
803
package Git::SVN;
804
use strict;
805
use warnings;
@@ -1375,18 +1467,20 @@ sub set_tree {
1467
fatal("Must have an existing revision to commit\n");
1468
}
1469
my $pool = SVN::Pool->new;
1378
- my $ed = SVN::Git::Editor->new({ r => $self->{last_rev},
1379
- ra => $self->ra->dup,
1380
- svn_path => $self->{path}
1381
- },
1470
+ my $mods = ::generate_diff($self->{last_commit}, $tree);
1471
+ my $types = ::check_diff_paths($self->ra, $self->{path},
1472
+ $self->{last_rev}, $mods);
1473
+ my %ed_opts = ( r => $self->{last_rev}, url => $self->ra->{url},
1474
+ svn_path => $self->{path},
1475
+ mods => $mods, types => $types );
1476
+ my $ed = SVN::Git::Editor->new(\%ed_opts,
1477
$self->ra->get_commit_editor(
1478
$log_entry->{log}, sub {
1479
$self->set_tree_cb($log_entry,
1480
$tree, @_);
1481
}),
1482
$pool);
1388
- my $mods = $ed->apply_diff($self->{last_commit}, $tree);
1389
- if (@$mods == 0) {
1483
+ if (!$ed->apply_diff($mods, $tree)) {
1484
print "No changes\nr$self->{last_rev} = $tree\n";
1485
}
1486
$pool->clear;
@@ -1898,7 +1992,7 @@ sub new {
1992
my $git_svn = shift;
1993
my $self = SVN::Delta::Editor->new(@_);
1994
bless $self, $class;
1901
- foreach (qw/svn_path r ra/) {
1995
+ foreach (qw/svn_path mods url types r/) {
1996
die "$_ required!\n" unless (defined $git_svn->{$_});
1997
$self->{$_} = $git_svn->{$_};
1998
}
@@ -1922,7 +2016,7 @@ sub repo_path {
2016
2017
sub url_path {
2018
my ($self, $path) = @_;
1925
- $self->{ra}->{url} . '/' . $self->repo_path($path);
2019
+ $self->{url} . '/' . $self->repo_path($path);
2020
}
2021
2022
sub rmdirs {
@@ -1972,7 +2066,10 @@ sub rmdirs {
2066
2067
sub open_or_add_dir {
2068
my ($self, $full_path, $baton) = @_;
1975
- my $t = $self->{ra}->check_path($full_path, $self->{r});
2069
+ my $t = $self->{types}->{$full_path};
2070
+ if (!defined $t) {
2071
+ die "$full_path not known in r$self->{r} or we have a bug!\n";
2072
+ }
2073
if ($t == $SVN::Node::none) {
2074
return $self->add_directory($full_path, $baton,
2075
undef, -1, $self->{pool});
@@ -1989,9 +2086,9 @@ sub open_or_add_dir {
2086
sub ensure_path {
2087
my ($self, $path) = @_;
2088
my $bat = $self->{bat};
1992
- $path = $self->repo_path($path);
1993
- return $bat->{''} unless (length $path);
1994
- my @p = split m#/+#, $path;
2089
+ my $repo_path = $self->repo_path($path);
2090
+ return $bat->{''} unless (length $repo_path);
2091
+ my @p = split m#/+#, $repo_path;
2092
my $c = shift @p;
2093
$bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
2094
while (@p) {
@@ -2129,59 +2226,9 @@ sub abort_edit {
2226
2227
# this drives the editor
2228
sub apply_diff {
2132
- my ($self, $tree_a, $tree_b) = @_;
2133
- my @diff_tree = qw(diff-tree -z -r);
2134
- if ($::_cp_similarity) {
2135
- push @diff_tree, "-C$::_cp_similarity";
2136
- } else {
2137
- push @diff_tree, '-C';
2138
- }
2139
- push @diff_tree, '--find-copies-harder' if $::_find_copies_harder;
2140
- push @diff_tree, "-l$::_l" if defined $::_l;
2141
- push @diff_tree, $tree_a, $tree_b;
2142
- my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
2143
- my $nl = $/;
2144
- local $/ = "\0";
2145
- my $state = 'meta';
2146
- my @mods;
2147
- while (<$diff_fh>) {
2148
- chomp $_; # this gets rid of the trailing "\0"
2149
- if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
2150
- $::sha1\s($::sha1)\s
2151
- ([MTCRAD])\d*$/xo) {
2152
- push @mods, { mode_a => $1, mode_b => $2,
2153
- sha1_b => $3, chg => $4 };
2154
- if ($4 =~ /^(?:C|R)$/) {
2155
- $state = 'file_a';
2156
- } else {
2157
- $state = 'file_b';
2158
- }
2159
- } elsif ($state eq 'file_a') {
2160
- my $x = $mods[$#mods] or croak "Empty array\n";
2161
- if ($x->{chg} !~ /^(?:C|R)$/) {
2162
- croak "Error parsing $_, $x->{chg}\n";
2163
- }
2164
- $x->{file_a} = $_;
2165
- $state = 'file_b';
2166
- } elsif ($state eq 'file_b') {
2167
- my $x = $mods[$#mods] or croak "Empty array\n";
2168
- if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
2169
- croak "Error parsing $_, $x->{chg}\n";
2170
- }
2171
- if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
2172
- croak "Error parsing $_, $x->{chg}\n";
2173
- }
2174
- $x->{file_b} = $_;
2175
- $state = 'meta';
2176
- } else {
2177
- croak "Error parsing $_\n";
2178
- }
2179
- }
2180
- command_close_pipe($diff_fh, $ctx);
2181
- $/ = $nl;
2182
-
2229
+ my ($self, $mods, $tree_b) = @_;
2230
my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
2184
- foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @mods) {
2231
+ foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
2232
my $f = $m->{chg};
2233
if (defined $o{$f}) {
2234
$self->$f($m);
@@ -2190,12 +2237,12 @@ sub apply_diff {
2237
}
2238
}
2239
$self->rmdirs($tree_b) if $::_rmdir;
2193
- if (@mods == 0) {
2240
+ if (@$mods == 0) {
2241
$self->abort_edit;
2242
} else {
2243
$self->close_edit;
2244
}
2198
- \@mods;
2245
+ return scalar @$mods;
2246
}
2247
2248
package Git::SVN::Ra;
@@ -2256,14 +2303,6 @@ sub DESTROY {
2303
# do not call the real DESTROY since we store ourselves in %RA
2304
}
2305
2259
-sub dup {
2260
- my ($self) = @_;
2261
- my $dup = SVN::Ra->new(pool => SVN::Pool->new,
2262
- map { $_ => $self->{$_} } qw/config url
2263
- auth auth_provider_callbacks repos_root svn_path/);
2264
- bless $dup, ref $self;
2265
-}
2266
-
2306
sub get_log {
2307
my ($self, @args) = @_;
2308
my $pool = SVN::Pool->new;
@@ -2922,6 +2961,8 @@ $log_entry hashref as returned by libsvn_log_entry()
2961
author => 'committer name'
2962
};
2963
2964
+
2965
+# this is generated by generate_diff();
2966
@mods = array of diff-index line hashes, each element represents one line
2967
of diff-index output
2968