blob: 2e1040ae59a913b7db63ccf1b02a0d5fc47dbe6a [file] [log] [blame]
simon7a7b4d52022-09-23 02:09:42 -04001#!/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
20set -u
21
22# avoid [[ which is not POSIX sh.
23if test "$#" != 1 ; then
24 echo "$0 requires an argument."
25 exit 1
26fi
27
28if test ! -f "$1" ; then
29 echo "file does not exist: $1"
30 exit 1
31fi
32
33# Do not create a change id if requested
34if test "false" = "$(git config --bool --get gerrit.createChangeId)" ; then
35 exit 0
36fi
37
38if git rev-parse --verify HEAD >/dev/null 2>&1; then
39 refhash="$(git rev-parse HEAD)"
40else
41 refhash="$(git hash-object -t tree /dev/null)"
42fi
43
44random=$({ git var GIT_COMMITTER_IDENT ; echo "$refhash" ; cat "$1"; } | git hash-object --stdin)
45dest="$1.tmp.${random}"
46
47trap 'rm -f "${dest}"' EXIT
48
49if ! git stripspace --strip-comments < "$1" > "${dest}" ; then
50 echo "cannot strip comments from $1"
51 exit 1
52fi
53
54if test ! -s "${dest}" ; then
55 echo "file is empty: $1"
56 exit 1
57fi
58
59reviewurl="$(git config --get gerrit.reviewUrl)"
60if 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
68else
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
76fi
77
78if ! mv "${dest}" "$1" ; then
79 echo "cannot mv ${dest} to $1"
80 exit 1
81fi