git-svn: convert to using Git.pm
Thanks to Git.pm, I've been able to greatly reduce the amount of extra work that needs to be done to manage input/output pipes in Perl. chomp usage has also been greatly reduced, too. All tests (including full-svn-test) still pass, but this has not been tested extensively in the real-world. Signed-off-by: Eric Wong <normalperson@yhbt.net> Signed-off-by: Junio C Hamano <junkio@cox.net>
Eric Wong committed
Dec 15, 2006 at 10:59 UTC
aef4e921a01350600071bf18fc2706b8fca55e47
1 file changed
+135
-193
git-svn.perl
+135
-193
@@ -46,6 +46,8 @@ use File::Copy qw/copy/;
46
use POSIX qw/strftime/;
47
use IPC::Open3;
48
use Memoize;
49
+use Git qw/command command_oneline command_noisy
50
+ command_output_pipe command_input_pipe command_close_pipe/;
51
memoize('revisions_eq');
52
memoize('cmt_metadata');
53
memoize('get_commit_time');
@@ -232,28 +234,27 @@ sub version {
234
}
235
236
sub rebuild {
235
- if (quiet_run(qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0")) {
237
+ if (!verify_ref("refs/remotes/$GIT_SVN^0")) {
238
copy_remote_ref();
239
}
240
$SVN_URL = shift or undef;
241
my $newest_rev = 0;
242
if ($_upgrade) {
241
- sys('git-update-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
243
+ command_noisy('update-ref',"refs/remotes/$GIT_SVN","
244
+ $GIT_SVN-HEAD");
245
} else {
246
check_upgrade_needed();
247
}
248
246
- my $pid = open(my $rev_list,'-|');
247
- defined $pid or croak $!;
248
- if ($pid == 0) {
249
- exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
250
- }
249
+ my ($rev_list, $ctx) = command_output_pipe("rev-list",
250
+ "refs/remotes/$GIT_SVN");
251
my $latest;
252
while (<$rev_list>) {
253
chomp;
254
my $c = $_;
255
croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
256
- my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
256
+ my @commit = grep(/^git-svn-id: /,
257
+ command(qw/cat-file commit/, $c));
258
next if (!@commit); # skip merges
259
my ($url, $rev, $uuid) = extract_metadata($commit[$#commit]);
260
if (!defined $rev || !$uuid) {
@@ -279,7 +280,7 @@ sub rebuild {
280
print "r$rev = $c\n";
281
$newest_rev = $rev if ($rev > $newest_rev);
282
}
282
- close $rev_list or croak $?;
283
+ command_close_pipe($rev_list, $ctx);
284
285
goto out if $_use_lib;
286
if (!chdir $SVN_WC) {
@@ -287,7 +288,7 @@ sub rebuild {
288
chdir $SVN_WC or croak $!;
289
}
290
290
- $pid = fork;
291
+ my $pid = fork;
292
defined $pid or croak $!;
293
if ($pid == 0) {
294
my @svn_up = qw(svn up);
@@ -323,10 +324,10 @@ sub init {
324
325
$SVN_URL = $url;
326
unless (-d $GIT_DIR) {
326
- my @init_db = ('git-init-db');
327
+ my @init_db = ('init-db');
328
push @init_db, "--template=$_template" if defined $_template;
329
push @init_db, "--shared" if defined $_shared;
329
- sys(@init_db);
330
+ command_noisy(@init_db);
331
}
332
setup_git_svn();
333
}
@@ -335,9 +336,8 @@ sub fetch {
336
check_upgrade_needed();
337
$SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
338
my $ret = $_use_lib ? fetch_lib(@_) : fetch_cmd(@_);
338
- if ($ret->{commit} && quiet_run(qw(git-rev-parse --verify
339
- refs/heads/master^0))) {
340
- sys(qw(git-update-ref refs/heads/master),$ret->{commit});
339
+ if ($ret->{commit} && !verify_ref('refs/heads/master^0')) {
340
+ command_noisy(qw(update-ref refs/heads/master),$ret->{commit});
341
}
342
return $ret;
343
}
@@ -416,16 +416,16 @@ sub fetch_lib {
416
read_uuid();
417
if (defined $last_commit) {
418
unless (-e $GIT_SVN_INDEX) {
419
- sys(qw/git-read-tree/, $last_commit);
419
+ command_noisy('read-tree', $last_commit);
420
}
421
- chomp (my $x = `git-write-tree`);
422
- my ($y) = (`git-cat-file commit $last_commit`
421
+ my $x = command_oneline('write-tree');
422
+ my ($y) = (command(qw/cat-file commit/, $last_commit)
423
=~ /^tree ($sha1)/m);
424
if ($y ne $x) {
425
unlink $GIT_SVN_INDEX or croak $!;
426
- sys(qw/git-read-tree/, $last_commit);
426
+ command_noisy('read-tree', $last_commit);
427
}
428
- chomp ($x = `git-write-tree`);
428
+ $x = command_oneline('write-tree');
429
if ($y ne $x) {
430
print STDERR "trees ($last_commit) $y != $x\n",
431
"Something is seriously wrong...\n";
@@ -489,16 +489,15 @@ sub commit {
489
}
490
my @revs;
491
foreach my $c (@commits) {
492
- chomp(my @tmp = safe_qx('git-rev-parse',$c));
492
+ my @tmp = command('rev-parse',$c);
493
if (scalar @tmp == 1) {
494
push @revs, $tmp[0];
495
} elsif (scalar @tmp > 1) {
496
- push @revs, reverse (safe_qx('git-rev-list',@tmp));
496
+ push @revs, reverse(command('rev-list',@tmp));
497
} else {
498
die "Failed to rev-parse $c\n";
499
}
500
}
501
- chomp @revs;
501
$_use_lib ? commit_lib(@revs) : commit_cmd(@revs);
502
print "Done committing ",scalar @revs," revisions to SVN\n";
503
}
@@ -606,10 +605,10 @@ sub commit_lib {
605
sub dcommit {
606
my $head = shift || 'HEAD';
607
my $gs = "refs/remotes/$GIT_SVN";
609
- chomp(my @refs = safe_qx(qw/git-rev-list --no-merges/, "$gs..$head"));
608
+ my @refs = command(qw/rev-list --no-merges/, "$gs..$head");
609
my $last_rev;
610
foreach my $d (reverse @refs) {
612
- if (quiet_run('git-rev-parse','--verify',"$d~1") != 0) {
611
+ if (!verify_ref("$d~1")) {
612
die "Commit $d\n",
613
"has no parent commit, and therefore ",
614
"nothing to diff against.\n",
@@ -633,7 +632,7 @@ sub dcommit {
632
}
633
return if $_dry_run;
634
fetch();
636
- my @diff = safe_qx('git-diff-tree', $head, $gs);
635
+ my @diff = command('diff-tree', $head, $gs, '--');
636
my @finish;
637
if (@diff) {
638
@finish = qw/rebase/;
@@ -645,7 +644,7 @@ sub dcommit {
644
"Resetting to the latest $gs\n";
645
@finish = qw/reset --mixed/;
646
}
648
- sys('git', @finish, $gs);
647
+ command_noisy(@finish, $gs);
648
}
649
650
sub show_ignore {
@@ -689,7 +688,7 @@ sub graft_branches {
688
689
if (%$grafts) {
690
# temporarily disable our grafts file to make this idempotent
692
- chomp($gr_sha1 = safe_qx(qw/git-hash-object -w/,$gr_file));
691
+ chomp($gr_sha1 = command(qw/hash-object -w/,$gr_file));
692
rename $gr_file, "$gr_file~$gr_sha1" or croak $!;
693
}
694
@@ -743,7 +742,7 @@ sub multi_init {
742
unless (-d $GIT_SVN_DIR) {
743
print "GIT_SVN_ID set to 'trunk' for $_trunk\n" if $ch_id;
744
init($_trunk);
746
- sys('git-repo-config', 'svn.trunk', $_trunk);
745
+ command_noisy('repo-config', 'svn.trunk', $_trunk);
746
}
747
complete_url_ls_init($url, $_branches, '--branches/-b', '');
748
complete_url_ls_init($url, $_tags, '--tags/-t', 'tags/');
@@ -781,11 +780,8 @@ sub show_log {
780
}
781
782
config_pager();
784
- my $pid = open(my $log,'-|');
785
- defined $pid or croak $!;
786
- if (!$pid) {
787
- exec(git_svn_log_cmd($r_min,$r_max), @args) or croak $!;
788
- }
783
+ @args = (git_svn_log_cmd($r_min, $r_max), @args);
784
+ my $log = command_output_pipe(@args);
785
run_pager();
786
my (@k, $c, $d);
787
@@ -832,7 +828,7 @@ sub show_log {
828
process_commit($_, $r_min, $r_max) foreach reverse @k;
829
}
830
out:
835
- close $log;
831
+ eval { command_close_pipe($log) };
832
print '-' x72,"\n" unless $_incremental || $_oneline;
833
}
834
@@ -912,7 +908,7 @@ sub cmt_showable {
908
return 1 if defined $c->{r};
909
if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
910
$c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
915
- my @msg = safe_qx(qw/git-cat-file commit/, $c->{c});
911
+ my @msg = command(qw/cat-file commit/, $c->{c});
912
shift @msg while ($msg[0] ne "\n");
913
shift @msg;
914
@{$c->{l}} = grep !/^git-svn-id: /, @msg;
@@ -961,7 +957,7 @@ sub log_use_color {
957
958
sub git_svn_log_cmd {
959
my ($r_min, $r_max) = @_;
964
- my @cmd = (qw/git-log --abbrev-commit --pretty=raw
960
+ my @cmd = (qw/log --abbrev-commit --pretty=raw
961
--default/, "refs/remotes/$GIT_SVN");
962
push @cmd, '-r' unless $_non_recursive;
963
push @cmd, qw/--raw --name-status/ if $_verbose;
@@ -1071,7 +1067,7 @@ sub complete_url_ls_init {
1067
waitpid $pid, 0;
1068
croak $? if $?;
1069
my ($n) = ($switch =~ /^--(\w+)/);
1074
- sys('git-repo-config', "svn.$n", $var);
1070
+ command_noisy('repo-config', "svn.$n", $var);
1071
}
1072
1073
sub common_prefix {
@@ -1103,11 +1099,8 @@ sub graft_tree_joins {
1099
1100
git_svn_each(sub {
1101
my $i = shift;
1106
- defined(my $pid = open my $fh, '-|') or croak $!;
1107
- if (!$pid) {
1108
- exec qw/git-rev-list --pretty=raw/,
1109
- "refs/remotes/$i" or croak $!;
1110
- }
1102
+ my @args = (qw/rev-list --pretty=raw/, "refs/remotes/$i");
1103
+ my ($fh, $ctx) = command_output_pipe(@args);
1104
while (<$fh>) {
1105
next unless /^commit ($sha1)$/o;
1106
my $c = $1;
@@ -1130,9 +1123,7 @@ sub graft_tree_joins {
1123
1124
foreach my $p (@{$tree_map{$t}}) {
1125
next if $p eq $c;
1133
- my $mb = eval {
1134
- safe_qx('git-merge-base', $c, $p)
1135
- };
1126
+ my $mb = eval { command('merge-base', $c, $p) };
1127
next unless ($@ || $?);
1128
if (defined $r_a) {
1129
# see if SVN says it's a relative
@@ -1161,7 +1152,7 @@ sub graft_tree_joins {
1152
# what should we do when $ct == $s ?
1153
}
1154
}
1164
- close $fh or croak $?;
1155
+ command_close_pipe($fh, $ctx);
1156
});
1157
}
1158
@@ -1297,6 +1288,11 @@ sub read_uuid {
1288
}
1289
}
1290
1291
+sub verify_ref {
1292
+ my ($ref) = @_;
1293
+ eval { command_oneline([ 'rev-parse', $ref ], { STDERR => 0 }) };
1294
+}
1295
+
1296
sub quiet_run {
1297
my $pid = fork;
1298
defined $pid or croak $!;
@@ -1379,13 +1375,14 @@ sub assert_svn_wc_clean {
1375
sub get_tree_from_treeish {
1376
my ($treeish) = @_;
1377
croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
1382
- chomp(my $type = `git-cat-file -t $treeish`);
1378
+ my $type = command_oneline(qw/cat-file -t/, $treeish);
1379
my $expected;
1380
while ($type eq 'tag') {
1385
- chomp(($treeish, $type) = `git-cat-file tag $treeish`);
1381
+ ($treeish, $type) = command(qw/cat-file tag/, $treeish);
1382
}
1383
if ($type eq 'commit') {
1388
- $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
1384
+ $expected = (grep /^tree /, command(qw/cat-file commit/,
1385
+ $treeish))[0];
1386
($expected) = ($expected =~ /^tree ($sha1)$/);
1387
die "Unable to get tree from $treeish\n" unless $expected;
1388
} elsif ($type eq 'tree') {
@@ -1407,7 +1404,7 @@ sub assert_tree {
1404
}
1405
my $old_index = set_index($tmpindex);
1406
index_changes(1);
1410
- chomp(my $tree = `git-write-tree`);
1407
+ my $tree = command_oneline('write-tree');
1408
restore_index($old_index);
1409
if ($tree ne $expected) {
1410
croak "Tree mismatch, Got: $tree, Expected: $expected\n";
@@ -1415,8 +1412,20 @@ sub assert_tree {
1412
unlink $tmpindex;
1413
}
1414
1418
-sub parse_diff_tree {
1419
- my $diff_fh = shift;
1415
+sub get_diff {
1416
+ my ($from, $treeish) = @_;
1417
+ assert_tree($from);
1418
+ print "diff-tree $from $treeish\n";
1419
+ my @diff_tree = qw(diff-tree -z -r);
1420
+ if ($_cp_similarity) {
1421
+ push @diff_tree, "-C$_cp_similarity";
1422
+ } else {
1423
+ push @diff_tree, '-C';
1424
+ }
1425
+ push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
1426
+ push @diff_tree, "-l$_l" if defined $_l;
1427
+ push @diff_tree, $from, $treeish;
1428
+ my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
1429
local $/ = "\0";
1430
my $state = 'meta';
1431
my @mods;
@@ -1452,8 +1461,7 @@ sub parse_diff_tree {
1461
croak "Error parsing $_\n";
1462
}
1463
}
1455
- close $diff_fh or croak $?;
1456
-
1464
+ command_close_pipe($diff_fh, $ctx);
1465
return \@mods;
1466
}
1467
@@ -1547,26 +1555,6 @@ sub precommit_check {
1555
}
1556
1557
1550
-sub get_diff {
1551
- my ($from, $treeish) = @_;
1552
- assert_tree($from);
1553
- print "diff-tree $from $treeish\n";
1554
- my $pid = open my $diff_fh, '-|';
1555
- defined $pid or croak $!;
1556
- if ($pid == 0) {
1557
- my @diff_tree = qw(git-diff-tree -z -r);
1558
- if ($_cp_similarity) {
1559
- push @diff_tree, "-C$_cp_similarity";
1560
- } else {
1561
- push @diff_tree, '-C';
1562
- }
1563
- push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
1564
- push @diff_tree, "-l$_l" if defined $_l;
1565
- exec(@diff_tree, $from, $treeish) or croak $!;
1566
- }
1567
- return parse_diff_tree($diff_fh);
1568
-}
1569
-
1558
sub svn_checkout_tree {
1559
my ($from, $treeish) = @_;
1560
my $mods = get_diff($from->{commit}, $treeish);
@@ -1676,14 +1664,10 @@ sub get_commit_message {
1664
my %log_msg = ( msg => '' );
1665
open my $msg, '>', $commit_msg or croak $!;
1666
1679
- chomp(my $type = `git-cat-file -t $commit`);
1667
+ my $type = command_oneline(qw/cat-file -t/, $commit);
1668
if ($type eq 'commit' || $type eq 'tag') {
1681
- my $pid = open my $msg_fh, '-|';
1682
- defined $pid or croak $!;
1683
-
1684
- if ($pid == 0) {
1685
- exec('git-cat-file', $type, $commit) or croak $!;
1686
- }
1669
+ my ($msg_fh, $ctx) = command_output_pipe('cat-file',
1670
+ $type, $commit);
1671
my $in_msg = 0;
1672
while (<$msg_fh>) {
1673
if (!$in_msg) {
@@ -1695,7 +1679,7 @@ sub get_commit_message {
1679
print $msg $_ or croak $!;
1680
}
1681
}
1698
- close $msg_fh or croak $?;
1682
+ command_close_pipe($msg_fh, $ctx);
1683
}
1684
close $msg or croak $!;
1685
@@ -1771,13 +1755,8 @@ sub svn_commit_tree {
1755
}
1756
1757
sub rev_list_raw {
1774
- my (@args) = @_;
1775
- my $pid = open my $fh, '-|';
1776
- defined $pid or croak $!;
1777
- if (!$pid) {
1778
- exec(qw/git-rev-list --pretty=raw/, @args) or croak $!;
1779
- }
1780
- return { fh => $fh, t => { } };
1758
+ my ($fh, $c) = command_output_pipe(qw/rev-list --pretty=raw/, @_);
1759
+ return { fh => $fh, ctx => $c, t => { } };
1760
}
1761
1762
sub next_rev_list_entry {
@@ -1799,6 +1778,7 @@ sub next_rev_list_entry {
1778
$x->{m} .= $_;
1779
}
1780
}
1781
+ command_close_pipe($fh, $rl->{ctx});
1782
return ($x != $rl->{t}) ? $x : undef;
1783
}
1784
@@ -1924,15 +1904,10 @@ sub sys { system(@_) == 0 or croak $? }
1904
sub do_update_index {
1905
my ($z_cmd, $cmd, $no_text_base) = @_;
1906
1927
- my $z = open my $p, '-|';
1928
- defined $z or croak $!;
1929
- unless ($z) { exec @$z_cmd or croak $! }
1907
+ my ($p, $pctx) = command_output_pipe(@$z_cmd);
1908
1931
- my $pid = open my $ui, '|-';
1932
- defined $pid or croak $!;
1933
- unless ($pid) {
1934
- exec('git-update-index',"--$cmd",'-z','--stdin') or croak $!;
1935
- }
1909
+ my ($ui, $uctx) = command_input_pipe('update-index',
1910
+ "--$cmd",'-z','--stdin');
1911
local $/ = "\0";
1912
while (my $x = <$p>) {
1913
chomp $x;
@@ -1956,7 +1931,8 @@ sub do_update_index {
1931
}
1932
print $ui $x,"\0";
1933
}
1959
- close $ui or croak $?;
1934
+ command_close_pipe($p, $pctx);
1935
+ command_close_pipe($ui, $uctx);
1936
}
1937
1938
sub index_changes {
@@ -1968,10 +1944,10 @@ sub index_changes {
1944
close $fd or croak $!;
1945
}
1946
my $no_text_base = shift;
1971
- do_update_index([qw/git-diff-files --name-only -z/],
1947
+ do_update_index([qw/diff-files --name-only -z/],
1948
'remove',
1949
$no_text_base);
1974
- do_update_index([qw/git-ls-files -z --others/,
1950
+ do_update_index([qw/ls-files -z --others/,
1951
"--exclude-from=$GIT_SVN_DIR/info/exclude"],
1952
'add',
1953
$no_text_base);
@@ -2004,10 +1980,10 @@ sub assert_revision_unknown {
1980
1981
sub trees_eq {
1982
my ($x, $y) = @_;
2007
- my @x = safe_qx('git-cat-file','commit',$x);
2008
- my @y = safe_qx('git-cat-file','commit',$y);
2009
- if (($y[0] ne $x[0]) || $x[0] !~ /^tree $sha1\n$/
2010
- || $y[0] !~ /^tree $sha1\n$/) {
1983
+ my @x = command(qw/cat-file commit/,$x);
1984
+ my @y = command(qw/cat-file commit/,$y);
1985
+ if (($y[0] ne $x[0]) || $x[0] ne "tree $sha1"
1986
+ || $y[0] ne "tree $sha1") {
1987
print STDERR "Trees not equal: $y[0] != $x[0]\n";
1988
return 0
1989
}
@@ -2039,33 +2015,24 @@ sub git_commit {
2015
if (!defined $tree) {
2016
my $index = set_index($GIT_SVN_INDEX);
2017
index_changes();
2042
- chomp($tree = `git-write-tree`);
2018
+ $tree = command_oneline('write-tree');
2019
croak $? if $?;
2020
restore_index($index);
2021
}
2022
2023
# just in case we clobber the existing ref, we still want that ref
2024
# as our parent:
2049
- open my $null, '>', '/dev/null' or croak $!;
2050
- open my $stderr, '>&', \*STDERR or croak $!;
2051
- open STDERR, '>&', $null or croak $!;
2052
- if (my $cur = eval { safe_qx('git-rev-parse',
2053
- "refs/remotes/$GIT_SVN^0") }) {
2025
+ if (my $cur = verify_ref("refs/remotes/$GIT_SVN^0")) {
2026
chomp $cur;
2027
push @tmp_parents, $cur;
2028
}
2057
- open STDERR, '>&', $stderr or croak $!;
2058
- close $stderr or croak $!;
2059
- close $null or croak $!;
2029
2030
if (exists $tree_map{$tree}) {
2031
foreach my $p (@{$tree_map{$tree}}) {
2032
my $skip;
2033
foreach (@tmp_parents) {
2034
# see if a common parent is found
2066
- my $mb = eval {
2067
- safe_qx('git-merge-base', $_, $p)
2068
- };
2035
+ my $mb = eval { command('merge-base', $_, $p) };
2036
next if ($@ || $?);
2037
$skip = 1;
2038
last;
@@ -2107,7 +2074,7 @@ sub git_commit {
2074
if ($commit !~ /^$sha1$/o) {
2075
die "Failed to commit, invalid sha1: $commit\n";
2076
}
2110
- sys('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
2077
+ command_noisy('update-ref',"refs/remotes/$GIT_SVN",$commit);
2078
revdb_set($REVDB, $log_msg->{revision}, $commit);
2079
2080
# this output is read via pipe, do not change:
@@ -2119,7 +2086,8 @@ sub git_commit {
2086
sub check_repack {
2087
if ($_repack && (--$_repack_nr == 0)) {
2088
$_repack_nr = $_repack;
2122
- sys("git repack $_repack_flags");
2089
+ # repack doesn't use any arguments with spaces in them, does it?
2090
+ command_noisy('repack', split(/\s+/, $_repack_flags));
2091
}
2092
}
2093
@@ -2238,20 +2206,11 @@ sub check_upgrade_needed {
2206
open my $fh, '>>',$REVDB or croak $!;
2207
close $fh;
2208
}
2241
- my $old = eval {
2242
- my $pid = open my $child, '-|';
2243
- defined $pid or croak $!;
2244
- if ($pid == 0) {
2245
- close STDERR;
2246
- exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $!;
2247
- }
2248
- my @ret = (<$child>);
2249
- close $child or croak $?;
2250
- die $? if $?; # just in case close didn't error out
2251
- return wantarray ? @ret : join('',@ret);
2209
+ return unless eval {
2210
+ command([qw/rev-parse --verify/,"$GIT_SVN-HEAD^0"],
2211
+ {STDERR => 0});
2212
};
2253
- return unless $old;
2254
- my $head = eval { safe_qx('git-rev-parse',"refs/remotes/$GIT_SVN") };
2213
+ my $head = eval { command('rev-parse',"refs/remotes/$GIT_SVN") };
2214
if ($@ || !$head) {
2215
print STDERR "Please run: $0 rebuild --upgrade\n";
2216
exit 1;
@@ -2263,12 +2222,8 @@ sub check_upgrade_needed {
2222
sub map_tree_joins {
2223
my %seen;
2224
foreach my $br (@_branch_from) {
2266
- my $pid = open my $pipe, '-|';
2267
- defined $pid or croak $!;
2268
- if ($pid == 0) {
2269
- exec(qw(git-rev-list --topo-order --pretty=raw), $br)
2270
- or croak $!;
2271
- }
2225
+ my $pipe = command_output_pipe(qw/rev-list
2226
+ --topo-order --pretty=raw/, $br);
2227
while (<$pipe>) {
2228
if (/^commit ($sha1)$/o) {
2229
my $commit = $1;
@@ -2284,7 +2239,7 @@ sub map_tree_joins {
2239
$seen{$commit} = 1;
2240
}
2241
}
2287
- close $pipe; # we could be breaking the pipe early
2242
+ eval { command_close_pipe($pipe) };
2243
}
2244
}
2245
@@ -2296,7 +2251,7 @@ sub load_all_refs {
2251
2252
# don't worry about rev-list on non-commit objects/tags,
2253
# it shouldn't blow up if a ref is a blob or tree...
2299
- chomp(@_branch_from = `git-rev-parse --symbolic --all`);
2254
+ @_branch_from = command(qw/rev-parse --symbolic --all/);
2255
}
2256
2257
# '<svn username> = real-name <email address>' mapping based on git-svnimport:
@@ -2330,7 +2285,7 @@ sub svn_propget_base {
2285
2286
sub git_svn_each {
2287
my $sub = shift;
2333
- foreach (`git-rev-parse --symbolic --all`) {
2288
+ foreach (command(qw/rev-parse --symbolic --all/)) {
2289
next unless s#^refs/remotes/##;
2290
chomp $_;
2291
next unless -f "$GIT_DIR/svn/$_/info/url";
@@ -2371,7 +2326,7 @@ sub migration_check {
2326
"$GIT_SVN_DIR\n\t(required for this version ",
2327
"($VERSION) of git-svn) does not.\n";
2328
2374
- foreach my $x (`git-rev-parse --symbolic --all`) {
2329
+ foreach my $x (command(qw/rev-parse --symbolic --all/)) {
2330
next unless $x =~ s#^refs/remotes/##;
2331
chomp $x;
2332
next unless -f "$GIT_DIR/$x/info/url";
@@ -2476,11 +2431,7 @@ sub write_grafts {
2431
my $p = $grafts->{$c};
2432
my %x; # real parents
2433
delete $p->{$c}; # commits are not self-reproducing...
2479
- my $pid = open my $ch, '-|';
2480
- defined $pid or croak $!;
2481
- if (!$pid) {
2482
- exec(qw/git-cat-file commit/, $c) or croak $!;
2483
- }
2434
+ my $ch = command_output_pipe(qw/cat-file commit/, $c);
2435
while (<$ch>) {
2436
if (/^parent ($sha1)/) {
2437
$x{$1} = $p->{$1} = 1;
@@ -2488,7 +2439,7 @@ sub write_grafts {
2439
last unless /^\S/;
2440
}
2441
}
2491
- close $ch; # breaking the pipe
2442
+ eval { command_close_pipe($ch) }; # breaking the pipe
2443
2444
# if real parents are the only ones in the grafts, drop it
2445
next if join(' ',sort keys %$p) eq join(' ',sort keys %x);
@@ -2500,7 +2451,7 @@ sub write_grafts {
2451
next if $del{$i} || $p->{$i} == 2;
2452
foreach my $j (@jp) {
2453
next if $i eq $j || $del{$j} || $p->{$j} == 2;
2503
- $mb = eval { safe_qx('git-merge-base',$i,$j) };
2454
+ $mb = eval { command('merge-base', $i, $j) };
2455
next unless $mb;
2456
chomp $mb;
2457
next if $x{$mb};
@@ -2571,15 +2522,12 @@ sub extract_metadata {
2522
2523
sub cmt_metadata {
2524
return extract_metadata((grep(/^git-svn-id: /,
2574
- safe_qx(qw/git-cat-file commit/, shift)))[-1]);
2525
+ command(qw/cat-file commit/, shift)))[-1]);
2526
}
2527
2528
sub get_commit_time {
2529
my $cmt = shift;
2579
- defined(my $pid = open my $fh, '-|') or croak $!;
2580
- if (!$pid) {
2581
- exec qw/git-rev-list --pretty=raw -n1/, $cmt or croak $!;
2582
- }
2530
+ my $fh = command_output_pipe(qw/rev-list --pretty=raw -n1/, $cmt);
2531
while (<$fh>) {
2532
/^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next;
2533
my ($s, $tz) = ($1, $2);
@@ -2588,7 +2536,7 @@ sub get_commit_time {
2536
} elsif ($tz =~ s/^\-//) {
2537
$s -= tz_to_s_offset($tz);
2538
}
2591
- close $fh;
2539
+ eval { command_close_pipe($fh) };
2540
return $s;
2541
}
2542
die "Can't get commit time for commit: $cmt\n";
@@ -2748,7 +2696,6 @@ sub libsvn_load {
2696
push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
2697
push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor';
2698
*SVN::Git::Fetcher::process_rm = *process_rm;
2751
- *SVN::Git::Fetcher::safe_qx = *safe_qx;
2699
my $kill_stupid_warnings = $SVN::Node::none.$SVN::Node::file.
2700
$SVN::Node::dir.$SVN::Node::unknown.
2701
$SVN::Node::none.$SVN::Node::file.
@@ -2965,7 +2912,7 @@ sub libsvn_get_file {
2912
my $mode = exists $props->{'svn:executable'} ? '100755' : '100644';
2913
if (exists $props->{'svn:special'}) {
2914
$mode = '120000';
2968
- my $link = `git-cat-file blob $hash`;
2915
+ my $link = `git-cat-file blob $hash`; # no chomping symlinks
2916
$link =~ s/^link // or die "svn:special file with contents: <",
2917
$link, "> is not understood\n";
2918
defined($pid = open3($in, $out, '>&STDERR',
@@ -3069,19 +3016,17 @@ sub libsvn_log_entry {
3016
sub process_rm {
3017
my ($gui, $last_commit, $f, $q) = @_;
3018
# remove entire directories.
3072
- if (safe_qx('git-ls-tree',$last_commit,'--',$f) =~ /^040000 tree/) {
3073
- defined(my $pid = open my $ls, '-|') or croak $!;
3074
- if (!$pid) {
3075
- exec(qw/git-ls-tree -r --name-only -z/,
3076
- $last_commit,'--',$f) or croak $!;
3077
- }
3019
+ if (command('ls-tree',$last_commit,'--',$f) =~ /^040000 tree/) {
3020
+ my ($ls, $ctx) = command_output_pipe(qw/ls-tree
3021
+ -r --name-only -z/,
3022
+ $last_commit,'--',$f);
3023
local $/ = "\0";
3024
while (<$ls>) {
3025
print $gui '0 ',0 x 40,"\t",$_ or croak $!;
3026
print "\tD\t$_\n" unless $q;
3027
}
3028
print "\tD\t$f/\n" unless $q;
3084
- close $ls or croak $?;
3029
+ command_close_pipe($ls, $ctx);
3030
return $SVN::Node::dir;
3031
} else {
3032
print $gui '0 ',0 x 40,"\t",$f,"\0" or croak $!;
@@ -3112,7 +3057,7 @@ sub libsvn_fetch_delta {
3057
3058
sub libsvn_fetch_full {
3059
my ($last_commit, $paths, $rev, $author, $date, $msg) = @_;
3115
- open my $gui, '| git-update-index -z --index-info' or croak $!;
3060
+ my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
3061
my %amr;
3062
my $ut = { empty => {}, dir_prop => {}, file_prop => {} };
3063
my $p = $SVN->{svn_path};
@@ -3166,20 +3111,14 @@ sub libsvn_fetch_full {
3111
%{$ut->{dir_prop}->{''}} = %$props;
3112
$pool->clear;
3113
}
3169
- close $gui or croak $?;
3114
+ command_close_pipe($gui, $ctx);
3115
libsvn_log_entry($rev, $author, $date, $msg, [$last_commit], $ut);
3116
}
3117
3118
sub svn_grab_base_rev {
3174
- defined(my $pid = open my $fh, '-|') or croak $!;
3175
- if (!$pid) {
3176
- open my $null, '>', '/dev/null' or croak $!;
3177
- open STDERR, '>&', $null or croak $!;
3178
- exec qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0"
3179
- or croak $!;
3180
- }
3181
- chomp(my $c = do { local $/; <$fh> });
3182
- close $fh;
3119
+ my $c = eval { command_oneline([qw/rev-parse --verify/,
3120
+ "refs/remotes/$GIT_SVN^0"],
3121
+ { STDERR => 0 }) };
3122
if (defined $c && length $c) {
3123
my ($url, $rev, $uuid) = cmt_metadata($c);
3124
return ($rev, $c) if defined $rev;
@@ -3358,7 +3297,7 @@ sub libsvn_find_parent_branch {
3297
if (revisions_eq($branch_from, $r0, $r)) {
3298
unlink $GIT_SVN_INDEX;
3299
print STDERR "Found branch parent: ($GIT_SVN) $parent\n";
3361
- sys(qw/git-read-tree/, $parent);
3300
+ command_noisy('read-tree', $parent);
3301
unless (libsvn_can_do_switch()) {
3302
return libsvn_fetch_full($parent, $paths, $rev,
3303
$author, $date, $msg);
@@ -3367,7 +3306,7 @@ sub libsvn_find_parent_branch {
3306
# included with SVN 1.4.2 (the latest version at the moment),
3307
# so we can't rely on it.
3308
my $ra = libsvn_connect("$url/$branch_from");
3370
- my $ed = SVN::Git::Fetcher->new({c => $parent, q => $_q});
3309
+ my $ed = SVN::Git::Fetcher->new({c => $parent, q => $_q });
3310
my $pool = SVN::Pool->new;
3311
my $reporter = $ra->do_switch($rev, '', 1, $SVN->{url},
3312
$ed, $pool);
@@ -3413,9 +3352,10 @@ sub libsvn_new_tree {
3352
$ut = $ed;
3353
} else {
3354
$ut = { empty => {}, dir_prop => {}, file_prop => {} };
3416
- open my $gui, '| git-update-index -z --index-info' or croak $!;
3355
+ my ($gui, $ctx) = command_input_pipe(qw/update-index
3356
+ -z --index-info/);
3357
libsvn_traverse($gui, '', $SVN->{svn_path}, $rev, undef, $ut);
3418
- close $gui or croak $?;
3358
+ command_close_pipe($gui, $ctx);
3359
}
3360
libsvn_log_entry($rev, $author, $date, $msg, [], $ut);
3361
}
@@ -3487,7 +3427,7 @@ sub libsvn_commit_cb {
3427
my $log = libsvn_log_entry($rev,$committer,$date,$msg);
3428
$log->{tree} = get_tree_from_treeish($c);
3429
my $cmt = git_commit($log, $cmt_last, $c);
3490
- my @diff = safe_qx('git-diff-tree', $cmt, $c);
3430
+ my @diff = command('diff-tree', $cmt, $c);
3431
if (@diff) {
3432
print STDERR "Trees differ: $cmt $c\n",
3433
join('',@diff),"\n";
@@ -3579,8 +3519,8 @@ sub revdb_get {
3519
sub copy_remote_ref {
3520
my $origin = $_cp_remote ? $_cp_remote : 'origin';
3521
my $ref = "refs/remotes/$GIT_SVN";
3582
- if (safe_qx('git-ls-remote', $origin, $ref)) {
3583
- sys(qw/git fetch/, $origin, "$ref:$ref");
3522
+ if (command('ls-remote', $origin, $ref)) {
3523
+ command_noisy('fetch', $origin, "$ref:$ref");
3524
} elsif ($_cp_remote && !$_upgrade) {
3525
die "Unable to find remote reference: ",
3526
"refs/remotes/$GIT_SVN on $origin\n";
@@ -3592,14 +3532,14 @@ use strict;
3532
use warnings;
3533
use Carp qw/croak/;
3534
use IO::File qw//;
3535
+use Git qw/command command_oneline command_noisy
3536
+ command_output_pipe command_input_pipe command_close_pipe/;
3537
3538
# file baton members: path, mode_a, mode_b, pool, fh, blob, base
3539
sub new {
3540
my ($class, $git_svn) = @_;
3541
my $self = SVN::Delta::Editor->new;
3542
bless $self, $class;
3601
- open my $gui, '| git-update-index -z --index-info' or croak $!;
3602
- $self->{gui} = $gui;
3543
$self->{c} = $git_svn->{c} if exists $git_svn->{c};
3544
$self->{q} = $git_svn->{q};
3545
$self->{empty} = {};
@@ -3607,6 +3547,8 @@ sub new {
3547
$self->{file_prop} = {};
3548
$self->{absent_dir} = {};
3549
$self->{absent_file} = {};
3550
+ ($self->{gui}, $self->{ctx}) = command_input_pipe(
3551
+ qw/update-index -z --index-info/);
3552
require Digest::MD5;
3553
$self;
3554
}
@@ -3629,7 +3571,7 @@ sub delete_entry {
3571
3572
sub open_file {
3573
my ($self, $path, $pb, $rev) = @_;
3632
- my ($mode, $blob) = (safe_qx('git-ls-tree',$self->{c},'--',$path)
3574
+ my ($mode, $blob) = (command('ls-tree', $self->{c}, '--',$path)
3575
=~ /^(\d{6}) blob ([a-f\d]{40})\t/);
3576
unless (defined $mode && defined $blob) {
3577
die "$path was not found in commit $self->{c} (r$rev)\n";
@@ -3764,13 +3706,13 @@ sub close_file {
3706
3707
sub abort_edit {
3708
my $self = shift;
3767
- close $self->{gui};
3709
+ eval { command_close_pipe($self->{gui}, $self->{ctx}) };
3710
$self->SUPER::abort_edit(@_);
3711
}
3712
3713
sub close_edit {
3714
my $self = shift;
3773
- close $self->{gui} or croak $!;
3715
+ command_close_pipe($self->{gui}, $self->{ctx});
3716
$self->{git_commit_ok} = 1;
3717
$self->SUPER::close_edit(@_);
3718
}
@@ -3781,6 +3723,8 @@ use strict;
3723
use warnings;
3724
use Carp qw/croak/;
3725
use IO::File;
3726
+use Git qw/command command_oneline command_noisy
3727
+ command_output_pipe command_input_pipe command_close_pipe/;
3728
3729
sub new {
3730
my $class = shift;
@@ -3830,10 +3774,8 @@ sub rmdirs {
3774
delete $rm->{''}; # we never delete the url we're tracking
3775
return unless %$rm;
3776
3833
- defined(my $pid = open my $fh,'-|') or croak $!;
3834
- if (!$pid) {
3835
- exec qw/git-ls-tree --name-only -r -z/, $self->{c} or croak $!;
3836
- }
3777
+ my ($fh, $ctx) = command_output_pipe(
3778
+ qw/ls-tree --name-only -r -z/, $self->{c});
3779
local $/ = "\0";
3780
while (<$fh>) {
3781
chomp;
@@ -3842,11 +3784,11 @@ sub rmdirs {
3784
delete $rm->{join '/', @dn};
3785
}
3786
unless (%$rm) {
3845
- close $fh;
3787
+ eval { command_close_pipe($fh) };
3788
return;
3789
}
3790
}
3849
- close $fh;
3791
+ command_close_pipe($fh, $ctx);
3792
3793
my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
3794
foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {