Skip to content

Commit d1cc130

Browse files
sgrimmgitster
authored andcommitted
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. Signed-off-by: Steven Grimm <koreth@midwinter.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent af66366 commit d1cc130

File tree

6 files changed

+163
-5
lines changed

6 files changed

+163
-5
lines changed

Documentation/git-commit.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ OPTIONS
7474
-m <msg>|--message=<msg>::
7575
Use the given <msg> as the commit message.
7676

77+
-t <file>|--template=<file>::
78+
Use the contents of the given file as the initial version
79+
of the commit message. The editor is invoked and you can
80+
make subsequent changes. If a message is specified using
81+
the `-m` or `-F` options, this option has no effect. The
82+
template file may also be specified using the `commit.template`
83+
configuration variable.
84+
7785
-s|--signoff::
7886
Add Signed-off-by line at the end of the commit message.
7987

git-commit.sh

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright (c) 2005 Linus Torvalds
44
# Copyright (c) 2006 Junio C Hamano
55

6-
USAGE='[-a | --interactive] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit> | --amend] [-u] [-e] [--author <author>] [[-i | -o] <path>...]'
6+
USAGE='[-a | --interactive] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit> | --amend] [-u] [-e] [--author <author>] [--template <file>] [[-i | -o] <path>...]'
77
SUBDIRECTORY_OK=Yes
88
. git-sh-setup
99
require_work_tree
@@ -87,6 +87,7 @@ signoff=
8787
force_author=
8888
only_include_assumed=
8989
untracked_files=
90+
templatefile="`git config commit.template`"
9091
while case "$#" in 0) break;; esac
9192
do
9293
case "$1" in
@@ -248,6 +249,13 @@ $1"
248249
signoff=t
249250
shift
250251
;;
252+
-t|--t|--te|--tem|--temp|--templ|--templa|--templat|--template)
253+
case "$#" in 1) usage ;; esac
254+
shift
255+
templatefile="$1"
256+
no_edit=
257+
shift
258+
;;
251259
-q|--q|--qu|--qui|--quie|--quiet)
252260
quiet=t
253261
shift
@@ -321,6 +329,14 @@ t,,[1-9]*)
321329
die "No paths with -i does not make sense." ;;
322330
esac
323331

332+
if test ! -z "$templatefile" -a -z "$log_given"
333+
then
334+
if test ! -f "$templatefile"
335+
then
336+
die "Commit template file does not exist."
337+
fi
338+
fi
339+
324340
################################################################
325341
# Prepare index to have a tree to be committed
326342

@@ -454,6 +470,9 @@ then
454470
elif test -f "$GIT_DIR/SQUASH_MSG"
455471
then
456472
cat "$GIT_DIR/SQUASH_MSG"
473+
elif test "$templatefile" != ""
474+
then
475+
cat "$templatefile"
457476
fi | git stripspace >"$GIT_DIR"/COMMIT_EDITMSG
458477

459478
case "$signoff" in
@@ -572,10 +591,35 @@ else
572591
fi |
573592
git stripspace >"$GIT_DIR"/COMMIT_MSG
574593

