diff options
| author | Shawn O. Pearce <sop@google.com> | 2009-08-22 18:17:46 -0700 |
|---|---|---|
| committer | Shawn O. Pearce <sop@google.com> | 2009-08-22 18:22:04 -0700 |
| commit | a949fa5d202f0a1f812d7630f3e5bf0f02ca4e98 (patch) | |
| tree | fb0a8ad39a99827747dce6272bcd10dd33c41756 | |
| parent | 0afac0856cee05929ec8aa952f6023985e5ad1e4 (diff) | |
| download | git-repo-a949fa5d202f0a1f812d7630f3e5bf0f02ca4e98.tar.gz | |
Automatically install Gerrit Code Review's commit-msg hook
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.
Change-Id: Ide42e93b068832f099d68a79c2863d22145d05ad
Signed-off-by: Shawn O. Pearce <sop@google.com>
| -rwxr-xr-x | hooks/commit-msg | 44 | ||||
| -rw-r--r-- | project.py | 22 |
2 files changed, 62 insertions, 4 deletions
diff --git a/hooks/commit-msg b/hooks/commit-msg new file mode 100755 index 00000000..ecd6a20b --- /dev/null +++ b/hooks/commit-msg | |||
| @@ -0,0 +1,44 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | MSG="$1" | ||
| 4 | |||
| 5 | # Check for, and add if missing, a unique Change-Id | ||
| 6 | # | ||
| 7 | add_ChangeId() { | ||
| 8 | if grep '^Change-Id: ' "$MSG" >/dev/null | ||
| 9 | then | ||
| 10 | return | ||
| 11 | fi | ||
| 12 | |||
| 13 | id=$(_gen_ChangeId) | ||
| 14 | out="$MSG.new" | ||
| 15 | ftt="$MSG.footers" | ||
| 16 | sed -e '/^[A-Za-z][A-Za-z0-9-]*: /,$d' <"$MSG" >"$out" | ||
| 17 | sed -ne '/^[A-Za-z][A-Za-z0-9-]*: /,$p' <"$MSG" >"$ftt" | ||
| 18 | if ! [ -s "$ftt" ] | ||
| 19 | then | ||
| 20 | echo >>"$out" | ||
| 21 | fi | ||
| 22 | echo "Change-Id: I$id" >>"$out" | ||
| 23 | cat "$ftt" >>"$out" | ||
| 24 | mv -f "$out" "$MSG" | ||
| 25 | rm -f "$out" "$ftt" | ||
| 26 | } | ||
| 27 | _gen_ChangeIdInput() { | ||
| 28 | echo "tree $(git write-tree)" | ||
| 29 | if parent=$(git rev-parse HEAD^0 2>/dev/null) | ||
| 30 | then | ||
| 31 | echo "parent $parent" | ||
| 32 | fi | ||
| 33 | echo "author $(git var GIT_AUTHOR_IDENT)" | ||
| 34 | echo "committer $(git var GIT_COMMITTER_IDENT)" | ||
| 35 | echo | ||
| 36 | cat "$MSG" | ||
| 37 | } | ||
| 38 | _gen_ChangeId() { | ||
| 39 | _gen_ChangeIdInput | | ||
| 40 | git hash-object -t commit --stdin | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | add_ChangeId | ||
| @@ -1056,13 +1056,27 @@ class Project(object): | |||
| 1056 | if not os.path.exists(hooks): | 1056 | if not os.path.exists(hooks): |
| 1057 | os.makedirs(hooks) | 1057 | os.makedirs(hooks) |
| 1058 | for stock_hook in repo_hooks(): | 1058 | for stock_hook in repo_hooks(): |
| 1059 | dst = os.path.join(hooks, os.path.basename(stock_hook)) | 1059 | name = os.path.basename(stock_hook) |
| 1060 | |||
| 1061 | if name in ('commit-msg') and not self.remote.review: | ||
| 1062 | # Don't install a Gerrit Code Review hook if this | ||
| 1063 | # project does not appear to use it for reviews. | ||
| 1064 | # | ||
| 1065 | continue | ||
| 1066 | |||
| 1067 | dst = os.path.join(hooks, name) | ||
| 1068 | if os.path.islink(dst): | ||
| 1069 | continue | ||
| 1070 | if os.path.exists(dst): | ||
| 1071 | if filecmp.cmp(stock_hook, dst, shallow=False): | ||
| 1072 | os.remove(dst) | ||
| 1073 | else: | ||
| 1074 | _error("%s: Not replacing %s hook", self.relpath, name) | ||
| 1075 | continue | ||
| 1060 | try: | 1076 | try: |
| 1061 | os.symlink(relpath(stock_hook, dst), dst) | 1077 | os.symlink(relpath(stock_hook, dst), dst) |
| 1062 | except OSError, e: | 1078 | except OSError, e: |
| 1063 | if e.errno == errno.EEXIST: | 1079 | if e.errno == errno.EPERM: |
| 1064 | pass | ||
| 1065 | elif e.errno == errno.EPERM: | ||
| 1066 | raise GitError('filesystem must support symlinks') | 1080 | raise GitError('filesystem must support symlinks') |
| 1067 | else: | 1081 | else: |
| 1068 | raise | 1082 | raise |
