blob: a6721d40d768bbab1d934bc8950455365f7e932f [file] [log] [blame]
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07001#!/bin/sh
Mike Frysinger5591d992024-04-23 12:17:13 -04002# 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. Pearce9452e4e2009-08-22 18:17:46 -07006#
Mike Bjorge93229642016-02-29 11:48:15 -08007# Part of Gerrit Code Review (https://www.gerritcodereview.com/)
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07008#
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. Pearce9452e4e2009-08-22 18:17:46 -070022
Evan Benna8c34d12022-07-29 09:53:27 +100023set -u
24
David Pursehouse3995ebd2020-02-16 12:38:36 +090025# avoid [[ which is not POSIX sh.
26if test "$#" != 1 ; then
27 echo "$0 requires an argument."
28 exit 1
29fi
David Pursehouse55693aa2013-02-13 09:55:32 +090030
David Pursehouse3995ebd2020-02-16 12:38:36 +090031if test ! -f "$1" ; then
32 echo "file does not exist: $1"
33 exit 1
34fi
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070035
David Pursehouse3995ebd2020-02-16 12:38:36 +090036# Do not create a change id if requested
Mike Frysinger5591d992024-04-23 12:17:13 -040037case "$(git config --get gerrit.createChangeId)" in
Orgad Shaneha50c4e32023-11-30 12:21:17 +020038 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 ;;
49esac
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070050
Evan Benna8c34d12022-07-29 09:53:27 +100051
52if git rev-parse --verify HEAD >/dev/null 2>&1; then
53 refhash="$(git rev-parse HEAD)"
54else
55 refhash="$(git hash-object -t tree /dev/null)"
56fi
57
58random=$({ git var GIT_COMMITTER_IDENT ; echo "$refhash" ; cat "$1"; } | git hash-object --stdin)
David Pursehouse3995ebd2020-02-16 12:38:36 +090059dest="$1.tmp.${random}"
Mike Bjorge93229642016-02-29 11:48:15 -080060
Evan Benna8c34d12022-07-29 09:53:27 +100061trap 'rm -f "$dest" "$dest-2"' EXIT
Dave Borowitza8d53912014-07-15 11:30:06 -070062
Orgad Shaneha50c4e32023-11-30 12:21:17 +020063if ! cat "$1" | sed -e '/>8/q' | git stripspace --strip-comments > "${dest}" ; then
David Pursehouse3995ebd2020-02-16 12:38:36 +090064 echo "cannot strip comments from $1"
65 exit 1
66fi
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070067
David Pursehouse3995ebd2020-02-16 12:38:36 +090068if test ! -s "${dest}" ; then
69 echo "file is empty: $1"
70 exit 1
71fi
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070072
Evan Benna8c34d12022-07-29 09:53:27 +100073reviewurl="$(git config --get gerrit.reviewUrl)"
74if test -n "${reviewurl}" ; then
75 token="Link"
76 value="${reviewurl%/}/id/I$random"
Orgad Shaneha50c4e32023-11-30 12:21:17 +020077 pattern=".*/id/I[0-9a-f]\{40\}"
Evan Benna8c34d12022-07-29 09:53:27 +100078else
79 token="Change-Id"
80 value="I$random"
81 pattern=".*"
82fi
83
84if git interpret-trailers --parse < "$1" | grep -q "^$token: $pattern$" ; then
85 exit 0
86fi
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 Pursehouse3995ebd2020-02-16 12:38:36 +090090# Avoid the --in-place option which only appeared in Git 2.8
Evan Benna8c34d12022-07-29 09:53:27 +100091if ! 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
95fi
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
102if ! git -c trailer.where=before interpret-trailers \
103 --trailer "Signed-off-by: $token: $value" < "$dest-2" |
Orgad Shaneha50c4e32023-11-30 12:21:17 +0200104 sed -e "s/^Signed-off-by: \($token: \)/\1/" \
Evan Benna8c34d12022-07-29 09:53:27 +1000105 -e "/^Signed-off-by: SENTINEL/d" > "$dest" ; then
106 echo "cannot insert $token line in $1"
David Pursehouse3995ebd2020-02-16 12:38:36 +0900107 exit 1
108fi
Mike Bjorge93229642016-02-29 11:48:15 -0800109
David Pursehouse3995ebd2020-02-16 12:38:36 +0900110if ! mv "${dest}" "$1" ; then
111 echo "cannot mv ${dest} to $1"
112 exit 1
113fi