Merge update-ensure-tag-in-branch itno update-allow-tags-branches.
[git-central.git] / server / update-allow-tags-branches
1 #!/bin/sh
2 #
3 # An example hook script to blocks unannotated tags from entering.
4 # Called by git-receive-pack with arguments: refname sha1-old sha1-new
5 #
6 # Config
7 # ------
8 # hooks.update-allow-tags-branches.unannotatedtag
9 #   This boolean sets whether unannotated tags will be allowed into the
10 #   repository.  By default they won't be.
11 # hooks.update-allow-tags-branches.deletetag
12 #   This boolean sets whether deleting tags will be allowed in the
13 #   repository.  By default they won't be.
14 # hooks.update-allow-tags-branches.deletebranch
15 #   This boolean sets whether deleting branches will be allowed in the
16 #   repository.  By default they won't be.
17 # hooks.update-allow-tags-branches.nakedtag
18 #   This boolean sets whether tags are allowed into the repo to commits
19 #   that are not pointed to by a branch. By default they won't be.
20 #
21
22 . $(dirname $0)/functions
23
24 # --- Command line
25 refname="$1"
26 oldrev="$2"
27 newrev="$3"
28
29 # --- Config
30 allowunannotatedtag=$(git config --bool hooks.update-allow-tags-branches.unannotatedtag)
31 allowdeletebranch=$(git config --bool hooks.update-allow-tags-branches.deletebranch)
32 allowdeletetag=$(git config --bool hooks.update-allow-tags-branches.deletetag)
33 allownakedtag=$(git config --bool hooks.update-allow-tags-branches.nakedtag)
34
35 # --- Check types
36 # if $newrev is 0000...0000, it's a commit to delete a ref.
37 if [ "$newrev" = "0000000000000000000000000000000000000000" ]; then
38         newrev_type=delete
39 else
40         newrev_type=$(git-cat-file -t $newrev)
41 fi
42
43 case "$refname","$newrev_type" in
44         refs/tags/*,commit)
45                 # un-annotated tag
46                 short_refname=${refname##refs/tags/}
47                 if [ "$allowunannotatedtag" != "true" ]; then
48                         display_error_message "Unannotated tags ($short_refname) are not allowed"
49                         exit 1
50                 fi
51                 contains=$(git branch --contains "$newrev" | wc -l)
52                 if [ $contains -eq 0 -a "$allownakedtag" != "true" ] ; then
53                         display_error_message "The tag $short_refname is not included in any branch"
54                         exit 1
55                 fi
56                 ;;
57         refs/tags/*,delete)
58                 # delete tag
59                 if [ "$allowdeletetag" != "true" ]; then
60                         display_error_message "Deleting tags is not allowed"
61                         exit 1
62                 fi
63                 ;;
64         refs/tags/*,tag)
65                 # annotated tag
66                 short_refname=${refname##refs/tags/}
67                 contains=$(git branch --contains "$newrev" | wc -l)
68                 if [ $contains -eq 0 -a "$allownakedtag" != "true" ] ; then
69                         display_error_message "The tag $short_refname is not included in any branch"
70                         exit 1
71                 fi
72                 ;;
73         refs/heads/*,commit)
74                 # branch
75                 ;;
76         refs/heads/*,delete)
77                 # delete branch
78                 if [ "$allowdeletebranch" != "true" ]; then
79                         echo "*** Deleting a branch is not allowed in this repository" >&2
80                         exit 1
81                 fi
82                 ;;
83         refs/remotes/*,commit)
84                 # tracking branch
85                 ;;
86         refs/remotes/*,delete)
87                 # delete tracking branch
88                 if [ "$allowdeletebranch" != "true" ]; then
89                         echo "*** Deleting a tracking branch is not allowed in this repository" >&2
90                         exit 1
91                 fi
92                 ;;
93         *)
94                 # Anything else (is there anything else?)
95                 echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2
96                 exit 1
97                 ;;
98 esac
99
100 # --- Finished
101 exit 0