Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 1 | #!/bin/sh |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame^] | 2 | # From Gerrit Code Review 3.1.3 |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 3 | # |
Mike Bjorge | 9322964 | 2016-02-29 11:48:15 -0800 | [diff] [blame] | 4 | # Part of Gerrit Code Review (https://www.gerritcodereview.com/) |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 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. |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 19 | |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame^] | 20 | # avoid [[ which is not POSIX sh. |
| 21 | if test "$#" != 1 ; then |
| 22 | echo "$0 requires an argument." |
| 23 | exit 1 |
| 24 | fi |
David Pursehouse | 55693aa | 2013-02-13 09:55:32 +0900 | [diff] [blame] | 25 | |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame^] | 26 | if test ! -f "$1" ; then |
| 27 | echo "file does not exist: $1" |
| 28 | exit 1 |
| 29 | fi |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 30 | |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame^] | 31 | # Do not create a change id if requested |
| 32 | if test "false" = "`git config --bool --get gerrit.createChangeId`" ; then |
| 33 | exit 0 |
| 34 | fi |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 35 | |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame^] | 36 | # $RANDOM will be undefined if not using bash, so don't use set -u |
| 37 | random=$( (whoami ; hostname ; date; cat $1 ; echo $RANDOM) | git hash-object --stdin) |
| 38 | dest="$1.tmp.${random}" |
Mike Bjorge | 9322964 | 2016-02-29 11:48:15 -0800 | [diff] [blame] | 39 | |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame^] | 40 | trap 'rm -f "${dest}"' EXIT |
Dave Borowitz | a8d5391 | 2014-07-15 11:30:06 -0700 | [diff] [blame] | 41 | |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame^] | 42 | if ! git stripspace --strip-comments < "$1" > "${dest}" ; then |
| 43 | echo "cannot strip comments from $1" |
| 44 | exit 1 |
| 45 | fi |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 46 | |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame^] | 47 | if test ! -s "${dest}" ; then |
| 48 | echo "file is empty: $1" |
| 49 | exit 1 |
| 50 | fi |
Shawn O. Pearce | 9452e4e | 2009-08-22 18:17:46 -0700 | [diff] [blame] | 51 | |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame^] | 52 | # 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 |
| 54 | if ! 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 |
| 58 | fi |
Mike Bjorge | 9322964 | 2016-02-29 11:48:15 -0800 | [diff] [blame] | 59 | |
David Pursehouse | 3995ebd | 2020-02-16 12:38:36 +0900 | [diff] [blame^] | 60 | if ! mv "${dest}" "$1" ; then |
| 61 | echo "cannot mv ${dest} to $1" |
| 62 | exit 1 |
| 63 | fi |