git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCHv4 18/21] git notes merge: --commit should fail if underlying notes ref has moved

From
Johan Herland <johan@herland.net>
Date
Oct 21, 2010, 02:08 UTC
Message-ID
<1287626936-32232-19-git-send-email-johan@herland.net>
In-Reply-To
<1287626936-32232-1-git-send-email-johan@herland.net>

When manually resolving a notes merge, if the merging ref has moved since the merge started, we should fail to complete the merge, and alert the user to what's going on.

This situation may arise if you start a 'git notes merge' which results in conflicts, and you then update the current notes ref (using for example 'git notes add/copy/amend/edit/remove/prune', 'git update-ref', etc.), before you get around to resolving the notes conflicts and calling 'git notes merge --commit'.

We detect this situation by comparing the first parent of the partial merge commit (which was created when the merge started) to the current value of the merging notes ref (pointed to by the .git/NOTES_MERGE_REF symref).

If we don't fail in this situation, the notes merge commit would overwrite the updated notes ref, thus losing the changes that happened in the meantime.

The patch includes a testcase verifying that we fail correctly in this situation.

Signed-off-by: Johan Herland <johan@herland.net>
---
 builtin/notes.c                       |   11 ++++-
 t/t3310-notes-merge-manual-resolve.sh |   76 +++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+), 2 deletions(-)
