Skip to content

Commit 9204575

Browse files
author
Eric James Michael Ritz
committed
Add a default Git commit template message
Git allows users to have "templates" for commits which, when they exist, automatically fill the user's editor with the template contents whenever they make a commit. This does not prevent the user from further editing the commit message; all it does is pre-populate the message with pre-written text. In an effort to help improve the quality of commit messages on PHP Mode, this patch introduces a commit template along with a Git hook that, when applied, will automatically fill commit messages with said template. The template has comments describing some basic best-practices for writing Git messages, and has commented-out lines of some metadata we use, i.e. lines like GitHub-Issue: #9001 Reviewed-by: Vegeta This patch updates the Makefile with a new rule, `dev`, that will install the hook. However, developers have another way to use the template, which will be necessary for anyone who happens to already be using the `prepare-commit-msg` hook for something: users can open `.git/config` in the project's top-level directory (tip: `git rev-parse --show-toplevel`) and add the following: [commit] template = ./etc/git/commit-template.txt This will apply the commit template for *every* commit. In contrast, the hook will only use the template if it can determine the user does not already have a template in place or that a different tool has pre-populated the commit message with some text. So in that regard the two approachs to using the template are different. Signed-off-by: Eric James Michael Ritz <ejmr@no.current.address>
1 parent cf1907b commit 9204575

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ $(AUTOLOADS): php-project.el php-mode.el
1919
clean:
2020
rm -f $(ELCS) $(AUTOLOADS)
2121

22+
# Perform any operations that will be useful for developers
23+
# who contribute to PHP Mode.
24+
dev:
25+
cp etc/git/prepare-commit-msg .git/hooks/prepare-commit-msg
26+
chmod u+x .git/hooks/prepare-commit-msg
27+
2228
# Runs all unit tests from php-mode-test.el and shows the results. The
2329
# script will exit with the status code zero if all tests pass. If any
2430
# test fails the script exits with a non-zero status and shows

etc/git/commit-template.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SUBJECT, 50 Characters, No Period
2+
# "If applied, this commit will..."
3+
4+
# BODY, 72 Characters
5+
# Answers the question, "Why?", not, "How?"
6+
7+
# METADATA
8+
# GitHub-Issue:
9+
# Resolves:
10+
# See-also:
11+
# Reviewed-by:
12+
# Special-thanks:
13+
# HANGING INDENT

etc/git/prepare-commit-msg

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh
2+
#
3+
# prepare-commit-msg
4+
# ==================
5+
#
6+
# ## SYNOPSIS
7+
#
8+
# This hook fills the user's editor with a pre-written commit message
9+
# template, intended to help contributors adhere to good style and
10+
# practices when it comes to writing Git commit messages.
11+
#
12+
########################################################################
13+
14+
# This hook will always recieve three arguments. We only care about the
15+
# first for our purposes, but still assign useful names to the others
16+
# in case we need them in the future.
17+
COMMIT_MESSAGE=$1
18+
COMMIT_SOURCE=$2
19+
COMMIT_SHA1=$3
20+
21+
# If the commit message already contains content then the developer
22+
# is probably using his or her own template. In which case we do not
23+
# trample over it. We test for a pre-existing template by reading
24+
# the first line of the commit message this hook recieved, and check
25+
# so see if it is an empty string (apply our template) or not (leave
26+
# the message alone).
27+
TITLE_LINE=$(head -n1 $COMMIT_MESSAGE)
28+
29+
if [ -z "$TITLE_LINE" ]; then
30+
project_dir=$(git rev-parse --show-toplevel)
31+
template="$project_dir/etc/git/commit-template.txt"
32+
echo "$(cat $template)\n$(cat $COMMIT_MESSAGE)" > $COMMIT_MESSAGE
33+
fi
34+
35+
exit 0

0 commit comments

Comments
 (0)