@matheusbsbs / matheus-bagliano / commits / aed97c677c
Fork 0

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>

Pierre Habouzit committed Apr 16, 2009 at 22:00 UTC aed97c677cdfd1b9bbc9b33b5418ad4a29109082
1 file changed +10 -1
templates/hooks--update.sample
+10 -1
@@ -16,6 +16,9 @@
16 # hooks.allowdeletebranch
17 # This boolean sets whether deleting branches will be allowed in the
18 # 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.
22 #
23
24 # --- Command line
@@ -39,6 +42,7 @@ fi
42 # --- Config
43 allowunannotated=$(git config --bool hooks.allowunannotated)
44 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
@@ -52,7 +56,8 @@ esac
56
57 # --- Check types
58 # 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
61 newrev_type=delete
62 else
63 newrev_type=$(git-cat-file -t $newrev)
@@ -80,6 +85,10 @@ case "$refname","$newrev_type" in
85 ;;
86 refs/heads/*,commit)
87 # 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
92 ;;
93 refs/heads/*,delete)
94 # delete branch