blob: 112df63f51e436431715c23e88f03b13af1d2520 [file] [log] [blame]
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07001#!/bin/sh
Orgad Shaneha50c4e32023-11-30 12:21:17 +02002# From Gerrit Code Review 3.10.0 d5403dbf335ba7d48977fc95170c3f7027c34659
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
Orgad Shaneha50c4e32023-11-30 12:21:17 +020034create_setting=$(git config --get gerrit.createChangeId)
35case "$create_setting" 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 ;;
47esac
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070048
Evan Benna8c34d12022-07-29 09:53:27 +100049
50if git rev-parse --verify HEAD >/dev/null 2>&1; then
51 refhash="$(git rev-parse HEAD)"
52else
53 refhash="$(git hash-object -t tree /dev/null)"
54fi
55
56random=$({ git var GIT_COMMITTER_IDENT ; echo "$refhash" ; cat "$1"; } | git hash-object --stdin)
David Pursehouse3995ebd2020-02-16 12:38:36 +090057dest="$1.tmp.${random}"
Mike Bjorge93229642016-02-29 11:48:15 -080058
Evan Benna8c34d12022-07-29 09:53:27 +100059trap 'rm -f "$dest" "$dest-2"' EXIT
Dave Borowitza8d53912014-07-15 11:30:06 -070060
Orgad Shaneha50c4e32023-11-30 12:21:17 +020061if ! cat "$1" | sed -e '/>8/q' | git stripspace --strip-comments > "${dest}" ; then
David Pursehouse3995ebd2020-02-16 12:38:36 +090062 echo "cannot strip comments from $1"
63 exit 1
64fi
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070065
David Pursehouse3995ebd2020-02-16 12:38:36 +090066if test ! -s "${dest}" ; then
67 echo "file is empty: $1"
68 exit 1
69fi
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070070
Evan Benna8c34d12022-07-29 09:53:27 +100071reviewurl="$(git config --get gerrit.reviewUrl)"
72if test -n "${reviewurl}" ; then
73 token="Link"
74 value="${reviewurl%/}/id/I$random"
Orgad Shaneha50c4e32023-11-30 12:21:17 +020075 pattern=".*/id/I[0-9a-f]\{40\}"
Evan Benna8c34d12022-07-29 09:53:27 +100076else
77 token="Change-Id"
78 value="I$random"
79 pattern=".*"
80fi
81
82if git interpret-trailers --parse < "$1" | grep -q "^$token: $pattern$" ; then
83 exit 0
84fi
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.
David Pursehouse3995ebd2020-02-16 12:38:36 +090088# Avoid the --in-place option which only appeared in Git 2.8
Evan Benna8c34d12022-07-29 09:53:27 +100089if ! git interpret-trailers \
90 --trailer "Signed-off-by: SENTINEL" < "$1" > "$dest-2" ; then
91 echo "cannot insert Signed-off-by sentinel line in $1"
92 exit 1
93fi
94
95# Make sure the trailer appears before any Signed-off-by trailers by inserting
96# it as if it was a Signed-off-by trailer and then use sed to remove the
97# Signed-off-by prefix and the Signed-off-by sentinel line.
98# Avoid the --in-place option which only appeared in Git 2.8
99# Avoid the --where option which only appeared in Git 2.15
100if ! git -c trailer.where=before interpret-trailers \
101 --trailer "Signed-off-by: $token: $value" < "$dest-2" |
Orgad Shaneha50c4e32023-11-30 12:21:17 +0200102 sed -e "s/^Signed-off-by: \($token: \)/\1/" \
Evan Benna8c34d12022-07-29 09:53:27 +1000103 -e "/^Signed-off-by: SENTINEL/d" > "$dest" ; then
104 echo "cannot insert $token line in $1"
David Pursehouse3995ebd2020-02-16 12:38:36 +0900105 exit 1
106fi
Mike Bjorge93229642016-02-29 11:48:15 -0800107
David Pursehouse3995ebd2020-02-16 12:38:36 +0900108if ! mv "${dest}" "$1" ; then
109 echo "cannot mv ${dest} to $1"
110 exit 1
111fi