{"thread":{"id":"24441","subject":"[RFC/PATCH] merge: honor prepare-commit-msg hook","startedAt":"2010-07-20T04:29:04Z","lastAt":"2010-07-22T00:14:44Z","messageCount":2,"participants":["Jay Soffian","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"145836","messageId":"1279600144-89629-1-git-send-email-jaysoffian@gmail.com","threadId":"24441","inReplyTo":null,"subject":"[RFC/PATCH] merge: honor prepare-commit-msg hook","fromName":"Jay Soffian","fromEmail":"jaysoffian@gmail.com","sentAt":"2010-07-20T04:29:04Z","receivedAt":"2010-07-20T04:29:04Z","isPatch":true,"sender":{"key":"jaysoffian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/155970?v=4"},"body":"---\nI couldn't figure out why my prepare-commit-msg wasn't being honored\nby git merge. I assumed that the standard commit hooks would be honored,\nas after-all, merge is creating a new commit. Alas, this is not the case\nsince merge calls commit_tree directly.\n\nI wonder whether the low-level commit_tree should be responsible for\ninvoking this hook instead of the commit porcelain?\n\nIn any case, this does what I want, but I'm not sure it's the best approach.\n\nComments?\n\n builtin/merge.c |   38 ++++++++++++++++++++++++++++++--------\n 1 files changed, 30 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin/merge.c b/builtin/merge.c\nindex 37ce4f5..28a9960 100644\n--- a/builtin/merge.c\n+++ b/builtin/merge.c\n@@ -768,6 +768,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@@ -779,6 +806,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@@ -808,6 +836,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@@ -1276,14 +1305,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\",\n-- \n1.7.2.rc3.53.g670aa\n"},{"id":"145973","messageId":"7vsk3cgyob.fsf@alter.siamese.dyndns.org","threadId":"24441","inReplyTo":"1279600144-89629-1-git-send-email-jaysoffian@gmail.com","subject":"Re: [RFC/PATCH] merge: honor prepare-commit-msg hook","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-07-22T00:14:44Z","receivedAt":"2010-07-22T00:14:44Z","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> ---\n> I couldn't figure out why my prepare-commit-msg wasn't being honored\n> by git merge.\n\nIt has been that way from day one, it appears.\n\nThe bypassing of pre-commit hook was and remains to be a conscious design\ndecision.  When you are pulling from your contributors who may have\nobjectionable contents that you have to merge, the damage is already\ndone; you _could_ yell at them to fix their branch and re-pull in theory,\nbut that wouldn't work very well in practice.\n\nOn the other hand, I think letting people use prepare-commit-msg for\nmerges might  make sense.  Indeed, \"git commit\" is prepared to call\nprepare-commit-msg telling the hook that it is concluding a merge, when\nyour \"git merge\" stopped due to a conflict (or you stopped it from making\na new commit with --no-commit).\n\nI don't know about the other hooks \"git commit\" normally calls.  Both\n\"commit-msg\" and \"post-commit\" may make sense, but I don't care too deeply\neither way---I don't care too deeply for pre-commit either ;-).\n"}]}