summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHan-Wen Nienhuys <hanwen@google.com>2018-12-18 12:56:36 +0100
committerJonathan Nieder <jrn@google.com>2019-03-13 22:50:58 -0700
commit8025e1243ca7c3fdbf693e5cc1661d085d15e04e (patch)
treea361ee5e08704866def22edf53a06e866ce367d1
parent164d7feeec15205da4b57d84bacb7e33e1072a2a (diff)
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. Bug: Issue 10600 Change-Id: I78b50a789860cc11d63d891b0507786890158754 (cherry picked from commit 627d07c2bfc505eb46b30f9deb80c85b90b4921f)
-rwxr-xr-xresources/com/google/gerrit/server/commit-msg_test.sh14
-rwxr-xr-xresources/com/google/gerrit/server/tools/root/hooks/commit-msg35
2 files changed, 40 insertions, 9 deletions
diff --git a/resources/com/google/gerrit/server/commit-msg_test.sh b/resources/com/google/gerrit/server/commit-msg_test.sh
index 81371dd019..a9247711be 100755
--- a/resources/com/google/gerrit/server/commit-msg_test.sh
+++ b/resources/com/google/gerrit/server/commit-msg_test.sh
@@ -26,6 +26,18 @@ function test_empty {
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.
function test_preserve_changeid {
cat << EOF > input
@@ -128,7 +140,7 @@ EOF
# Test driver.
-
+git init
for func in $( declare -F | awk '{print $3;}' | sort); do
case ${func} in
test_*)
diff --git a/resources/com/google/gerrit/server/tools/root/hooks/commit-msg b/resources/com/google/gerrit/server/tools/root/hooks/commit-msg
index 3f4cd7aa6e..e9ff5977e3 100755
--- a/resources/com/google/gerrit/server/tools/root/hooks/commit-msg
+++ b/resources/com/google/gerrit/server/tools/root/hooks/commit-msg
@@ -27,11 +27,6 @@ if test ! -f "$1" ; then
exit 1
fi
-if test ! -s "$1" ; then
- echo "file is empty: $1"
- exit 1
-fi
-
# Do not create a change id if requested
if test "false" = "`git config --bool --get gerrit.createChangeId`" ; then
exit 0
@@ -41,8 +36,32 @@ fi
random=$( (whoami ; hostname ; date; cat $1 ; echo $RANDOM) | git hash-object --stdin)
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 --if-exists option which only appeared in Git 2.15
-cat "$1" \
- | git -c trailer.ifexists=doNothing interpret-trailers --trailer "Change-Id: I${random}" > "${dest}" \
- && mv "${dest}" "$1"
+if ! git -c trailer.ifexists=doNothing interpret-trailers \
+ --trailer "Change-Id: I${random}" < "$1" > "${dest}" ; then
+ 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