Skip to content

Commit aed97c6

Browse files
MadCodergitster
authored andcommitted
hook/update: example of how to prevent branch creation
Since git doesn't provide a receive.denyBranchCreation or similar, here is an example of how to be sure users cannot create branches remotely by pushing a new reference. This setup has been proven useful to prevent creation of spurious branches because of users having their remote.origin.push set to HEAD, when they use `git push` while being on a local topic branch of theirs instead of the proper one. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 55f0566 commit aed97c6

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

templates/hooks--update.sample

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
# hooks.allowdeletebranch
1717
# This boolean sets whether deleting branches will be allowed in the
1818
# repository. By default they won't be.
19+
# hooks.denycreatebranch
20+
# This boolean sets whether remotely creating branches will be denied
21+
# in the repository. By default this is allowed.
1922
#
2023

2124
# --- Command line
@@ -39,6 +42,7 @@ fi
3942
# --- Config
4043
allowunannotated=$(git config --bool hooks.allowunannotated)
4144
allowdeletebranch=$(git config --bool hooks.allowdeletebranch)
45+
denycreatebranch=$(git config --bool hooks.denycreatebranch)
4246
allowdeletetag=$(git config --bool hooks.allowdeletetag)
4347

4448
# check for no description
@@ -52,7 +56,8 @@ esac
5256

5357
# --- Check types
5458
# if $newrev is 0000...0000, it's a commit to delete a ref.
55-
if [ "$newrev" = "0000000000000000000000000000000000000000" ]; then
59+
zero="0000000000000000000000000000000000000000"
60+
if [ "$newrev" = "$zero" ]; then
5661
newrev_type=delete
5762
else
5863
newrev_type=$(git-cat-file -t $newrev)
@@ -80,6 +85,10 @@ case "$refname","$newrev_type" in
8085
;;
8186
refs/heads/*,commit)
8287
# branch
88+
if [ "$oldrev" = "$zero" -a "$denycreatebranch" = "true" ]; then
89+
echo "*** Creating a branch is not allowed in this repository" >&2
90+
exit 1
91+
fi
8392
;;
8493
refs/heads/*,delete)
8594
# delete branch

0 commit comments

Comments
 (0)