diff options
author | Evan Benn <evanbenn@chromium.org> | 2022-07-29 09:53:27 +1000 |
---|---|---|
committer | Mike Frysinger <vapier@google.com> | 2022-07-29 01:29:19 +0000 |
commit | a8c34d1075123ca58f56cc93e9564efdb13292b2 (patch) | |
tree | af4c4d487856728a518e42969c47db8c3711fb05 /hooks | |
parent | 5951e3043f8d9567bfcd6e0f328ae057e1dfad11 (diff) | |
download | git-repo-a8c34d1075123ca58f56cc93e9564efdb13292b2.tar.gz |
commit-msg: Sync commit-msg from gerrit 3.6.1
This includes:
- Ignore squash commits.
- Update to hash generation.
- Update to Change-Id and trailer generation.
TEST=cp hooks/commit-msg .git/hooks/commit-msg
TEST=git commit -s # right order
TEST=git commit; git commit --amend -s # right order
Change-Id: I4e4a2a02905d330f2863b562d7914fe6567a4118
Signed-off-by: Evan Benn <evanbenn@chromium.org>
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/339554
Tested-by: Evan Benn <evanbenn@google.com>
Reviewed-by: Mike Frysinger <vapier@google.com>
Diffstat (limited to 'hooks')
-rwxr-xr-x | hooks/commit-msg | 59 |
1 files changed, 50 insertions, 9 deletions
diff --git a/hooks/commit-msg b/hooks/commit-msg index 70d67eaf..8c6476fa 100755 --- a/hooks/commit-msg +++ b/hooks/commit-msg | |||
@@ -1,5 +1,5 @@ | |||
1 | #!/bin/sh | 1 | #!/bin/sh |
2 | # From Gerrit Code Review 3.1.3 | 2 | # From Gerrit Code Review 3.6.1 c67916dbdc07555c44e32a68f92ffc484b9b34f0 |
3 | # | 3 | # |
4 | # Part of Gerrit Code Review (https://www.gerritcodereview.com/) | 4 | # Part of Gerrit Code Review (https://www.gerritcodereview.com/) |
5 | # | 5 | # |
@@ -17,6 +17,8 @@ | |||
17 | # See the License for the specific language governing permissions and | 17 | # See the License for the specific language governing permissions and |
18 | # limitations under the License. | 18 | # limitations under the License. |
19 | 19 | ||
20 | set -u | ||
21 | |||
20 | # avoid [[ which is not POSIX sh. | 22 | # avoid [[ which is not POSIX sh. |
21 | if test "$#" != 1 ; then | 23 | if test "$#" != 1 ; then |
22 | echo "$0 requires an argument." | 24 | echo "$0 requires an argument." |
@@ -29,15 +31,25 @@ if test ! -f "$1" ; then | |||
29 | fi | 31 | fi |
30 | 32 | ||
31 | # Do not create a change id if requested | 33 | # Do not create a change id if requested |
32 | if test "false" = "`git config --bool --get gerrit.createChangeId`" ; then | 34 | if test "false" = "$(git config --bool --get gerrit.createChangeId)" ; then |
35 | exit 0 | ||
36 | fi | ||
37 | |||
38 | # Do not create a change id for squash commits. | ||
39 | if head -n1 "$1" | grep -q '^squash! '; then | ||
33 | exit 0 | 40 | exit 0 |
34 | fi | 41 | fi |
35 | 42 | ||
36 | # $RANDOM will be undefined if not using bash, so don't use set -u | 43 | if git rev-parse --verify HEAD >/dev/null 2>&1; then |
37 | random=$( (whoami ; hostname ; date; cat $1 ; echo $RANDOM) | git hash-object --stdin) | 44 | refhash="$(git rev-parse HEAD)" |
45 | else | ||
46 | refhash="$(git hash-object -t tree /dev/null)" | ||
47 | fi | ||
48 | |||
49 | random=$({ git var GIT_COMMITTER_IDENT ; echo "$refhash" ; cat "$1"; } | git hash-object --stdin) | ||
38 | dest="$1.tmp.${random}" | 50 | dest="$1.tmp.${random}" |
39 | 51 | ||
40 | trap 'rm -f "${dest}"' EXIT | 52 | trap 'rm -f "$dest" "$dest-2"' EXIT |
41 | 53 | ||
42 | if ! git stripspace --strip-comments < "$1" > "${dest}" ; then | 54 | if ! git stripspace --strip-comments < "$1" > "${dest}" ; then |
43 | echo "cannot strip comments from $1" | 55 | echo "cannot strip comments from $1" |
@@ -49,11 +61,40 @@ if test ! -s "${dest}" ; then | |||
49 | exit 1 | 61 | exit 1 |
50 | fi | 62 | fi |
51 | 63 | ||
64 | reviewurl="$(git config --get gerrit.reviewUrl)" | ||
65 | if test -n "${reviewurl}" ; then | ||
66 | token="Link" | ||
67 | value="${reviewurl%/}/id/I$random" | ||
68 | pattern=".*/id/I[0-9a-f]\{40\}$" | ||
69 | else | ||
70 | token="Change-Id" | ||
71 | value="I$random" | ||
72 | pattern=".*" | ||
73 | fi | ||
74 | |||
75 | if git interpret-trailers --parse < "$1" | grep -q "^$token: $pattern$" ; then | ||
76 | exit 0 | ||
77 | fi | ||
78 | |||
79 | # There must be a Signed-off-by trailer for the code below to work. Insert a | ||
80 | # sentinel at the end to make sure there is one. | ||
81 | # Avoid the --in-place option which only appeared in Git 2.8 | ||
82 | if ! git interpret-trailers \ | ||
83 | --trailer "Signed-off-by: SENTINEL" < "$1" > "$dest-2" ; then | ||
84 | echo "cannot insert Signed-off-by sentinel line in $1" | ||
85 | exit 1 | ||
86 | fi | ||
87 | |||
88 | # Make sure the trailer appears before any Signed-off-by trailers by inserting | ||
89 | # it as if it was a Signed-off-by trailer and then use sed to remove the | ||
90 | # Signed-off-by prefix and the Signed-off-by sentinel line. | ||
52 | # Avoid the --in-place option which only appeared in Git 2.8 | 91 | # 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 | 92 | # Avoid the --where option which only appeared in Git 2.15 |
54 | if ! git -c trailer.ifexists=doNothing interpret-trailers \ | 93 | if ! git -c trailer.where=before interpret-trailers \ |
55 | --trailer "Change-Id: I${random}" < "$1" > "${dest}" ; then | 94 | --trailer "Signed-off-by: $token: $value" < "$dest-2" | |
56 | echo "cannot insert change-id line in $1" | 95 | sed -re "s/^Signed-off-by: ($token: )/\1/" \ |
96 | -e "/^Signed-off-by: SENTINEL/d" > "$dest" ; then | ||
97 | echo "cannot insert $token line in $1" | ||
57 | exit 1 | 98 | exit 1 |
58 | fi | 99 | fi |
59 | 100 | ||