{"thread":{"id":"9175","subject":"[PATCH v2] Teach git-commit about commit message templates.","startedAt":"2007-07-23T13:32:45Z","lastAt":"2007-07-23T13:32:45Z","messageCount":1,"participants":["Steven Grimm"],"isPatch":true,"patchVersion":2,"patchTotal":null},"messages":[{"id":"48308","messageId":"20070723133245.GA24760@midwinter.com","threadId":"9175","inReplyTo":null,"subject":"[PATCH v2] Teach git-commit about commit message templates.","fromName":"Steven Grimm","fromEmail":"koreth@midwinter.com","sentAt":"2007-07-23T13:32:45Z","receivedAt":"2007-07-23T13:32:45Z","isPatch":true,"sender":{"key":"koreth@midwinter.com","avatar":"https://gravatar.com/avatar/71b4d2e8b62f168bdc9e9205341159e3567003b4f9e2127c617c5fa0a1f5bad2?d=mp&s=160"},"body":"These are useful in organizations that enforce particular formats\nfor commit messages, e.g., to specify bug IDs or test plans.\nUse of the template is not enforced; it is simply used as the\ninitial content when the editor is invoked.\n\nSigned-off-by: Steven Grimm <koreth@midwinter.com>\n---\n\tUpdated based on feedback from Dscho. This depends on his\n\tpatch to add the --strip-comments option to git-stripspace.\n\tThat seemed like a pretty uncontroversial change to me, but\n\tI can easily switch back to \"grep -v\" to remove the\n\tdependency if desired.\n\n\tI am using diff > /dev/null rather than git diff --quiet\n\tbecause the latter doesn't work at the moment (see the test\n\tcase I just sent to the list.)\n\n Documentation/git-commit.txt |    8 ++++\n git-commit.sh                |   40 ++++++++++++++----\n t/t7500-commit.sh            |   96 ++++++++++++++++++++++++++++++++++++++++++\n t/t7500/add-comments         |    4 ++\n t/t7500/add-content          |    3 +\n t/t7500/add-signed-off       |    3 +\n 6 files changed, 146 insertions(+), 8 deletions(-)\n create mode 100755 t/t7500-commit.sh\n create mode 100755 t/t7500/add-comments\n create mode 100755 t/t7500/add-content\n create mode 100755 t/t7500/add-signed-off\n\ndiff --git a/Documentation/git-commit.txt b/Documentation/git-commit.txt\nindex 8e0e7e2..3f36c67 100644\n--- a/Documentation/git-commit.txt\n+++ b/Documentation/git-commit.txt\n@@ -74,6 +74,14 @@ OPTIONS\n -m <msg>|--message=<msg>::\n \tUse the given <msg> as the commit message.\n \n+-t <file>|--template=<file>::\n+\tUse the contents of the given file as the initial version\n+\tof the commit message. The editor is invoked and you can\n+\tmake subsequent changes. If a message is specified using\n+\tthe `-m` or `-F` options, this option has no effect. The\n+\ttemplate file may also be specified using the `commit.template`\n+\tconfiguration variable.\n+\n -s|--signoff::\n \tAdd Signed-off-by line at the end of the commit message.\n \ndiff --git a/git-commit.sh b/git-commit.sh\nindex 92749df..5a471e1 100755\n--- a/git-commit.sh\n+++ b/git-commit.sh\n@@ -3,7 +3,7 @@\n # Copyright (c) 2005 Linus Torvalds\n # Copyright (c) 2006 Junio C Hamano\n \n-USAGE='[-a | --interactive] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit> | --amend] [-u] [-e] [--author <author>] [[-i | -o] <path>...]'\n+USAGE='[-a | --interactive] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit> | --amend] [-u] [-e] [--author <author>] [--template <file>] [[-i | -o] <path>...]'\n SUBDIRECTORY_OK=Yes\n . git-sh-setup\n require_work_tree\n@@ -87,6 +87,7 @@ signoff=\n force_author=\n only_include_assumed=\n untracked_files=\n+templatefile=\"`git config commit.template`\"\n while case \"$#\" in 0) break;; esac\n do\n \tcase \"$1\" in\n@@ -248,6 +249,13 @@ $1\"\n \t\tsignoff=t\n \t\tshift\n \t\t;;\n+\t-t|--t|--te|--tem|--temp|--templ|--templa|--templat|--template)\n+\t\tcase \"$#\" in 1) usage ;; esac\n+\t\tshift\n+\t\ttemplatefile=\"$1\"\n+\t\tno_edit=\n+\t\tshift\n+\t\t;;\n \t-q|--q|--qu|--qui|--quie|--quiet)\n \t\tquiet=t\n \t\tshift\n@@ -321,6 +329,14 @@ t,,[1-9]*)\n \tdie \"No paths with -i does not make sense.\" ;;\n esac\n \n+if test ! -z \"$templatefile\" -a -z \"$log_given\"\n+then\n+\tif test ! -f \"$templatefile\"\n+\tthen\n+\t\tdie \"Commit template file does not exist.\"\n+\tfi\n+fi\n+\n ################################################################\n # Prepare index to have a tree to be committed\n \n@@ -454,6 +470,9 @@ then\n elif test -f \"$GIT_DIR/SQUASH_MSG\"\n then\n \tcat \"$GIT_DIR/SQUASH_MSG\"\n+elif test \"$templatefile\" != \"\"\n+then\n+\tcat \"$templatefile\"\n fi | git stripspace >\"$GIT_DIR\"/COMMIT_EDITMSG\n \n case \"$signoff\" in\n@@ -565,17 +584,22 @@ then\n \t    s///\n \t    q\n \t}\n-\t/^#/d\n     ' \"$GIT_DIR\"/COMMIT_EDITMSG\n else\n     cat \"$GIT_DIR\"/COMMIT_EDITMSG\n fi |\n-git stripspace >\"$GIT_DIR\"/COMMIT_MSG\n-\n-if cnt=`grep -v -i '^Signed-off-by' \"$GIT_DIR\"/COMMIT_MSG |\n-\tgit stripspace |\n-\twc -l` &&\n-   test 0 -lt $cnt\n+git stripspace --strip-comments >\"$GIT_DIR\"/COMMIT_MSG\n+\n+# Make sure the commit message has actual new content (something other than\n+# comments, Signed-off-by lines, and lines from the template.)\n+grep -v -i '^Signed-off-by' \"$GIT_DIR\"/COMMIT_MSG |\n+\tgit stripspace > \"$GIT_DIR\"/COMMIT_BAREMSG\n+trap 'rm -f \"$GIT_DIR\"/COMMIT_BAREMSG' 0\n+\n+if test -s \"$GIT_DIR\"/COMMIT_BAREMSG &&\n+\ttest -z \"$templatefile\" ||\n+\t! (git stripspace --strip-comments < \"$templatefile\" |\n+\t   diff - \"$GIT_DIR\"/COMMIT_BAREMSG > /dev/null)\n then\n \tif test -z \"$TMP_INDEX\"\n \tthen\ndiff --git a/t/t7500-commit.sh b/t/t7500-commit.sh\nnew file mode 100755\nindex 0000000..f11ada8\n--- /dev/null\n+++ b/t/t7500-commit.sh\n@@ -0,0 +1,96 @@\n+#!/bin/sh\n+#\n+# Copyright (c) 2007 Steven Grimm\n+#\n+\n+test_description='git-commit\n+\n+Tests for selected commit options.'\n+\n+. ./test-lib.sh\n+\n+commit_msg_is () {\n+\ttest \"`git log --pretty=format:%s%b -1`\" = \"$1\"\n+}\n+\n+# A sanity check to see if commit is working at all.\n+test_expect_success 'a basic commit in an empty tree should succeed' '\n+\techo content > foo &&\n+\tgit add foo &&\n+\tgit commit -m \"initial commit\"\n+'\n+\n+test_expect_success 'nonexistent template file should return error' '\n+\techo changes >> foo &&\n+\tgit add foo &&\n+\t! git commit --template \"$PWD\"/notexist\n+'\n+\n+test_expect_success 'nonexistent template file in config should return error' '\n+\tgit config commit.template \"$PWD\"/notexist &&\n+\t! git commit &&\n+\tgit config --unset commit.template\n+'\n+\n+# From now on we'll use a template file that exists.\n+TEMPLATE=\"$PWD\"/template\n+\n+test_expect_success 'unedited template should not commit' '\n+\techo \"template line\" > \"$TEMPLATE\" &&\n+\t! git commit --template \"$TEMPLATE\"\n+'\n+\n+test_expect_success 'unedited template with comments should not commit' '\n+\techo \"# comment in template\" >> \"$TEMPLATE\" &&\n+\t! git commit --template \"$TEMPLATE\"\n+'\n+\n+test_expect_success 'a Signed-off-by line by itself should not commit' '\n+\t! GIT_EDITOR=../t7500/add-signed-off git commit --template \"$TEMPLATE\"\n+'\n+\n+test_expect_success 'adding comments to a template should not commit' '\n+\t! GIT_EDITOR=../t7500/add-comments git commit --template \"$TEMPLATE\"\n+'\n+\n+test_expect_success 'adding real content to a template should commit' '\n+\tGIT_EDITOR=../t7500/add-content git commit --template \"$TEMPLATE\" &&\n+\tcommit_msg_is \"template linecommit message\"\n+'\n+\n+test_expect_success '-t option should be short for --template' '\n+\techo \"short template\" > \"$TEMPLATE\" &&\n+\techo \"new content\" >> foo &&\n+\tgit add foo &&\n+\tGIT_EDITOR=../t7500/add-content git commit -t \"$TEMPLATE\" &&\n+\tcommit_msg_is \"short templatecommit message\"\n+'\n+\n+test_expect_success 'config-specified template should commit' '\n+\techo \"new template\" > \"$TEMPLATE\" &&\n+\tgit config commit.template \"$TEMPLATE\" &&\n+\techo \"more content\" >> foo &&\n+\tgit add foo &&\n+\tGIT_EDITOR=../t7500/add-content git commit &&\n+\tgit config --unset commit.template &&\n+\tcommit_msg_is \"new templatecommit message\"\n+'\n+\n+test_expect_success 'explicit commit message should override template' '\n+\techo \"still more content\" >> foo &&\n+\tgit add foo &&\n+\tGIT_EDITOR=../t7500/add-content git commit --template \"$TEMPLATE\" \\\n+\t\t-m \"command line msg\" &&\n+\tcommit_msg_is \"command line msg<unknown>\"\n+'\n+\n+test_expect_success 'commit message from file should override template' '\n+\techo \"content galore\" >> foo &&\n+\tgit add foo &&\n+\techo \"standard input msg\" |\n+\t\tGIT_EDITOR=../t7500/add-content git commit \\\n+\t\t\t--template \"$TEMPLATE\" --file - &&\n+\tcommit_msg_is \"standard input msg<unknown>\"\n+'\n+\n+test_done\ndiff --git a/t/t7500/add-comments b/t/t7500/add-comments\nnew file mode 100755\nindex 0000000..a72e65c\n--- /dev/null\n+++ b/t/t7500/add-comments\n@@ -0,0 +1,4 @@\n+#!/bin/sh\n+echo \"# this is a new comment\" >> \"$1\"\n+echo \"# and so is this\" >> \"$1\"\n+exit 0\ndiff --git a/t/t7500/add-content b/t/t7500/add-content\nnew file mode 100755\nindex 0000000..2fa3d86\n--- /dev/null\n+++ b/t/t7500/add-content\n@@ -0,0 +1,3 @@\n+#!/bin/sh\n+echo \"commit message\" >> \"$1\"\n+exit 0\ndiff --git a/t/t7500/add-signed-off b/t/t7500/add-signed-off\nnew file mode 100755\nindex 0000000..e1d856a\n--- /dev/null\n+++ b/t/t7500/add-signed-off\n@@ -0,0 +1,3 @@\n+#!/bin/sh\n+echo \"Signed-off-by: foo <bar@frotz>\" >> \"$1\"\n+exit 0\n-- \n1.5.3.rc2.4.g726f9\n"}]}