Skip to content

Commit 8a3ee7c

Browse files
agericJunio C Hamano
authored andcommitted
update-hook: Major overhaul (handling tags, mainly).
This is the update hook we use in all our git-repos. It has some improvements over the original version, namely: * Don't send every commit since dawn of time when adding a new tag. * When updating an annotated tag, just send the diffs since the last tag. * Add diffstat output for 'normal' commits (top) and annotated tags (bottom). * Block un-annotated tags in shared repos. I'm a bit uncertain about that last one, but it demonstrates how to disallow updates of a ref which we use, so I kept it. Note that git-describe is needed for the "changes since last annotated tag" thing to work. Signed-off-by: Andreas Ericsson <ae@op5.se> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent dcc6e28 commit 8a3ee7c

File tree

1 file changed

+65
-6
lines changed

1 file changed

+65
-6
lines changed

templates/hooks--update

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,89 @@
11
#!/bin/sh
22
#
33
# An example hook script to mail out commit update information.
4+
# It also blocks tags that aren't annotated.
45
# Called by git-receive-pack with arguments: refname sha1-old sha1-new
56
#
67
# To enable this hook:
78
# (1) change the recipient e-mail address
89
# (2) make this file executable by "chmod +x update".
910
#
1011

11-
recipient="commit-list@example.com"
12+
project=$(cat $GIT_DIR/description)
13+
recipients="commit-list@somewhere.com commit-list@somewhereelse.com"
1214

13-
if expr "$2" : '0*$' >/dev/null
15+
ref_type=$(git cat-file -t "$3")
16+
17+
# Only allow annotated tags in a shared repo
18+
# Remove this code to treat dumb tags the same as everything else
19+
case "$1","$ref_type" in
20+
refs/tags/*,commit)
21+
echo "*** Un-annotated tags are not allowed in this repo" >&2
22+
echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate."
23+
exit 1;;
24+
refs/tags/*,tag)
25+
echo "### Pushing version '${1##refs/tags/}' to the masses" >&2
26+
# recipients="release-announce@somwehere.com announce@somewhereelse.com"
27+
;;
28+
esac
29+
30+
# set this to 'cat' to get a very detailed listing.
31+
# short only kicks in when an annotated tag is added
32+
short='git shortlog'
33+
34+
# see 'date --help' for info on how to write this
35+
# The default is a human-readable iso8601-like format with minute
36+
# precision ('2006-01-25 15:58 +0100' for example)
37+
date_format="%F %R %z"
38+
39+
(if expr "$2" : '0*$' >/dev/null
1440
then
15-
echo "Created a new ref, with the following commits:"
16-
git-rev-list --pretty "$3"
41+
# new ref
42+
case "$1" in
43+
refs/tags/*)
44+
# a pushed and annotated tag (usually) means a new version
45+
tag="${1##refs/tags/}"
46+
if [ "$ref_type" = tag ]; then
47+
eval $(git cat-file tag $3 | \
48+
sed -n '4s/tagger \([^>]*>\)[^0-9]*\([0-9]*\).*/tagger="\1" ts="\2"/p')
49+
date=$(date --date="1970-01-01 00:00:00 $ts seconds" +"$date_format")
50+
echo "Tag '$tag' created by $tagger at $date"
51+
git cat-file tag $3 | sed -n '5,$p'
52+
echo
53+
fi
54+
prev=$(git describe "$3^" | sed 's/-g.*//')
55+
# the first tag in a repo will yield no $prev
56+
if [ -z "$prev" ]; then
57+
echo "Changes since the dawn of time:"
58+
git rev-list --pretty $3 | $short
59+
else
60+
echo "Changes since $prev:"
61+
git rev-list --pretty $prev..$3 | $short
62+
echo ---
63+
git diff $prev..$3 | diffstat -p1
64+
echo ---
65+
fi
66+
;;
67+
68+
refs/heads/*)
69+
branch="${1##refs/heads/}"
70+
echo "New branch '$branch' available with the following commits:"
71+
git-rev-list --pretty "$3"
72+
;;
73+
esac
1774
else
1875
base=$(git-merge-base "$2" "$3")
1976
case "$base" in
2077
"$2")
78+
git diff "$3" "^$base" | diffstat -p1
79+
echo
2180
echo "New commits:"
2281
;;
2382
*)
2483
echo "Rebased ref, commits from common ancestor:"
2584
;;
2685
esac
2786
git-rev-list --pretty "$3" "^$base"
28-
fi |
29-
mail -s "Changes to ref $1" "$recipient"
87+
fi) |
88+
mail -s "$project: Changes to '${1##refs/heads/}'" $recipients
3089
exit 0

0 commit comments

Comments
 (0)