Changeset View
Changeset View
Standalone View
Standalone View
.hooks/prepare-commit-msg
#!/bin/sh | #!/bin/sh | ||||
# prepare-commit-msg: Prepare a commit message upon `git commit` for the | # prepare-commit-msg: Prepare a commit message upon `git commit` for the | ||||
# user to edit. A script (rather than a static template) is used, so | # user to edit. A script (rather than a static template) is used, so | ||||
# that we can insert our template text other than at the top of the | # that we can insert our template text other than at the top of the | ||||
# message. | # message. Aims to automatically detect which ports were affected | ||||
# and suggests a commit title. | |||||
# | # | ||||
# Install by either setting the configuration of the repository to: | # Install by either setting the configuration of the repository to: | ||||
# git config --add core.hooksPath .hooks | # git config --add core.hooksPath .hooks | ||||
# or copy it to the hooks directory, but it will not get automatically updated: | # or copy it to the hooks directory, but it will not get automatically updated: | ||||
# cp .hooks/prepare-commit-msg .git/hooks/ | # cp .hooks/prepare-commit-msg .git/hooks/ | ||||
case "$2" in | case "$2" in | ||||
commit|message) | commit|message) | ||||
# It appears git invokes this script for interactive rebase but does | # It appears git invokes this script for interactive rebase but does | ||||
# not remove commented lines, so just exit if we're not called with the | # not remove commented lines, so just exit if we're not called with the | ||||
# default (comment-containing) template. | # default (comment-containing) template. | ||||
grep -E -q '^#' "$1" || exit 0 | grep -E -q '^#' "$1" || exit 0 | ||||
;; | ;; | ||||
template) | template) | ||||
exit 0 | exit 0 | ||||
;; | ;; | ||||
merge) | merge) | ||||
exit 0 | exit 0 | ||||
;; | ;; | ||||
esac | esac | ||||
outfile=$(mktemp /tmp/freebsd-git-commit.XXXXXXXX) | outfile=$(mktemp /tmp/freebsd-git-commit.XXXXXXXX) | ||||
if git rev-parse --verify HEAD >/dev/null 2>&1 | |||||
then | |||||
against=HEAD | |||||
else | |||||
# Initial commit: diff against an empty tree object | |||||
against=$(git hash-object -t tree /dev/null) | |||||
fi | |||||
PORTS="" | |||||
# only determine changed ports if we're not called for amend | |||||
if [ -z "$3" ]; then | |||||
FILES=$(git diff-index --name-only --cached \ | |||||
--diff-filter=ACMR $against -- ) | |||||
for f in $FILES; do | |||||
port=$(dirname $f | egrep "^[a-z]+/[^[:space:]]+$" \ | |||||
| awk -F/ '{ print $1"/"$2 }') | |||||
if [ -n "$port" ]; then | |||||
PORTS="${PORTS} $port" | |||||
fi | |||||
done | |||||
fi | |||||
if [ -n "$PORTS" ]; then | |||||
portslist=$(echo $PORTS | tr " " "\n" | sort | uniq) | |||||
portsnum=$(echo "$portslist" | wc -l) | |||||
affected_ports=$(echo $(echo "$portslist" | head -n2) \ | |||||
| sed "s/ /, /g") | |||||
if [ "$portsnum" -gt 2 ]; then | |||||
affected_ports="$affected_ports, and $(($portsnum - 2)) more" | |||||
fi | |||||
firstline="# Uncomment and add a short description of what changed: | |||||
# $(echo $affected_ports):$(echo " ") | |||||
" | |||||
fi | |||||
# Create a commit message template from three parts: | # Create a commit message template from three parts: | ||||
# | # | ||||
# 1. The beginning of the git-provided template (up to the first comment-only | # 1. The beginning of the git-provided template (up to the first comment-only | ||||
# line) which explains commented lines and such. | # line) which explains commented lines and such. | ||||
# | # | ||||
# 2. Our template. | # 2. Our template. | ||||
# | # | ||||
# 3. The remainder of the git-provided template (from the first comment-only | # 3. The remainder of the git-provided template (from the first comment-only | ||||
# line to the end of the file) which lists files staged for commit, files | # line to the end of the file) which lists files staged for commit, files | ||||
# not staged, and untracked files. | # not staged, and untracked files. | ||||
cat >"$outfile" <<EOF | cat >"$outfile" <<EOF | ||||
$(awk '1;/^#$/{exit}' "$1") | $firstline$(awk '1;/^#$/{exit}' "$1") | ||||
# category/port: Subject goes here, max 50 cols -| | # category/port: Subject goes here, max 50 cols -| | ||||
# <then a blank line> | # <then a blank line> | ||||
# 72 columns --| | # 72 columns --| | ||||
# | # | ||||
# Do not add a Submitted by line. If someone besides the committer sent in the | # Do not add a Submitted by line. If someone besides the committer sent in the | ||||
# change, the commit author should be set using \`git commit --author\`. | # change, the commit author should be set using \`git commit --author\`. | ||||
# | # | ||||
# Uncomment and complete these metadata fields, as appropriate: | # Uncomment and complete these metadata fields, as appropriate: | ||||
Show All 21 Lines |