simon | 7a7b4d5 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # From Gerrit Code Review 3.6.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 | set -u |
| 21 | |
| 22 | # avoid [[ which is not POSIX sh. |
| 23 | if test "$#" != 1 ; then |
| 24 | echo "$0 requires an argument." |
| 25 | exit 1 |
| 26 | fi |
| 27 | |
| 28 | if test ! -f "$1" ; then |
| 29 | echo "file does not exist: $1" |
| 30 | exit 1 |
| 31 | fi |
| 32 | |
| 33 | # Do not create a change id if requested |
| 34 | if test "false" = "$(git config --bool --get gerrit.createChangeId)" ; then |
| 35 | exit 0 |
| 36 | fi |
| 37 | |
| 38 | if git rev-parse --verify HEAD >/dev/null 2>&1; then |
| 39 | refhash="$(git rev-parse HEAD)" |
| 40 | else |
| 41 | refhash="$(git hash-object -t tree /dev/null)" |
| 42 | fi |
| 43 | |
| 44 | random=$({ git var GIT_COMMITTER_IDENT ; echo "$refhash" ; cat "$1"; } | git hash-object --stdin) |
| 45 | dest="$1.tmp.${random}" |
| 46 | |
| 47 | trap 'rm -f "${dest}"' EXIT |
| 48 | |
| 49 | if ! git stripspace --strip-comments < "$1" > "${dest}" ; then |
| 50 | echo "cannot strip comments from $1" |
| 51 | exit 1 |
| 52 | fi |
| 53 | |
| 54 | if test ! -s "${dest}" ; then |
| 55 | echo "file is empty: $1" |
| 56 | exit 1 |
| 57 | fi |
| 58 | |
| 59 | reviewurl="$(git config --get gerrit.reviewUrl)" |
| 60 | if test -n "${reviewurl}" ; then |
| 61 | if ! git interpret-trailers --parse < "$1" | grep -q '^Link:.*/id/I[0-9a-f]\{40\}$' ; then |
| 62 | if ! git interpret-trailers \ |
| 63 | --trailer "Link: ${reviewurl%/}/id/I${random}" < "$1" > "${dest}" ; then |
| 64 | echo "cannot insert link footer in $1" |
| 65 | exit 1 |
| 66 | fi |
| 67 | fi |
| 68 | else |
| 69 | # Avoid the --in-place option which only appeared in Git 2.8 |
| 70 | # Avoid the --if-exists option which only appeared in Git 2.15 |
| 71 | if ! git -c trailer.ifexists=doNothing interpret-trailers \ |
| 72 | --trailer "Change-Id: I${random}" < "$1" > "${dest}" ; then |
| 73 | echo "cannot insert change-id line in $1" |
| 74 | exit 1 |
| 75 | fi |
| 76 | fi |
| 77 | |
| 78 | if ! mv "${dest}" "$1" ; then |
| 79 | echo "cannot mv ${dest} to $1" |
| 80 | exit 1 |
| 81 | fi |