{"thread":{"id":"26505","subject":"[PATCH] merge: honor prepare-commit-msg hook","startedAt":"2011-02-15T01:07:50Z","lastAt":"2011-02-15T21:36:39Z","messageCount":2,"participants":["Jay Soffian","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"161141","messageId":"1297732070-32704-1-git-send-email-jaysoffian@gmail.com","threadId":"26505","inReplyTo":null,"subject":"[PATCH] merge: honor prepare-commit-msg hook","fromName":"Jay Soffian","fromEmail":"jaysoffian@gmail.com","sentAt":"2011-02-15T01:07:50Z","receivedAt":"2011-02-15T01:07:50Z","isPatch":true,"sender":{"key":"jaysoffian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/155970?v=4"},"body":"When a merge is stopped due to conflicts or --no-commit, the\nsubsequent commit calls the prepare-commit-msg hook. However,\nit is not called after a clean merge. Fix this inconsistency\nby invoking the hook after clean merges as well.\n\nSigned-off-by: Jay Soffian <jaysoffian@gmail.com>\n---\nI sent this out previously as an RFC:\n\n  http://thread.gmane.org/gmane.comp.version-control.git/151297\n\nBut never prepared a proper patch. Here it is.\n\n builtin/merge.c                    |   38 ++++++++++++++++++++++++++++-------\n t/t7505-prepare-commit-msg-hook.sh |   12 +++++++++++\n 2 files changed, 42 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin/merge.c b/builtin/merge.c\nindex 42fff38..83389ed 100644\n--- a/builtin/merge.c\n+++ b/builtin/merge.c\n@@ -795,6 +795,33 @@ static void add_strategies(const char *string, unsigned attr)\n \n }\n \n+static void write_merge_msg(void)\n+{\n+\tint fd = open(git_path(\"MERGE_MSG\"), O_WRONLY | O_CREAT, 0666);\n+\tif (fd < 0)\n+\t\tdie_errno(\"Could not open '%s' for writing\",\n+\t\t\t  git_path(\"MERGE_MSG\"));\n+\tif (write_in_full(fd, merge_msg.buf, merge_msg.len) !=\n+\t\tmerge_msg.len)\n+\t\tdie_errno(\"Could not write to '%s'\", git_path(\"MERGE_MSG\"));\n+\tclose(fd);\n+}\n+\n+static void read_merge_msg(void)\n+{\n+\tstrbuf_reset(&merge_msg);\n+\tif (strbuf_read_file(&merge_msg, git_path(\"MERGE_MSG\"), 0) < 0)\n+\t\tdie_errno(\"Could not read from '%s'\", git_path(\"MERGE_MSG\"));\n+}\n+\n+static void run_prepare_commit_msg(void)\n+{\n+\twrite_merge_msg();\n+\trun_hook(get_index_file(), \"prepare-commit-msg\",\n+\t\t git_path(\"MERGE_MSG\"), \"merge\", NULL, NULL);\n+\tread_merge_msg();\n+}\n+\n static int merge_trivial(void)\n {\n \tunsigned char result_tree[20], result_commit[20];\n@@ -806,6 +833,7 @@ static int merge_trivial(void)\n \tparent->next = xmalloc(sizeof(*parent->next));\n \tparent->next->item = remoteheads->item;\n \tparent->next->next = NULL;\n+\trun_prepare_commit_msg();\n \tcommit_tree(merge_msg.buf, result_tree, parent, result_commit, NULL);\n \tfinish(result_commit, \"In-index merge\");\n \tdrop_save();\n@@ -835,6 +863,7 @@ static int finish_automerge(struct commit_list *common,\n \t}\n \tfree_commit_list(remoteheads);\n \tstrbuf_addch(&merge_msg, '\\n');\n+\trun_prepare_commit_msg();\n \tcommit_tree(merge_msg.buf, result_tree, parents, result_commit, NULL);\n \tstrbuf_addf(&buf, \"Merge made by %s.\", wt_strategy);\n \tfinish(result_commit, buf.buf);\n@@ -1316,14 +1345,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)\n \t\t\tdie_errno(\"Could not write to '%s'\", git_path(\"MERGE_HEAD\"));\n \t\tclose(fd);\n \t\tstrbuf_addch(&merge_msg, '\\n');\n-\t\tfd = open(git_path(\"MERGE_MSG\"), O_WRONLY | O_CREAT, 0666);\n-\t\tif (fd < 0)\n-\t\t\tdie_errno(\"Could not open '%s' for writing\",\n-\t\t\t\t  git_path(\"MERGE_MSG\"));\n-\t\tif (write_in_full(fd, merge_msg.buf, merge_msg.len) !=\n-\t\t\tmerge_msg.len)\n-\t\t\tdie_errno(\"Could not write to '%s'\", git_path(\"MERGE_MSG\"));\n-\t\tclose(fd);\n+\t\twrite_merge_msg();\n \t\tfd = open(git_path(\"MERGE_MODE\"), O_WRONLY | O_CREAT | O_TRUNC, 0666);\n \t\tif (fd < 0)\n \t\t\tdie_errno(\"Could not open '%s' for writing\",\ndiff --git a/t/t7505-prepare-commit-msg-hook.sh b/t/t7505-prepare-commit-msg-hook.sh\nindex ff18962..5b4b694 100755\n--- a/t/t7505-prepare-commit-msg-hook.sh\n+++ b/t/t7505-prepare-commit-msg-hook.sh\n@@ -132,6 +132,18 @@ test_expect_success 'with hook (-c)' '\n \n '\n \n+test_expect_success 'with hook (merge)' '\n+\n+\thead=`git rev-parse HEAD` &&\n+\tgit checkout -b other HEAD@{1} &&\n+\techo \"more\" >> file &&\n+\tgit add file &&\n+\tgit commit -m other &&\n+\tgit checkout - &&\n+\tgit merge other &&\n+\ttest \"`git log -1 --pretty=format:%s`\" = merge\n+'\n+\n cat > \"$HOOK\" <<'EOF'\n #!/bin/sh\n exit 1\n-- \n1.7.4.5.g9affb\n"},{"id":"161217","messageId":"7voc6dm13s.fsf@alter.siamese.dyndns.org","threadId":"26505","inReplyTo":"1297732070-32704-1-git-send-email-jaysoffian@gmail.com","subject":"Re: [PATCH] merge: honor prepare-commit-msg hook","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2011-02-15T21:36:39Z","receivedAt":"2011-02-15T21:36:39Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jay Soffian <jaysoffian@gmail.com> writes:\n\n> When a merge is stopped due to conflicts or --no-commit, the\n> subsequent commit calls the prepare-commit-msg hook. However,\n> it is not called after a clean merge. Fix this inconsistency\n> by invoking the hook after clean merges as well.\n>\n> Signed-off-by: Jay Soffian <jaysoffian@gmail.com>\n> ---\n> I sent this out previously as an RFC:\n>\n>   http://thread.gmane.org/gmane.comp.version-control.git/151297\n>\n> But never prepared a proper patch. Here it is.\n\nLooks very sensible.  Thanks.\n\nWill queue.\n"}]}