git-svn: switch dcommit to using Git::SVN code
Signed-off-by: Eric Wong <normalperson@yhbt.net>
Eric Wong committed
Jan 14, 2007 at 03:14 UTC
d7ad3bed8cfbaf21aeaaff2cd10e3696d8785b78
1 file changed
+38
-84
git-svn.perl
+38
-84
@@ -107,7 +107,8 @@ my %cmd = (
107
init => [ \&cmd_init, "Initialize a repo for tracking" .
108
" (requires URL argument)",
109
\%init_opts ],
110
- dcommit => [ \&dcommit, 'Commit several diffs to merge with upstream',
110
+ dcommit => [ \&cmd_dcommit,
111
+ 'Commit several diffs to merge with upstream',
112
{ 'merge|m|M' => \$_merge,
113
'strategy|s=s' => \$_strategy,
114
'dry-run|n' => \$_dry_run,
@@ -482,49 +483,65 @@ sub commit_lib {
483
unlink $commit_msg;
484
}
485
485
-sub dcommit {
486
- my $head = shift || 'HEAD';
487
- my $gs = "refs/remotes/$GIT_SVN";
488
- my @refs = command(qw/rev-list --no-merges/, "$gs..$head");
486
+sub cmd_dcommit {
487
+ my $head = shift;
488
+ my $gs = Git::SVN->new;
489
+ $head ||= 'HEAD';
490
+ my @refs = command(qw/rev-list --no-merges/, $gs->refname."..$head");
491
my $last_rev;
492
foreach my $d (reverse @refs) {
493
if (!verify_ref("$d~1")) {
492
- die "Commit $d\n",
493
- "has no parent commit, and therefore ",
494
- "nothing to diff against.\n",
495
- "You should be working from a repository ",
496
- "originally created by git-svn\n";
494
+ fatal "Commit $d\n",
495
+ "has no parent commit, and therefore ",
496
+ "nothing to diff against.\n",
497
+ "You should be working from a repository ",
498
+ "originally created by git-svn\n";
499
}
500
unless (defined $last_rev) {
501
(undef, $last_rev, undef) = cmt_metadata("$d~1");
502
unless (defined $last_rev) {
501
- die "Unable to extract revision information ",
502
- "from commit $d~1\n";
503
+ fatal "Unable to extract revision information ",
504
+ "from commit $d~1\n";
505
}
506
}
507
if ($_dry_run) {
508
print "diff-tree $d~1 $d\n";
509
} else {
508
- if (my $r = commit_diff("$d~1", $d, undef, $last_rev)) {
509
- $last_rev = $r;
510
- } # else: no changes, same $last_rev
510
+ my $ra = $gs->ra;
511
+ my $pool = SVN::Pool->new;
512
+ my %ed_opts = ( r => $last_rev,
513
+ ra => $ra->dup,
514
+ svn_path => $ra->{svn_path} );
515
+ my $ed = SVN::Git::Editor->new(\%ed_opts,
516
+ $ra->get_commit_editor($::_message,
517
+ sub { print "Committed r$_[0]\n";
518
+ $last_rev = $_[0]; }),
519
+ $pool);
520
+ my $mods = $ed->apply_diff("$d~1", $d);
521
+ if (@$mods == 0) {
522
+ print "No changes\n$d~1 == $d\n";
523
+ }
524
}
525
}
526
return if $_dry_run;
514
- fetch();
515
- my @diff = command('diff-tree', 'HEAD', $gs, '--');
527
+ $gs->fetch;
528
+ # we always want to rebase against the current HEAD, not any
529
+ # head that was passed to us
530
+ my @diff = command('diff-tree', 'HEAD', $gs->refname, '--');
531
my @finish;
532
if (@diff) {
533
@finish = qw/rebase/;
534
push @finish, qw/--merge/ if $_merge;
535
push @finish, "--strategy=$_strategy" if $_strategy;
521
- print STDERR "W: HEAD and $gs differ, using @finish:\n", @diff;
536
+ print STDERR "W: HEAD and ", $gs->refname, " differ, ",
537
+ "using @finish:\n", "@diff";
538
} else {
523
- print "No changes between current HEAD and $gs\n",
524
- "Resetting to the latest $gs\n";
539
+ print "No changes between current HEAD and ",
540
+ $gs->refname, "\nResetting to the latest ",
541
+ $gs->refname, "\n";
542
@finish = qw/reset --mixed/;
543
}
527
- command_noisy(@finish, $gs);
544
+ command_noisy(@finish, $gs->refname);
545
}
546
547
sub cmd_show_ignore {
@@ -647,69 +664,6 @@ sub cmd_commit_diff {
664
$pool->clear;
665
}
666
650
-sub commit_diff_usage {
651
- print STDERR "Usage: $0 commit-diff <tree-ish> <tree-ish> [<URL>]\n";
652
- exit 1
653
-}
654
-
655
-sub commit_diff {
656
- my $ta = shift or commit_diff_usage();
657
- my $tb = shift or commit_diff_usage();
658
- if (!eval { $SVN_URL = shift || file_to_s("$GIT_SVN_DIR/info/url") }) {
659
- print STDERR "Needed URL or usable git-svn id command-line\n";
660
- commit_diff_usage();
661
- }
662
- my $r = shift;
663
- unless (defined $r) {
664
- if (defined $_revision) {
665
- $r = $_revision
666
- } else {
667
- die "-r|--revision is a required argument\n";
668
- }
669
- }
670
- if (defined $_message && defined $_file) {
671
- print STDERR "Both --message/-m and --file/-F specified ",
672
- "for the commit message.\n",
673
- "I have no idea what you mean\n";
674
- exit 1;
675
- }
676
- if (defined $_file) {
677
- $_message = file_to_s($_file);
678
- } else {
679
- $_message ||= get_commit_entry($tb,
680
- "$GIT_DIR/.svn-commit.tmp.$$")->{log};
681
- }
682
- $SVN ||= Git::SVN::Ra->new($SVN_URL);
683
- if ($r eq 'HEAD') {
684
- $r = $SVN->get_latest_revnum;
685
- } elsif ($r !~ /^\d+$/) {
686
- die "revision argument: $r not understood by git-svn\n";
687
- }
688
- my $rev_committed;
689
- my $pool = SVN::Pool->new;
690
- my $ed = SVN::Git::Editor->new({ r => $r,
691
- ra => $SVN->dup,
692
- svn_path => $SVN->{svn_path}
693
- },
694
- $SVN->get_commit_editor($_message,
695
- sub {
696
- $rev_committed = $_[0];
697
- print "Committed $_[0]\n";
698
- },
699
- $pool)
700
- );
701
- eval {
702
- my $mods = $ed->apply_diff($ta, $tb);
703
- if (@$mods == 0) {
704
- print "No changes\n$ta == $tb\n";
705
- }
706
- };
707
- $pool->clear;
708
- fatal "$@\n" if $@;
709
- $_message = $_file = undef;
710
- return $rev_committed;
711
-}
712
-
667
########################### utility functions #########################
668
669
sub rec_fetch {