1#!/bin/sh
2
3# prepare-commit-msg: Prepare a commit message upon `git commit` for the
4# user to edit.  A script (rather than a static template) is used, so
5# that we can insert our template text other than at the top of the
6# message.
7#
8# Install by copying into the git hooks directory - for example,
9# cp tools/tools/git/hooks/prepare-commit-msg .git/hooks/
10
11case "$2" in
12commit|message)
13	# It appears git invokes this script for interactive rebase but does
14	# not remove commented lines, so just exit if we're not called with the
15	# default (comment-containing) template.
16	grep -E -q '^#' "$1" || exit 0
17	;;
18template)
19	exit 0
20	;;
21merge)
22	exit 0
23	;;
24esac
25
26outfile=$(mktemp /tmp/freebsd-git-commit.XXXXXXXX)
27
28# Create a commit message template from three parts:
29#
30# 1. The beginning of the git-provided template (up to the first comment-only
31#    line) which explains commented lines and such.
32#
33# 2. Our template.
34#
35# 3. The remainder of the git-provided template (from the first comment-only
36#    line to the end of the file) which lists files staged for commit, files
37#    not staged, and untracked files.
38
39cat >$outfile <<EOF
40$(awk '1;/^#$/{exit}' $1)
41#                                                         72 columns --|
42#
43# Uncomment and complete these metadata fields, as appropriate:
44#
45# PR:		<If and which Problem Report is related.>
46# Reported by:	<If someone else reported the issue.>
47# Reviewed by:	<If someone else reviewed your modification.>
48# Tested by:	<If someone else tested the change.>
49# Approved by:	<If you needed approval for this commit.>
50# Obtained from:	<If the change is from a third party.>
51# Fixes:	<Short hash and title line of commit fixed by this change>
52# MFC after:	<N [day[s]|week[s]|month[s]].  Request a reminder email>
53# Relnotes:	<Set to 'yes' for mention in release notes.>
54# Security:	<Vulnerability reference (one per line) or description.>
55# Sponsored by:	<If the change was sponsored by an organization.>
56# Pull Request:	<https://github.com/freebsd/<repo>/pull/###>
57# Differential Revision:	<https://reviews.freebsd.org/D###>
58#
59# "Pull Request" and "Differential Revision" require the *full* GitHub or
60# Phabricator URL.  The commit author should be set appropriately, using
61# \`git commit --author\` if someone besides the committer sent in the change.
62$(awk '/^#$/,EOF' "$1")
63EOF
64
65mv "$outfile" "$1"
66