@matheusbsbs / matheus-bagliano / commits / d1cc130a5e
Fork 0

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>

Steven Grimm committed Jul 22, 2007 at 21:17 UTC d1cc130a5eb50d5bfe1e8b76cab3d8970fd70ad1
6 files changed +163 -5
Documentation/git-commit.txt
+8
@@ -74,6 +74,14 @@ OPTIONS
74 -m <msg>|--message=<msg>::
75 Use the given <msg> as the commit message.
76
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 +
85 -s|--signoff::
86 Add Signed-off-by line at the end of the commit message.
87
git-commit.sh
+49 -5
@@ -3,7 +3,7 @@
3 # Copyright (c) 2005 Linus Torvalds
4 # Copyright (c) 2006 Junio C Hamano
5
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>...]'
7 SUBDIRECTORY_OK=Yes
8 . git-sh-setup
9 require_work_tree
@@ -87,6 +87,7 @@ signoff=
87 force_author=
88 only_include_assumed=
89 untracked_files=
90 +templatefile="`git config commit.template`"
91 while case "$#" in 0) break;; esac
92 do
93 case "$1" in
@@ -248,6 +249,13 @@ $1"
249 signoff=t
250 shift
251 ;;
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 + ;;
259 -q|--q|--qu|--qui|--quie|--quiet)
260 quiet=t
261 shift
@@ -321,6 +329,14 @@ t,,[1-9]*)
329 die "No paths with -i does not make sense." ;;
330 esac
331
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 +
340 ################################################################
341 # Prepare index to have a tree to be committed
342
@@ -454,6 +470,9 @@ then
470 elif test -f "$GIT_DIR/SQUASH_MSG"
471 then
472 cat "$GIT_DIR/SQUASH_MSG"
473 +elif test "$templatefile" != ""
474 +then
475 + cat "$templatefile"
476 fi | git stripspace >"$GIT_DIR"/COMMIT_EDITMSG
477
478 case "$signoff" in
@@ -572,10 +591,35 @@ else
591 fi |
592 git stripspace >"$GIT_DIR"/COMMIT_MSG
593
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"
623 then
624 if test -z "$TMP_INDEX"
625 then
t/t7500-commit.sh new
+96
@@ -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 new
+4
@@ -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 new
+3
@@ -0,0 +1,3 @@
1 +#!/bin/sh
2 +echo "commit message" >> "$1"
3 +exit 0
t/t7500/add-signed-off new
+3
@@ -0,0 +1,3 @@
1 +#!/bin/sh
2 +echo "Signed-off-by: foo <bar@frotz>" >> "$1"
3 +exit 0