NBash

Форк
0
144 строки · 4.1 Кб
1
#!/bin/sh
2
#
3
# An example hook script to block unannotated tags from entering.
4
# Called by "git receive-pack" with arguments: refname sha1-old sha1-new
5
#
6
# To enable this hook, rename this file to "update".
7
#
8
# Config
9
# ------
10
# hooks.allowbadwhitespace
11
#   This boolean sets whether a commit that adds bad whitespace will be
12
#   allowed in the repository.  By default they won't be.
13
# hooks.allowunannotated
14
#   This boolean sets whether unannotated tags will be allowed into the
15
#   repository.  By default they won't be.
16
# hooks.allowdeletetag
17
#   This boolean sets whether deleting tags will be allowed in the
18
#   repository.  By default they won't be.
19
# hooks.allowmodifytag
20
#   This boolean sets whether a tag may be modified after creation. By default
21
#   it won't be.
22
# hooks.allowdeletebranch
23
#   This boolean sets whether deleting branches will be allowed in the
24
#   repository.  By default they won't be.
25
# hooks.denycreatebranch
26
#   This boolean sets whether remotely creating branches will be denied
27
#   in the repository.  By default this is allowed.
28
#
29

30
# --- Command line
31
refname="$1"
32
oldrev="$2"
33
newrev="$3"
34

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

43
if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
44
	echo "usage: $0 <ref> <oldrev> <newrev>" >&2
45
	exit 1
46
fi
47

48
# --- Config
49
allowunannotated=$(git config --type=bool hooks.allowunannotated)
50
allowdeletebranch=$(git config --type=bool hooks.allowdeletebranch)
51
denycreatebranch=$(git config --type=bool hooks.denycreatebranch)
52
allowdeletetag=$(git config --type=bool hooks.allowdeletetag)
53
allowmodifytag=$(git config --type=bool hooks.allowmodifytag)
54

55
# check for no description
56
projectdesc=$(sed -e '1q' "$GIT_DIR/description")
57
case "$projectdesc" in
58
"Unnamed repository"* | "")
59
	echo "*** Project description file hasn't been set" >&2
60
	exit 1
61
	;;
62
esac
63

64
# --- Check types
65
# if $newrev is 0000...0000, it's a commit to delete a ref.
66
zero=$(git hash-object --stdin </dev/null | tr '[0-9a-f]' '0')
67
if [ "$newrev" = "$zero" ]; then
68
	newrev_type=delete
69
else
70
	newrev_type=$(git cat-file -t $newrev)
71
fi
72

73
check_diff=no
74
case "$refname","$newrev_type" in
75
	refs/tags/*,commit)
76
		# un-annotated tag
77
		short_refname=${refname##refs/tags/}
78
		if [ "$allowunannotated" != "true" ]; then
79
			echo "*** The un-annotated tag, $short_refname, is not allowed in this repository" >&2
80
			echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2
81
			exit 1
82
		fi
83
		;;
84
	refs/tags/*,delete)
85
		# delete tag
86
		if [ "$allowdeletetag" != "true" ]; then
87
			echo "*** Deleting a tag is not allowed in this repository" >&2
88
			exit 1
89
		fi
90
		;;
91
	refs/tags/*,tag)
92
		# annotated tag
93
		if [ "$allowmodifytag" != "true" ] && git rev-parse $refname > /dev/null 2>&1
94
		then
95
			echo "*** Tag '$refname' already exists." >&2
96
			echo "*** Modifying a tag is not allowed in this repository." >&2
97
			exit 1
98
		fi
99
		;;
100
	refs/heads/*,commit)
101
		# branch
102
		check_diff=yes
103
		if [ "$oldrev" = "$zero" -a "$denycreatebranch" = "true" ]; then
104
			echo "*** Creating a branch is not allowed in this repository" >&2
105
			exit 1
106
		fi
107
		;;
108
	refs/heads/*,delete)
109
		# delete branch
110
		if [ "$allowdeletebranch" != "true" ]; then
111
			echo "*** Deleting a branch is not allowed in this repository" >&2
112
			exit 1
113
		fi
114
		;;
115
	refs/remotes/*,commit)
116
		# tracking branch
117
		check_diff=yes
118
		;;
119
	refs/remotes/*,delete)
120
		# delete tracking branch
121
		if [ "$allowdeletebranch" != "true" ]; then
122
			echo "*** Deleting a tracking branch is not allowed in this repository" >&2
123
			exit 1
124
		fi
125
		;;
126
	*)
127
		# Anything else (is there anything else?)
128
		echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2
129
		exit 1
130
		;;
131
esac
132

133
if [ $check_diff = yes ]; then
134
	allow_bad_whitespace=$(git config --bool hooks.allowbadwhitespace)
135
	if [ "$allow_bad_whitespace" != true ]; then
136
		# If pushing a new branch, compare it with the empty object.
137
		[ "$oldrev" != "$zero" ] ||
138
			oldrev=4b825dc642cb6eb9a060e54bf8d69288fbee4904
139
		exec git diff --check "$oldrev" "$newrev" --
140
	fi
141
fi
142

143
# --- Finished
144
exit 0
145

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.