git-svn: simplify usage of the SVN::Git::Editor interface
Signed-off-by: Eric Wong <normalperson@yhbt.net>
Eric Wong committed
Jan 27, 2007 at 14:33 UTC
6139535436d7ec9808fca75821f14d4d5061f343
1 file changed
+139
-142
git-svn.perl
+139
-142
@@ -349,25 +349,16 @@ sub cmd_dcommit {
349
if ($_dry_run) {
350
print "diff-tree $d~1 $d\n";
351
} else {
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);
352
my %ed_opts = ( r => $last_rev,
361
- mods => $mods,
362
- url => $ra->{url},
363
- types => $types,
353
+ log => get_commit_entry($d)->{log},
354
+ ra => $gs->ra,
355
+ tree_a => "$d~1",
356
+ tree_b => $d,
357
+ editor_cb => sub {
358
+ print "Committed r$_[0]\n";
359
+ $last_rev = $_[0]; },
360
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);
370
- if (!$ed->apply_diff($mods, $d)) {
361
+ if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
362
print "No changes\n$d~1 == $d\n";
363
}
364
}
@@ -474,19 +465,16 @@ sub cmd_commit_diff {
465
} elsif ($r !~ /^\d+$/) {
466
die "revision argument: $r not understood by git-svn\n";
467
}
477
- my $pool = SVN::Pool->new;
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);
486
- if (!$ed->apply_diff($mods, $tb)) {
468
+ my %ed_opts = ( r => $r,
469
+ log => $_message,
470
+ ra => $ra,
471
+ tree_a => $ta,
472
+ tree_b => $tb,
473
+ editor_cb => sub { print "Committed r$_[0]\n" },
474
+ svn_path => $svn_path );
475
+ if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
476
print "No changes\n$ta == $tb\n";
477
}
489
- $pool->clear;
478
}
479
480
########################### utility functions #########################
@@ -714,92 +702,6 @@ sub tz_to_s_offset {
702
return ($1 * 60) + ($tz * 3600);
703
}
704
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
-
705
package Git::SVN;
706
use strict;
707
use warnings;
@@ -1466,24 +1368,17 @@ sub set_tree {
1368
unless ($self->{last_rev}) {
1369
fatal("Must have an existing revision to commit\n");
1370
}
1469
- my $pool = SVN::Pool->new;
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);
1483
- if (!$ed->apply_diff($mods, $tree)) {
1371
+ my %ed_opts = ( r => $self->{last_rev},
1372
+ log => $log_entry->{log},
1373
+ ra => $self->ra,
1374
+ tree_a => $self->{last_commit},
1375
+ tree_b => $tree,
1376
+ editor_cb => sub {
1377
+ $self->set_tree_cb($log_entry, $tree, @_) },
1378
+ svn_path => $self->{path} );
1379
+ if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
1380
print "No changes\nr$self->{last_rev} = $tree\n";
1381
}
1486
- $pool->clear;
1382
}
1383
1384
sub skip_unknown_revs {
@@ -1988,15 +1883,28 @@ use Carp qw/croak/;
1883
use IO::File;
1884
1885
sub new {
1991
- my $class = shift;
1992
- my $git_svn = shift;
1993
- my $self = SVN::Delta::Editor->new(@_);
1886
+ my ($class, $opts) = @_;
1887
+ foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
1888
+ die "$_ required!\n" unless (defined $opts->{$_});
1889
+ }
1890
+
1891
+ my $pool = SVN::Pool->new;
1892
+ my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
1893
+ my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
1894
+ $opts->{r}, $mods);
1895
+
1896
+ # $opts->{ra} functions should not be used after this:
1897
+ my @ce = $opts->{ra}->get_commit_editor($opts->{log},
1898
+ $opts->{editor_cb}, $pool);
1899
+ my $self = SVN::Delta::Editor->new(@ce, $pool);
1900
bless $self, $class;
1995
- foreach (qw/svn_path mods url types r/) {
1996
- die "$_ required!\n" unless (defined $git_svn->{$_});
1997
- $self->{$_} = $git_svn->{$_};
1901
+ foreach (qw/svn_path r tree_a tree_b/) {
1902
+ $self->{$_} = $opts->{$_};
1903
}
1999
- $self->{pool} = SVN::Pool->new;
1904
+ $self->{url} = $opts->{ra}->{url};
1905
+ $self->{mods} = $mods;
1906
+ $self->{types} = $types;
1907
+ $self->{pool} = $pool;
1908
$self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
1909
$self->{rm} = { };
1910
$self->{path_prefix} = length $self->{svn_path} ?
@@ -2005,6 +1913,89 @@ sub new {
1913
return $self;
1914
}
1915
1916
+sub generate_diff {
1917
+ my ($tree_a, $tree_b) = @_;
1918
+ my @diff_tree = qw(diff-tree -z -r);
1919
+ if ($::_cp_similarity) {
1920
+ push @diff_tree, "-C$::_cp_similarity";
1921
+ } else {
1922
+ push @diff_tree, '-C';
1923
+ }
1924
+ push @diff_tree, '--find-copies-harder' if $::_find_copies_harder;
1925
+ push @diff_tree, "-l$::_l" if defined $::_l;
1926
+ push @diff_tree, $tree_a, $tree_b;
1927
+ my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
1928
+ local $/ = "\0";
1929
+ my $state = 'meta';
1930
+ my @mods;
1931
+ while (<$diff_fh>) {
1932
+ chomp $_; # this gets rid of the trailing "\0"
1933
+ if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
1934
+ $::sha1\s($::sha1)\s
1935
+ ([MTCRAD])\d*$/xo) {
1936
+ push @mods, { mode_a => $1, mode_b => $2,
1937
+ sha1_b => $3, chg => $4 };
1938
+ if ($4 =~ /^(?:C|R)$/) {
1939
+ $state = 'file_a';
1940
+ } else {
1941
+ $state = 'file_b';
1942
+ }
1943
+ } elsif ($state eq 'file_a') {
1944
+ my $x = $mods[$#mods] or croak "Empty array\n";
1945
+ if ($x->{chg} !~ /^(?:C|R)$/) {
1946
+ croak "Error parsing $_, $x->{chg}\n";
1947
+ }
1948
+ $x->{file_a} = $_;
1949
+ $state = 'file_b';
1950
+ } elsif ($state eq 'file_b') {
1951
+ my $x = $mods[$#mods] or croak "Empty array\n";
1952
+ if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
1953
+ croak "Error parsing $_, $x->{chg}\n";
1954
+ }
1955
+ if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
1956
+ croak "Error parsing $_, $x->{chg}\n";
1957
+ }
1958
+ $x->{file_b} = $_;
1959
+ $state = 'meta';
1960
+ } else {
1961
+ croak "Error parsing $_\n";
1962
+ }
1963
+ }
1964
+ command_close_pipe($diff_fh, $ctx);
1965
+ \@mods;
1966
+}
1967
+
1968
+sub check_diff_paths {
1969
+ my ($ra, $pfx, $rev, $mods) = @_;
1970
+ my %types;
1971
+ $pfx .= '/' if length $pfx;
1972
+
1973
+ sub type_diff_paths {
1974
+ my ($ra, $types, $path, $rev) = @_;
1975
+ my @p = split m#/+#, $path;
1976
+ my $c = shift @p;
1977
+ unless (defined $types->{$c}) {
1978
+ $types->{$c} = $ra->check_path($c, $rev);
1979
+ }
1980
+ while (@p) {
1981
+ $c .= '/' . shift @p;
1982
+ next if defined $types->{$c};
1983
+ $types->{$c} = $ra->check_path($c, $rev);
1984
+ }
1985
+ }
1986
+
1987
+ foreach my $m (@$mods) {
1988
+ foreach my $f (qw/file_a file_b/) {
1989
+ next unless defined $m->{$f};
1990
+ my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
1991
+ if (length $pfx.$dir && ! defined $types{$dir}) {
1992
+ type_diff_paths($ra, \%types, $pfx.$dir, $rev);
1993
+ }
1994
+ }
1995
+ }
1996
+ \%types;
1997
+}
1998
+
1999
sub split_path {
2000
return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
2001
}
@@ -2020,7 +2011,7 @@ sub url_path {
2011
}
2012
2013
sub rmdirs {
2023
- my ($self, $tree_b) = @_;
2014
+ my ($self) = @_;
2015
my $rm = $self->{rm};
2016
delete $rm->{''}; # we never delete the url we're tracking
2017
return unless %$rm;
@@ -2038,8 +2029,8 @@ sub rmdirs {
2029
delete $rm->{''}; # we never delete the url we're tracking
2030
return unless %$rm;
2031
2041
- my ($fh, $ctx) = command_output_pipe(
2042
- qw/ls-tree --name-only -r -z/, $tree_b);
2032
+ my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
2033
+ $self->{tree_b});
2034
local $/ = "\0";
2035
while (<$fh>) {
2036
chomp;
@@ -2221,12 +2212,18 @@ sub close_edit {
2212
sub abort_edit {
2213
my ($self) = @_;
2214
$self->SUPER::abort_edit($self->{pool});
2215
+}
2216
+
2217
+sub DESTROY {
2218
+ my $self = shift;
2219
+ $self->SUPER::DESTROY(@_);
2220
$self->{pool}->clear;
2221
}
2222
2223
# this drives the editor
2224
sub apply_diff {
2229
- my ($self, $mods, $tree_b) = @_;
2225
+ my ($self) = @_;
2226
+ my $mods = $self->{mods};
2227
my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
2228
foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
2229
my $f = $m->{chg};
@@ -2236,7 +2233,7 @@ sub apply_diff {
2233
fatal("Invalid change type: $f\n");
2234
}
2235
}
2239
- $self->rmdirs($tree_b) if $::_rmdir;
2236
+ $self->rmdirs if $::_rmdir;
2237
if (@$mods == 0) {
2238
$self->abort_edit;
2239
} else {