mirror of
https://gerrit.googlesource.com/git-repo
synced 2024-12-21 07:16:21 +00:00
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>
This commit is contained in:
parent
0afac0856c
commit
a949fa5d20
44
hooks/commit-msg
Executable file
44
hooks/commit-msg
Executable file
@ -0,0 +1,44 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
MSG="$1"
|
||||||
|
|
||||||
|
# Check for, and add if missing, a unique Change-Id
|
||||||
|
#
|
||||||
|
add_ChangeId() {
|
||||||
|
if grep '^Change-Id: ' "$MSG" >/dev/null
|
||||||
|
then
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
id=$(_gen_ChangeId)
|
||||||
|
out="$MSG.new"
|
||||||
|
ftt="$MSG.footers"
|
||||||
|
sed -e '/^[A-Za-z][A-Za-z0-9-]*: /,$d' <"$MSG" >"$out"
|
||||||
|
sed -ne '/^[A-Za-z][A-Za-z0-9-]*: /,$p' <"$MSG" >"$ftt"
|
||||||
|
if ! [ -s "$ftt" ]
|
||||||
|
then
|
||||||
|
echo >>"$out"
|
||||||
|
fi
|
||||||
|
echo "Change-Id: I$id" >>"$out"
|
||||||
|
cat "$ftt" >>"$out"
|
||||||
|
mv -f "$out" "$MSG"
|
||||||
|
rm -f "$out" "$ftt"
|
||||||
|
}
|
||||||
|
_gen_ChangeIdInput() {
|
||||||
|
echo "tree $(git write-tree)"
|
||||||
|
if parent=$(git rev-parse HEAD^0 2>/dev/null)
|
||||||
|
then
|
||||||
|
echo "parent $parent"
|
||||||
|
fi
|
||||||
|
echo "author $(git var GIT_AUTHOR_IDENT)"
|
||||||
|
echo "committer $(git var GIT_COMMITTER_IDENT)"
|
||||||
|
echo
|
||||||
|
cat "$MSG"
|
||||||
|
}
|
||||||
|
_gen_ChangeId() {
|
||||||
|
_gen_ChangeIdInput |
|
||||||
|
git hash-object -t commit --stdin
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
add_ChangeId
|
22
project.py
22
project.py
@ -1056,13 +1056,27 @@ class Project(object):
|
|||||||
if not os.path.exists(hooks):
|
if not os.path.exists(hooks):
|
||||||
os.makedirs(hooks)
|
os.makedirs(hooks)
|
||||||
for stock_hook in repo_hooks():
|
for stock_hook in repo_hooks():
|
||||||
dst = os.path.join(hooks, os.path.basename(stock_hook))
|
name = os.path.basename(stock_hook)
|
||||||
|
|
||||||
|
if name in ('commit-msg') and not self.remote.review:
|
||||||
|
# Don't install a Gerrit Code Review hook if this
|
||||||
|
# project does not appear to use it for reviews.
|
||||||
|
#
|
||||||
|
continue
|
||||||
|
|
||||||
|
dst = os.path.join(hooks, name)
|
||||||
|
if os.path.islink(dst):
|
||||||
|
continue
|
||||||
|
if os.path.exists(dst):
|
||||||
|
if filecmp.cmp(stock_hook, dst, shallow=False):
|
||||||
|
os.remove(dst)
|
||||||
|
else:
|
||||||
|
_error("%s: Not replacing %s hook", self.relpath, name)
|
||||||
|
continue
|
||||||
try:
|
try:
|
||||||
os.symlink(relpath(stock_hook, dst), dst)
|
os.symlink(relpath(stock_hook, dst), dst)
|
||||||
except OSError, e:
|
except OSError, e:
|
||||||
if e.errno == errno.EEXIST:
|
if e.errno == errno.EPERM:
|
||||||
pass
|
|
||||||
elif e.errno == errno.EPERM:
|
|
||||||
raise GitError('filesystem must support symlinks')
|
raise GitError('filesystem must support symlinks')
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
|
Loading…
Reference in New Issue
Block a user