@matheusbsbs / matheus-bagliano / commits / 809f38c8ab
Fork 0

git notes merge: Manual conflict resolution, part 1/2

Conflicts (that are to be resolved manually) are written into a special- purpose working tree, located at .git/NOTES_MERGE_WORKTREE. Within this directory, conflicting notes entries are stored (with conflict markers produced by ll_merge()) using the SHA1 of the annotated object. The .git/NOTES_MERGE_WORKTREE directory will only contain the _conflicting_ note entries. The non-conflicting note entries (aka. the partial merge result) are stored in 'local_tree', and the SHA1 of the resulting commit is written to 'result_sha1'. The return value from notes_merge() is -1. The user is told to edit the files within the .git/NOTES_MERGE_WORKTREE directory in order to resolve the conflicts. The patch also contains documentation and testcases for the correct setup of .git/NOTES_MERGE_WORKTREE. The next part will recombine the partial notes merge result with the resolved conflicts in .git/NOTES_MERGE_WORKTREE to produce the complete merge result. This patch has been improved by the following contributions: - Jonathan Nieder: Use trace_printf(...) instead of OUTPUT(o, 5, ...) Thanks-to: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johan Herland committed Nov 9, 2010 at 22:49 UTC 809f38c8abacdbeb7015fdeef03931568c7fddda
5 files changed +474 -13
Documentation/git-notes.txt
+7 -3
@@ -92,7 +92,9 @@ merge::
92 +
93 If conflicts arise and a strategy for automatically resolving
94 conflicting notes (see the -s/--strategy option) is not given,
95 -the merge fails (TODO).
95 +the "manual" resolver is used. This resolver checks out the
96 +conflicting notes in a special worktree (`.git/NOTES_MERGE_WORKTREE`),
97 +and instructs the user to manually resolve the conflicts there.
98
99 remove::
100 Remove the notes for a given object (defaults to HEAD).
@@ -191,8 +193,10 @@ object, in which case the history of the notes can be read with
193 NOTES MERGE STRATEGIES
194 ----------------------
195
194 -The default notes merge strategy is "manual", which is not yet
195 -implemented (TODO).
196 +The default notes merge strategy is "manual", which checks out
197 +conflicting notes in a special work tree for resolving notes conflicts
198 +(`.git/NOTES_MERGE_WORKTREE`), and instructs the user to resolve the
199 +conflicts in that work tree.
200
201 "ours" automatically resolves conflicting notes in favor of the local
202 version (i.e. the current notes ref).
builtin/notes.c
+4 -4
@@ -820,14 +820,14 @@ static int merge(int argc, const char **argv, const char *prefix)
820 /* Update default notes ref with new commit */
821 update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
822 0, DIE_ON_ERR);
823 - else
824 - /* TODO: */
825 - die("'git notes merge' cannot yet handle conflicts!");
823 + else /* Merge has unresolved conflicts */
824 + printf("Automatic notes merge failed. Fix conflicts in %s.\n",
825 + git_path(NOTES_MERGE_WORKTREE));
826
827 free_notes(t);
828 strbuf_release(&remote_ref);
829 strbuf_release(&msg);
830 - return 0;
830 + return result < 0; /* return non-zero on conflicts */
831 }
832
833 static int remove_cmd(int argc, const char **argv, const char *prefix)
notes-merge.c
+161 -5
@@ -3,6 +3,9 @@
3 #include "refs.h"
4 #include "diff.h"
5 #include "diffcore.h"
6 +#include "xdiff-interface.h"
7 +#include "ll-merge.h"
8 +#include "dir.h"
9 #include "notes.h"
10 #include "notes-merge.h"
11
@@ -263,16 +266,169 @@ static void diff_tree_local(struct notes_merge_options *o,
266 diff_tree_release_paths(&opt);
267 }
268
269 +static void check_notes_merge_worktree(struct notes_merge_options *o)
270 +{
271 + if (!o->has_worktree) {
272 + /*
273 + * Must establish NOTES_MERGE_WORKTREE.
274 + * Abort if NOTES_MERGE_WORKTREE already exists
275 + */
276 + if (file_exists(git_path(NOTES_MERGE_WORKTREE))) {
277 + if (advice_resolve_conflict)
278 + die("You have not concluded your previous "
279 + "notes merge (%s exists).\nPlease, use "
280 + "'git notes merge --commit' or 'git notes "
281 + "merge --reset' to commit/abort the "
282 + "previous merge before you start a new "
283 + "notes merge.", git_path("NOTES_MERGE_*"));
284 + else
285 + die("You have not concluded your notes merge "
286 + "(%s exists).", git_path("NOTES_MERGE_*"));
287 + }
288 +
289 + if (safe_create_leading_directories(git_path(
290 + NOTES_MERGE_WORKTREE "/.test")))
291 + die_errno("unable to create directory %s",
292 + git_path(NOTES_MERGE_WORKTREE));
293 + o->has_worktree = 1;
294 + } else if (!file_exists(git_path(NOTES_MERGE_WORKTREE)))
295 + /* NOTES_MERGE_WORKTREE should already be established */
296 + die("missing '%s'. This should not happen",
297 + git_path(NOTES_MERGE_WORKTREE));
298 +}
299 +
300 +static void write_buf_to_worktree(const unsigned char *obj,
301 + const char *buf, unsigned long size)
302 +{
303 + int fd;
304 + char *path = git_path(NOTES_MERGE_WORKTREE "/%s", sha1_to_hex(obj));
305 + if (safe_create_leading_directories(path))
306 + die_errno("unable to create directory for '%s'", path);
307 + if (file_exists(path))
308 + die("found existing file at '%s'", path);
309 +
310 + fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0666);
311 + if (fd < 0)
312 + die_errno("failed to open '%s'", path);
313 +
314 + while (size > 0) {
315 + long ret = write_in_full(fd, buf, size);
316 + if (ret < 0) {
317 + /* Ignore epipe */
318 + if (errno == EPIPE)
319 + break;
320 + die_errno("notes-merge");
321 + } else if (!ret) {
322 + die("notes-merge: disk full?");
323 + }
324 + size -= ret;
325 + buf += ret;
326 + }
327 +
328 + close(fd);
329 +}
330 +
331 +static void write_note_to_worktree(const unsigned char *obj,
332 + const unsigned char *note)
333 +{
334 + enum object_type type;
335 + unsigned long size;
336 + void *buf = read_sha1_file(note, &type, &size);
337 +
338 + if (!buf)
339 + die("cannot read note %s for object %s",
340 + sha1_to_hex(note), sha1_to_hex(obj));
341 + if (type != OBJ_BLOB)
342 + die("blob expected in note %s for object %s",
343 + sha1_to_hex(note), sha1_to_hex(obj));
344 + write_buf_to_worktree(obj, buf, size);
345 + free(buf);
346 +}
347 +
348 +static int ll_merge_in_worktree(struct notes_merge_options *o,
349 + struct notes_merge_pair *p)
350 +{
351 + mmbuffer_t result_buf;
352 + mmfile_t base, local, remote;
353 + int status;
354 +
355 + read_mmblob(&base, p->base);
356 + read_mmblob(&local, p->local);
357 + read_mmblob(&remote, p->remote);
358 +
359 + status = ll_merge(&result_buf, sha1_to_hex(p->obj), &base, NULL,
360 + &local, o->local_ref, &remote, o->remote_ref, 0);
361 +
362 + free(base.ptr);
363 + free(local.ptr);
364 + free(remote.ptr);
365 +
366 + if ((status < 0) || !result_buf.ptr)
367 + die("Failed to execute internal merge");
368 +
369 + write_buf_to_worktree(p->obj, result_buf.ptr, result_buf.size);
370 + free(result_buf.ptr);
371 +
372 + return status;
373 +}
374 +
375 +static int merge_one_change_manual(struct notes_merge_options *o,
376 + struct notes_merge_pair *p,
377 + struct notes_tree *t)
378 +{
379 + const char *lref = o->local_ref ? o->local_ref : "local version";
380 + const char *rref = o->remote_ref ? o->remote_ref : "remote version";
381 +
382 + trace_printf("\t\t\tmerge_one_change_manual(obj = %.7s, base = %.7s, "
383 + "local = %.7s, remote = %.7s)\n",
384 + sha1_to_hex(p->obj), sha1_to_hex(p->base),
385 + sha1_to_hex(p->local), sha1_to_hex(p->remote));
386 +
387 + OUTPUT(o, 2, "Auto-merging notes for %s", sha1_to_hex(p->obj));
388 + check_notes_merge_worktree(o);
389 + if (is_null_sha1(p->local)) {
390 + /* D/F conflict, checkout p->remote */
391 + assert(!is_null_sha1(p->remote));
392 + OUTPUT(o, 1, "CONFLICT (delete/modify): Notes for object %s "
393 + "deleted in %s and modified in %s. Version from %s "
394 + "left in tree.", sha1_to_hex(p->obj), lref, rref, rref);
395 + write_note_to_worktree(p->obj, p->remote);
396 + } else if (is_null_sha1(p->remote)) {
397 + /* D/F conflict, checkout p->local */
398 + assert(!is_null_sha1(p->local));
399 + OUTPUT(o, 1, "CONFLICT (delete/modify): Notes for object %s "
400 + "deleted in %s and modified in %s. Version from %s "
401 + "left in tree.", sha1_to_hex(p->obj), rref, lref, lref);
402 + write_note_to_worktree(p->obj, p->local);
403 + } else {
404 + /* "regular" conflict, checkout result of ll_merge() */
405 + const char *reason = "content";
406 + if (is_null_sha1(p->base))
407 + reason = "add/add";
408 + assert(!is_null_sha1(p->local));
409 + assert(!is_null_sha1(p->remote));
410 + OUTPUT(o, 1, "CONFLICT (%s): Merge conflict in notes for "
411 + "object %s", reason, sha1_to_hex(p->obj));
412 + ll_merge_in_worktree(o, p);
413 + }
414 +
415 + trace_printf("\t\t\tremoving from partial merge result\n");
416 + remove_note(t, p->obj);
417 +
418 + return 1;
419 +}
420 +
421 static int merge_one_change(struct notes_merge_options *o,
422 struct notes_merge_pair *p, struct notes_tree *t)
423 {
424 /*
270 - * Return 0 if change was resolved (and added to notes_tree),
271 - * 1 if conflict
425 + * Return 0 if change is successfully resolved (stored in notes_tree).
426 + * Return 1 is change results in a conflict (NOT stored in notes_tree,
427 + * but instead written to NOTES_MERGE_WORKTREE with conflict markers).
428 */
429 switch (o->strategy) {
430 case NOTES_MERGE_RESOLVE_MANUAL:
275 - return 1;
431 + return merge_one_change_manual(o, p, t);
432 case NOTES_MERGE_RESOLVE_OURS:
433 OUTPUT(o, 2, "Using local notes for %s", sha1_to_hex(p->obj));
434 /* nothing to do */
@@ -479,8 +635,8 @@ int notes_merge(struct notes_merge_options *o,
635 result = merge_from_diffs(o, base_tree_sha1, local->tree->object.sha1,
636 remote->tree->object.sha1, local_tree);
637
482 - if (result > 0) { /* successful non-trivial merge */
483 - /* Commit result */
638 + if (result != 0) { /* non-trivial merge (with or without conflicts) */
639 + /* Commit (partial) result */
640 struct commit_list *parents = NULL;
641 commit_list_insert(remote, &parents); /* LIFO order */
642 commit_list_insert(local, &parents);
notes-merge.h
+10 -1
@@ -1,6 +1,8 @@
1 #ifndef NOTES_MERGE_H
2 #define NOTES_MERGE_H
3
4 +#define NOTES_MERGE_WORKTREE "NOTES_MERGE_WORKTREE"
5 +
6 enum notes_merge_verbosity {
7 NOTES_MERGE_VERBOSITY_DEFAULT = 2,
8 NOTES_MERGE_VERBOSITY_MAX = 5
@@ -17,6 +19,7 @@ struct notes_merge_options {
19 NOTES_MERGE_RESOLVE_THEIRS,
20 NOTES_MERGE_RESOLVE_UNION
21 } strategy;
22 + unsigned has_worktree:1;
23 };
24
25 void init_notes_merge_options(struct notes_merge_options *o);
@@ -51,7 +54,13 @@ void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
54 * 2. The merge successfully completes, producing a merge commit. local_tree
55 * contains the updated notes tree, the SHA1 of the resulting commit is
56 * written into 'result_sha1', and 1 is returned.
54 - * 3. The merge fails. result_sha1 is set to null_sha1, and -1 is returned.
57 + * 3. The merge results in conflicts. This is similar to #2 in that the
58 + * partial merge result (i.e. merge result minus the unmerged entries)
59 + * are stored in 'local_tree', and the SHA1 or the resulting commit
60 + * (to be amended when the conflicts have been resolved) is written into
61 + * 'result_sha1'. The unmerged entries are written into the
62 + * .git/NOTES_MERGE_WORKTREE directory with conflict markers.
63 + * -1 is returned.
64 *
65 * Both o->local_ref and o->remote_ref must be given (non-NULL), but either ref
66 * (although not both) may refer to a non-existing notes ref, in which case
t/t3310-notes-merge-manual-resolve.sh new
+292
@@ -0,0 +1,292 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2010 Johan Herland
4 +#
5 +
6 +test_description='Test notes merging with manual conflict resolution'
7 +
8 +. ./test-lib.sh
9 +
10 +# Set up a notes merge scenario with different kinds of conflicts
11 +test_expect_success 'setup commits' '
12 + test_commit 1st &&
13 + test_commit 2nd &&
14 + test_commit 3rd &&
15 + test_commit 4th &&
16 + test_commit 5th
17 +'
18 +
19 +commit_sha1=$(git rev-parse 1st^{commit})
20 +commit_sha2=$(git rev-parse 2nd^{commit})
21 +commit_sha3=$(git rev-parse 3rd^{commit})
22 +commit_sha4=$(git rev-parse 4th^{commit})
23 +commit_sha5=$(git rev-parse 5th^{commit})
24 +
25 +verify_notes () {
26 + notes_ref="$1"
27 + git -c core.notesRef="refs/notes/$notes_ref" notes |
28 + sort >"output_notes_$notes_ref" &&
29 + test_cmp "expect_notes_$notes_ref" "output_notes_$notes_ref" &&
30 + git -c core.notesRef="refs/notes/$notes_ref" log --format="%H %s%n%N" \
31 + >"output_log_$notes_ref" &&
32 + test_cmp "expect_log_$notes_ref" "output_log_$notes_ref"
33 +}
34 +
35 +cat <<EOF | sort >expect_notes_x
36 +6e8e3febca3c2bb896704335cc4d0c34cb2f8715 $commit_sha4
37 +e5388c10860456ee60673025345fe2e153eb8cf8 $commit_sha3
38 +ceefa674873670e7ecd131814d909723cce2b669 $commit_sha2
39 +EOF
40 +
41 +cat >expect_log_x <<EOF
42 +$commit_sha5 5th
43 +
44 +$commit_sha4 4th
45 +x notes on 4th commit
46 +
47 +$commit_sha3 3rd
48 +x notes on 3rd commit
49 +
50 +$commit_sha2 2nd
51 +x notes on 2nd commit
52 +
53 +$commit_sha1 1st
54 +
55 +EOF
56 +
57 +test_expect_success 'setup merge base (x)' '
58 + git config core.notesRef refs/notes/x &&
59 + git notes add -m "x notes on 2nd commit" 2nd &&
60 + git notes add -m "x notes on 3rd commit" 3rd &&
61 + git notes add -m "x notes on 4th commit" 4th &&
62 + verify_notes x
63 +'
64 +
65 +cat <<EOF | sort >expect_notes_y
66 +e2bfd06a37dd2031684a59a6e2b033e212239c78 $commit_sha4
67 +5772f42408c0dd6f097a7ca2d24de0e78d1c46b1 $commit_sha3
68 +b0a6021ec006d07e80e9b20ec9b444cbd9d560d3 $commit_sha1
69 +EOF
70 +
71 +cat >expect_log_y <<EOF
72 +$commit_sha5 5th
73 +
74 +$commit_sha4 4th
75 +y notes on 4th commit
76 +
77 +$commit_sha3 3rd
78 +y notes on 3rd commit
79 +
80 +$commit_sha2 2nd
81 +
82 +$commit_sha1 1st
83 +y notes on 1st commit
84 +
85 +EOF
86 +
87 +test_expect_success 'setup local branch (y)' '
88 + git update-ref refs/notes/y refs/notes/x &&
89 + git config core.notesRef refs/notes/y &&
90 + git notes add -f -m "y notes on 1st commit" 1st &&
91 + git notes remove 2nd &&
92 + git notes add -f -m "y notes on 3rd commit" 3rd &&
93 + git notes add -f -m "y notes on 4th commit" 4th &&
94 + verify_notes y
95 +'
96 +
97 +cat <<EOF | sort >expect_notes_z
98 +cff59c793c20bb49a4e01bc06fb06bad642e0d54 $commit_sha4
99 +283b48219aee9a4105f6cab337e789065c82c2b9 $commit_sha2
100 +0a81da8956346e19bcb27a906f04af327e03e31b $commit_sha1
101 +EOF
102 +
103 +cat >expect_log_z <<EOF
104 +$commit_sha5 5th
105 +
106 +$commit_sha4 4th
107 +z notes on 4th commit
108 +
109 +$commit_sha3 3rd
110 +
111 +$commit_sha2 2nd
112 +z notes on 2nd commit
113 +
114 +$commit_sha1 1st
115 +z notes on 1st commit
116 +
117 +EOF
118 +
119 +test_expect_success 'setup remote branch (z)' '
120 + git update-ref refs/notes/z refs/notes/x &&
121 + git config core.notesRef refs/notes/z &&
122 + git notes add -f -m "z notes on 1st commit" 1st &&
123 + git notes add -f -m "z notes on 2nd commit" 2nd &&
124 + git notes remove 3rd &&
125 + git notes add -f -m "z notes on 4th commit" 4th &&
126 + verify_notes z
127 +'
128 +
129 +# At this point, before merging z into y, we have the following status:
130 +#
131 +# commit | base/x | local/y | remote/z | diff from x to y/z
132 +# -------|---------|---------|----------|---------------------------
133 +# 1st | [none] | b0a6021 | 0a81da8 | added / added (diff)
134 +# 2nd | ceefa67 | [none] | 283b482 | removed / changed
135 +# 3rd | e5388c1 | 5772f42 | [none] | changed / removed
136 +# 4th | 6e8e3fe | e2bfd06 | cff59c7 | changed / changed (diff)
137 +# 5th | [none] | [none] | [none] | [none]
138 +
139 +cat <<EOF | sort >expect_conflicts
140 +$commit_sha1
141 +$commit_sha2
142 +$commit_sha3
143 +$commit_sha4
144 +EOF
145 +
146 +cat >expect_conflict_$commit_sha1 <<EOF
147 +<<<<<<< refs/notes/m
148 +y notes on 1st commit
149 +=======
150 +z notes on 1st commit
151 +>>>>>>> refs/notes/z
152 +EOF
153 +
154 +cat >expect_conflict_$commit_sha2 <<EOF
155 +z notes on 2nd commit
156 +EOF
157 +
158 +cat >expect_conflict_$commit_sha3 <<EOF
159 +y notes on 3rd commit
160 +EOF
161 +
162 +cat >expect_conflict_$commit_sha4 <<EOF
163 +<<<<<<< refs/notes/m
164 +y notes on 4th commit
165 +=======
166 +z notes on 4th commit
167 +>>>>>>> refs/notes/z
168 +EOF
169 +
170 +cp expect_notes_y expect_notes_m
171 +cp expect_log_y expect_log_m
172 +
173 +git rev-parse refs/notes/y > pre_merge_y
174 +
175 +test_expect_success 'merge z into m (== y) with default ("manual") resolver => Conflicting 3-way merge' '
176 + git update-ref refs/notes/m refs/notes/y &&
177 + git config core.notesRef refs/notes/m &&
178 + test_must_fail git notes merge z >output &&
179 + # Output should point to where to resolve conflicts
180 + grep -q "\\.git/NOTES_MERGE_WORKTREE" output &&
181 + # Inspect merge conflicts
182 + ls .git/NOTES_MERGE_WORKTREE >output_conflicts &&
183 + test_cmp expect_conflicts output_conflicts &&
184 + ( for f in $(cat expect_conflicts); do
185 + test_cmp "expect_conflict_$f" ".git/NOTES_MERGE_WORKTREE/$f" ||
186 + exit 1
187 + done ) &&
188 + # Verify that current notes tree (pre-merge) has not changed (m == y)
189 + verify_notes y &&
190 + verify_notes m &&
191 + test "$(git rev-parse refs/notes/m)" = "$(cat pre_merge_y)"
192 +'
193 +
194 +cat <<EOF | sort >expect_notes_z
195 +00494adecf2d9635a02fa431308d67993f853968 $commit_sha4
196 +283b48219aee9a4105f6cab337e789065c82c2b9 $commit_sha2
197 +0a81da8956346e19bcb27a906f04af327e03e31b $commit_sha1
198 +EOF
199 +
200 +cat >expect_log_z <<EOF
201 +$commit_sha5 5th
202 +
203 +$commit_sha4 4th
204 +z notes on 4th commit
205 +
206 +More z notes on 4th commit
207 +
208 +$commit_sha3 3rd
209 +
210 +$commit_sha2 2nd
211 +z notes on 2nd commit
212 +
213 +$commit_sha1 1st
214 +z notes on 1st commit
215 +
216 +EOF
217 +
218 +test_expect_success 'change notes in z' '
219 + git notes --ref z append -m "More z notes on 4th commit" 4th &&
220 + verify_notes z
221 +'
222 +
223 +test_expect_success 'cannot do merge w/conflicts when previous merge is unfinished' '
224 + test -d .git/NOTES_MERGE_WORKTREE &&
225 + test_must_fail git notes merge z >output 2>&1 &&
226 + # Output should indicate what is wrong
227 + grep -q "\\.git/NOTES_MERGE_\\* exists" output
228 +'
229 +
230 +# Setup non-conflicting merge between x and new notes ref w
231 +
232 +cat <<EOF | sort >expect_notes_w
233 +ceefa674873670e7ecd131814d909723cce2b669 $commit_sha2
234 +f75d1df88cbfe4258d49852f26cfc83f2ad4494b $commit_sha1
235 +EOF
236 +
237 +cat >expect_log_w <<EOF
238 +$commit_sha5 5th
239 +
240 +$commit_sha4 4th
241 +
242 +$commit_sha3 3rd
243 +
244 +$commit_sha2 2nd
245 +x notes on 2nd commit
246 +
247 +$commit_sha1 1st
248 +w notes on 1st commit
249 +
250 +EOF
251 +
252 +test_expect_success 'setup unrelated notes ref (w)' '
253 + git config core.notesRef refs/notes/w &&
254 + git notes add -m "w notes on 1st commit" 1st &&
255 + git notes add -m "x notes on 2nd commit" 2nd &&
256 + verify_notes w
257 +'
258 +
259 +cat <<EOF | sort >expect_notes_w
260 +6e8e3febca3c2bb896704335cc4d0c34cb2f8715 $commit_sha4
261 +e5388c10860456ee60673025345fe2e153eb8cf8 $commit_sha3
262 +ceefa674873670e7ecd131814d909723cce2b669 $commit_sha2
263 +f75d1df88cbfe4258d49852f26cfc83f2ad4494b $commit_sha1
264 +EOF
265 +
266 +cat >expect_log_w <<EOF
267 +$commit_sha5 5th
268 +
269 +$commit_sha4 4th
270 +x notes on 4th commit
271 +
272 +$commit_sha3 3rd
273 +x notes on 3rd commit
274 +
275 +$commit_sha2 2nd
276 +x notes on 2nd commit
277 +
278 +$commit_sha1 1st
279 +w notes on 1st commit
280 +
281 +EOF
282 +
283 +test_expect_success 'can do merge without conflicts even if previous merge is unfinished (x => w)' '
284 + test -d .git/NOTES_MERGE_WORKTREE &&
285 + git notes merge x &&
286 + verify_notes w &&
287 + # Verify that other notes refs has not changed (x and y)
288 + verify_notes x &&
289 + verify_notes y
290 +'
291 +
292 +test_done