blob: 8c6476fade326949fc800372ef197bb386e503d4 [file] [log] [blame]
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07001#!/bin/sh
Evan Benna8c34d12022-07-29 09:53:27 +10002# From Gerrit Code Review 3.6.1 c67916dbdc07555c44e32a68f92ffc484b9b34f0
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07003#
Mike Bjorge93229642016-02-29 11:48:15 -08004# Part of Gerrit Code Review (https://www.gerritcodereview.com/)
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07005#
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. Pearce9452e4e2009-08-22 18:17:46 -070019
Evan Benna8c34d12022-07-29 09:53:27 +100020set -u
21
David Pursehouse3995ebd2020-02-16 12:38:36 +090022# avoid [[ which is not POSIX sh.
23if test "$#" != 1 ; then
24 echo "$0 requires an argument."
25 exit 1
26fi
David Pursehouse55693aa2013-02-13 09:55:32 +090027
David Pursehouse3995ebd2020-02-16 12:38:36 +090028if test ! -f "$1" ; then
29 echo "file does not exist: $1"
30 exit 1
31fi
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070032
David Pursehouse3995ebd2020-02-16 12:38:36 +090033# Do not create a change id if requested
Evan Benna8c34d12022-07-29 09:53:27 +100034if test "false" = "$(git config --bool --get gerrit.createChangeId)" ; then
David Pursehouse3995ebd2020-02-16 12:38:36 +090035 exit 0
36fi
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070037
Evan Benna8c34d12022-07-29 09:53:27 +100038# Do not create a change id for squash commits.
39if head -n1 "$1" | grep -q '^squash! '; then
40 exit 0
41fi
42
43if git rev-parse --verify HEAD >/dev/null 2>&1; then
44 refhash="$(git rev-parse HEAD)"
45else
46 refhash="$(git hash-object -t tree /dev/null)"
47fi
48
49random=$({ git var GIT_COMMITTER_IDENT ; echo "$refhash" ; cat "$1"; } | git hash-object --stdin)
David Pursehouse3995ebd2020-02-16 12:38:36 +090050dest="$1.tmp.${random}"
Mike Bjorge93229642016-02-29 11:48:15 -080051
Evan Benna8c34d12022-07-29 09:53:27 +100052trap 'rm -f "$dest" "$dest-2"' EXIT
Dave Borowitza8d53912014-07-15 11:30:06 -070053
David Pursehouse3995ebd2020-02-16 12:38:36 +090054if ! git stripspace --strip-comments < "$1" > "${dest}" ; then
55 echo "cannot strip comments from $1"
56 exit 1
57fi
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070058
David Pursehouse3995ebd2020-02-16 12:38:36 +090059if test ! -s "${dest}" ; then
60 echo "file is empty: $1"
61 exit 1
62fi
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070063
Evan Benna8c34d12022-07-29 09:53:27 +100064reviewurl="$(git config --get gerrit.reviewUrl)"
65if test -n "${reviewurl}" ; then
66 token="Link"
67 value="${reviewurl%/}/id/I$random"
68 pattern=".*/id/I[0-9a-f]\{40\}$"
69else
70 token="Change-Id"
71 value="I$random"
72 pattern=".*"
73fi
74
75if git interpret-trailers --parse < "$1" | grep -q "^$token: $pattern$" ; then
76 exit 0
77fi
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 Pursehouse3995ebd2020-02-16 12:38:36 +090081# Avoid the --in-place option which only appeared in Git 2.8
Evan Benna8c34d12022-07-29 09:53:27 +100082if ! 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
86fi
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
93if ! 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 Pursehouse3995ebd2020-02-16 12:38:36 +090098 exit 1
99fi
Mike Bjorge93229642016-02-29 11:48:15 -0800100
David Pursehouse3995ebd2020-02-16 12:38:36 +0900101if ! mv "${dest}" "$1" ; then
102 echo "cannot mv ${dest} to $1"
103 exit 1
104fi