diff --git a/builtin/notes.c b/builtin/notes.c
index 3c373ec..d3754ab 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -786,7 +786,7 @@ static int merge_reset(struct notes_merge_options *o)
 static int merge_commit(struct notes_merge_options *o)
 {
 	struct strbuf msg = STRBUF_INIT;
-	unsigned char sha1[20];
+	unsigned char sha1[20], parent_sha1[20];
 	struct notes_tree *t;
 	struct commit *partial;
 	struct pretty_print_context pretty_ctx;
@@ -803,6 +803,11 @@ static int merge_commit(struct notes_merge_options *o)
 	else if (parse_commit(partial))
 		die("Could not parse commit from NOTES_MERGE_PARTIAL.");
 
+	if (partial->parents)
+		hashcpy(parent_sha1, partial->parents->item->object.sha1);
+	else
+		hashclr(parent_sha1);
+
 	t = xcalloc(1, sizeof(struct notes_tree));
 	init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
 
@@ -818,7 +823,9 @@ static int merge_commit(struct notes_merge_options *o)
 	format_commit_message(partial, "%s", &msg, &pretty_ctx);
 	strbuf_trim(&msg);
 	strbuf_insert(&msg, 0, "notes: ", 7);
-	update_ref(msg.buf, o->local_ref, sha1, NULL, 0, DIE_ON_ERR);
+	update_ref(msg.buf, o->local_ref, sha1,
+		   is_null_sha1(parent_sha1) ? NULL : parent_sha1,
+		   0, DIE_ON_ERR);
 
 	free_notes(t);
 	strbuf_release(&msg);
diff --git a/t/t3310-notes-merge-manual-resolve.sh b/t/t3310-notes-merge-manual-resolve.sh
index 8568307..dfd4954 100755
--- a/t/t3310-notes-merge-manual-resolve.sh
+++ b/t/t3310-notes-merge-manual-resolve.sh
@@ -477,4 +477,80 @@ EOF
 	verify_notes z
 '
 
+cp expect_notes_y expect_notes_m
+cp expect_log_y expect_log_m
+
+test_expect_success 'redo merge of z into m (== y) with default ("manual") resolver => Conflicting 3-way merge' '
+	git update-ref refs/notes/m refs/notes/y &&
+	test_must_fail git notes merge z >output &&
+	# Output should point to where to resolve conflicts
+	grep -q "\\.git/NOTES_MERGE_WORKTREE" output &&
+	# Inspect merge conflicts
+	ls .git/NOTES_MERGE_WORKTREE >output_conflicts &&
+	test_cmp expect_conflicts output_conflicts &&
+	( for f in $(cat expect_conflicts); do
+		test_cmp "expect_conflict_$f" ".git/NOTES_MERGE_WORKTREE/$f" ||
+		exit 1
+	done ) &&
+	# Verify that current notes tree (pre-merge) has not changed (m == y)
+	verify_notes y &&
+	verify_notes m &&
+	test "$(git rev-parse refs/notes/m)" = "$(cat pre_merge_y)"
+'
+
+cp expect_notes_w expect_notes_m
+cp expect_log_w expect_log_m
+
+test_expect_success 'reset notes ref m to somewhere else (w)' '
+	git update-ref refs/notes/m refs/notes/w &&
+	verify_notes m &&
+	test "$(git rev-parse refs/notes/m)" = "$(git rev-parse refs/notes/w)"
+'
+
+test_expect_success 'fail to finalize conflicting merge if underlying ref has moved in the meantime (m != NOTES_MERGE_PARTIAL^1)' '
+	# Resolve conflicts
+	cat >.git/NOTES_MERGE_WORKTREE/$commit_sha1 <<EOF &&
+y and z notes on 1st commit
+EOF
+	cat >.git/NOTES_MERGE_WORKTREE/$commit_sha4 <<EOF &&
+y and z notes on 4th commit
+EOF
+	# Fail to finalize merge
+	test_must_fail git notes merge --commit >output 2>&1 &&
+	# .git/NOTES_MERGE_* must remain
+	test -f .git/NOTES_MERGE_PARTIAL &&
+	test -f .git/NOTES_MERGE_REF &&
+	test -f .git/NOTES_MERGE_WORKTREE/$commit_sha1 &&
+	test -f .git/NOTES_MERGE_WORKTREE/$commit_sha2 &&
+	test -f .git/NOTES_MERGE_WORKTREE/$commit_sha3 &&
+	test -f .git/NOTES_MERGE_WORKTREE/$commit_sha4 &&
+	# Refs are unchanged
+	test "$(git rev-parse refs/notes/m)" = "$(git rev-parse refs/notes/w)"
+	test "$(git rev-parse refs/notes/y)" = "$(git rev-parse NOTES_MERGE_PARTIAL^1)"
+	test "$(git rev-parse refs/notes/m)" != "$(git rev-parse NOTES_MERGE_PARTIAL^1)"
+	# Mention refs/notes/m, and its current and expected value in output
+	grep -q "refs/notes/m" output &&
+	grep -q "$(git rev-parse refs/notes/m)" output &&
+	grep -q "$(git rev-parse NOTES_MERGE_PARTIAL^1)" output &&
+	# Verify that other notes refs has not changed (w, x, y and z)
+	verify_notes w &&
+	verify_notes x &&
+	verify_notes y &&
+	verify_notes z
+'
+
+test_expect_success 'resolve situation by aborting the notes merge' '
+	git notes merge --reset &&
+	# No .git/NOTES_MERGE_* files left
+	test_must_fail ls .git/NOTES_MERGE_* >output 2>/dev/null &&
+	test_cmp /dev/null output &&
+	# m has not moved (still == w)
+	test "$(git rev-parse refs/notes/m)" = "$(git rev-parse refs/notes/w)"
+	# Verify that other notes refs has not changed (w, x, y and z)
+	verify_notes w &&
+	verify_notes x &&
+	verify_notes y &&
+	verify_notes z
+'
+
 test_done
-- 
1.7.3.98.g5ad7d9
Previous: Johan HerlandNext: Johan Herland
Message 25 of 43 in “[PATCHv4 00/21] git notes merge”
  1. Johan HerlandOct 21, 2010
  2. 01/21 notes.c: Hexify SHA1 in die() message from init_notes()Johan Herland, Oct 21, 2010
  3. 02/21 (trivial) notes.h: Minor documentation fixes to copy_notes()Johan Herland, Oct 21, 2010
  4. 03/21 notes.h: Make default_notes_ref() available in notes APIJohan Herland, Oct 21, 2010
  5. 04/21 notes.c: Reorder functions in preparation for next commitJohan Herland, Oct 21, 2010
  6. 05/21 notes.h/c: Clarify the handling of notes objects that are == null_sha1Johan Herland, Oct 21, 2010
  7. Jonathan NiederOct 21, 2010
  8. Johan HerlandOct 21, 2010
  9. Jonathan NiederOct 21, 2010
  10. Johan HerlandOct 22, 2010
  11. 06/21 notes.h/c: Propagate combine_notes_fn return value to add_note() and beyondJohan Herland, Oct 21, 2010
  12. Jonathan NiederOct 21, 2010
  13. Johan HerlandOct 21, 2010
  14. 07/21 (trivial) t3303: Indent with tabs instead of spaces for consistencyJohan Herland, Oct 21, 2010
  15. 08/21 notes.c: Use two newlines (instead of one) when concatenating notesJohan Herland, Oct 21, 2010
  16. 09/21 builtin/notes.c: Split notes ref DWIMmery into a separate functionJohan Herland, Oct 21, 2010
  17. 10/21 git notes merge: Initial implementation handling trivial merges onlyJohan Herland, Oct 21, 2010
  18. 11/21 builtin/notes.c: Refactor creation of notes commits.Johan Herland, Oct 21, 2010
  19. 12/21 git notes merge: Handle real, non-conflicting notes mergesJohan Herland, Oct 21, 2010
  20. 13/21 git notes merge: Add automatic conflict resolvers (ours, theirs, union)Johan Herland, Oct 21, 2010
  21. 14/21 Documentation: Preliminary docs on 'git notes merge'Johan Herland, Oct 21, 2010
  22. 15/21 git notes merge: Manual conflict resolution, part 1/2Johan Herland, Oct 21, 2010
  23. 16/21 git notes merge: Manual conflict resolution, part 2/2Johan Herland, Oct 21, 2010
  24. 17/21 git notes merge: List conflicting notes in notes merge commit messageJohan Herland, Oct 21, 2010
  25. 18/21 git notes merge: --commit should fail if underlying notes ref has movedJohan Herland, Oct 21, 2010
  26. 19/21 git notes merge: Add another auto-resolving strategy: "cat_sort_uniq"Johan Herland, Oct 21, 2010
  27. 20/21 git notes merge: Add testcases for merging notes trees at different fanoutsJohan Herland, Oct 21, 2010
  28. 21/21 Provide 'git notes get-ref' to easily retrieve current notes refJohan Herland, Oct 21, 2010
  29. Sverre RabbelierOct 21, 2010
  30. Junio C HamanoOct 21, 2010
  31. Jonathan NiederOct 21, 2010
  32. git merge --abort? [was: Re: [PATCHv4 00/21] git notes merge]Johan Herland, Oct 22, 2010
  33. Jonathan NiederOct 22, 2010
  34. Johan HerlandOct 22, 2010
  35. Sverre RabbelierOct 22, 2010
  36. Jonathan NiederOct 22, 2010
  37. Sverre RabbelierOct 22, 2010
  38. Johan HerlandOct 22, 2010
  39. Sverre RabbelierOct 22, 2010
  40. Johan HerlandOct 23, 2010
  41. Sverre RabbelierOct 23, 2010
  42. Johan HerlandOct 22, 2010
  43. Sverre RabbelierOct 23, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.