2010-08-03 15:00:14 +00:00
|
|
|
#!/usr/bin/env bash
|
2010-08-27 20:43:57 +00:00
|
|
|
#=============================================================================
|
2012-02-29 21:26:33 +00:00
|
|
|
# Copyright 2010-2012 Kitware, Inc.
|
2010-08-27 20:43:57 +00:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#=============================================================================
|
2010-03-31 21:39:00 +00:00
|
|
|
|
2012-02-29 21:26:33 +00:00
|
|
|
. "${BASH_SOURCE%/*}/hooks-config.bash"
|
|
|
|
|
2013-04-04 17:48:59 +00:00
|
|
|
# Start with project-specific hook.
|
|
|
|
hooks_start commit-msg "$@"
|
|
|
|
|
2010-04-28 17:35:19 +00:00
|
|
|
# Prepare a copy of the message:
|
|
|
|
# - strip comment lines
|
|
|
|
# - stop at "diff --git" (git commit -v)
|
2010-04-22 15:42:14 +00:00
|
|
|
commit_msg="$GIT_DIR/COMMIT_MSG"
|
2010-04-28 17:35:19 +00:00
|
|
|
sed -n -e '/^#/d' -e '/^diff --git/q' -e 'p;d' "$1" > "$commit_msg"
|
2010-04-22 15:42:14 +00:00
|
|
|
|
2011-01-11 23:01:50 +00:00
|
|
|
die_advice='
|
|
|
|
To continue editing, run the command
|
|
|
|
git commit -e -F '"$commit_msg"'
|
|
|
|
(assuming your working directory is at the top).'
|
|
|
|
|
2010-03-31 21:39:00 +00:00
|
|
|
die() {
|
|
|
|
echo 'commit-msg hook failure' 1>&2
|
|
|
|
echo '-----------------------' 1>&2
|
|
|
|
echo '' 1>&2
|
|
|
|
echo "$@" 1>&2
|
2011-01-11 23:01:50 +00:00
|
|
|
test -n "$die_advice" && echo "$die_advice" 1>&2
|
2010-03-31 21:39:00 +00:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# Check the commit message layout with a simple state machine.
|
|
|
|
|
2010-09-14 18:59:45 +00:00
|
|
|
msg_is_merge() {
|
|
|
|
echo "$line" | grep "^Merge " >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
2010-11-17 22:54:50 +00:00
|
|
|
msg_is_revert() {
|
|
|
|
echo "$line" | grep "^Revert " >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
2010-03-31 21:39:00 +00:00
|
|
|
msg_first() {
|
|
|
|
len=$(echo -n "$line" | wc -c)
|
|
|
|
if test $len -eq 0; then
|
|
|
|
# not yet first line
|
|
|
|
return
|
|
|
|
elif test $len -lt 8; then
|
|
|
|
die 'The first line must be at least 8 characters:
|
|
|
|
--------
|
|
|
|
'"$line"'
|
2010-08-30 17:26:19 +00:00
|
|
|
--------'
|
2010-11-17 22:54:50 +00:00
|
|
|
elif test $len -gt 78 && ! msg_is_merge && ! msg_is_revert; then
|
2010-03-31 21:39:00 +00:00
|
|
|
die 'The first line may be at most 78 characters:
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
'"$line"'
|
2010-08-30 17:26:19 +00:00
|
|
|
------------------------------------------------------------------------------'
|
2010-06-16 19:53:39 +00:00
|
|
|
elif echo "$line" | grep "^[ ]\|[ ]$" >/dev/null 2>&1; then
|
|
|
|
die 'The first line may not have leading or trailing space:
|
2010-08-30 17:26:19 +00:00
|
|
|
['"$line"']'
|
2010-03-31 21:39:00 +00:00
|
|
|
else
|
|
|
|
# first line okay
|
|
|
|
state=second
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_second() {
|
|
|
|
if test "x$line" != "x"; then
|
|
|
|
die 'The second line must be empty:
|
|
|
|
'"$line"
|
|
|
|
else
|
|
|
|
state=rest
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_rest() {
|
2010-12-30 17:13:20 +00:00
|
|
|
if echo "$line" | grep -q "^Change-Id:"; then
|
|
|
|
state=gerrit
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_gerrit() {
|
|
|
|
test "x$line" = "x" && return
|
|
|
|
echo "$line" | grep -q '^[A-Za-z0-9-][A-Za-z0-9-]*:' && return
|
|
|
|
die 'The Change-Id line must appear in a footer at the bottom.'
|
2010-03-31 21:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Pipe commit message into the state machine.
|
|
|
|
state=first
|
2010-04-22 15:42:14 +00:00
|
|
|
cat "$commit_msg" |
|
2010-06-16 19:53:39 +00:00
|
|
|
while IFS='' read line; do
|
2010-03-31 21:39:00 +00:00
|
|
|
msg_$state || break
|
2010-04-22 15:42:14 +00:00
|
|
|
done &&
|
2010-08-30 17:27:14 +00:00
|
|
|
rm -f "$commit_msg" || exit 1
|
2011-01-11 23:01:50 +00:00
|
|
|
die_advice='' # No more temporary message file.
|
2010-08-30 17:27:14 +00:00
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# Optionally run Gerrit's commit-msg hook to add a Change-Id line.
|
|
|
|
|
|
|
|
gerrit_advice() {
|
|
|
|
gerrits=$(git config -l |grep '^remote\.[^=]\+url=.*review.*$')
|
2010-08-31 19:21:15 +00:00
|
|
|
test "x$gerrits" != "x" || return 0
|
2010-08-30 17:27:14 +00:00
|
|
|
|
|
|
|
echo 'Some config values look like Gerrit Code Review URLs:'
|
|
|
|
echo ''
|
|
|
|
echo "$gerrits" | sed 's/^/ /'
|
|
|
|
echo '
|
|
|
|
This hook can automatically add a "Change-Id" footer to commit messages
|
|
|
|
to make interaction with Gerrit easier. To enable this feature, run
|
|
|
|
|
|
|
|
git config hooks.GerritId true
|
|
|
|
|
|
|
|
Then run "git commit --amend" to fix this commit. Otherwise, run
|
|
|
|
|
|
|
|
git config hooks.GerritId false
|
|
|
|
|
|
|
|
to disable the feature and this message.'
|
|
|
|
}
|
|
|
|
|
|
|
|
gerrit_error() {
|
|
|
|
die 'non-bool config value hooks.GerritId = '"$hooks_GerritId"
|
|
|
|
}
|
|
|
|
|
|
|
|
gerrit_hook() {
|
2012-10-10 14:53:23 +00:00
|
|
|
"$HOOKS_DIR/gerrit/commit-msg" "$@" ||
|
2010-08-30 17:27:14 +00:00
|
|
|
die 'gerrit/commit-msg failed'
|
|
|
|
}
|
|
|
|
|
|
|
|
hooks_GerritId=$(git config --get hooks.GerritId)
|
|
|
|
case "$hooks_GerritId" in
|
|
|
|
'true') gerrit_hook "$@" ;;
|
|
|
|
'false') ;;
|
|
|
|
'') gerrit_advice ;;
|
|
|
|
*) gerrit_error ;;
|
|
|
|
esac
|
2010-11-16 22:40:16 +00:00
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# Chain to project-specific hook.
|
|
|
|
hooks_chain commit-msg "$@"
|