575-
if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
576-
git stripspace |
577-
wc -l` &&
578-
test 0 -lt $cnt
594+
# Test whether the commit message has any content we didn't supply.
595+
have_commitmsg=
596+
grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
597+
git stripspace > "$GIT_DIR"/COMMIT_BAREMSG
598+
599+
# Is the commit message totally empty?
600+
if test -s "$GIT_DIR"/COMMIT_BAREMSG
601+
then
602+
if test "$templatefile" != ""
603+
then
604+
# Test whether this is just the unaltered template.
605+
if cnt=`sed -e '/^#/d' < "$templatefile" |
606+
git stripspace |
607+
diff "$GIT_DIR"/COMMIT_BAREMSG - |
608+
wc -l` &&
609+
test 0 -lt $cnt
610+
then
611+
have_commitmsg=t
612+
fi
613+
else
614+
# No template, so the content in the commit message must
615+
# have come from the user.
616+
have_commitmsg=t
617+
fi
618+
fi
619+
620+
rm -f "$GIT_DIR"/COMMIT_BAREMSG
621+
622+
if test "$have_commitmsg" = "t"
579623
then
580624
if test -z "$TMP_INDEX"
581625
then

t/t7500-commit.sh

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2007 Steven Grimm
4+
#
5+
6+
test_description='git-commit
7+
8+
Tests for selected commit options.'
9+
10+
. ./test-lib.sh
11+
12+
commit_msg_is () {
13+
test "`git log --pretty=format:%s%b -1`" = "$1"
14+
}
15+
16+
# A sanity check to see if commit is working at all.
17+
test_expect_success 'a basic commit in an empty tree should succeed' '
18+
echo content > foo &&
19+
git add foo &&
20+
git commit -m "initial commit"
21+
'
22+
23+
test_expect_success 'nonexistent template file should return error' '
24+
echo changes >> foo &&
25+
git add foo &&
26+
! git commit --template "$PWD"/notexist
27+
'
28+
29+
test_expect_success 'nonexistent template file in config should return error' '
30+
git config commit.template "$PWD"/notexist &&
31+
! git commit &&
32+
git config --unset commit.template
33+
'
34+
35+
# From now on we'll use a template file that exists.
36+
TEMPLATE="$PWD"/template
37+
38+
test_expect_success 'unedited template should not commit' '
39+
echo "template line" > "$TEMPLATE" &&
40+
! git commit --template "$TEMPLATE"
41+
'
42+
43+
test_expect_success 'unedited template with comments should not commit' '
44+
echo "# comment in template" >> "$TEMPLATE" &&
45+
! git commit --template "$TEMPLATE"
46+
'
47+
48+
test_expect_success 'a Signed-off-by line by itself should not commit' '
49+
! GIT_EDITOR=../t7500/add-signed-off git commit --template "$TEMPLATE"
50+
'
51+
52+
test_expect_success 'adding comments to a template should not commit' '
53+
! GIT_EDITOR=../t7500/add-comments git commit --template "$TEMPLATE"
54+
'
55+
56+
test_expect_success 'adding real content to a template should commit' '
57+
GIT_EDITOR=../t7500/add-content git commit --template "$TEMPLATE" &&
58+
commit_msg_is "template linecommit message"
59+
'
60+
61+
test_expect_success '-t option should be short for --template' '
62+
echo "short template" > "$TEMPLATE" &&
63+
echo "new content" >> foo &&
64+
git add foo &&
65+
GIT_EDITOR=../t7500/add-content git commit -t "$TEMPLATE" &&
66+
commit_msg_is "short templatecommit message"
67+
'
68+
69+
test_expect_success 'config-specified template should commit' '
70+
echo "new template" > "$TEMPLATE" &&
71+
git config commit.template "$TEMPLATE" &&
72+
echo "more content" >> foo &&
73+
git add foo &&
74+
GIT_EDITOR=../t7500/add-content git commit &&
75+
git config --unset commit.template &&
76+
commit_msg_is "new templatecommit message"
77+
'
78+
79+
test_expect_success 'explicit commit message should override template' '
80+
echo "still more content" >> foo &&
81+
git add foo &&
82+
GIT_EDITOR=../t7500/add-content git commit --template "$TEMPLATE" \
83+
-m "command line msg" &&
84+
commit_msg_is "command line msg<unknown>"
85+
'
86+
87+
test_expect_success 'commit message from file should override template' '
88+
echo "content galore" >> foo &&
89+
git add foo &&
90+
echo "standard input msg" |
91+
GIT_EDITOR=../t7500/add-content git commit \
92+
--template "$TEMPLATE" --file - &&
93+
commit_msg_is "standard input msg<unknown>"
94+
'
95+
96+
test_done

t/t7500/add-comments

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
echo "# this is a new comment" >> "$1"
3+
echo "# and so is this" >> "$1"
4+
exit 0

t/t7500/add-content

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
echo "commit message" >> "$1"
3+
exit 0

t/t7500/add-signed-off

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
echo "Signed-off-by: foo <bar@frotz>" >> "$1"
3+
exit 0

0 commit comments

Comments
 (0)