diff options
| author | Maxim Cournoyer <maxim.cournoyer@gmail.com> | 2023-10-08 12:18:13 -0400 |
|---|---|---|
| committer | Maxim Cournoyer <maxim.cournoyer@gmail.com> | 2023-10-22 16:09:04 -0400 |
| commit | 8005e09b261d65bf0f7469cd8e89423c1c1db820 (patch) | |
| tree | f98b345a8de8dfeb831e6d20fd82f4890936d825 /etc/git/commit-msg | |
| parent | faeebdc37e9137d62e11175e83988f77496a6641 (diff) | |
build: Add a commit-msg hook that embeds Change-Id in commit messages.
Partially implements <https://issues.guix.gnu.org/66027>.
This will make it possible to track a merged commit back to its original
posting on the mailing list, and open the door to new opportunities such as
closing fully merged series automatically.
* Makefile.am (COMMIT_MSG_MAGIC): New variable.
(.git/hooks/commit-msg): New target.
* etc/git/commit-msg: New file.
* doc/contributing.texi (Configuring Git): Document Change-Id.
Series-changes: 3
- Clarify documentation text, as suggested by Simon
Change-Id: Ia92fa958eae600fdd4e180bad494c85db8bb4dd6
Reviewed-by: Simon Tournier <zimon.toutoune@gmail.com>
Diffstat (limited to 'etc/git/commit-msg')
| -rwxr-xr-x | etc/git/commit-msg | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/etc/git/commit-msg b/etc/git/commit-msg new file mode 100755 index 00000000000..dfa07918bba --- /dev/null +++ b/etc/git/commit-msg | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | #!/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 | |||
| 20 | ### Guix modifications start | ||
| 21 | COMMIT_MSG_MAGIC=VGhpcyBpcyB0aGUgY29tbWl0LW1zZyBob29rIG9mIEd1aXg= | ||
| 22 | top=$(git rev-parse --show-toplevel) | ||
| 23 | if 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 | ||
| 30 | fi | ||
| 31 | ### Guix modifications end | ||
| 32 | |||
| 33 | set -u | ||
| 34 | |||
| 35 | # avoid [[ which is not POSIX sh. | ||
| 36 | if test "$#" != 1 ; then | ||
| 37 | echo "$0 requires an argument." | ||
| 38 | exit 1 | ||
| 39 | fi | ||
| 40 | |||
| 41 | if test ! -f "$1" ; then | ||
| 42 | echo "file does not exist: $1" | ||
| 43 | exit 1 | ||
| 44 | fi | ||
| 45 | |||
| 46 | # Do not create a change id if requested | ||
| 47 | if test "false" = "$(git config --bool --get gerrit.createChangeId)" ; then | ||
| 48 | exit 0 | ||
| 49 | fi | ||
| 50 | |||
| 51 | if git rev-parse --verify HEAD >/dev/null 2>&1; then | ||
| 52 | refhash="$(git rev-parse HEAD)" | ||
| 53 | else | ||
| 54 | refhash="$(git hash-object -t tree /dev/null)" | ||
| 55 | fi | ||
| 56 | |||
| 57 | random=$({ git var GIT_COMMITTER_IDENT ; echo "$refhash" ; cat "$1"; } | git hash-object --stdin) | ||
| 58 | dest="$1.tmp.${random}" | ||
| 59 | |||
| 60 | trap 'rm -f "${dest}"' EXIT | ||
| 61 | |||
| 62 | if ! git stripspace --strip-comments < "$1" > "${dest}" ; then | ||
| 63 | echo "cannot strip comments from $1" | ||
| 64 | exit 1 | ||
| 65 | fi | ||
| 66 | |||
| 67 | if test ! -s "${dest}" ; then | ||
| 68 | echo "file is empty: $1" | ||
| 69 | exit 1 | ||
| 70 | fi | ||
| 71 | |||
| 72 | reviewurl="$(git config --get gerrit.reviewUrl)" | ||
| 73 | if test -n "${reviewurl}" ; then | ||
| 74 | if ! git interpret-trailers --parse < "$1" | grep -q '^Link:.*/id/I[0-9a-f]\{40\}$' ; then | ||
| 75 | if ! git interpret-trailers \ | ||
| 76 | --trailer "Link: ${reviewurl%/}/id/I${random}" < "$1" > "${dest}" ; then | ||
| 77 | echo "cannot insert link footer in $1" | ||
| 78 | exit 1 | ||
| 79 | fi | ||
| 80 | fi | ||
| 81 | else | ||
| 82 | # Avoid the --in-place option which only appeared in Git 2.8 | ||
| 83 | # Avoid the --if-exists option which only appeared in Git 2.15 | ||
| 84 | if ! git -c trailer.ifexists=doNothing interpret-trailers \ | ||
| 85 | --trailer "Change-Id: I${random}" < "$1" > "${dest}" ; then | ||
| 86 | echo "cannot insert change-id line in $1" | ||
| 87 | exit 1 | ||
| 88 | fi | ||
| 89 | fi | ||
| 90 | |||
| 91 | if ! mv "${dest}" "$1" ; then | ||
| 92 | echo "cannot mv ${dest} to $1" | ||
| 93 | exit 1 | ||
| 94 | fi | ||
