From: Paolo Bonzini <bonzini@gnu.org>
To: Git Mailing List <git@vger.kernel.org>
Subject: [PATCH] git-commit: add a prepare-commit-msg hook
Date: Fri, 18 Jan 2008 15:51:25 +0100 [thread overview]
Message-ID: <4790BCED.4050207@gnu.org> (raw)
The prepare-commit-msg hook is run whenever a "fresh" commit message
(i.e. not one taken from another commit with -c) is shown in the editor.
It can modify the commit message in-place and/or abort the commit.
While the default hook just adds a Signed-Off-By line at the bottom
of the commit messsage, the hook is more intended to build a template
for the commit message following project standards.
Signed-Off-By: Paolo Bonzini <bonzini@gnu.org>
---
Documentation/git-commit.txt | 13 ++++++++-----
Documentation/hooks.txt | 21 +++++++++++++++++++++
builtin-commit.c | 3 +++
templates/hooks--commit-msg | 3 +++
templates/hooks--prepare-commit-msg | 14 ++++++++++++++
5 files changed, 49 insertions(+), 5 deletions(-)
create mode 100644 templates/hooks--prepare-commit-msg
The real motivation for this is building the commit message
from GNU ChangeLogs. But I figured that putting it in the
real commit message rather than a cover letter would have
implied rejection of the patch...
diff --git a/Documentation/git-commit.txt b/Documentation/git-commit.txt
index c3725b2..c68191b 100644
--- a/Documentation/git-commit.txt
+++ b/Documentation/git-commit.txt
@@ -62,18 +62,21 @@ OPTIONS
and the authorship information (including the timestamp)
when creating the commit. With '-C', the editor is not
invoked; with '-c' the user can further edit the commit
- message.
+ message. In either case, the prepare-commit-msg hook is
+ bypassed.
-F <file>::
Take the commit message from the given file. Use '-' to
- read the message from the standard input.
+ read the message from the standard input. This option
+ bypasses the prepare-commit-msg hook.
--author <author>::
Override the author name used in the commit. Use
`A U Thor <author@example.com>` format.
-m <msg>|--message=<msg>::
- Use the given <msg> as the commit message.
+ Use the given <msg> as the commit message. This option
+ bypasses the prepare-commit-msg hook.
-t <file>|--template=<file>::
Use the contents of the given file as the initial version
@@ -280,8 +283,8 @@ order).
HOOKS
-----
-This command can run `commit-msg`, `pre-commit`, and
-`post-commit` hooks. See link:hooks.html[hooks] for more
+This command can run `commit-msg`, `prepare-commit-msg`, `pre-commit`,
+and `post-commit` hooks. See link:hooks.html[hooks] for more
information.
diff --git a/Documentation/hooks.txt b/Documentation/hooks.txt
index f110162..2ef5567 100644
--- a/Documentation/hooks.txt
+++ b/Documentation/hooks.txt
@@ -61,6 +61,27 @@ The default 'pre-commit' hook, when enabled, catches introduction
of lines with trailing whitespaces and aborts the commit when
such a line is found.
+prepare-commit-msg
+------------------
+
+This hook is invoked by `git-commit` right before starting the editor
+with an empty log message. It is not executed if the commit message is
+specified otherwise, such as with the `\-m`, `\-F`, `\-c`, `\-C` options.
+It takes a single parameter, the name of the file that holds git's own
+template commit log message. Exiting with non-zero status causes
+`git-commit` to abort.
+
+The hook is allowed to edit the message file in place, and
+can be used to augment the default commit message with some
+project standard information. It can also be used for the same
+purpose as the pre-commit message, if the verification has
+to be skipped for automatic commits (e.g. during rebasing).
+
+The default 'prepare-commit-msg' hook adds a Signed-Off-By line
+(doing it with a hook is not necessarily a good idea, but doing
+it in 'commit-msg' is worse because you are not reminded in
+the editor).
+
commit-msg
----------
diff --git a/builtin-commit.c b/builtin-commit.c
index 0227936..1de0d02 100644
--- a/builtin-commit.c
+++ b/builtin-commit.c
@@ -869,6 +869,9 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
char index[PATH_MAX];
const char *env[2] = { index, NULL };
snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
+ if (!edit_message) {
+ run_hook(index_file, "prepare-commit-msg", git_path(commit_editmsg));
+ }
launch_editor(git_path(commit_editmsg), NULL, env);
}
if (!no_verify &&
diff --git a/templates/hooks--commit-msg b/templates/hooks--commit-msg
index c5cdb9d..4ef86eb 100644
--- a/templates/hooks--commit-msg
+++ b/templates/hooks--commit-msg
@@ -9,6 +9,9 @@
# To enable this hook, make this file executable.
# Uncomment the below to add a Signed-off-by line to the message.
+# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
+# hook is more suited to it.
+#
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
diff --git a/templates/hooks--prepare-commit-msg b/templates/hooks--prepare-commit-msg
new file mode 100644
index 0000000..176283b
--- /dev/null
+++ b/templates/hooks--prepare-commit-msg
@@ -0,0 +1,14 @@
+#!/bin/sh
+#
+# An example hook script to prepare the commit log message.
+# Called by git-commit with one argument, the name of the file
+# that has the commit message. The hook should exit with non-zero
+# status after issuing an appropriate message if it wants to stop the
+# commit. The hook is allowed to edit the commit message file.
+#
+# To enable this hook, make this file executable.
+
+# This example adds a Signed-off-by line to the message, that can
+# still be edited.
+SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
+grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
--
1.5.3.4.910.gc5122-dirty
next reply other threads:[~2008-01-18 14:51 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-01-18 14:51 Paolo Bonzini [this message]
2008-01-18 15:47 ` [PATCH] git-commit: add a prepare-commit-msg hook Johannes Schindelin
2008-01-18 15:51 ` Paolo Bonzini
2008-01-18 16:06 ` Johannes Schindelin
2008-01-18 16:37 ` Paolo Bonzini
2008-01-18 18:06 ` Johannes Schindelin
2008-01-18 18:51 ` Paolo Bonzini
2008-01-18 19:01 ` Benoit Sigoure
2008-01-18 19:05 ` Alex Riesen
2008-01-18 19:46 ` Paolo Bonzini
2008-01-18 21:08 ` Alex Riesen
2008-01-18 22:05 ` Junio C Hamano
2008-01-19 9:32 ` Paolo Bonzini
2008-01-19 11:20 ` Johannes Schindelin
2008-01-19 15:41 ` Benoit Sigoure
2008-01-19 16:04 ` Paolo Bonzini
2008-01-20 22:28 ` Junio C Hamano
2008-01-21 6:16 ` Paolo Bonzini
2008-01-21 11:04 ` Johannes Schindelin
2008-01-21 12:14 ` Paolo Bonzini
2008-01-21 12:46 ` Johannes Schindelin
2008-01-21 12:59 ` Paolo Bonzini
2008-01-21 22:44 ` Junio C Hamano
-- strict thread matches above, loose matches on Subject: below --
2008-01-21 14:27 Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4790BCED.4050207@gnu.org \
--to=bonzini@gnu.org \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).