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

[PATCH v2 3/4] merge: make prepare_to_commit responsible for write_merge_state

From
Ryan Biesemeyer <ryan@yaauie.com>
Date
Jan 9, 2014, 00:45 UTC
Message-ID
<1389228344-38813-4-git-send-email-ryan@yaauie.com>
In-Reply-To
<1389228344-38813-1-git-send-email-ryan@yaauie.com>

When merging, make the prepare-commit-msg hook have access to the merge state in order to make decisions about the commit message it is preparing.

Since `abort_commit` is *only* called after `write_merge_state`, and a successful commit *always* calls `drop_save` to clear the saved state, this change effectively ensures that the merge state is also available to the prepare-commit-msg hook and while the message is being edited.

Signed-off-by: Ryan Biesemeyer <ryan@yaauie.com>
---
 builtin/merge.c                    |  3 ++-
 t/t7505-prepare-commit-msg-hook.sh | 21 +++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/builtin/merge.c b/builtin/merge.c
index 4941a6c..b7bfc9c 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -802,7 +802,6 @@ static void abort_commit(struct commit_list *remoteheads, const char *err_msg)
 		error("%s", err_msg);
 	fprintf(stderr,
 		_("Not committing merge; use 'git commit' to complete the merge.\n"));
-	write_merge_state(remoteheads);
 	exit(1);
 }
 
@@ -816,6 +815,8 @@ N_("Please enter a commit message to explain why this merge is necessary,\n"
 static void prepare_to_commit(struct commit_list *remoteheads)
 {
 	struct strbuf msg = STRBUF_INIT;
+
+	write_merge_state(remoteheads);
 	strbuf_addbuf(&msg, &merge_msg);
 	strbuf_addch(&msg, '\n');
 	if (0 < option_edit)
diff --git a/t/t7505-prepare-commit-msg-hook.sh b/t/t7505-prepare-commit-msg-hook.sh
index 697ecc0..7ccf870 100755
--- a/t/t7505-prepare-commit-msg-hook.sh
+++ b/t/t7505-prepare-commit-msg-hook.sh
@@ -183,4 +183,25 @@ test_expect_success 'with failing hook (merge)' '
   )
 '
 
+test_expect_success 'should have MERGE_HEAD (merge)' '
+
+	git checkout -B other HEAD@{1} &&
+	echo "more" >> file &&
+	git add file &&
+	rm -f "$HOOK" &&
+	git commit -m other &&
+	git checkout - &&
+	write_script "$HOOK" <<-EOF &&
+	if [ -s "$(git rev-parse --git-dir)/MERGE_HEAD" ]; then
+		exit 0
+	else
+		exit 1
+	fi
+	EOF
+	git merge other &&
+	test "`git log -1 --pretty=format:%s`" = "Merge branch '"'"'other'"'"'" &&
+	test ! -s "$(git rev-parse --git-dir)/MERGE_HEAD"
+
+'
+
 test_done
-- 
1.8.5
Previous: Junio C HamanoNext: Junio C Hamano
Message 15 of 18 in “merge: make merge state available to prepare-commit-msg hook”
  1. merge: make merge state available to prepare-commit-msg hookRyan Biesemeyer, Jan 8, 2014
  2. Ryan BiesemeyerJan 8, 2014
  3. Matthieu MoyJan 8, 2014
  4. Ryan BiesemeyerJan 8, 2014
  5. Jonathan NiederJan 8, 2014
  6. Matthieu MoyJan 8, 2014
  7. Jonathan NiederJan 8, 2014
  8. Matthieu MoyJan 9, 2014
  9. Ryan BiesemeyerJan 8, 2014
  10. 0/4 merge: make merge state available to prepare-commit-msg hookRyan Biesemeyer, Jan 9, 2014
  11. 1/4 t7505: add missing &&Ryan Biesemeyer, Jan 9, 2014
  12. 2/4 t7505: ensure cleanup after hook blocks mergeRyan Biesemeyer, Jan 9, 2014
  13. Matthieu MoyJan 9, 2014
  14. Junio C HamanoJan 10, 2014
  15. 3/4 merge: make prepare_to_commit responsible for write_merge_stateRyan Biesemeyer, Jan 9, 2014
  16. Junio C HamanoJan 11, 2014
  17. Junio C HamanoJan 11, 2014
  18. 4/4 merge: drop unused arg from abort_commit method signatureRyan Biesemeyer, Jan 9, 2014

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.