blob: c4d426bc6ee9430ee7813263ce6d5da7ec78c3c6 [file] [log] [blame]
Junio C Hamano8d5afef2005-08-02 16:45:21 -07001#!/bin/sh
2#
Martin Amdisen9537f212016-02-25 09:10:12 +01003# An example hook script to block unannotated tags from entering.
Ben Walton100e7622010-03-20 10:48:09 -04004# Called by "git receive-pack" with arguments: refname sha1-old sha1-new
Junio C Hamano8d5afef2005-08-02 16:45:21 -07005#
Junio C Hamanof98f8cb2008-06-24 18:45:21 -07006# To enable this hook, rename this file to "update".
Junio C Hamano8d5afef2005-08-02 16:45:21 -07007#
Andy Parkins829a6862007-01-26 09:01:04 +00008# Config
9# ------
Andy Parkins829a6862007-01-26 09:01:04 +000010# hooks.allowunannotated
11# This boolean sets whether unannotated tags will be allowed into the
12# repository. By default they won't be.
Gerrit Papead7638b2007-11-08 09:47:39 +000013# hooks.allowdeletetag
14# This boolean sets whether deleting tags will be allowed in the
15# repository. By default they won't be.
Heiko Voigt7742c652009-05-08 17:22:30 +020016# hooks.allowmodifytag
17# This boolean sets whether a tag may be modified after creation. By default
18# it won't be.
Gerrit Papead7638b2007-11-08 09:47:39 +000019# hooks.allowdeletebranch
20# This boolean sets whether deleting branches will be allowed in the
21# repository. By default they won't be.
Pierre Habouzitaed97c62009-04-16 22:00:44 +020022# hooks.denycreatebranch
23# This boolean sets whether remotely creating branches will be denied
24# in the repository. By default this is allowed.
Andy Parkins829a6862007-01-26 09:01:04 +000025#
Junio C Hamano8d5afef2005-08-02 16:45:21 -070026
Andy Parkins829a6862007-01-26 09:01:04 +000027# --- Command line
28refname="$1"
29oldrev="$2"
30newrev="$3"
Andreas Ericsson8a3ee7c2006-01-04 16:17:59 +000031
Andy Parkins829a6862007-01-26 09:01:04 +000032# --- Safety check
33if [ -z "$GIT_DIR" ]; then
34 echo "Don't run this script from the command line." >&2
35 echo " (if you want, you could supply GIT_DIR then run" >&2
36 echo " $0 <ref> <oldrev> <newrev>)" >&2
37 exit 1
38fi
Andreas Ericsson8a3ee7c2006-01-04 16:17:59 +000039
Andy Parkins829a6862007-01-26 09:01:04 +000040if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
David Aguilar9a8a84c2013-02-23 16:50:23 -080041 echo "usage: $0 <ref> <oldrev> <newrev>" >&2
Andy Parkins829a6862007-01-26 09:01:04 +000042 exit 1
43fi
Andreas Ericsson8a3ee7c2006-01-04 16:17:59 +000044
Andy Parkins829a6862007-01-26 09:01:04 +000045# --- Config
Lucius Hu81e3db42020-01-19 22:53:32 +000046allowunannotated=$(git config --type=bool hooks.allowunannotated)
47allowdeletebranch=$(git config --type=bool hooks.allowdeletebranch)
48denycreatebranch=$(git config --type=bool hooks.denycreatebranch)
49allowdeletetag=$(git config --type=bool hooks.allowdeletetag)
50allowmodifytag=$(git config --type=bool hooks.allowmodifytag)
Andreas Ericsson8a3ee7c2006-01-04 16:17:59 +000051
Andy Parkins0a0d0802007-03-20 10:58:32 +000052# check for no description
Gerrit Pape1756fed2007-11-08 14:02:00 +000053projectdesc=$(sed -e '1q' "$GIT_DIR/description")
John Tapsell28001d02009-02-19 07:36:00 +000054case "$projectdesc" in
55"Unnamed repository"* | "")
Andy Parkins0a0d0802007-03-20 10:58:32 +000056 echo "*** Project description file hasn't been set" >&2
57 exit 1
John Tapsell28001d02009-02-19 07:36:00 +000058 ;;
59esac
Andy Parkins0a0d0802007-03-20 10:58:32 +000060
Andy Parkins829a6862007-01-26 09:01:04 +000061# --- Check types
Gerrit Papead7638b2007-11-08 09:47:39 +000062# if $newrev is 0000...0000, it's a commit to delete a ref.
Denton Liud8d3d632020-09-23 02:38:45 -070063zero=$(git hash-object --stdin </dev/null | tr '[0-9a-f]' '0')
Pierre Habouzitaed97c62009-04-16 22:00:44 +020064if [ "$newrev" = "$zero" ]; then
Gerrit Papead7638b2007-11-08 09:47:39 +000065 newrev_type=delete
Gerrit Pape91776492007-04-16 08:31:35 +000066else
Ben Walton100e7622010-03-20 10:48:09 -040067 newrev_type=$(git cat-file -t $newrev)
Gerrit Pape91776492007-04-16 08:31:35 +000068fi
Andy Parkins829a6862007-01-26 09:01:04 +000069
70case "$refname","$newrev_type" in
71 refs/tags/*,commit)
Pavel Roskin3dff5372007-02-03 23:49:16 -050072 # un-annotated tag
Andy Parkins829a6862007-01-26 09:01:04 +000073 short_refname=${refname##refs/tags/}
Gerrit Papeb8ac23b2007-03-02 12:20:10 +000074 if [ "$allowunannotated" != "true" ]; then
Andy Parkins46d409d2007-03-23 10:21:59 +000075 echo "*** The un-annotated tag, $short_refname, is not allowed in this repository" >&2
Andy Parkins829a6862007-01-26 09:01:04 +000076 echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2
77 exit 1
Andreas Ericsson8a3ee7c2006-01-04 16:17:59 +000078 fi
79 ;;
Gerrit Papead7638b2007-11-08 09:47:39 +000080 refs/tags/*,delete)
81 # delete tag
82 if [ "$allowdeletetag" != "true" ]; then
83 echo "*** Deleting a tag is not allowed in this repository" >&2
84 exit 1
85 fi
86 ;;
Andy Parkins829a6862007-01-26 09:01:04 +000087 refs/tags/*,tag)
88 # annotated tag
Heiko Voigt7742c652009-05-08 17:22:30 +020089 if [ "$allowmodifytag" != "true" ] && git rev-parse $refname > /dev/null 2>&1
90 then
91 echo "*** Tag '$refname' already exists." >&2
92 echo "*** Modifying a tag is not allowed in this repository." >&2
93 exit 1
94 fi
Andreas Ericsson8a3ee7c2006-01-04 16:17:59 +000095 ;;
Andy Parkins829a6862007-01-26 09:01:04 +000096 refs/heads/*,commit)
97 # branch
Pierre Habouzitaed97c62009-04-16 22:00:44 +020098 if [ "$oldrev" = "$zero" -a "$denycreatebranch" = "true" ]; then
99 echo "*** Creating a branch is not allowed in this repository" >&2
100 exit 1
101 fi
Andy Parkins829a6862007-01-26 09:01:04 +0000102 ;;
Gerrit Papead7638b2007-11-08 09:47:39 +0000103 refs/heads/*,delete)
104 # delete branch
105 if [ "$allowdeletebranch" != "true" ]; then
106 echo "*** Deleting a branch is not allowed in this repository" >&2
107 exit 1
108 fi
109 ;;
Andy Parkins829a6862007-01-26 09:01:04 +0000110 refs/remotes/*,commit)
111 # tracking branch
Junio C Hamano86b13da2005-09-02 10:53:15 -0700112 ;;
Gerrit Papead7638b2007-11-08 09:47:39 +0000113 refs/remotes/*,delete)
114 # delete tracking branch
115 if [ "$allowdeletebranch" != "true" ]; then
116 echo "*** Deleting a tracking branch is not allowed in this repository" >&2
117 exit 1
118 fi
119 ;;
Junio C Hamano86b13da2005-09-02 10:53:15 -0700120 *)
Andy Parkins829a6862007-01-26 09:01:04 +0000121 # Anything else (is there anything else?)
Andy Parkins46d409d2007-03-23 10:21:59 +0000122 echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2
Andy Parkins829a6862007-01-26 09:01:04 +0000123 exit 1
Junio C Hamano86b13da2005-09-02 10:53:15 -0700124 ;;
Andy Parkins829a6862007-01-26 09:01:04 +0000125esac
126
Andy Parkins829a6862007-01-26 09:01:04 +0000127# --- Finished
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700128exit 0