diff options
| -rwxr-xr-x | hooks/commit-msg | 101 | ||||
| -rw-r--r-- | project.py | 22 |
2 files changed, 119 insertions, 4 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 | ||
| @@ -1055,13 +1055,27 @@ class Project(object): | |||
| 1055 | if not os.path.exists(hooks): | 1055 | if not os.path.exists(hooks): |
| 1056 | os.makedirs(hooks) | 1056 | os.makedirs(hooks) |
| 1057 | for stock_hook in repo_hooks(): | 1057 | for stock_hook in repo_hooks(): |
| 1058 | dst = os.path.join(hooks, os.path.basename(stock_hook)) | 1058 | name = os.path.basename(stock_hook) |
| 1059 | |||
| 1060 | if name in ('commit-msg') and not self.remote.review: | ||
| 1061 | # Don't install a Gerrit Code Review hook if this | ||
| 1062 | # project does not appear to use it for reviews. | ||
| 1063 | # | ||
| 1064 | continue | ||
| 1065 | |||
| 1066 | dst = os.path.join(hooks, name) | ||
| 1067 | if os.path.islink(dst): | ||
| 1068 | continue | ||
| 1069 | if os.path.exists(dst): | ||
| 1070 | if filecmp.cmp(stock_hook, dst, shallow=False): | ||
| 1071 | os.remove(dst) | ||
| 1072 | else: | ||
| 1073 | _error("%s: Not replacing %s hook", self.relpath, name) | ||
| 1074 | continue | ||
| 1059 | try: | 1075 | try: |
| 1060 | os.symlink(relpath(stock_hook, dst), dst) | 1076 | os.symlink(relpath(stock_hook, dst), dst) |
| 1061 | except OSError, e: | 1077 | except OSError, e: |
| 1062 | if e.errno == errno.EEXIST: | 1078 | if e.errno == errno.EPERM: |
| 1063 | pass | ||
| 1064 | elif e.errno == errno.EPERM: | ||
| 1065 | raise GitError('filesystem must support symlinks') | 1079 | raise GitError('filesystem must support symlinks') |
| 1066 | else: | 1080 | else: |
| 1067 | raise | 1081 | raise |
