blob: 70d67eaf9a1a96f50828468d521f3bc0e41b5ce2 [file] [log] [blame]
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -07001#!/bin/sh
David Pursehouse3995ebd2020-02-16 12:38:36 +09002# From Gerrit Code Review 3.1.3
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
David Pursehouse3995ebd2020-02-16 12:38:36 +090020# avoid [[ which is not POSIX sh.
21if test "$#" != 1 ; then
22 echo "$0 requires an argument."
23 exit 1
24fi
David Pursehouse55693aa2013-02-13 09:55:32 +090025
David Pursehouse3995ebd2020-02-16 12:38:36 +090026if test ! -f "$1" ; then
27 echo "file does not exist: $1"
28 exit 1
29fi
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070030
David Pursehouse3995ebd2020-02-16 12:38:36 +090031# Do not create a change id if requested
32if test "false" = "`git config --bool --get gerrit.createChangeId`" ; then
33 exit 0
34fi
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070035
David Pursehouse3995ebd2020-02-16 12:38:36 +090036# $RANDOM will be undefined if not using bash, so don't use set -u
37random=$( (whoami ; hostname ; date; cat $1 ; echo $RANDOM) | git hash-object --stdin)
38dest="$1.tmp.${random}"
Mike Bjorge93229642016-02-29 11:48:15 -080039
David Pursehouse3995ebd2020-02-16 12:38:36 +090040trap 'rm -f "${dest}"' EXIT
Dave Borowitza8d53912014-07-15 11:30:06 -070041
David Pursehouse3995ebd2020-02-16 12:38:36 +090042if ! git stripspace --strip-comments < "$1" > "${dest}" ; then
43 echo "cannot strip comments from $1"
44 exit 1
45fi
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070046
David Pursehouse3995ebd2020-02-16 12:38:36 +090047if test ! -s "${dest}" ; then
48 echo "file is empty: $1"
49 exit 1
50fi
Shawn O. Pearce9452e4e2009-08-22 18:17:46 -070051
David Pursehouse3995ebd2020-02-16 12:38:36 +090052# Avoid the --in-place option which only appeared in Git 2.8
53# Avoid the --if-exists option which only appeared in Git 2.15
54if ! git -c trailer.ifexists=doNothing interpret-trailers \
55 --trailer "Change-Id: I${random}" < "$1" > "${dest}" ; then
56 echo "cannot insert change-id line in $1"
57 exit 1
58fi
Mike Bjorge93229642016-02-29 11:48:15 -080059
David Pursehouse3995ebd2020-02-16 12:38:36 +090060if ! mv "${dest}" "$1" ; then
61 echo "cannot mv ${dest} to $1"
62 exit 1
63fi