Handle messages with only comments in the commit-msg hook
Commit messages with comments occur when the editor is aborted, and should be treated as empty. We do this using git-stripspace, which was introduced in 2005, so this has no compatibility implications. Change-Id: I78b50a789860cc11d63d891b0507786890158754
This commit is contained in:
parent
572f4e3804
commit
627d07c2bf
@ -26,6 +26,18 @@ function test_empty {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function test_empty_with_comments {
|
||||||
|
rm -f input
|
||||||
|
cat << EOF > input
|
||||||
|
# comment
|
||||||
|
|
||||||
|
# comment2
|
||||||
|
EOF
|
||||||
|
if ${hook} input ; then
|
||||||
|
fail "must fail on empty message"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
# a Change-Id already set is preserved.
|
# a Change-Id already set is preserved.
|
||||||
function test_preserve_changeid {
|
function test_preserve_changeid {
|
||||||
cat << EOF > input
|
cat << EOF > input
|
||||||
@ -113,7 +125,7 @@ EOF
|
|||||||
|
|
||||||
|
|
||||||
# Test driver.
|
# Test driver.
|
||||||
|
git init
|
||||||
for func in $( declare -F | awk '{print $3;}' | sort); do
|
for func in $( declare -F | awk '{print $3;}' | sort); do
|
||||||
case ${func} in
|
case ${func} in
|
||||||
test_*)
|
test_*)
|
||||||
|
@ -27,17 +27,36 @@ if test ! -f "$1" ; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if test ! -s "$1" ; then
|
|
||||||
echo "file is empty: $1"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# $RANDOM will be undefined if not using bash, so don't use set -u
|
# $RANDOM will be undefined if not using bash, so don't use set -u
|
||||||
random=$( (whoami ; hostname ; date; cat $1 ; echo $RANDOM) | git hash-object --stdin)
|
random=$( (whoami ; hostname ; date; cat $1 ; echo $RANDOM) | git hash-object --stdin)
|
||||||
dest="$1.tmp.${random}"
|
dest="$1.tmp.${random}"
|
||||||
|
|
||||||
|
trap 'rm -f "${dest}"' EXIT
|
||||||
|
|
||||||
|
if ! git stripspace --strip-comments < "$1" > "${dest}" ; then
|
||||||
|
echo "cannot strip comments from $1"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if test ! -s "${dest}" ; then
|
||||||
|
echo "file is empty: $1"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! mv "${dest}" "$1" ; then
|
||||||
|
echo "cannot mv ${dest} to $1"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
# Avoid the --in-place option which only appeared in Git 2.8
|
# Avoid the --in-place option which only appeared in Git 2.8
|
||||||
# Avoid the --if-exists option which only appeared in Git 2.15
|
# Avoid the --if-exists option which only appeared in Git 2.15
|
||||||
cat "$1" \
|
if ! git -c trailer.ifexists=doNothing interpret-trailers \
|
||||||
| git -c trailer.ifexists=doNothing interpret-trailers --trailer "Change-Id: I${random}" > "${dest}" \
|
--trailer "Change-Id: I${random}" < "$1" > "${dest}" ; then
|
||||||
&& mv "${dest}" "$1"
|
echo "cannot insert change-id line in $1"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! mv "${dest}" "$1" ; then
|
||||||
|
echo "cannot mv ${dest} to $1"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
Loading…
Reference in New Issue
Block a user