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

[PATCH 1/2] commit.c - provide commit-type to the hooks/pre-commit script

From
Mark Levedahl <mlevedahl@gmail.com>
Date
Jun 5, 2010, 17:39 UTC
Message-ID
<1275759590-16342-1-git-send-email-mlevedahl@gmail.com>

If hooks/pre-commit acts based upon the changes to be checked in rather than just the resulting content, the script needs to know which commit to use. For a normal commit, this is HEAD, but when amending this is HEAD~1. So, this modifies commit.c to pass $1 as normal|amend depending upon the commit type. Existing scripts are unaffected as they did not expect any argument so will silently ignore this extra bit of info.

Signed-off-by: Mark Levedahl <mlevedahl@gmail.com>
---
 Documentation/githooks.txt         |    8 +++++---
 builtin/commit.c                   |    2 +-
 templates/hooks--pre-commit.sample |   10 ++++++++--
 3 files changed, 14 insertions(+), 6 deletions(-)
diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt
index 7183aa9..dbe1a1a 100644
--- a/Documentation/githooks.txt
+++ b/Documentation/githooks.txt
@@ -73,9 +73,11 @@ pre-commit
 ~~~~~~~~~~
 
 This hook is invoked by 'git commit', and can be bypassed
-with `\--no-verify` option.  It takes no parameter, and is
-invoked before obtaining the proposed commit log message and
-making a commit.  Exiting with non-zero status from this script
+with `\--no-verify` option.  It takes a single parameter indicating
+the commit type (normal|amend), and is invoked before obtaining the
+proposed commit log message and making a commit.  For $1=normal,
+the commit is against HEAD, while for $1=amend, the commit is
+against HEAD~1.  Exiting with non-zero status from this script
 causes the 'git commit' to abort.
 
 The default 'pre-commit' hook, when enabled, catches introduction
diff --git a/builtin/commit.c b/builtin/commit.c
index ddf77e4..8543f09 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -544,7 +544,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	const char *hook_arg2 = NULL;
 	int ident_shown = 0;
 
-	if (!no_verify && run_hook(index_file, "pre-commit", NULL))
+	if (!no_verify && run_hook(index_file, "pre-commit", amend ? "amend" : "normal", NULL))
 		return 0;
 
 	if (message.len) {
diff --git a/templates/hooks--pre-commit.sample b/templates/hooks--pre-commit.sample
index b187c4b..0c0c79f 100755
--- a/templates/hooks--pre-commit.sample
+++ b/templates/hooks--pre-commit.sample
@@ -1,7 +1,8 @@
 #!/bin/sh
 #
 # An example hook script to verify what is about to be committed.
-# Called by "git commit" with no arguments.  The hook should
+# Called by "git commit" with a single argument indicating the
+# commit-type (normal|amend). The hook should
 # exit with non-zero status after issuing an appropriate message if
 # it wants to stop the commit.
 #
@@ -9,7 +10,12 @@
 
 if git rev-parse --verify HEAD >/dev/null 2>&1
 then
-	against=HEAD
+	if test "$1" = "amend"
+	then
+		against=HEAD~1
+	else
+		against=HEAD
+	fi
 else
 	# Initial commit: diff against an empty tree object
 	against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
-- 
1.7.1.270.g2a29a
Next: Mark Levedahl
Message 1 of 7 in “commit.c - provide commit-type to the hooks/pre-commit script”
  1. 1/2 commit.c - provide commit-type to the hooks/pre-commit scriptMark Levedahl, Jun 5, 2010
  2. 2/2 git-gui - provide commit-type to the hooks/pre-commit scriptMark Levedahl, Jun 5, 2010
  3. Jeff KingJun 6, 2010
  4. Mark LevedahlJun 7, 2010
  5. Johannes SixtJun 7, 2010
  6. Jeff KingJun 7, 2010
  7. Mark LevedahlJun 8, 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.