git-svn: fix dcommit losing changes when out-of-date from svn
There was a bug in dcommit (and commit-diff) which caused deltas to be generated against the latest version of the changed file in a repository, and not the revision we are diffing (the tree) against locally. This bug can cause recent changes to the svn repository to be silently clobbered by git-svn if our repository is out-of-date. Thanks to Steven Grimm for noticing the bug. The (few) people using the commit-diff command are now required to use the -r/--revision argument. dcommit usage is unchanged. Signed-off-by: Eric Wong <normalperson@yhbt.net> Signed-off-by: Junio C Hamano <junkio@cox.net>
Eric Wong committed
Nov 9, 2006 at 01:19 UTC
45bf473a7bc2c40c8aea3d34a0eab7a41e77a8ff
4 files changed
+102
-4
Documentation/git-svn.txt
+1
@@ -120,6 +120,7 @@ manually joining branches on commit.
120
URL of the target Subversion repository. The final argument
121
(URL) may be omitted if you are working from a git-svn-aware
122
repository (that has been init-ed with git-svn).
123
+ The -r<revision> option is required for this.
124
125
'graft-branches'::
126
This command attempts to detect merges/branches from already
git-svn.perl
+26
-3
@@ -134,6 +134,7 @@ my %cmd = (
134
'commit-diff' => [ \&commit_diff, 'Commit a diff between two trees',
135
{ 'message|m=s' => \$_message,
136
'file|F=s' => \$_file,
137
+ 'revision|r=s' => \$_revision,
138
%cmt_opts } ],
139
dcommit => [ \&dcommit, 'Commit several diffs to merge with upstream',
140
{ 'merge|m|M' => \$_merge,
@@ -586,11 +587,21 @@ sub commit_lib {
587
sub dcommit {
588
my $gs = "refs/remotes/$GIT_SVN";
589
chomp(my @refs = safe_qx(qw/git-rev-list --no-merges/, "$gs..HEAD"));
590
+ my $last_rev;
591
foreach my $d (reverse @refs) {
592
+ unless (defined $last_rev) {
593
+ (undef, $last_rev, undef) = cmt_metadata("$d~1");
594
+ unless (defined $last_rev) {
595
+ die "Unable to extract revision information ",
596
+ "from commit $d~1\n";
597
+ }
598
+ }
599
if ($_dry_run) {
600
print "diff-tree $d~1 $d\n";
601
} else {
593
- commit_diff("$d~1", $d);
602
+ if (my $r = commit_diff("$d~1", $d, undef, $last_rev)) {
603
+ $last_rev = $r;
604
+ } # else: no changes, same $last_rev
605
}
606
}
607
return if $_dry_run;
@@ -814,6 +825,8 @@ sub commit_diff {
825
print STDERR "Needed URL or usable git-svn id command-line\n";
826
commit_diff_usage();
827
}
828
+ my $r = shift || $_revision;
829
+ die "-r|--revision is a required argument\n" unless (defined $r);
830
if (defined $_message && defined $_file) {
831
print STDERR "Both --message/-m and --file/-F specified ",
832
"for the commit message.\n",
@@ -830,13 +843,22 @@ sub commit_diff {
843
($repo, $SVN_PATH) = repo_path_split($SVN_URL);
844
$SVN_LOG ||= libsvn_connect($repo);
845
$SVN ||= libsvn_connect($repo);
846
+ if ($r eq 'HEAD') {
847
+ $r = $SVN->get_latest_revnum;
848
+ } elsif ($r !~ /^\d+$/) {
849
+ die "revision argument: $r not understood by git-svn\n";
850
+ }
851
my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
834
- my $ed = SVN::Git::Editor->new({ r => $SVN->get_latest_revnum,
852
+ my $rev_committed;
853
+ my $ed = SVN::Git::Editor->new({ r => $r,
854
ra => $SVN_LOG, c => $tb,
855
svn_path => $SVN_PATH
856
},
857
$SVN->get_commit_editor($_message,
839
- sub {print "Committed $_[0]\n"},@lock)
858
+ sub {
859
+ $rev_committed = $_[0];
860
+ print "Committed $_[0]\n";
861
+ }, @lock)
862
);
863
my $mods = libsvn_checkout_tree($ta, $tb, $ed);
864
if (@$mods == 0) {
@@ -846,6 +868,7 @@ sub commit_diff {
868
$ed->close_edit;
869
}
870
$_message = $_file = undef;
871
+ return $rev_committed;
872
}
873
874
########################### utility functions #########################
t/t9105-git-svn-commit-diff.sh
+1
-1
@@ -33,7 +33,7 @@ prev=`git rev-parse --verify HEAD^1`
33
34
test_expect_success 'test the commit-diff command' "
35
test -n '$prev' && test -n '$head' &&
36
- git-svn commit-diff '$prev' '$head' '$svnrepo' &&
36
+ git-svn commit-diff -r1 '$prev' '$head' '$svnrepo' &&
37
svn co $svnrepo wc &&
38
cmp readme wc/readme
39
"
t/t9106-git-svn-commit-diff-clobber.sh
new
+74
@@ -0,0 +1,74 @@
1
+#!/bin/sh
2
+#
3
+# Copyright (c) 2006 Eric Wong
4
+test_description='git-svn commit-diff clobber'
5
+. ./lib-git-svn.sh
6
+
7
+if test -n "$GIT_SVN_NO_LIB" && test "$GIT_SVN_NO_LIB" -ne 0
8
+then
9
+ echo 'Skipping: commit-diff clobber needs SVN libraries'
10
+ test_done
11
+ exit 0
12
+fi
13
+
14
+test_expect_success 'initialize repo' "
15
+ mkdir import &&
16
+ cd import &&
17
+ echo initial > file &&
18
+ svn import -m 'initial' . $svnrepo &&
19
+ cd .. &&
20
+ echo initial > file &&
21
+ git update-index --add file &&
22
+ git commit -a -m 'initial'
23
+ "
24
+test_expect_success 'commit change from svn side' "
25
+ svn co $svnrepo t.svn &&
26
+ cd t.svn &&
27
+ echo second line from svn >> file &&
28
+ svn commit -m 'second line from svn' &&
29
+ cd .. &&
30
+ rm -rf t.svn
31
+ "
32
+
33
+test_expect_failure 'commit conflicting change from git' "
34
+ echo second line from git >> file &&
35
+ git commit -a -m 'second line from git' &&
36
+ git-svn commit-diff -r1 HEAD~1 HEAD $svnrepo
37
+ " || true
38
+
39
+test_expect_success 'commit complementing change from git' "
40
+ git reset --hard HEAD~1 &&
41
+ echo second line from svn >> file &&
42
+ git commit -a -m 'second line from svn' &&
43
+ echo third line from git >> file &&
44
+ git commit -a -m 'third line from git' &&
45
+ git-svn commit-diff -r2 HEAD~1 HEAD $svnrepo
46
+ "
47
+
48
+test_expect_failure 'dcommit fails to commit because of conflict' "
49
+ git-svn init $svnrepo &&
50
+ git-svn fetch &&
51
+ git reset --hard refs/remotes/git-svn &&
52
+ svn co $svnrepo t.svn &&
53
+ cd t.svn &&
54
+ echo fourth line from svn >> file &&
55
+ svn commit -m 'fourth line from svn' &&
56
+ cd .. &&
57
+ rm -rf t.svn &&
58
+ echo 'fourth line from git' >> file &&
59
+ git commit -a -m 'fourth line from git' &&
60
+ git-svn dcommit
61
+ " || true
62
+
63
+test_expect_success 'dcommit does the svn equivalent of an index merge' "
64
+ git reset --hard refs/remotes/git-svn &&
65
+ echo 'index merge' > file2 &&
66
+ git update-index --add file2 &&
67
+ git commit -a -m 'index merge' &&
68
+ echo 'more changes' >> file2 &&
69
+ git update-index file2 &&
70
+ git commit -a -m 'more changes' &&
71
+ git-svn dcommit
72
+ "
73
+
74
+test_done