summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xetc/git/commit-msg69
1 files changed, 49 insertions, 20 deletions
diff --git a/etc/git/commit-msg b/etc/git/commit-msg
index dfa07918bb..69bd1fcde1 100755
--- a/etc/git/commit-msg
+++ b/etc/git/commit-msg
@@ -1,5 +1,5 @@
#!/bin/sh
-# From Gerrit Code Review 3.6.1
+# From Gerrit Code Review 3.11.1.
#
# Part of Gerrit Code Review (https://www.gerritcodereview.com/)
#
@@ -44,9 +44,20 @@ if test ! -f "$1" ; then
fi
# Do not create a change id if requested
-if test "false" = "$(git config --bool --get gerrit.createChangeId)" ; then
- exit 0
-fi
+case "$(git config --get gerrit.createChangeId)" in
+ false)
+ exit 0
+ ;;
+ always)
+ ;;
+ *)
+ # Do not create a change id for squash/fixup commits.
+ if head -n1 "$1" | LC_ALL=C grep -q '^[a-z][a-z]*! '; then
+ exit 0
+ fi
+ ;;
+esac
+
if git rev-parse --verify HEAD >/dev/null 2>&1; then
refhash="$(git rev-parse HEAD)"
@@ -57,9 +68,9 @@ fi
random=$({ git var GIT_COMMITTER_IDENT ; echo "$refhash" ; cat "$1"; } | git hash-object --stdin)
dest="$1.tmp.${random}"
-trap 'rm -f "${dest}"' EXIT
+trap 'rm -f "$dest" "$dest-2"' EXIT
-if ! git stripspace --strip-comments < "$1" > "${dest}" ; then
+if ! cat "$1" | sed -e '/>8/q' | git stripspace --strip-comments > "${dest}" ; then
echo "cannot strip comments from $1"
exit 1
fi
@@ -71,21 +82,39 @@ fi
reviewurl="$(git config --get gerrit.reviewUrl)"
if test -n "${reviewurl}" ; then
- if ! git interpret-trailers --parse < "$1" | grep -q '^Link:.*/id/I[0-9a-f]\{40\}$' ; then
- if ! git interpret-trailers \
- --trailer "Link: ${reviewurl%/}/id/I${random}" < "$1" > "${dest}" ; then
- echo "cannot insert link footer in $1"
- exit 1
- fi
- fi
+ token="Link"
+ value="${reviewurl%/}/id/I$random"
+ pattern=".*/id/I[0-9a-f]\{40\}"
else
- # 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 \
- --trailer "Change-Id: I${random}" < "$1" > "${dest}" ; then
- echo "cannot insert change-id line in $1"
- exit 1
- fi
+ token="Change-Id"
+ value="I$random"
+ pattern=".*"
+fi
+
+if git interpret-trailers --parse < "$1" | grep -q "^$token: $pattern$" ; then
+ exit 0
+fi
+
+# There must be a Signed-off-by trailer for the code below to work. Insert a
+# sentinel at the end to make sure there is one.
+# Avoid the --in-place option which only appeared in Git 2.8
+if ! git interpret-trailers \
+ --trailer "Signed-off-by: SENTINEL" < "$1" > "$dest-2" ; then
+ echo "cannot insert Signed-off-by sentinel line in $1"
+ exit 1
+fi
+
+# Make sure the trailer appears before any Signed-off-by trailers by inserting
+# it as if it was a Signed-off-by trailer and then use sed to remove the
+# Signed-off-by prefix and the Signed-off-by sentinel line.
+# Avoid the --in-place option which only appeared in Git 2.8
+# Avoid the --where option which only appeared in Git 2.15
+if ! git -c trailer.where=before interpret-trailers \
+ --trailer "Signed-off-by: $token: $value" < "$dest-2" |
+ sed -e "s/^Signed-off-by: \($token: \)/\1/" \
+ -e "/^Signed-off-by: SENTINEL/d" > "$dest" ; then
+ echo "cannot insert $token line in $1"
+ exit 1
fi
if ! mv "${dest}" "$1" ; then