Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 1 | #!/bin/sh |
Evan Benn | a8c34d1 | 2022-07-29 09:53:27 +1000 | [diff] [blame^] | 2 | # From Gerrit Code Review 3.6.1 c67916dbdc07555c44e32a68f92ffc484b9b34f0 |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 3 | # |
Mike Bjorge | 9322964 | 2016-02-29 11:48:15 -0800 | [diff] [blame] | 4 | # Part of Gerrit Code Review (https://www.gerritcodereview.com/) |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 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. |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 19 | |
Evan Benn | a8c34d1 | 2022-07-29 09:53:27 +1000 | [diff] [blame^] | 20 | set -u |
| 21 | |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame] | 22 | # avoid [[ which is not POSIX sh. |
| 23 | if test "$#" != 1 ; then |
| 24 | echo "$0 requires an argument." |
| 25 | exit 1 |
| 26 | fi |
David Pursehouse | 55693aa | 2013-02-13 09:55:32 +0900 | [diff] [blame] | 27 | |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame] | 28 | if test ! -f "$1" ; then |
| 29 | echo "file does not exist: $1" |
| 30 | exit 1 |
| 31 | fi |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 32 | |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame] | 33 | # Do not create a change id if requested |
Evan Benn | a8c34d1 | 2022-07-29 09:53:27 +1000 | [diff] [blame^] | 34 | if test "false" = "$(git config --bool --get gerrit.createChangeId)" ; then |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame] | 35 | exit 0 |
| 36 | fi |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 37 | |
Evan Benn | a8c34d1 | 2022-07-29 09:53:27 +1000 | [diff] [blame^] | 38 | # Do not create a change id for squash commits. |
| 39 | if head -n1 "$1" | grep -q '^squash! '; then |
| 40 | exit 0 |
| 41 | fi |
| 42 | |
| 43 | if git rev-parse --verify HEAD >/dev/null 2>&1; then |
| 44 | refhash="$(git rev-parse HEAD)" |
| 45 | else |
| 46 | refhash="$(git hash-object -t tree /dev/null)" |
| 47 | fi |
| 48 | |
| 49 | random=$({ git var GIT_COMMITTER_IDENT ; echo "$refhash" ; cat "$1"; } | git hash-object --stdin) |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame] | 50 | dest="$1.tmp.${random}" |
Mike Bjorge | 9322964 | 2016-02-29 11:48:15 -0800 | [diff] [blame] | 51 | |
Evan Benn | a8c34d1 | 2022-07-29 09:53:27 +1000 | [diff] [blame^] | 52 | trap 'rm -f "$dest" "$dest-2"' EXIT |
Dave Borowitz | a8d5391 | 2014-07-15 11:30:06 -0700 | [diff] [blame] | 53 | |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame] | 54 | if ! git stripspace --strip-comments < "$1" > "${dest}" ; then |
| 55 | echo "cannot strip comments from $1" |
| 56 | exit 1 |
| 57 | fi |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 58 | |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame] | 59 | if test ! -s "${dest}" ; then |
| 60 | echo "file is empty: $1" |
| 61 | exit 1 |
| 62 | fi |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 63 | |
Evan Benn | a8c34d1 | 2022-07-29 09:53:27 +1000 | [diff] [blame^] | 64 | reviewurl="$(git config --get gerrit.reviewUrl)" |
| 65 | if test -n "${reviewurl}" ; then |
| 66 | token="Link" |
| 67 | value="${reviewurl%/}/id/I$random" |
| 68 | pattern=".*/id/I[0-9a-f]\{40\}$" |
| 69 | else |
| 70 | token="Change-Id" |
| 71 | value="I$random" |
| 72 | pattern=".*" |
| 73 | fi |
| 74 | |
| 75 | if git interpret-trailers --parse < "$1" | grep -q "^$token: $pattern$" ; then |
| 76 | exit 0 |
| 77 | fi |
| 78 | |
| 79 | # There must be a Signed-off-by trailer for the code below to work. Insert a |
| 80 | # sentinel at the end to make sure there is one. |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame] | 81 | # Avoid the --in-place option which only appeared in Git 2.8 |
Evan Benn | a8c34d1 | 2022-07-29 09:53:27 +1000 | [diff] [blame^] | 82 | if ! git interpret-trailers \ |
| 83 | --trailer "Signed-off-by: SENTINEL" < "$1" > "$dest-2" ; then |
| 84 | echo "cannot insert Signed-off-by sentinel line in $1" |
| 85 | exit 1 |
| 86 | fi |
| 87 | |
| 88 | # Make sure the trailer appears before any Signed-off-by trailers by inserting |
| 89 | # it as if it was a Signed-off-by trailer and then use sed to remove the |
| 90 | # Signed-off-by prefix and the Signed-off-by sentinel line. |
| 91 | # Avoid the --in-place option which only appeared in Git 2.8 |
| 92 | # Avoid the --where option which only appeared in Git 2.15 |
| 93 | if ! git -c trailer.where=before interpret-trailers \ |
| 94 | --trailer "Signed-off-by: $token: $value" < "$dest-2" | |
| 95 | sed -re "s/^Signed-off-by: ($token: )/\1/" \ |
| 96 | -e "/^Signed-off-by: SENTINEL/d" > "$dest" ; then |
| 97 | echo "cannot insert $token line in $1" |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame] | 98 | exit 1 |
| 99 | fi |
Mike Bjorge | 9322964 | 2016-02-29 11:48:15 -0800 | [diff] [blame] | 100 | |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame] | 101 | if ! mv "${dest}" "$1" ; then |
| 102 | echo "cannot mv ${dest} to $1" |
| 103 | exit 1 |
| 104 | fi |