summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan Nieder <jrn@google.com>2019-01-24 20:04:01 -0800
committerJonathan Nieder <jrn@google.com>2019-03-13 22:51:49 -0700
commit8c2dbd1eaf417b7875e69577e24c1478e6acbbd2 (patch)
tree437bb9ac71f4fbfa3a445824b6fa04c28f50db4d
parent8025e1243ca7c3fdbf693e5cc1661d085d15e04e (diff)
Do not strip out "-- >8 --" comment in commit-msg hook
When commiting with "git commit --cleanup=scissors" or "git commit --verbose", Git includes the following lines in the commit message template, with additional information that should not go in the commit message after it: # ------------------------ >8 ------------------------ # Do not modify or remove the line above. # Everything below it will be ignored. In I78b50a789860cc11d63d891b0507786890158754 (Handle messages with only comments in the commit-msg hook, 2018-12-18), we started stripping comments from the proposed commit message in order to determine whether it is empty and as an accidental side effect we lost this line. As a result, Git includes the supporting information (e.g., the diff) in the commit message. Fortunately we only needed to strip out comments in order to check for emptiness. Afterward, the hook invokes "git interpret-trailers", which is prepared to cope with comments and scissor lines in front of a diff in recent versions of Git[*]. Fix the hook to work in scissors mode by using the stripped commit message only for the emptiness check and going back to the unstripped message for subsequent steps. This way, users can run "git commit -v" without having the diff end up in the resulting commit message. Users with older versions of Git will not benefit from this fix, but it does not produce a regression there, either: "git commit" in cleanup modes other than scissors continues to work as expected. In other words, in all cases this works as well as before I78b50a78986. [*] v2.13.1~16^2 (interpret-trailers: honor the cut line, 2017-05-15) Bug: Issue 10346 Change-Id: I633e5db4643851376422f839d969094043abb5c5 (cherry picked from commit 731eb42b8aed36cb9b3b584458479484e77c4f48)
-rwxr-xr-xresources/com/google/gerrit/server/commit-msg_test.sh37
-rwxr-xr-xresources/com/google/gerrit/server/tools/root/hooks/commit-msg5
2 files changed, 37 insertions, 5 deletions
diff --git a/resources/com/google/gerrit/server/commit-msg_test.sh b/resources/com/google/gerrit/server/commit-msg_test.sh
index a9247711be..d797be30fd 100755
--- a/resources/com/google/gerrit/server/commit-msg_test.sh
+++ b/resources/com/google/gerrit/server/commit-msg_test.sh
@@ -11,6 +11,11 @@ function fail {
exit 1
}
+function prereq_modern_git {
+ # "git interpret-trailers --where" was introduced in Git 2.15.0.
+ git interpret-trailers -h 2>&1 | grep -e --where > /dev/null
+}
+
function test_nonexistent_argument {
rm -f input
if ${hook} input ; then
@@ -38,6 +43,38 @@ EOF
fi
}
+function test_keep_cutoff_line {
+ if ! prereq_modern_git ; then
+ echo "old version of Git detected; skipping scissors test."
+ return 0
+ fi
+ rm -f input
+ cat << EOF > input
+Do something nice
+
+# Please enter the commit message for your changes.
+# ------------------------ >8 ------------------------
+# Do not modify or remove the line above.
+# Everything below it will be ignored.
+diff --git a/file.txt b/file.txt
+index 625fd613d9..03aeba3b21 100755
+--- a/file.txt
++++ b/file.txt
+@@ -38,6 +38,7 @@
+ context
+ line
+
++hello, world
+
+ context
+ line
+EOF
+ ${hook} input || fail "failed hook execution"
+ grep '>8' input || fail "lost cut-off line"
+ sed -n -e '1,/>8/ p' input >top
+ grep '^Change-Id' top || fail "missing Change-Id above cut-off line"
+}
+
# a Change-Id already set is preserved.
function test_preserve_changeid {
cat << EOF > input
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 e9ff5977e3..290123258b 100755
--- a/resources/com/google/gerrit/server/tools/root/hooks/commit-msg
+++ b/resources/com/google/gerrit/server/tools/root/hooks/commit-msg
@@ -48,11 +48,6 @@ if test ! -s "${dest}" ; then
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
if ! git -c trailer.ifexists=doNothing interpret-trailers \