| Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| Mike Frysinger | 5591d99 | 2024-04-23 12:17:13 -0400 | [diff] [blame] | 2 | # DO NOT EDIT THIS FILE |
| 3 | # All updates should be sent upstream: https://gerrit.googlesource.com/gerrit/ |
| 4 | # This is synced from commit: 62f5bbea67f6dafa6e22a601a0c298214c510caf |
| 5 | # DO NOT EDIT THIS FILE |
| Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 6 | # |
| Mike Bjorge | 9322964 | 2016-02-29 11:48:15 -0800 | [diff] [blame] | 7 | # Part of Gerrit Code Review (https://www.gerritcodereview.com/) |
| Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 8 | # |
| 9 | # Copyright (C) 2009 The Android Open Source Project |
| 10 | # |
| 11 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 12 | # you may not use this file except in compliance with the License. |
| 13 | # You may obtain a copy of the License at |
| 14 | # |
| 15 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | # |
| 17 | # Unless required by applicable law or agreed to in writing, software |
| 18 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | # See the License for the specific language governing permissions and |
| 21 | # limitations under the License. |
| Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 22 | |
| Evan Benn | a8c34d1 | 2022-07-29 09:53:27 +1000 | [diff] [blame] | 23 | set -u |
| 24 | |
| David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame] | 25 | # avoid [[ which is not POSIX sh. |
| 26 | if test "$#" != 1 ; then |
| 27 | echo "$0 requires an argument." |
| 28 | exit 1 |
| 29 | fi |
| David Pursehouse | 55693aa | 2013-02-13 09:55:32 +0900 | [diff] [blame] | 30 | |
| David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame] | 31 | if test ! -f "$1" ; then |
| 32 | echo "file does not exist: $1" |
| 33 | exit 1 |
| 34 | fi |
| Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 35 | |
| David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame] | 36 | # Do not create a change id if requested |
| Mike Frysinger | 5591d99 | 2024-04-23 12:17:13 -0400 | [diff] [blame] | 37 | case "$(git config --get gerrit.createChangeId)" in |
| Orgad Shaneh | a50c4e3 | 2023-11-30 12:21:17 +0200 | [diff] [blame] | 38 | false) |
| 39 | exit 0 |
| 40 | ;; |
| 41 | always) |
| 42 | ;; |
| 43 | *) |
| 44 | # Do not create a change id for squash/fixup commits. |
| 45 | if head -n1 "$1" | LC_ALL=C grep -q '^[a-z][a-z]*! '; then |
| 46 | exit 0 |
| 47 | fi |
| 48 | ;; |
| 49 | esac |
| Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 50 | |
| Evan Benn | a8c34d1 | 2022-07-29 09:53:27 +1000 | [diff] [blame] | 51 | |
| 52 | if git rev-parse --verify HEAD >/dev/null 2>&1; then |
| 53 | refhash="$(git rev-parse HEAD)" |
| 54 | else |
| 55 | refhash="$(git hash-object -t tree /dev/null)" |
| 56 | fi |
| 57 | |
| 58 | 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] | 59 | dest="$1.tmp.${random}" |
| Mike Bjorge | 9322964 | 2016-02-29 11:48:15 -0800 | [diff] [blame] | 60 | |
| Evan Benn | a8c34d1 | 2022-07-29 09:53:27 +1000 | [diff] [blame] | 61 | trap 'rm -f "$dest" "$dest-2"' EXIT |
| Dave Borowitz | a8d5391 | 2014-07-15 11:30:06 -0700 | [diff] [blame] | 62 | |
| Orgad Shaneh | a50c4e3 | 2023-11-30 12:21:17 +0200 | [diff] [blame] | 63 | if ! cat "$1" | sed -e '/>8/q' | git stripspace --strip-comments > "${dest}" ; then |
| David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame] | 64 | echo "cannot strip comments from $1" |
| 65 | exit 1 |
| 66 | fi |
| Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 67 | |
| David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame] | 68 | if test ! -s "${dest}" ; then |
| 69 | echo "file is empty: $1" |
| 70 | exit 1 |
| 71 | fi |
| Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 72 | |
| Evan Benn | a8c34d1 | 2022-07-29 09:53:27 +1000 | [diff] [blame] | 73 | reviewurl="$(git config --get gerrit.reviewUrl)" |
| 74 | if test -n "${reviewurl}" ; then |
| 75 | token="Link" |
| 76 | value="${reviewurl%/}/id/I$random" |
| Orgad Shaneh | a50c4e3 | 2023-11-30 12:21:17 +0200 | [diff] [blame] | 77 | pattern=".*/id/I[0-9a-f]\{40\}" |
| Evan Benn | a8c34d1 | 2022-07-29 09:53:27 +1000 | [diff] [blame] | 78 | else |
| 79 | token="Change-Id" |
| 80 | value="I$random" |
| 81 | pattern=".*" |
| 82 | fi |
| 83 | |
| 84 | if git interpret-trailers --parse < "$1" | grep -q "^$token: $pattern$" ; then |
| 85 | exit 0 |
| 86 | fi |
| 87 | |
| 88 | # There must be a Signed-off-by trailer for the code below to work. Insert a |
| 89 | # sentinel at the end to make sure there is one. |
| David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame] | 90 | # Avoid the --in-place option which only appeared in Git 2.8 |
| Evan Benn | a8c34d1 | 2022-07-29 09:53:27 +1000 | [diff] [blame] | 91 | if ! git interpret-trailers \ |
| 92 | --trailer "Signed-off-by: SENTINEL" < "$1" > "$dest-2" ; then |
| 93 | echo "cannot insert Signed-off-by sentinel line in $1" |
| 94 | exit 1 |
| 95 | fi |
| 96 | |
| 97 | # Make sure the trailer appears before any Signed-off-by trailers by inserting |
| 98 | # it as if it was a Signed-off-by trailer and then use sed to remove the |
| 99 | # Signed-off-by prefix and the Signed-off-by sentinel line. |
| 100 | # Avoid the --in-place option which only appeared in Git 2.8 |
| 101 | # Avoid the --where option which only appeared in Git 2.15 |
| 102 | if ! git -c trailer.where=before interpret-trailers \ |
| 103 | --trailer "Signed-off-by: $token: $value" < "$dest-2" | |
| Orgad Shaneh | a50c4e3 | 2023-11-30 12:21:17 +0200 | [diff] [blame] | 104 | sed -e "s/^Signed-off-by: \($token: \)/\1/" \ |
| Evan Benn | a8c34d1 | 2022-07-29 09:53:27 +1000 | [diff] [blame] | 105 | -e "/^Signed-off-by: SENTINEL/d" > "$dest" ; then |
| 106 | echo "cannot insert $token line in $1" |
| David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame] | 107 | exit 1 |
| 108 | fi |
| Mike Bjorge | 9322964 | 2016-02-29 11:48:15 -0800 | [diff] [blame] | 109 | |
| David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame] | 110 | if ! mv "${dest}" "$1" ; then |
| 111 | echo "cannot mv ${dest} to $1" |
| 112 | exit 1 |
| 113 | fi |