{"thread":{"id":"24018","subject":"[PATCH 1/2] commit.c - provide commit-type to the hooks/pre-commit script","startedAt":"2010-06-05T17:39:50Z","lastAt":"2010-06-08T02:55:27Z","messageCount":7,"participants":["Mark Levedahl","Jeff King","Johannes Sixt"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"143043","messageId":"1275759590-16342-1-git-send-email-mlevedahl@gmail.com","threadId":"24018","inReplyTo":null,"subject":"[PATCH 1/2] commit.c - provide commit-type to the hooks/pre-commit script","fromName":"Mark Levedahl","fromEmail":"mlevedahl@gmail.com","sentAt":"2010-06-05T17:39:50Z","receivedAt":"2010-06-05T17:39:50Z","isPatch":true,"sender":{"key":"mdl123@verizon.net","avatar":"https://avatars.githubusercontent.com/u/5302462?v=4"},"body":"If hooks/pre-commit acts based upon the changes to be checked in\nrather than just the resulting content, the script needs to know which\ncommit to use. For a normal commit, this is HEAD, but when amending this\nis HEAD~1. So, this modifies commit.c to pass $1 as normal|amend depending\nupon the commit type. Existing scripts are unaffected as they did not\nexpect any argument so will silently ignore this extra bit of info.\n\nSigned-off-by: Mark Levedahl <mlevedahl@gmail.com>\n---\n Documentation/githooks.txt         |    8 +++++---\n builtin/commit.c                   |    2 +-\n templates/hooks--pre-commit.sample |   10 ++++++++--\n 3 files changed, 14 insertions(+), 6 deletions(-)\n\ndiff --git a/Documentation/githooks.txt b/Documentation/githooks.txt\nindex 7183aa9..dbe1a1a 100644\n--- a/Documentation/githooks.txt\n+++ b/Documentation/githooks.txt\n@@ -73,9 +73,11 @@ pre-commit\n ~~~~~~~~~~\n \n This hook is invoked by 'git commit', and can be bypassed\n-with `\\--no-verify` option.  It takes no parameter, and is\n-invoked before obtaining the proposed commit log message and\n-making a commit.  Exiting with non-zero status from this script\n+with `\\--no-verify` option.  It takes a single parameter indicating\n+the commit type (normal|amend), and is invoked before obtaining the\n+proposed commit log message and making a commit.  For $1=normal,\n+the commit is against HEAD, while for $1=amend, the commit is\n+against HEAD~1.  Exiting with non-zero status from this script\n causes the 'git commit' to abort.\n \n The default 'pre-commit' hook, when enabled, catches introduction\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex ddf77e4..8543f09 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -544,7 +544,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,\n \tconst char *hook_arg2 = NULL;\n \tint ident_shown = 0;\n \n-\tif (!no_verify && run_hook(index_file, \"pre-commit\", NULL))\n+\tif (!no_verify && run_hook(index_file, \"pre-commit\", amend ? \"amend\" : \"normal\", NULL))\n \t\treturn 0;\n \n \tif (message.len) {\ndiff --git a/templates/hooks--pre-commit.sample b/templates/hooks--pre-commit.sample\nindex b187c4b..0c0c79f 100755\n--- a/templates/hooks--pre-commit.sample\n+++ b/templates/hooks--pre-commit.sample\n@@ -1,7 +1,8 @@\n #!/bin/sh\n #\n # An example hook script to verify what is about to be committed.\n-# Called by \"git commit\" with no arguments.  The hook should\n+# Called by \"git commit\" with a single argument indicating the\n+# commit-type (normal|amend). The hook should\n # exit with non-zero status after issuing an appropriate message if\n # it wants to stop the commit.\n #\n@@ -9,7 +10,12 @@\n \n if git rev-parse --verify HEAD >/dev/null 2>&1\n then\n-\tagainst=HEAD\n+\tif test \"$1\" = \"amend\"\n+\tthen\n+\t\tagainst=HEAD~1\n+\telse\n+\t\tagainst=HEAD\n+\tfi\n else\n \t# Initial commit: diff against an empty tree object\n \tagainst=4b825dc642cb6eb9a060e54bf8d69288fbee4904\n-- \n1.7.1.270.g2a29a\n"},{"id":"143044","messageId":"1275759800-16577-1-git-send-email-mlevedahl@gmail.com","threadId":"24018","inReplyTo":"1275759590-16342-1-git-send-email-mlevedahl@gmail.com","subject":"[PATCH 2/2] git-gui - provide commit-type to the hooks/pre-commit script","fromName":"Mark Levedahl","fromEmail":"mlevedahl@gmail.com","sentAt":"2010-06-05T17:43:20Z","receivedAt":"2010-06-05T17:43:20Z","isPatch":true,"sender":{"key":"mdl123@verizon.net","avatar":"https://avatars.githubusercontent.com/u/5302462?v=4"},"body":"If hooks/pre-commit acts based upon the changes to be checked in\nrather than just the resulting content, the script needs to know which\ncommit to use. For a normal commit, this is HEAD, but when amending this\nis HEAD~1. So, this modifies commit.c to pass $1 as normal|amend depending\nupon the commit type. Existing scripts are unaffected as they did not\nexpect any argument so will silently ignore this extra bit of info.\n\nSigned-off-by: Mark Levedahl <mlevedahl@gmail.com>\n---\n git-gui/lib/commit.tcl |    2 +-\n 1 files changed, 1 insertions(+), 1 deletions(-)\n\ndiff --git a/git-gui/lib/commit.tcl b/git-gui/lib/commit.tcl\nindex 7f459cd..8150fa6 100644\n--- a/git-gui/lib/commit.tcl\n+++ b/git-gui/lib/commit.tcl\n@@ -225,7 +225,7 @@ A good commit message has the following format:\n \n \t# -- Run the pre-commit hook.\n \t#\n-\tset fd_ph [githook_read pre-commit]\n+\tset fd_ph [githook_read pre-commit $commit_type]\n \tif {$fd_ph eq {}} {\n \t\tcommit_commitmsg $curHEAD $msg_p\n \t\treturn\n-- \n1.7.1.270.g2a29a\n"},{"id":"143116","messageId":"20100606221037.GE6993@coredump.intra.peff.net","threadId":"24018","inReplyTo":"1275759590-16342-1-git-send-email-mlevedahl@gmail.com","subject":"Re: [PATCH 1/2] commit.c - provide commit-type to the hooks/pre-commit script","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2010-06-06T22:10:37Z","receivedAt":"2010-06-06T22:10:37Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Sat, Jun 05, 2010 at 01:39:50PM -0400, Mark Levedahl wrote:\n\n> -\tif (!no_verify && run_hook(index_file, \"pre-commit\", NULL))\n> +\tif (!no_verify && run_hook(index_file, \"pre-commit\", amend ? \"amend\" : \"normal\", NULL))\n> [...]\n> -\tagainst=HEAD\n> +\tif test \"$1\" = \"amend\"\n> +\tthen\n> +\t\tagainst=HEAD~1\n> +\telse\n> +\t\tagainst=HEAD\n> +\tfi\n\nIs there a reason to use the magic \"amend\" and \"normal\" words, if\nscripts are just going to end up changing them back into HEAD~1 and HEAD\nanyway?\n\n-Peff\n"},{"id":"143128","messageId":"4C0C3B82.3030504@gmail.com","threadId":"24018","inReplyTo":"20100606221037.GE6993@coredump.intra.peff.net","subject":"Re: [PATCH 1/2] commit.c - provide commit-type to the hooks/pre-commit script","fromName":"Mark Levedahl","fromEmail":"mlevedahl@gmail.com","sentAt":"2010-06-07T00:21:22Z","receivedAt":"2010-06-07T00:21:22Z","isPatch":true,"sender":{"key":"mdl123@verizon.net","avatar":"https://avatars.githubusercontent.com/u/5302462?v=4"},"body":"On 06/06/2010 06:10 PM, Jeff King wrote:\n> On Sat, Jun 05, 2010 at 01:39:50PM -0400, Mark Levedahl wrote:\n>\n>    \n>> -\tif (!no_verify&&  run_hook(index_file, \"pre-commit\", NULL))\n>> +\tif (!no_verify&&  run_hook(index_file, \"pre-commit\", amend ? \"amend\" : \"normal\", NULL))\n>> [...]\n>> -\tagainst=HEAD\n>> +\tif test \"$1\" = \"amend\"\n>> +\tthen\n>> +\t\tagainst=HEAD~1\n>> +\telse\n>> +\t\tagainst=HEAD\n>> +\tfi\n>>      \n> Is there a reason to use the magic \"amend\" and \"normal\" words, if\n> scripts are just going to end up changing them back into HEAD~1 and HEAD\n> anyway?\n>\n> -Peff\n>\n>    \nWell, git-gui's internal $commit_type variable has the values \nnormal|amend, so I went with those. So , yes there is a reason, but \nmaybe not a great one :^). Given some consensus, this is trivial to \nchange. I'll let things cook a while longer before sending a new patch.\n\nMark\n"},{"id":"143139","messageId":"4C0C912D.9080404@viscovery.net","threadId":"24018","inReplyTo":"20100606221037.GE6993@coredump.intra.peff.net","subject":"Re: [PATCH 1/2] commit.c - provide commit-type to the hooks/pre-commit script","fromName":"Johannes Sixt","fromEmail":"j.sixt@viscovery.net","sentAt":"2010-06-07T06:26:53Z","receivedAt":"2010-06-07T06:26:53Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Am 6/7/2010 0:10, schrieb Jeff King:\n> On Sat, Jun 05, 2010 at 01:39:50PM -0400, Mark Levedahl wrote:\n> \n>> -\tif (!no_verify && run_hook(index_file, \"pre-commit\", NULL))\n>> +\tif (!no_verify && run_hook(index_file, \"pre-commit\", amend ? \"amend\" : \"normal\", NULL))\n>> [...]\n>> -\tagainst=HEAD\n>> +\tif test \"$1\" = \"amend\"\n>> +\tthen\n>> +\t\tagainst=HEAD~1\n>> +\telse\n>> +\t\tagainst=HEAD\n>> +\tfi\n> \n> Is there a reason to use the magic \"amend\" and \"normal\" words, if\n> scripts are just going to end up changing them back into HEAD~1 and HEAD\n> anyway?\n\npre-commit might act differently when a commit is amended, the most likely\nreason I can think of is to always allow to amend. When you have only a\nSHA1, you can get that information only with an additional process.\n\n-- Hannes\n"},{"id":"143141","messageId":"20100607063817.GA23361@coredump.intra.peff.net","threadId":"24018","inReplyTo":"4C0C912D.9080404@viscovery.net","subject":"Re: [PATCH 1/2] commit.c - provide commit-type to the hooks/pre-commit script","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2010-06-07T06:38:17Z","receivedAt":"2010-06-07T06:38:17Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, Jun 07, 2010 at 08:26:53AM +0200, Johannes Sixt wrote:\n\n> > Is there a reason to use the magic \"amend\" and \"normal\" words, if\n> > scripts are just going to end up changing them back into HEAD~1 and HEAD\n> > anyway?\n> \n> pre-commit might act differently when a commit is amended, the most likely\n> reason I can think of is to always allow to amend. When you have only a\n> SHA1, you can get that information only with an additional process.\n\nActually, I meant to provide the hook with the literal words \"HEAD~1\"\nand \"HEAD\", not the sha1. So they are effectively magic words, but they\nalso happen to be useful for directly feeding to git commands.\n\nIt also extends naturally to indicating a merge commit (\"HEAD\" or\n\"HEAD~1\" followed by some other ref). I don't know if that is useful or\nnot, but it seems like the same realm of information as whether or not\nwe are amending.\n\n-Peff\n"},{"id":"143201","messageId":"4C0DB11F.90904@gmail.com","threadId":"24018","inReplyTo":"20100607063817.GA23361@coredump.intra.peff.net","subject":"Re: [PATCH 1/2] commit.c - provide commit-type to the hooks/pre-commit script","fromName":"Mark Levedahl","fromEmail":"mlevedahl@gmail.com","sentAt":"2010-06-08T02:55:27Z","receivedAt":"2010-06-08T02:55:27Z","isPatch":true,"sender":{"key":"mdl123@verizon.net","avatar":"https://avatars.githubusercontent.com/u/5302462?v=4"},"body":"On 06/07/2010 02:38 AM, Jeff King wrote:\n> Actually, I meant to provide the hook with the literal words \"HEAD~1\"\n> and \"HEAD\", not the sha1. So they are effectively magic words, but they\n> also happen to be useful for directly feeding to git commands.\n>\n> It also extends naturally to indicating a merge commit (\"HEAD\" or\n> \"HEAD~1\" followed by some other ref). I don't know if that is useful or\n> not, but it seems like the same realm of information as whether or not\n> we are amending.\n>\n> -Peff\n>\n>    \nI agree with this, though I have no itch regarding all of the commit \nparents for a merge commit so I'll leave that for another to scratch. \nSo, I'm sending a new series to use HEAD~1 and HEAD rather than amend \nand normal.\n\nMark\n"}]}