git-rerere: reuse recorded resolve.
In a workflow that employs relatively long lived topic branches, the developer sometimes needs to resolve the same conflict over and over again until the topic branches are done (either merged to the "release" branch, or sent out and accepted upstream). This commit introduces a new command, "git rerere", to help this process by recording the conflicted automerge results and corresponding hand-resolve results on the initial manual merge, and later by noticing the same conflicted automerge and applying the previously recorded hand resolution using three-way merge. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Jan 28, 2006 at 23:15 UTC
8389b52b2a51d5b110b508cc67f0f41f99c30d3f
8 files changed
+408
-2
Documentation/git-rerere.txt
new
+177
@@ -0,0 +1,177 @@
1
+git-rerere(1)
2
+=============
3
+
4
+NAME
5
+----
6
+git-rerere - Reuse recorded resolve
7
+
8
+SYNOPSIS
9
+--------
10
+'git-rerere'
11
+
12
+
13
+DESCRIPTION
14
+-----------
15
+
16
+In a workflow that employs relatively long lived topic branches,
17
+the developer sometimes needs to resolve the same conflict over
18
+and over again until the topic branches are done (either merged
19
+to the "release" branch, or sent out and accepted upstream).
20
+
21
+This command helps this process by recording conflicted
22
+automerge results and corresponding hand-resolve results on the
23
+initial manual merge, and later by noticing the same automerge
24
+results and applying the previously recorded hand resolution.
25
+
26
+[NOTE]
27
+You need to create `$GIT_DIR/rr-cache` directory to enable this
28
+command.
29
+
30
+DISCUSSION
31
+----------
32
+
33
+When your topic branch modifies overlapping area that your
34
+master branch (or upstream) touched since your topic branch
35
+forked from it, you may want to test it with the latest master,
36
+even before your topic branch is ready to be pushed upstream:
37
+
38
+------------
39
+ o---*---o topic
40
+ /
41
+ o---o---o---*---o---o master
42
+------------
43
+
44
+For such a test, you need to merge master and topic somehow.
45
+One way to do it is to pull master into the topic branch:
46
+
47
+------------
48
+ $ git checkout topic
49
+ $ git pull . master
50
+
51
+ o---*---o---+ topic
52
+ / /
53
+ o---o---o---*---o---o master
54
+------------
55
+
56
+The commits marked with `*` touch the same area in the same
57
+file; you need to resolve the conflicts when creating the commit
58
+marked with `+`. Then you can test the result to make sure your
59
+work-in-progress still works with what is in the latest master.
60
+
61
+After this test merge, there are two ways to continue your work
62
+on the topic. The easiest is to build on top of the test merge
63
+commit `+`, and when your work in the topic branch is finally
64
+ready, pull the topic branch into master, and/or ask the
65
+upstream to pull from you. By that time, however, the master or
66
+the upstream might have been advanced since the test merge `+`,
67
+in which case the final commit graph would look like this:
68
+
69
+------------
70
+ $ git checkout topic
71
+ $ git pull . master
72
+ $ ... work on both topic and master branches
73
+ $ git checkout master
74
+ $ git pull . topic
75
+
76
+ o---*---o---+---o---o topic
77
+ / / \
78
+ o---o---o---*---o---o---o---o---+ master
79
+------------
80
+
81
+When your topic branch is long-lived, however, your topic branch
82
+would end up having many such "Merge from master" commits on it,
83
+which would unnecessarily clutter the development history.
84
+Readers of the Linux kernel mailing list may remember that Linus
85
+complained about such too frequent test merges when a subsystem
86
+maintainer asked to pull from a branch full of "useless merges".
87
+
88
+As an alternative, to keep the topic branch clean of test
89
+merges, you could blow away the test merge, and keep building on
90
+top of the tip before the test merge:
91
+
92
+------------
93
+ $ git checkout topic
94
+ $ git pull . master
95
+ $ git reset --hard HEAD^ ;# rewind the test merge
96
+ $ ... work on both topic and master branches
97
+ $ git checkout master
98
+ $ git pull . topic
99
+
100
+ o---*---o-------o---o topic
101
+ / \
102
+ o---o---o---*---o---o---o---o---+ master
103
+------------
104
+
105
+This would leave only one merge commit when your topic branch is
106
+finally ready and merged into the master branch. This merge
107
+would require you to resolve the conflict, introduced by the
108
+commits marked with `*`. However, often this conflict is the
109
+same conflict you resolved when you created the test merge you
110
+blew away. `git-rerere` command helps you to resolve this final
111
+conflicted merge using the information from your earlier hand
112
+resolve.
113
+
114
+Running `git-rerere` command immediately after a conflicted
115
+automerge records the conflicted working tree files, with the
116
+usual conflict markers `<<<<<<<`, `=======`, and `>>>>>>>` in
117
+them. Later, after you are done resolving the conflicts,
118
+running `git-rerere` again records the resolved state of these
119
+files. Suppose you did this when you created the test merge of
120
+master into the topic branch.
121
+
122
+Next time, running `git-rerere` after seeing a conflicted
123
+automerge, if the conflict is the same as the earlier one
124
+recorded, it is noticed and a three-way merge between the
125
+earlier conflicted automerge, the earlier manual resolution, and
126
+the current conflicted automerge is performed by the command.
127
+If this three-way merge resolves cleanly, the result is written
128
+out to your working tree file, so you would not have to manually
129
+resolve it. Note that `git-rerere` leaves the index file alone,
130
+so you still need to do the final sanity checks with `git diff`
131
+(or `git diff -c`) and `git update-index` when you are
132
+satisfied.
133
+
134
+As a convenience measure, `git-merge` automatically invokes
135
+`git-rerere` when it exits with a failed automerge, which
136
+records it if it is a new conflict, or reuses the earlier hand
137
+resolve when it is not. `git-commit` also invokes `git-rerere`
138
+when recording a merge result. What this means is that you do
139
+not have to do anything special yourself (Note: you still have
140
+to create `$GIT_DIR/rr-cache` directory to enable this command).
141
+
142
+In our example, when you did the test merge, the manual
143
+resolution is recorded, and it will be reused when you do the
144
+actual merge later with updated master and topic branch, as long
145
+as the earlier resolution is still applicable.
146
+
147
+The information `git-rerere` records is also used when running
148
+`git-rebase`. After blowing away the test merge and continuing
149
+development on the topic branch:
150
+
151
+------------
152
+ o---*---o-------o---o topic
153
+ /
154
+ o---o---o---*---o---o---o---o master
155
+
156
+ $ git rebase master topic
157
+
158
+ o---*---o-------o---o topic
159
+ /
160
+ o---o---o---*---o---o---o---o master
161
+------------
162
+
163
+you could run `git rebase master topic`, to keep yourself
164
+up-to-date even before your topic is ready to be sent upstream.
165
+This would result in falling back to three-way merge, and it
166
+would conflict the same way the test merge you resolved earlier.
167
+`git-rerere` is run by `git rebase` to help you resolve this
168
+conflict.
169
+
170
+
171
+Author
172
+------
173
+Written by Junio C Hamano <junkio@cox.net>
174
+
175
+GIT
176
+---
177
+Part of the gitlink:git[7] suite
Documentation/git.txt
+3
@@ -313,6 +313,9 @@ gitlink:git-rebase[1]::
313
gitlink:git-repack[1]::
314
Pack unpacked objects in a repository.
315
316
+gitlink:git-rerere[1]::
317
+ Reuse recorded resolution of conflicted merges.
318
+
319
gitlink:git-reset[1]::
320
Reset current HEAD to the specified state.
321
Makefile
+1
-1
@@ -116,7 +116,7 @@ SCRIPT_SH = \
116
117
SCRIPT_PERL = \
118
git-archimport.perl git-cvsimport.perl git-relink.perl \
119
- git-shortlog.perl git-fmt-merge-msg.perl \
119
+ git-shortlog.perl git-fmt-merge-msg.perl git-rerere.perl \
120
git-svnimport.perl git-mv.perl git-cvsexportcommit.perl
121
122
SCRIPT_PYTHON = \
git-am.sh
+1
@@ -88,6 +88,7 @@ fall_back_3way () {
88
# saying that we reverted all those changes.
89
90
git-merge-resolve $orig_tree -- HEAD $his_tree || {
91
+ git-rerere
92
echo Failed to merge in the changes.
93
exit 1
94
}
git-commit.sh
+1
@@ -245,6 +245,7 @@ else
245
fi
246
ret="$?"
247
rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG"
248
+git-rerere
249
250
if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
251
then
git-merge.sh
+1
@@ -309,5 +309,6 @@ Conflicts:
309
sed -e 's/^[^ ]* / /' |
310
uniq
311
} >>"$GIT_DIR/MERGE_MSG"
312
+ git rerere
313
die "Automatic merge failed; fix up by hand"
314
fi
git-rerere.perl
new
+223
@@ -0,0 +1,223 @@
1
+#!/usr/bin/perl
2
+#
3
+# REuse REcorded REsolve. This tool records a conflicted automerge
4
+# result and its hand resolution, and helps to resolve future
5
+# automerge that results in the same conflict.
6
+#
7
+# To enable this feature, create a directory 'rr-cache' under your
8
+# .git/ directory.
9
+
10
+use Digest;
11
+use File::Path;
12
+use File::Copy;
13
+
14
+my $git_dir = $::ENV{GIT_DIR} || ".git";
15
+my $rr_dir = "$git_dir/rr-cache";
16
+my $merge_rr = "$git_dir/rr-cache/MERGE_RR";
17
+
18
+my %merge_rr = ();
19
+
20
+sub read_rr {
21
+ if (!-f $merge_rr) {
22
+ %merge_rr = ();
23
+ return;
24
+ }
25
+ my $in;
26
+ local $/ = "\0";
27
+ open $in, "<$merge_rr" or die "$!: $merge_rr";
28
+ while (<$in>) {
29
+ chomp;
30
+ my ($name, $path) = /^([0-9a-f]{40})\t(.*)$/s;
31
+ $merge_rr{$path} = $name;
32
+ }
33
+ close $in;
34
+}
35
+
36
+sub write_rr {
37
+ my $out;
38
+ open $out, ">$merge_rr" or die "$!: $merge_rr";
39
+ for my $path (sort keys %merge_rr) {
40
+ my $name = $merge_rr{$path};
41
+ print $out "$name\t$path\0";
42
+ }
43
+ close $out;
44
+}
45
+
46
+sub compute_conflict_name {
47
+ my ($path) = @_;
48
+ my @side = ();
49
+ my $in;
50
+ open $in, "<$path" or die "$!: $path";
51
+
52
+ my $sha1 = Digest->new("SHA-1");
53
+ my $hunk = 0;
54
+ while (<$in>) {
55
+ if (/^<<<<<<< .*/) {
56
+ $hunk++;
57
+ @side = ([], undef);
58
+ }
59
+ elsif (/^=======$/) {
60
+ $side[1] = [];
61
+ }
62
+ elsif (/^>>>>>>> .*/) {
63
+ my ($one, $two);
64
+ $one = join('', @{$side[0]});
65
+ $two = join('', @{$side[1]});
66
+ if ($two le $one) {
67
+ ($one, $two) = ($two, $one);
68
+ }
69
+ $sha1->add($one);
70
+ $sha1->add("\0");
71
+ $sha1->add($two);
72
+ $sha1->add("\0");
73
+ @side = ();
74
+ }
75
+ elsif (@side == 0) {
76
+ next;
77
+ }
78
+ elsif (defined $side[1]) {
79
+ push @{$side[1]}, $_;
80
+ }
81
+ else {
82
+ push @{$side[0]}, $_;
83
+ }
84
+ }
85
+ close $in;
86
+ return ($sha1->hexdigest, $hunk);
87
+}
88
+
89
+sub record_preimage {
90
+ my ($path, $name) = @_;
91
+ my @side = ();
92
+ my ($in, $out);
93
+ open $in, "<$path" or die "$!: $path";
94
+ open $out, ">$name" or die "$!: $name";
95
+
96
+ while (<$in>) {
97
+ if (/^<<<<<<< .*/) {
98
+ @side = ([], undef);
99
+ }
100
+ elsif (/^=======$/) {
101
+ $side[1] = [];
102
+ }
103
+ elsif (/^>>>>>>> .*/) {
104
+ my ($one, $two);
105
+ $one = join('', @{$side[0]});
106
+ $two = join('', @{$side[1]});
107
+ if ($two le $one) {
108
+ ($one, $two) = ($two, $one);
109
+ }
110
+ print $out "<<<<<<<\n";
111
+ print $out $one;
112
+ print $out "=======\n";
113
+ print $out $two;
114
+ print $out ">>>>>>>\n";
115
+ @side = ();
116
+ }
117
+ elsif (@side == 0) {
118
+ print $out $_;
119
+ }
120
+ elsif (defined $side[1]) {
121
+ push @{$side[1]}, $_;
122
+ }
123
+ else {
124
+ push @{$side[0]}, $_;
125
+ }
126
+ }
127
+ close $out;
128
+ close $in;
129
+}
130
+
131
+sub find_conflict {
132
+ my $in;
133
+ local $/ = "\0";
134
+ open $in, '-|', qw(git ls-files -z -u) or die "$!: ls-files";
135
+ my %path = ();
136
+ my @path = ();
137
+ while (<$in>) {
138
+ chomp;
139
+ my ($mode, $sha1, $stage, $path) =
140
+ /^([0-7]+) ([0-9a-f]{40}) ([123])\t(.*)$/s;
141
+ $path{$path} |= (1 << $stage);
142
+ }
143
+ close $in;
144
+ while (my ($path, $status) = each %path) {
145
+ if ($status == 14) { push @path, $path; }
146
+ }
147
+ return @path;
148
+}
149
+
150
+sub merge {
151
+ my ($name, $path) = @_;
152
+ record_preimage($path, "$rr_dir/$name/thisimage");
153
+ unless (system('merge', map { "$rr_dir/$name/${_}image" }
154
+ qw(this pre post))) {
155
+ my $in;
156
+ open $in, "<$rr_dir/$name/thisimage" or
157
+ die "$!: $name/thisimage";
158
+ my $out;
159
+ open $out, ">$path" or die "$!: $path";
160
+ while (<$in>) { print $out $_; }
161
+ close $in;
162
+ close $out;
163
+ return 1;
164
+ }
165
+ return 0;
166
+}
167
+
168
+-d "$rr_dir" || exit(0);
169
+
170
+read_rr();
171
+my %conflict = map { $_ => 1 } find_conflict();
172
+
173
+# MERGE_RR records paths with conflicts immediately after merge
174
+# failed. Some of the conflicted paths might have been hand resolved
175
+# in the working tree since then, but the initial run would catch all
176
+# and register their preimages.
177
+
178
+for my $path (keys %conflict) {
179
+ # This path has conflict. If it is not recorded yet,
180
+ # record the pre-image.
181
+ if (!exists $merge_rr{$path}) {
182
+ my ($name, $hunk) = compute_conflict_name($path);
183
+ next unless ($hunk);
184
+ $merge_rr{$path} = $name;
185
+ if (! -d "$rr_dir/$name") {
186
+ mkpath("$rr_dir/$name", 0, 0777);
187
+ print STDERR "Recorded preimage for '$path'\n";
188
+ record_preimage($path, "$rr_dir/$name/preimage");
189
+ }
190
+ }
191
+}
192
+
193
+# Now some of the paths that had conflicts earlier might have been
194
+# hand resolved. Others may be similar to a conflict already that
195
+# was resolved before.
196
+
197
+for my $path (keys %merge_rr) {
198
+ my $name = $merge_rr{$path};
199
+
200
+ # We could resolve this automatically if we have images.
201
+ if (-f "$rr_dir/$name/preimage" &&
202
+ -f "$rr_dir/$name/postimage") {
203
+ if (merge($name, $path)) {
204
+ print STDERR "Resolved '$path' using previous resolution.\n";
205
+ # Then we do not have to worry about this path
206
+ # anymore.
207
+ delete $merge_rr{$path};
208
+ next;
209
+ }
210
+ }
211
+
212
+ # Let's see if we have resolved it.
213
+ (undef, my $hunk) = compute_conflict_name($path);
214
+ next if ($hunk);
215
+
216
+ print STDERR "Recorded resolution for '$path'.\n";
217
+ copy($path, "$rr_dir/$name/postimage");
218
+ # And we do not have to worry about this path anymore.
219
+ delete $merge_rr{$path};
220
+}
221
+
222
+# Write out the rest.
223
+write_rr();
git-reset.sh
+1
-1
@@ -100,4 +100,4 @@ case "$reset_type" in
100
;;
101
esac
102
103
-rm -f "$GIT_DIR/MERGE_HEAD"
103
+rm -f "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/rr-cache/MERGE_RR"