diff options
| author | Shawn O. Pearce <sop@google.com> | 2009-08-22 18:17:46 -0700 |
|---|---|---|
| committer | Shawn O. Pearce <sop@google.com> | 2010-03-06 19:21:00 -0800 |
| commit | 9452e4ec0941fbee163e35ebdcd6ca6ee7df55cb (patch) | |
| tree | 63730cd5665d1a6233c4e0d922a886cbd035cad3 /hooks/commit-msg | |
| parent | 4c50deea28badb7007fa6b78c187de50eacdd07a (diff) | |
| download | git-repo-9452e4ec0941fbee163e35ebdcd6ca6ee7df55cb.tar.gz | |
Automatically install Gerrit Code Review's commit-msg hookv1.6.9
Most users of repo are also using Gerrit Code Review, and will want
the commit-msg hook to be automatically installed into their local
projects so that Change-Ids are assigned when commits are created,
not when they are first uploaded.
(cherry picked from commit a949fa5d202f0a1f812d7630f3e5bf0f02ca4e98
but squashed with latest hook script from version 2.1.2)
Change-Id: Ie68b2d60ac85d8c2285d2e1e6a4536eb76695547
Signed-off-by: Shawn O. Pearce <sop@google.com>
Diffstat (limited to 'hooks/commit-msg')
| -rwxr-xr-x | hooks/commit-msg | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/hooks/commit-msg b/hooks/commit-msg new file mode 100755 index 00000000..712921c9 --- /dev/null +++ b/hooks/commit-msg | |||
| @@ -0,0 +1,101 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | # From Gerrit Code Review 2.1.2-rc2-33-g7e30c72 | ||
| 3 | # | ||
| 4 | # Part of Gerrit Code Review (http://code.google.com/p/gerrit/) | ||
| 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 | |||
| 21 | CHANGE_ID_AFTER="Bug|Issue" | ||
| 22 | MSG="$1" | ||
| 23 | |||
| 24 | # Check for, and add if missing, a unique Change-Id | ||
| 25 | # | ||
| 26 | add_ChangeId() { | ||
| 27 | clean_message=$(sed -e ' | ||
| 28 | /^diff --git a\/.*/{ | ||
| 29 | s/// | ||
| 30 | q | ||
| 31 | } | ||
| 32 | /^Signed-off-by:/d | ||
| 33 | /^#/d | ||
| 34 | ' "$MSG" | git stripspace) | ||
| 35 | if test -z "$clean_message" | ||
| 36 | then | ||
| 37 | return | ||
| 38 | fi | ||
| 39 | |||
| 40 | if grep -i '^Change-Id:' "$MSG" >/dev/null | ||
| 41 | then | ||
| 42 | return | ||
| 43 | fi | ||
| 44 | |||
| 45 | id=$(_gen_ChangeId) | ||
| 46 | perl -e ' | ||
| 47 | $MSG = shift; | ||
| 48 | $id = shift; | ||
| 49 | $CHANGE_ID_AFTER = shift; | ||
| 50 | |||
| 51 | undef $/; | ||
| 52 | open(I, $MSG); $_ = <I>; close I; | ||
| 53 | s|^diff --git a/.*||ms; | ||
| 54 | s|^#.*$||mg; | ||
| 55 | exit unless $_; | ||
| 56 | |||
| 57 | @message = split /\n/; | ||
| 58 | $haveFooter = 0; | ||
| 59 | $startFooter = @message; | ||
| 60 | for($line = @message - 1; $line >= 0; $line--) { | ||
| 61 | $_ = $message[$line]; | ||
| 62 | |||
| 63 | ($haveFooter++, next) if /^[a-zA-Z0-9-]+:/; | ||
| 64 | next if /^[ []/; | ||
| 65 | $startFooter = $line if ($haveFooter && /^\r?$/); | ||
| 66 | last; | ||
| 67 | } | ||
| 68 | |||
| 69 | @footer = @message[$startFooter+1..@message]; | ||
| 70 | @message = @message[0..$startFooter]; | ||
| 71 | push(@footer, "") unless @footer; | ||
| 72 | |||
| 73 | for ($line = 0; $line < @footer; $line++) { | ||
| 74 | $_ = $footer[$line]; | ||
| 75 | next if /^($CHANGE_ID_AFTER):/i; | ||
| 76 | last; | ||
| 77 | } | ||
| 78 | splice(@footer, $line, 0, "Change-Id: I$id"); | ||
| 79 | |||
| 80 | $_ = join("\n", @message, @footer); | ||
| 81 | open(O, ">$MSG"); print O; close O; | ||
| 82 | ' "$MSG" "$id" "$CHANGE_ID_AFTER" | ||
| 83 | } | ||
| 84 | _gen_ChangeIdInput() { | ||
| 85 | echo "tree $(git write-tree)" | ||
| 86 | if parent=$(git rev-parse HEAD^0 2>/dev/null) | ||
| 87 | then | ||
| 88 | echo "parent $parent" | ||
| 89 | fi | ||
| 90 | echo "author $(git var GIT_AUTHOR_IDENT)" | ||
| 91 | echo "committer $(git var GIT_COMMITTER_IDENT)" | ||
| 92 | echo | ||
| 93 | printf '%s' "$clean_message" | ||
| 94 | } | ||
| 95 | _gen_ChangeId() { | ||
| 96 | _gen_ChangeIdInput | | ||
| 97 | git hash-object -t commit --stdin | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | add_ChangeId | ||
