hooks--update.sample 3.1 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
# hooks.denycreatebranch
#   This boolean sets whether remotely creating branches will be denied
#   in the repository.  By default this is allowed.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#

# --- 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
43 44
allowunannotated=$(git config --bool hooks.allowunannotated)
allowdeletebranch=$(git config --bool hooks.allowdeletebranch)
45
denycreatebranch=$(git config --bool hooks.denycreatebranch)
46
allowdeletetag=$(git config --bool hooks.allowdeletetag)
47

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

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

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

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