Merge branch 'es/worktree-repair-copied' into cw/worktrees-relative
[git/gitster.git] / t / t3310-notes-merge-manual-resolve.sh
blob04866b89bed3146fe9506c748c0b824ef70b12b3
1 #!/bin/sh
3 # Copyright (c) 2010 Johan Herland
6 test_description='Test notes merging with manual conflict resolution'
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
11 # Set up a notes merge scenario with different kinds of conflicts
12 test_expect_success 'setup commits' '
13 test_commit 1st &&
14 test_commit 2nd &&
15 test_commit 3rd &&
16 test_commit 4th &&
17 test_commit 5th &&
19 test_oid_cache <<-EOF
20 hash04a sha1:6e8e3febca3c2bb896704335cc4d0c34cb2f8715
21 hash03a sha1:e5388c10860456ee60673025345fe2e153eb8cf8
22 hash02a sha1:ceefa674873670e7ecd131814d909723cce2b669
23 hash04b sha1:e2bfd06a37dd2031684a59a6e2b033e212239c78
24 hash03b sha1:5772f42408c0dd6f097a7ca2d24de0e78d1c46b1
25 hash01b sha1:b0a6021ec006d07e80e9b20ec9b444cbd9d560d3
26 hash04c sha1:cff59c793c20bb49a4e01bc06fb06bad642e0d54
27 hash02c sha1:283b48219aee9a4105f6cab337e789065c82c2b9
28 hash01c sha1:0a81da8956346e19bcb27a906f04af327e03e31b
29 hash04d sha1:00494adecf2d9635a02fa431308d67993f853968
30 hash01e sha1:f75d1df88cbfe4258d49852f26cfc83f2ad4494b
31 hash04f sha1:021faa20e931fb48986ffc6282b4bb05553ac946
32 hash01f sha1:0a59e787e6d688aa6309e56e8c1b89431a0fc1c1
33 hash05g sha1:304dfb4325cf243025b9957486eb605a9b51c199
35 hash04a sha256:f18a935e65866345098b3b754071dbf9f3aa3520eb27a7b036b278c5e2f1ed7e
36 hash03a sha256:713035dc94067a64e5fa6e4e1821b7c3bde49a77c7cb3f80eaadefa1ca41b3d2
37 hash02a sha256:f160a67e048b6fa75bec3952184154045076692cf5dccd3da21e3fd34b7a3f0f
38 hash04b sha256:c7fba0d6104917fbf35258f40b9fa4fc697cfa992deecd1570a3b08d0a5587a9
39 hash03b sha256:7287a2d78a3766c181b08df38951d784b08b72a44f571ed6d855bd0be22c70f6
40 hash01b sha256:da96cf778c15d0a2bb76f98b2a62f6c9c01730fa7030e8f08ef0191048e7d620
41 hash04c sha256:cb615d2def4b834d5f55b2351df97dc92bee4f5009d285201427f349081c8aca
42 hash02c sha256:63bb527e0b4e1c8e1dd0d54dd778ca7c3718689fd6e37c473044cfbcf1cacfdb
43 hash01c sha256:5b87237ac1fbae0246256fed9f9a1f077c4140fb7e6444925f8dbfa5ae406cd8
44 hash04d sha256:eeddc9f9f6cb3d6b39b861659853f10891dc373e0b6eecb09e03e39b6ce64714
45 hash01e sha256:108f521b1a74c2e6d0b52a4eda87e09162bf847f7d190cfce496ee1af0b29a5a
46 hash04f sha256:901acda0454502b3bbd281f130c419e6c8de78afcf72a8def8d45ad31462bce4
47 hash01f sha256:a2d99d1b8bf23c8af7d9d91368454adc110dfd5cc068a4cebb486ee8f5a1e16c
48 hash05g sha256:4fef015b01da8efe929a68e3bb9b8fbad81f53995f097befe8ebc93f12ab98ec
49 EOF
52 commit_sha1=$(git rev-parse 1st^{commit})
53 commit_sha2=$(git rev-parse 2nd^{commit})
54 commit_sha3=$(git rev-parse 3rd^{commit})
55 commit_sha4=$(git rev-parse 4th^{commit})
56 commit_sha5=$(git rev-parse 5th^{commit})
58 verify_notes () {
59 notes_ref="$1"
60 git -c core.notesRef="refs/notes/$notes_ref" notes |
61 sort >"output_notes_$notes_ref" &&
62 test_cmp "expect_notes_$notes_ref" "output_notes_$notes_ref" &&
63 git -c core.notesRef="refs/notes/$notes_ref" log --format="%H %s%n%N" \
64 >"output_log_$notes_ref" &&
65 test_cmp "expect_log_$notes_ref" "output_log_$notes_ref"
68 notes_merge_files_gone () {
69 # No .git/NOTES_MERGE_* files left
70 { ls .git/NOTES_MERGE_* >output || :; } &&
71 test_must_be_empty output
74 cat <<EOF | sort >expect_notes_x
75 $(test_oid hash04a) $commit_sha4
76 $(test_oid hash03a) $commit_sha3
77 $(test_oid hash02a) $commit_sha2
78 EOF
80 cat >expect_log_x <<EOF
81 $commit_sha5 5th
83 $commit_sha4 4th
84 x notes on 4th commit
86 $commit_sha3 3rd
87 x notes on 3rd commit
89 $commit_sha2 2nd
90 x notes on 2nd commit
92 $commit_sha1 1st
94 EOF
96 test_expect_success 'setup merge base (x)' '
97 git config core.notesRef refs/notes/x &&
98 git notes add -m "x notes on 2nd commit" 2nd &&
99 git notes add -m "x notes on 3rd commit" 3rd &&
100 git notes add -m "x notes on 4th commit" 4th &&
101 verify_notes x
104 cat <<EOF | sort >expect_notes_y
105 $(test_oid hash04b) $commit_sha4
106 $(test_oid hash03b) $commit_sha3
107 $(test_oid hash01b) $commit_sha1
110 cat >expect_log_y <<EOF
111 $commit_sha5 5th
113 $commit_sha4 4th
114 y notes on 4th commit
116 $commit_sha3 3rd
117 y notes on 3rd commit
119 $commit_sha2 2nd
121 $commit_sha1 1st
122 y notes on 1st commit
126 test_expect_success 'setup local branch (y)' '
127 git update-ref refs/notes/y refs/notes/x &&
128 git config core.notesRef refs/notes/y &&
129 git notes add -f -m "y notes on 1st commit" 1st &&
130 git notes remove 2nd &&
131 git notes add -f -m "y notes on 3rd commit" 3rd &&
132 git notes add -f -m "y notes on 4th commit" 4th &&
133 verify_notes y
136 cat <<EOF | sort >expect_notes_z
137 $(test_oid hash04c) $commit_sha4
138 $(test_oid hash02c) $commit_sha2
139 $(test_oid hash01c) $commit_sha1
142 cat >expect_log_z <<EOF
143 $commit_sha5 5th
145 $commit_sha4 4th
146 z notes on 4th commit
148 $commit_sha3 3rd
150 $commit_sha2 2nd
151 z notes on 2nd commit
153 $commit_sha1 1st
154 z notes on 1st commit
158 test_expect_success 'setup remote branch (z)' '
159 git update-ref refs/notes/z refs/notes/x &&
160 git config core.notesRef refs/notes/z &&
161 git notes add -f -m "z notes on 1st commit" 1st &&
162 git notes add -f -m "z notes on 2nd commit" 2nd &&
163 git notes remove 3rd &&
164 git notes add -f -m "z notes on 4th commit" 4th &&
165 verify_notes z
168 # At this point, before merging z into y, we have the following status:
170 # commit | base/x | local/y | remote/z | diff from x to y/z
171 # -------|---------|---------|----------|---------------------------
172 # 1st | [none] | b0a6021 | 0a81da8 | added / added (diff)
173 # 2nd | ceefa67 | [none] | 283b482 | removed / changed
174 # 3rd | e5388c1 | 5772f42 | [none] | changed / removed
175 # 4th | 6e8e3fe | e2bfd06 | cff59c7 | changed / changed (diff)
176 # 5th | [none] | [none] | [none] | [none]
178 cat <<EOF | sort >expect_conflicts
179 $commit_sha1
180 $commit_sha2
181 $commit_sha3
182 $commit_sha4
185 cat >expect_conflict_$commit_sha1 <<EOF
186 <<<<<<< refs/notes/m
187 y notes on 1st commit
188 =======
189 z notes on 1st commit
190 >>>>>>> refs/notes/z
193 cat >expect_conflict_$commit_sha2 <<EOF
194 z notes on 2nd commit
197 cat >expect_conflict_$commit_sha3 <<EOF
198 y notes on 3rd commit
201 cat >expect_conflict_$commit_sha4 <<EOF
202 <<<<<<< refs/notes/m
203 y notes on 4th commit
204 =======
205 z notes on 4th commit
206 >>>>>>> refs/notes/z
209 cp expect_notes_y expect_notes_m
210 cp expect_log_y expect_log_m
212 git rev-parse refs/notes/y > pre_merge_y
213 git rev-parse refs/notes/z > pre_merge_z
215 test_expect_success 'merge z into m (== y) with default ("manual") resolver => Conflicting 3-way merge' '
216 git update-ref refs/notes/m refs/notes/y &&
217 git config core.notesRef refs/notes/m &&
218 test_must_fail git notes merge z >output 2>&1 &&
219 # Output should point to where to resolve conflicts
220 test_grep "\\.git/NOTES_MERGE_WORKTREE" output &&
221 # Inspect merge conflicts
222 ls .git/NOTES_MERGE_WORKTREE >output_conflicts &&
223 test_cmp expect_conflicts output_conflicts &&
224 ( for f in $(cat expect_conflicts); do
225 test_cmp "expect_conflict_$f" ".git/NOTES_MERGE_WORKTREE/$f" ||
226 exit 1
227 done ) &&
228 # Verify that current notes tree (pre-merge) has not changed (m == y)
229 verify_notes y &&
230 verify_notes m &&
231 test "$(git rev-parse refs/notes/m)" = "$(cat pre_merge_y)"
234 cat <<EOF | sort >expect_notes_z
235 $(test_oid hash04d) $commit_sha4
236 $(test_oid hash02c) $commit_sha2
237 $(test_oid hash01c) $commit_sha1
240 cat >expect_log_z <<EOF
241 $commit_sha5 5th
243 $commit_sha4 4th
244 z notes on 4th commit
246 More z notes on 4th commit
248 $commit_sha3 3rd
250 $commit_sha2 2nd
251 z notes on 2nd commit
253 $commit_sha1 1st
254 z notes on 1st commit
258 test_expect_success 'change notes in z' '
259 git notes --ref z append -m "More z notes on 4th commit" 4th &&
260 verify_notes z
263 test_expect_success 'cannot do merge w/conflicts when previous merge is unfinished' '
264 test -d .git/NOTES_MERGE_WORKTREE &&
265 test_must_fail git notes merge z >output 2>&1 &&
266 # Output should indicate what is wrong
267 test_grep -q "\\.git/NOTES_MERGE_\\* exists" output
270 # Setup non-conflicting merge between x and new notes ref w
272 cat <<EOF | sort >expect_notes_w
273 $(test_oid hash02a) $commit_sha2
274 $(test_oid hash01e) $commit_sha1
277 cat >expect_log_w <<EOF
278 $commit_sha5 5th
280 $commit_sha4 4th
282 $commit_sha3 3rd
284 $commit_sha2 2nd
285 x notes on 2nd commit
287 $commit_sha1 1st
288 w notes on 1st commit
292 test_expect_success 'setup unrelated notes ref (w)' '
293 git config core.notesRef refs/notes/w &&
294 git notes add -m "w notes on 1st commit" 1st &&
295 git notes add -m "x notes on 2nd commit" 2nd &&
296 verify_notes w
299 cat <<EOF | sort >expect_notes_w
300 $(test_oid hash04a) $commit_sha4
301 $(test_oid hash03a) $commit_sha3
302 $(test_oid hash02a) $commit_sha2
303 $(test_oid hash01e) $commit_sha1
306 cat >expect_log_w <<EOF
307 $commit_sha5 5th
309 $commit_sha4 4th
310 x notes on 4th commit
312 $commit_sha3 3rd
313 x notes on 3rd commit
315 $commit_sha2 2nd
316 x notes on 2nd commit
318 $commit_sha1 1st
319 w notes on 1st commit
323 test_expect_success 'can do merge without conflicts even if previous merge is unfinished (x => w)' '
324 test -d .git/NOTES_MERGE_WORKTREE &&
325 git notes merge x &&
326 verify_notes w &&
327 # Verify that other notes refs has not changed (x and y)
328 verify_notes x &&
329 verify_notes y
332 cat <<EOF | sort >expect_notes_m
333 $(test_oid hash04f) $commit_sha4
334 $(test_oid hash03b) $commit_sha3
335 $(test_oid hash02c) $commit_sha2
336 $(test_oid hash01f) $commit_sha1
339 cat >expect_log_m <<EOF
340 $commit_sha5 5th
342 $commit_sha4 4th
343 y and z notes on 4th commit
345 $commit_sha3 3rd
346 y notes on 3rd commit
348 $commit_sha2 2nd
349 z notes on 2nd commit
351 $commit_sha1 1st
352 y and z notes on 1st commit
356 test_expect_success 'do not allow mixing --commit and --abort' '
357 test_must_fail git notes merge --commit --abort
360 test_expect_success 'do not allow mixing --commit and --strategy' '
361 test_must_fail git notes merge --commit --strategy theirs
364 test_expect_success 'do not allow mixing --abort and --strategy' '
365 test_must_fail git notes merge --abort --strategy theirs
368 test_expect_success 'finalize conflicting merge (z => m)' '
369 # Resolve conflicts and finalize merge
370 cat >.git/NOTES_MERGE_WORKTREE/$commit_sha1 <<EOF &&
371 y and z notes on 1st commit
373 cat >.git/NOTES_MERGE_WORKTREE/$commit_sha4 <<EOF &&
374 y and z notes on 4th commit
376 git notes merge --commit &&
377 notes_merge_files_gone &&
378 # Merge commit has pre-merge y and pre-merge z as parents
379 test "$(git rev-parse refs/notes/m^1)" = "$(cat pre_merge_y)" &&
380 test "$(git rev-parse refs/notes/m^2)" = "$(cat pre_merge_z)" &&
381 # Merge commit mentions the notes refs merged
382 git log -1 --format=%B refs/notes/m > merge_commit_msg &&
383 grep -q refs/notes/m merge_commit_msg &&
384 grep -q refs/notes/z merge_commit_msg &&
385 # Merge commit mentions conflicting notes
386 grep -q "Conflicts" merge_commit_msg &&
387 ( for sha1 in $(cat expect_conflicts); do
388 grep -q "$sha1" merge_commit_msg ||
389 exit 1
390 done ) &&
391 # Verify contents of merge result
392 verify_notes m &&
393 # Verify that other notes refs has not changed (w, x, y and z)
394 verify_notes w &&
395 verify_notes x &&
396 verify_notes y &&
397 verify_notes z
400 cat >expect_conflict_$commit_sha4 <<EOF
401 <<<<<<< refs/notes/m
402 y notes on 4th commit
403 =======
404 z notes on 4th commit
406 More z notes on 4th commit
407 >>>>>>> refs/notes/z
410 cp expect_notes_y expect_notes_m
411 cp expect_log_y expect_log_m
413 git rev-parse refs/notes/y > pre_merge_y
414 git rev-parse refs/notes/z > pre_merge_z
416 test_expect_success 'redo merge of z into m (== y) with default ("manual") resolver => Conflicting 3-way merge' '
417 git update-ref refs/notes/m refs/notes/y &&
418 git config core.notesRef refs/notes/m &&
419 test_must_fail git notes merge z >output 2>&1 &&
420 # Output should point to where to resolve conflicts
421 test_grep "\\.git/NOTES_MERGE_WORKTREE" output &&
422 # Inspect merge conflicts
423 ls .git/NOTES_MERGE_WORKTREE >output_conflicts &&
424 test_cmp expect_conflicts output_conflicts &&
425 ( for f in $(cat expect_conflicts); do
426 test_cmp "expect_conflict_$f" ".git/NOTES_MERGE_WORKTREE/$f" ||
427 exit 1
428 done ) &&
429 # Verify that current notes tree (pre-merge) has not changed (m == y)
430 verify_notes y &&
431 verify_notes m &&
432 test "$(git rev-parse refs/notes/m)" = "$(cat pre_merge_y)"
435 test_expect_success 'abort notes merge' '
436 git notes merge --abort &&
437 notes_merge_files_gone &&
438 # m has not moved (still == y)
439 test "$(git rev-parse refs/notes/m)" = "$(cat pre_merge_y)" &&
440 # Verify that other notes refs has not changed (w, x, y and z)
441 verify_notes w &&
442 verify_notes x &&
443 verify_notes y &&
444 verify_notes z
447 git rev-parse refs/notes/y > pre_merge_y
448 git rev-parse refs/notes/z > pre_merge_z
450 test_expect_success 'redo merge of z into m (== y) with default ("manual") resolver => Conflicting 3-way merge' '
451 test_must_fail git notes merge z >output 2>&1 &&
452 # Output should point to where to resolve conflicts
453 test_grep "\\.git/NOTES_MERGE_WORKTREE" output &&
454 # Inspect merge conflicts
455 ls .git/NOTES_MERGE_WORKTREE >output_conflicts &&
456 test_cmp expect_conflicts output_conflicts &&
457 ( for f in $(cat expect_conflicts); do
458 test_cmp "expect_conflict_$f" ".git/NOTES_MERGE_WORKTREE/$f" ||
459 exit 1
460 done ) &&
461 # Verify that current notes tree (pre-merge) has not changed (m == y)
462 verify_notes y &&
463 verify_notes m &&
464 test "$(git rev-parse refs/notes/m)" = "$(cat pre_merge_y)"
467 cat <<EOF | sort >expect_notes_m
468 $(test_oid hash05g) $commit_sha5
469 $(test_oid hash02c) $commit_sha2
470 $(test_oid hash01f) $commit_sha1
473 cat >expect_log_m <<EOF
474 $commit_sha5 5th
475 new note on 5th commit
477 $commit_sha4 4th
479 $commit_sha3 3rd
481 $commit_sha2 2nd
482 z notes on 2nd commit
484 $commit_sha1 1st
485 y and z notes on 1st commit
489 test_expect_success 'add + remove notes in finalized merge (z => m)' '
490 # Resolve one conflict
491 cat >.git/NOTES_MERGE_WORKTREE/$commit_sha1 <<EOF &&
492 y and z notes on 1st commit
494 # Remove another conflict
495 rm .git/NOTES_MERGE_WORKTREE/$commit_sha4 &&
496 # Remove a D/F conflict
497 rm .git/NOTES_MERGE_WORKTREE/$commit_sha3 &&
498 # Add a new note
499 echo "new note on 5th commit" > .git/NOTES_MERGE_WORKTREE/$commit_sha5 &&
500 # Finalize merge
501 git notes merge --commit &&
502 notes_merge_files_gone &&
503 # Merge commit has pre-merge y and pre-merge z as parents
504 test "$(git rev-parse refs/notes/m^1)" = "$(cat pre_merge_y)" &&
505 test "$(git rev-parse refs/notes/m^2)" = "$(cat pre_merge_z)" &&
506 # Merge commit mentions the notes refs merged
507 git log -1 --format=%B refs/notes/m > merge_commit_msg &&
508 grep -q refs/notes/m merge_commit_msg &&
509 grep -q refs/notes/z merge_commit_msg &&
510 # Merge commit mentions conflicting notes
511 grep -q "Conflicts" merge_commit_msg &&
512 ( for sha1 in $(cat expect_conflicts); do
513 grep -q "$sha1" merge_commit_msg ||
514 exit 1
515 done ) &&
516 # Verify contents of merge result
517 verify_notes m &&
518 # Verify that other notes refs has not changed (w, x, y and z)
519 verify_notes w &&
520 verify_notes x &&
521 verify_notes y &&
522 verify_notes z
525 cp expect_notes_y expect_notes_m
526 cp expect_log_y expect_log_m
528 test_expect_success 'redo merge of z into m (== y) with default ("manual") resolver => Conflicting 3-way merge' '
529 git update-ref refs/notes/m refs/notes/y &&
530 test_must_fail git notes merge z >output 2>&1 &&
531 # Output should point to where to resolve conflicts
532 test_grep "\\.git/NOTES_MERGE_WORKTREE" output &&
533 # Inspect merge conflicts
534 ls .git/NOTES_MERGE_WORKTREE >output_conflicts &&
535 test_cmp expect_conflicts output_conflicts &&
536 ( for f in $(cat expect_conflicts); do
537 test_cmp "expect_conflict_$f" ".git/NOTES_MERGE_WORKTREE/$f" ||
538 exit 1
539 done ) &&
540 # Verify that current notes tree (pre-merge) has not changed (m == y)
541 verify_notes y &&
542 verify_notes m &&
543 test "$(git rev-parse refs/notes/m)" = "$(cat pre_merge_y)"
546 cp expect_notes_w expect_notes_m
547 cp expect_log_w expect_log_m
549 test_expect_success 'reset notes ref m to somewhere else (w)' '
550 git update-ref refs/notes/m refs/notes/w &&
551 verify_notes m &&
552 test "$(git rev-parse refs/notes/m)" = "$(git rev-parse refs/notes/w)"
555 test_expect_success 'fail to finalize conflicting merge if underlying ref has moved in the meantime (m != NOTES_MERGE_PARTIAL^1)' '
556 # Resolve conflicts
557 cat >.git/NOTES_MERGE_WORKTREE/$commit_sha1 <<EOF &&
558 y and z notes on 1st commit
560 cat >.git/NOTES_MERGE_WORKTREE/$commit_sha4 <<EOF &&
561 y and z notes on 4th commit
563 # Fail to finalize merge
564 test_must_fail git notes merge --commit >output 2>&1 &&
565 # NOTES_MERGE_* refs and .git/NOTES_MERGE_* state files must remain
566 git rev-parse --verify NOTES_MERGE_PARTIAL &&
567 git rev-parse --verify NOTES_MERGE_REF &&
568 test -f .git/NOTES_MERGE_WORKTREE/$commit_sha1 &&
569 test -f .git/NOTES_MERGE_WORKTREE/$commit_sha2 &&
570 test -f .git/NOTES_MERGE_WORKTREE/$commit_sha3 &&
571 test -f .git/NOTES_MERGE_WORKTREE/$commit_sha4 &&
572 # Refs are unchanged
573 test "$(git rev-parse refs/notes/m)" = "$(git rev-parse refs/notes/w)" &&
574 test "$(git rev-parse refs/notes/y)" = "$(git rev-parse NOTES_MERGE_PARTIAL^1)" &&
575 test "$(git rev-parse refs/notes/m)" != "$(git rev-parse NOTES_MERGE_PARTIAL^1)" &&
576 # Mention refs/notes/m, and its current and expected value in output
577 test_grep -q "refs/notes/m" output &&
578 test_grep -q "$(git rev-parse refs/notes/m)" output &&
579 test_grep -q "$(git rev-parse NOTES_MERGE_PARTIAL^1)" output &&
580 # Verify that other notes refs has not changed (w, x, y and z)
581 verify_notes w &&
582 verify_notes x &&
583 verify_notes y &&
584 verify_notes z
587 test_expect_success 'resolve situation by aborting the notes merge' '
588 git notes merge --abort &&
589 notes_merge_files_gone &&
590 # m has not moved (still == w)
591 test "$(git rev-parse refs/notes/m)" = "$(git rev-parse refs/notes/w)" &&
592 # Verify that other notes refs has not changed (w, x, y and z)
593 verify_notes w &&
594 verify_notes x &&
595 verify_notes y &&
596 verify_notes z
599 cat >expect_notes <<EOF
604 test_expect_success 'switch cwd before committing notes merge' '
605 git notes add -m foo HEAD &&
606 git notes --ref=other add -m bar HEAD &&
607 test_must_fail git notes merge refs/notes/other &&
609 cd .git/NOTES_MERGE_WORKTREE &&
610 echo "foo" > $(git rev-parse HEAD) &&
611 echo "bar" >> $(git rev-parse HEAD) &&
612 git notes merge --commit
613 ) &&
614 git notes show HEAD > actual_notes &&
615 test_cmp expect_notes actual_notes
618 test_done