Skip to contentSkip to conversation

Teach git-commit about commit message templates.

PATCH New Steven Grimm 1 participant 1 message Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Teach git-commit about commit message templates.

These are useful in organizations that enforce particular formats for commit messages, e.g., to specify bug IDs or test plans. Use of the template is not enforced; it is simply used as the initial content when the editor is invoked.

---
Updated based on feedback from Dscho. This depends on his patch to add the --strip-comments option to git-stripspace. That seemed like a pretty uncontroversial change to me, but I can easily switch back to "grep -v" to remove the dependency if desired.

I am using diff > /dev/null rather than git diff --quiet because the latter doesn't work at the moment (see the test case I just sent to the list.)

 Documentation/git-commit.txt |    8 ++++
 git-commit.sh                |   40 ++++++++++++++----
 t/t7500-commit.sh            |   96 ++++++++++++++++++++++++++++++++++++++++++
 t/t7500/add-comments         |    4 ++
 t/t7500/add-content          |    3 +
 t/t7500/add-signed-off       |    3 +
 6 files changed, 146 insertions(+), 8 deletions(-)
 create mode 100755 t/t7500-commit.sh
 create mode 100755 t/t7500/add-comments
 create mode 100755 t/t7500/add-content
 create mode 100755 t/t7500/add-signed-off
Documentation/git-commit.txt +8 -0
index 8e0e7e2..3f36c67 100644
@@ -74,6 +74,14 @@ OPTIONS
 -m <msg>|--message=<msg>::
 	Use the given <msg> as the commit message.
 
+-t <file>|--template=<file>::
+	Use the contents of the given file as the initial version
+	of the commit message. The editor is invoked and you can
+	make subsequent changes. If a message is specified using
+	the `-m` or `-F` options, this option has no effect. The
+	template file may also be specified using the `commit.template`
+	configuration variable.
+
 -s|--signoff::
 	Add Signed-off-by line at the end of the commit message.
 
git-commit.sh +32 -8
index 92749df..5a471e1 100755
@@ -3,7 +3,7 @@
 # Copyright (c) 2005 Linus Torvalds
 # Copyright (c) 2006 Junio C Hamano
 
-USAGE='[-a | --interactive] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit> | --amend] [-u] [-e] [--author <author>] [[-i | -o] <path>...]'
+USAGE='[-a | --interactive] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit> | --amend] [-u] [-e] [--author <author>] [--template <file>] [[-i | -o] <path>...]'
 SUBDIRECTORY_OK=Yes
 . git-sh-setup
 require_work_tree
@@ -87,6 +87,7 @@ signoff=
 force_author=
 only_include_assumed=
 untracked_files=
+templatefile="`git config commit.template`"
 while case "$#" in 0) break;; esac
 do
 	case "$1" in
@@ -248,6 +249,13 @@ $1"
 		signoff=t
 		shift
 		;;
+	-t|--t|--te|--tem|--temp|--templ|--templa|--templat|--template)
+		case "$#" in 1) usage ;; esac
+		shift
+		templatefile="$1"
+		no_edit=
+		shift
+		;;
 	-q|--q|--qu|--qui|--quie|--quiet)
 		quiet=t
 		shift
@@ -321,6 +329,14 @@ t,,[1-9]*)
 	die "No paths with -i does not make sense." ;;
 esac
 
+if test ! -z "$templatefile" -a -z "$log_given"
+then
+	if test ! -f "$templatefile"
+	then
+		die "Commit template file does not exist."
+	fi
+fi
+
 ################################################################
 # Prepare index to have a tree to be committed
 
@@ -454,6 +470,9 @@ then
 elif test -f "$GIT_DIR/SQUASH_MSG"
 then
 	cat "$GIT_DIR/SQUASH_MSG"
+elif test "$templatefile" != ""
+then
+	cat "$templatefile"
 fi | git stripspace >"$GIT_DIR"/COMMIT_EDITMSG
 
 case "$signoff" in
@@ -565,17 +584,22 @@ then
 	    s///
 	    q
 	}
-	/^#/d
     ' "$GIT_DIR"/COMMIT_EDITMSG
 else
     cat "$GIT_DIR"/COMMIT_EDITMSG
 fi |
-git stripspace >"$GIT_DIR"/COMMIT_MSG
-
-if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
-	git stripspace |
-	wc -l` &&
-   test 0 -lt $cnt
+git stripspace --strip-comments >"$GIT_DIR"/COMMIT_MSG
+
+# Make sure the commit message has actual new content (something other than
+# comments, Signed-off-by lines, and lines from the template.)
+grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
+	git stripspace > "$GIT_DIR"/COMMIT_BAREMSG
+trap 'rm -f "$GIT_DIR"/COMMIT_BAREMSG' 0
+
+if test -s "$GIT_DIR"/COMMIT_BAREMSG &&
+	test -z "$templatefile" ||
+	! (git stripspace --strip-comments < "$templatefile" |
+	   diff - "$GIT_DIR"/COMMIT_BAREMSG > /dev/null)
 then
 	if test -z "$TMP_INDEX"
 	then
t/t7500-commit.sh new file +96 -0

96 lines not on this page · raw message

t/t7500/add-comments new file +4 -0
new file mode 100755
index 0000000..a72e65c
@@ -0,0 +1,4 @@
+#!/bin/sh
+echo "# this is a new comment" >> "$1"
+echo "# and so is this" >> "$1"
+exit 0
t/t7500/add-content new file +3 -0
new file mode 100755
index 0000000..2fa3d86
@@ -0,0 +1,3 @@
+#!/bin/sh
+echo "commit message" >> "$1"
+exit 0
t/t7500/add-signed-off new file +3 -0
new file mode 100755
index 0000000..e1d856a
@@ -0,0 +1,3 @@
+#!/bin/sh
+echo "Signed-off-by: foo <bar@frotz>" >> "$1"
+exit 0
Signature · 1 line
1.5.3.rc2.4.g726f9