hooks--update.sample 2.8 KB
Newer Older
J
Junio C Hamano 已提交
1 2
#!/bin/sh
#
3
# An example hook script to blocks unannotated tags from entering.
4
# Called by git-receive-pack with arguments: refname sha1-old sha1-new
J
Junio C Hamano 已提交
5
#
6
# To enable this hook, rename this file to "update".
J
Junio C Hamano 已提交
7
#
8 9 10 11 12
# Config
# ------
# hooks.allowunannotated
#   This boolean sets whether unannotated tags will be allowed into the
#   repository.  By default they won't be.
13 14 15 16 17 18
# hooks.allowdeletetag
#   This boolean sets whether deleting tags will be allowed in the
#   repository.  By default they won't be.
# hooks.allowdeletebranch
#   This boolean sets whether deleting branches will be allowed in the
#   repository.  By default they won't be.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#

# --- Command line
refname="$1"
oldrev="$2"
newrev="$3"

# --- Safety check
if [ -z "$GIT_DIR" ]; then
	echo "Don't run this script from the command line." >&2
	echo " (if you want, you could supply GIT_DIR then run" >&2
	echo "  $0 <ref> <oldrev> <newrev>)" >&2
	exit 1
fi

if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
	echo "Usage: $0 <ref> <oldrev> <newrev>" >&2
	exit 1
fi

# --- Config
40 41 42
allowunannotated=$(git config --bool hooks.allowunannotated)
allowdeletebranch=$(git config --bool hooks.allowdeletebranch)
allowdeletetag=$(git config --bool hooks.allowdeletetag)
43

44
# check for no description
45
projectdesc=$(sed -e '1q' "$GIT_DIR/description")
46 47
case "$projectdesc" in
"Unnamed repository"* | "")
48 49
	echo "*** Project description file hasn't been set" >&2
	exit 1
50 51
	;;
esac
52

53
# --- Check types
54
# if $newrev is 0000...0000, it's a commit to delete a ref.
55
if [ "$newrev" = "0000000000000000000000000000000000000000" ]; then
56
	newrev_type=delete
57 58 59
else
	newrev_type=$(git-cat-file -t $newrev)
fi
60 61 62

case "$refname","$newrev_type" in
	refs/tags/*,commit)
P
Pavel Roskin 已提交
63
		# un-annotated tag
64
		short_refname=${refname##refs/tags/}
65
		if [ "$allowunannotated" != "true" ]; then
66
			echo "*** The un-annotated tag, $short_refname, is not allowed in this repository" >&2
67 68
			echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2
			exit 1
69
		fi
70
		;;
71 72 73 74 75 76 77
	refs/tags/*,delete)
		# delete tag
		if [ "$allowdeletetag" != "true" ]; then
			echo "*** Deleting a tag is not allowed in this repository" >&2
			exit 1
		fi
		;;
78 79
	refs/tags/*,tag)
		# annotated tag
80
		;;
81 82 83
	refs/heads/*,commit)
		# branch
		;;
84 85 86 87 88 89 90
	refs/heads/*,delete)
		# delete branch
		if [ "$allowdeletebranch" != "true" ]; then
			echo "*** Deleting a branch is not allowed in this repository" >&2
			exit 1
		fi
		;;
91 92 93
	refs/remotes/*,commit)
		# tracking branch
		;;
94 95 96 97 98 99 100
	refs/remotes/*,delete)
		# delete tracking branch
		if [ "$allowdeletebranch" != "true" ]; then
			echo "*** Deleting a tracking branch is not allowed in this repository" >&2
			exit 1
		fi
		;;
101 102
	*)
		# Anything else (is there anything else?)
103
		echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2
104 105 106 107 108
		exit 1
		;;
esac

# --- Finished
J
Junio C Hamano 已提交
109
exit 0