@@ -21,8 +21,185 @@ const (
21
21
HookPostReceive HookName = "post-receive"
22
22
)
23
23
24
- // ServerSideHooks contains a list of Git hooks are supported on the server side.
25
- var ServerSideHooks = []HookName {HookPreReceive , HookUpdate , HookPostReceive }
24
+ var (
25
+ // ServerSideHooks contains a list of Git hooks that are supported on the server side.
26
+ ServerSideHooks = []HookName {HookPreReceive , HookUpdate , HookPostReceive }
27
+ // ServerSideHookSamples contains samples of Git hooks that are supported on the server side.
28
+ ServerSideHookSamples = map [HookName ]string {
29
+ HookPreReceive : `#!/bin/sh
30
+ #
31
+ # An example hook script to make use of push options.
32
+ # The example simply echoes all push options that start with 'echoback='
33
+ # and rejects all pushes when the "reject" push option is used.
34
+ #
35
+ # To enable this hook, rename this file to "pre-receive".
36
+
37
+ if test -n "$GIT_PUSH_OPTION_COUNT"
38
+ then
39
+ i=0
40
+ while test "$i" -lt "$GIT_PUSH_OPTION_COUNT"
41
+ do
42
+ eval "value=\$GIT_PUSH_OPTION_$i"
43
+ case "$value" in
44
+ echoback=*)
45
+ echo "echo from the pre-receive-hook: ${value#*=}" >&2
46
+ ;;
47
+ reject)
48
+ exit 1
49
+ esac
50
+ i=$((i + 1))
51
+ done
52
+ fi
53
+ ` ,
54
+ HookUpdate : `#!/bin/sh
55
+ #
56
+ # An example hook script to block unannotated tags from entering.
57
+ # Called by "git receive-pack" with arguments: refname sha1-old sha1-new
58
+ #
59
+ # To enable this hook, rename this file to "update".
60
+ #
61
+ # Config
62
+ # ------
63
+ # hooks.allowunannotated
64
+ # This boolean sets whether unannotated tags will be allowed into the
65
+ # repository. By default they won't be.
66
+ # hooks.allowdeletetag
67
+ # This boolean sets whether deleting tags will be allowed in the
68
+ # repository. By default they won't be.
69
+ # hooks.allowmodifytag
70
+ # This boolean sets whether a tag may be modified after creation. By default
71
+ # it won't be.
72
+ # hooks.allowdeletebranch
73
+ # This boolean sets whether deleting branches will be allowed in the
74
+ # repository. By default they won't be.
75
+ # hooks.denycreatebranch
76
+ # This boolean sets whether remotely creating branches will be denied
77
+ # in the repository. By default this is allowed.
78
+ #
79
+
80
+ # --- Command line
81
+ refname="$1"
82
+ oldrev="$2"
83
+ newrev="$3"
84
+
85
+ # --- Safety check
86
+ if [ -z "$GIT_DIR" ]; then
87
+ echo "Don't run this script from the command line." >&2
88
+ echo " (if you want, you could supply GIT_DIR then run" >&2
89
+ echo " $0 <ref> <oldrev> <newrev>)" >&2
90
+ exit 1
91
+ fi
92
+
93
+ if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
94
+ echo "usage: $0 <ref> <oldrev> <newrev>" >&2
95
+ exit 1
96
+ fi
97
+
98
+ # --- Config
99
+ allowunannotated=$(git config --bool hooks.allowunannotated)
100
+ allowdeletebranch=$(git config --bool hooks.allowdeletebranch)
101
+ denycreatebranch=$(git config --bool hooks.denycreatebranch)
102
+ allowdeletetag=$(git config --bool hooks.allowdeletetag)
103
+ allowmodifytag=$(git config --bool hooks.allowmodifytag)
104
+
105
+ # check for no description
106
+ projectdesc=$(sed -e '1q' "$GIT_DIR/description")
107
+ case "$projectdesc" in
108
+ "Unnamed repository"* | "")
109
+ echo "*** Project description file hasn't been set" >&2
110
+ exit 1
111
+ ;;
112
+ esac
113
+
114
+ # --- Check types
115
+ # if $newrev is 0000...0000, it's a commit to delete a ref.
116
+ zero="0000000000000000000000000000000000000000"
117
+ if [ "$newrev" = "$zero" ]; then
118
+ newrev_type=delete
119
+ else
120
+ newrev_type=$(git cat-file -t $newrev)
121
+ fi
122
+
123
+ case "$refname","$newrev_type" in
124
+ refs/tags/*,commit)
125
+ # un-annotated tag
126
+ short_refname=${refname##refs/tags/}
127
+ if [ "$allowunannotated" != "true" ]; then
128
+ echo "*** The un-annotated tag, $short_refname, is not allowed in this repository" >&2
129
+ echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2
130
+ exit 1
131
+ fi
132
+ ;;
133
+ refs/tags/*,delete)
134
+ # delete tag
135
+ if [ "$allowdeletetag" != "true" ]; then
136
+ echo "*** Deleting a tag is not allowed in this repository" >&2
137
+ exit 1
138
+ fi
139
+ ;;
140
+ refs/tags/*,tag)
141
+ # annotated tag
142
+ if [ "$allowmodifytag" != "true" ] && git rev-parse $refname > /dev/null 2>&1
143
+ then
144
+ echo "*** Tag '$refname' already exists." >&2
145
+ echo "*** Modifying a tag is not allowed in this repository." >&2
146
+ exit 1
147
+ fi
148
+ ;;
149
+ refs/heads/*,commit)
150
+ # branch
151
+ if [ "$oldrev" = "$zero" -a "$denycreatebranch" = "true" ]; then
152
+ echo "*** Creating a branch is not allowed in this repository" >&2
153
+ exit 1
154
+ fi
155
+ ;;
156
+ refs/heads/*,delete)
157
+ # delete branch
158
+ if [ "$allowdeletebranch" != "true" ]; then
159
+ echo "*** Deleting a branch is not allowed in this repository" >&2
160
+ exit 1
161
+ fi
162
+ ;;
163
+ refs/remotes/*,commit)
164
+ # tracking branch
165
+ ;;
166
+ refs/remotes/*,delete)
167
+ # delete tracking branch
168
+ if [ "$allowdeletebranch" != "true" ]; then
169
+ echo "*** Deleting a tracking branch is not allowed in this repository" >&2
170
+ exit 1
171
+ fi
172
+ ;;
173
+ *)
174
+ # Anything else (is there anything else?)
175
+ echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2
176
+ exit 1
177
+ ;;
178
+ esac
179
+
180
+ # --- Finished
181
+ exit 0
182
+ ` ,
183
+ HookPostReceive : `#!/bin/sh
184
+ #
185
+ # An example hook script for the "post-receive" event.
186
+ #
187
+ # The "post-receive" script is run after receive-pack has accepted a pack
188
+ # and the repository has been updated. It is passed arguments in through
189
+ # stdin in the form
190
+ # <oldrev> <newrev> <refname>
191
+ # For example:
192
+ # aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
193
+
194
+ while read oldrev newrev refname
195
+ do
196
+ branch=$(git rev-parse --symbolic --abbrev-ref $refname)
197
+ if [ "master" = "$branch" ]; then
198
+ # Do something
199
+ fi
200
+ done` ,
201
+ }
202
+ )
26
203
27
204
// Hook contains information of a Git hook.
28
205
type Hook struct {
0 commit comments