commit-msg: Fix detection of Link trailers
An additional $ was making detection of Link trailers to fail, causing
Link trailers to repeatedly be added when amending a commit.
The $ is added in the `grep` command below, so it is not needed in the
pattern.
Release-Notes: The commit-msg hook now detects existing Link trailers.
Bug: Issue 16945
Change-Id: Ib6ba75002b4d1aec7dc82f951775ebb3ca58a416
diff --git a/resources/com/google/gerrit/server/commit-msg_test.sh b/resources/com/google/gerrit/server/commit-msg_test.sh
index 0cc3da0..1772eb7 100755
--- a/resources/com/google/gerrit/server/commit-msg_test.sh
+++ b/resources/com/google/gerrit/server/commit-msg_test.sh
@@ -166,6 +166,30 @@
fi
}
+function test_preserve_link {
+ cat << EOF > input
+bla bla
+
+Link: https://myhost/id/I1234567890123456789012345678901234567890
+EOF
+
+ git config gerrit.reviewUrl https://myhost/
+ ${hook} input || fail "failed hook execution"
+ git config --unset gerrit.reviewUrl
+ found=$(grep -c '^Change-Id' input) || :
+ if [[ "${found}" != "0" ]]; then
+ fail "got ${found} Change-Ids, want 0"
+ fi
+ found=$(grep -c '^Link: https://myhost/id/I' input) || :
+ if [[ "${found}" != "1" ]]; then
+ fail "got ${found} Link footers, want 1"
+ fi
+ found=$(grep -c '^Link: https://myhost/id/I1234567890123456789012345678901234567890$' input) || :
+ if [[ "${found}" != "1" ]]; then
+ fail "got ${found} Link: https://myhost/id/I123..., want 1"
+ fi
+}
+
# Change-Id goes after existing trailers.
function test_at_end {
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 0154d43..5c7dffa 100755
--- a/resources/com/google/gerrit/server/tools/root/hooks/commit-msg
+++ b/resources/com/google/gerrit/server/tools/root/hooks/commit-msg
@@ -64,7 +64,7 @@
if test -n "${reviewurl}" ; then
token="Link"
value="${reviewurl%/}/id/I$random"
- pattern=".*/id/I[0-9a-f]\{40\}$"
+ pattern=".*/id/I[0-9a-f]\{40\}"
else
token="Change-Id"
value="I$random"