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