| 1 | #!/bin/sh |
| 2 | # From Gerrit Code Review 3.11.1. |
| 3 | # |
| 4 | # Part of Gerrit Code Review (https://www.gerritcodereview.com/) |
| 5 | # |
| 6 | # Copyright (C) 2009 The Android Open Source Project |
| 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | # you may not use this file except in compliance with the License. |
| 10 | # You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | # See the License for the specific language governing permissions and |
| 18 | # limitations under the License. |
| 19 | |
| 20 | ### Guix modifications start |
| 21 | COMMIT_MSG_MAGIC=VGhpcyBpcyB0aGUgY29tbWl0LW1zZyBob29rIG9mIEd1aXg= |
| 22 | top=$(git rev-parse --show-toplevel) |
| 23 | if test -d "$top/.git/hooks/commit-msg.d/"; then |
| 24 | for msg_hook in "$top/.git/hooks/commit-msg.d/"*; do |
| 25 | if ! sh "$msg_hook"; then |
| 26 | echo "error while running $msg_hook" |
| 27 | exit 1 |
| 28 | fi |
| 29 | done |
| 30 | fi |
| 31 | ### Guix modifications end |
| 32 | |
| 33 | set -u |
| 34 | |
| 35 | # avoid [[ which is not POSIX sh. |
| 36 | if test "$#" != 1 ; then |
| 37 | echo "$0 requires an argument." |
| 38 | exit 1 |
| 39 | fi |
| 40 | |
| 41 | if test ! -f "$1" ; then |
| 42 | echo "file does not exist: $1" |
| 43 | exit 1 |
| 44 | fi |
| 45 | |
| 46 | # Do not create a change id if requested |
| 47 | case "$(git config --get gerrit.createChangeId)" in |
| 48 | false) |
| 49 | exit 0 |
| 50 | ;; |
| 51 | always) |
| 52 | ;; |
| 53 | *) |
| 54 | # Do not create a change id for squash/fixup commits. |
| 55 | if head -n1 "$1" | LC_ALL=C grep -q '^[a-z][a-z]*! '; then |
| 56 | exit 0 |
| 57 | fi |
| 58 | ;; |
| 59 | esac |
| 60 | |
| 61 | |
| 62 | if git rev-parse --verify HEAD >/dev/null 2>&1; then |
| 63 | refhash="$(git rev-parse HEAD)" |
| 64 | else |
| 65 | refhash="$(git hash-object -t tree /dev/null)" |
| 66 | fi |
| 67 | |
| 68 | random=$({ git var GIT_COMMITTER_IDENT ; echo "$refhash" ; cat "$1"; } | git hash-object --stdin) |
| 69 | dest="$1.tmp.${random}" |
| 70 | |
| 71 | trap 'rm -f "$dest" "$dest-2"' EXIT |
| 72 | |
| 73 | if ! cat "$1" | sed -e '/>8/q' | git stripspace --strip-comments > "${dest}" ; then |
| 74 | echo "cannot strip comments from $1" |
| 75 | exit 1 |
| 76 | fi |
| 77 | |
| 78 | if test ! -s "${dest}" ; then |
| 79 | echo "file is empty: $1" |
| 80 | exit 1 |
| 81 | fi |
| 82 | |
| 83 | reviewurl="$(git config --get gerrit.reviewUrl)" |
| 84 | if test -n "${reviewurl}" ; then |
| 85 | token="Link" |
| 86 | value="${reviewurl%/}/id/I$random" |
| 87 | pattern=".*/id/I[0-9a-f]\{40\}" |
| 88 | else |
| 89 | token="Change-Id" |
| 90 | value="I$random" |
| 91 | pattern=".*" |
| 92 | fi |
| 93 | |
| 94 | if git interpret-trailers --parse < "$1" | grep -q "^$token: $pattern$" ; then |
| 95 | exit 0 |
| 96 | fi |
| 97 | |
| 98 | # There must be a Signed-off-by trailer for the code below to work. Insert a |
| 99 | # sentinel at the end to make sure there is one. |
| 100 | # Avoid the --in-place option which only appeared in Git 2.8 |
| 101 | if ! git interpret-trailers \ |
| 102 | --trailer "Signed-off-by: SENTINEL" < "$1" > "$dest-2" ; then |
| 103 | echo "cannot insert Signed-off-by sentinel line in $1" |
| 104 | exit 1 |
| 105 | fi |
| 106 | |
| 107 | # Make sure the trailer appears before any Signed-off-by trailers by inserting |
| 108 | # it as if it was a Signed-off-by trailer and then use sed to remove the |
| 109 | # Signed-off-by prefix and the Signed-off-by sentinel line. |
| 110 | # Avoid the --in-place option which only appeared in Git 2.8 |
| 111 | # Avoid the --where option which only appeared in Git 2.15 |
| 112 | if ! git -c trailer.where=before interpret-trailers \ |
| 113 | --trailer "Signed-off-by: $token: $value" < "$dest-2" | |
| 114 | sed -e "s/^Signed-off-by: \($token: \)/\1/" \ |
| 115 | -e "/^Signed-off-by: SENTINEL/d" > "$dest" ; then |
| 116 | echo "cannot insert $token line in $1" |
| 117 | exit 1 |
| 118 | fi |
| 119 | |
| 120 | if ! mv "${dest}" "$1" ; then |
| 121 | echo "cannot mv ${dest} to $1" |
| 122 | exit 1 |
| 123 | fi |