projects/guix·blob hurd-team · refs

123 lines · 3.4 KiB

1#!/bin/sh
2# From Gerrit Code Review 3.11.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
20### Guix modifications start
21COMMIT_MSG_MAGIC=VGhpcyBpcyB0aGUgY29tbWl0LW1zZyBob29rIG9mIEd1aXg=
22top=$(git rev-parse --show-toplevel)
23if test -d "$top/.git/hooks/commit-msg.d/"; then
24 for msg_hook in "$top/.git/hooks/commit-msg.d/"*; do
25 if ! sh "$msg_hook"; then
26 echo "error while running $msg_hook"
27 exit 1
28 fi
29 done
30fi
31### Guix modifications end
32
33set -u
34
35# avoid [[ which is not POSIX sh.
36if test "$#" != 1 ; then
37 echo "$0 requires an argument."
38 exit 1
39fi
40
41if test ! -f "$1" ; then
42 echo "file does not exist: $1"
43 exit 1
44fi
45
46# Do not create a change id if requested
47case "$(git config --get gerrit.createChangeId)" in
48 false)
49 exit 0
50 ;;
51 always)
52 ;;
53 *)
54 # Do not create a change id for squash/fixup commits.
55 if head -n1 "$1" | LC_ALL=C grep -q '^[a-z][a-z]*! '; then
56 exit 0
57 fi
58 ;;
59esac
60
61
62if git rev-parse --verify HEAD >/dev/null 2>&1; then
63 refhash="$(git rev-parse HEAD)"
64else
65 refhash="$(git hash-object -t tree /dev/null)"
66fi
67
68random=$({ git var GIT_COMMITTER_IDENT ; echo "$refhash" ; cat "$1"; } | git hash-object --stdin)
69dest="$1.tmp.${random}"
70
71trap 'rm -f "$dest" "$dest-2"' EXIT
72
73if ! cat "$1" | sed -e '/>8/q' | git stripspace --strip-comments > "${dest}" ; then
74 echo "cannot strip comments from $1"
75 exit 1
76fi
77
78if test ! -s "${dest}" ; then
79 echo "file is empty: $1"
80 exit 1
81fi
82
83reviewurl="$(git config --get gerrit.reviewUrl)"
84if test -n "${reviewurl}" ; then
85 token="Link"
86 value="${reviewurl%/}/id/I$random"
87 pattern=".*/id/I[0-9a-f]\{40\}"
88else
89 token="Change-Id"
90 value="I$random"
91 pattern=".*"
92fi
93
94if git interpret-trailers --parse < "$1" | grep -q "^$token: $pattern$" ; then
95 exit 0
96fi
97
98# There must be a Signed-off-by trailer for the code below to work. Insert a
99# sentinel at the end to make sure there is one.
100# Avoid the --in-place option which only appeared in Git 2.8
101if ! git interpret-trailers \
102 --trailer "Signed-off-by: SENTINEL" < "$1" > "$dest-2" ; then
103 echo "cannot insert Signed-off-by sentinel line in $1"
104 exit 1
105fi
106
107# Make sure the trailer appears before any Signed-off-by trailers by inserting
108# it as if it was a Signed-off-by trailer and then use sed to remove the
109# Signed-off-by prefix and the Signed-off-by sentinel line.
110# Avoid the --in-place option which only appeared in Git 2.8
111# Avoid the --where option which only appeared in Git 2.15
112if ! git -c trailer.where=before interpret-trailers \
113 --trailer "Signed-off-by: $token: $value" < "$dest-2" |
114 sed -e "s/^Signed-off-by: \($token: \)/\1/" \
115 -e "/^Signed-off-by: SENTINEL/d" > "$dest" ; then
116 echo "cannot insert $token line in $1"
117 exit 1
118fi
119
120if ! mv "${dest}" "$1" ; then
121 echo "cannot mv ${dest} to $1"
122 exit 1
123fi