Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
Martin Amdisen | 9537f21 | 2016-02-25 09:10:12 +0100 | [diff] [blame] | 3 | # An example hook script to block unannotated tags from entering. |
Ben Walton | 100e762 | 2010-03-20 10:48:09 -0400 | [diff] [blame] | 4 | # Called by "git receive-pack" with arguments: refname sha1-old sha1-new |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 5 | # |
Junio C Hamano | f98f8cb | 2008-06-24 18:45:21 -0700 | [diff] [blame] | 6 | # To enable this hook, rename this file to "update". |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 7 | # |
Andy Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 8 | # Config |
| 9 | # ------ |
Andy Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 10 | # hooks.allowunannotated |
| 11 | # This boolean sets whether unannotated tags will be allowed into the |
| 12 | # repository. By default they won't be. |
Gerrit Pape | ad7638b | 2007-11-08 09:47:39 +0000 | [diff] [blame] | 13 | # hooks.allowdeletetag |
| 14 | # This boolean sets whether deleting tags will be allowed in the |
| 15 | # repository. By default they won't be. |
Heiko Voigt | 7742c65 | 2009-05-08 17:22:30 +0200 | [diff] [blame] | 16 | # hooks.allowmodifytag |
| 17 | # This boolean sets whether a tag may be modified after creation. By default |
| 18 | # it won't be. |
Gerrit Pape | ad7638b | 2007-11-08 09:47:39 +0000 | [diff] [blame] | 19 | # hooks.allowdeletebranch |
| 20 | # This boolean sets whether deleting branches will be allowed in the |
| 21 | # repository. By default they won't be. |
Pierre Habouzit | aed97c6 | 2009-04-16 22:00:44 +0200 | [diff] [blame] | 22 | # hooks.denycreatebranch |
| 23 | # This boolean sets whether remotely creating branches will be denied |
| 24 | # in the repository. By default this is allowed. |
Andy Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 25 | # |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 26 | |
Andy Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 27 | # --- Command line |
| 28 | refname="$1" |
| 29 | oldrev="$2" |
| 30 | newrev="$3" |
Andreas Ericsson | 8a3ee7c | 2006-01-04 16:17:59 +0000 | [diff] [blame] | 31 | |
Andy Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 32 | # --- Safety check |
| 33 | if [ -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 |
| 38 | fi |
Andreas Ericsson | 8a3ee7c | 2006-01-04 16:17:59 +0000 | [diff] [blame] | 39 | |
Andy Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 40 | if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then |
David Aguilar | 9a8a84c | 2013-02-23 16:50:23 -0800 | [diff] [blame] | 41 | echo "usage: $0 <ref> <oldrev> <newrev>" >&2 |
Andy Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 42 | exit 1 |
| 43 | fi |
Andreas Ericsson | 8a3ee7c | 2006-01-04 16:17:59 +0000 | [diff] [blame] | 44 | |
Andy Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 45 | # --- Config |
Lucius Hu | 81e3db4 | 2020-01-19 22:53:32 +0000 | [diff] [blame] | 46 | allowunannotated=$(git config --type=bool hooks.allowunannotated) |
| 47 | allowdeletebranch=$(git config --type=bool hooks.allowdeletebranch) |
| 48 | denycreatebranch=$(git config --type=bool hooks.denycreatebranch) |
| 49 | allowdeletetag=$(git config --type=bool hooks.allowdeletetag) |
| 50 | allowmodifytag=$(git config --type=bool hooks.allowmodifytag) |
Andreas Ericsson | 8a3ee7c | 2006-01-04 16:17:59 +0000 | [diff] [blame] | 51 | |
Andy Parkins | 0a0d080 | 2007-03-20 10:58:32 +0000 | [diff] [blame] | 52 | # check for no description |
Gerrit Pape | 1756fed | 2007-11-08 14:02:00 +0000 | [diff] [blame] | 53 | projectdesc=$(sed -e '1q' "$GIT_DIR/description") |
John Tapsell | 28001d0 | 2009-02-19 07:36:00 +0000 | [diff] [blame] | 54 | case "$projectdesc" in |
| 55 | "Unnamed repository"* | "") |
Andy Parkins | 0a0d080 | 2007-03-20 10:58:32 +0000 | [diff] [blame] | 56 | echo "*** Project description file hasn't been set" >&2 |
| 57 | exit 1 |
John Tapsell | 28001d0 | 2009-02-19 07:36:00 +0000 | [diff] [blame] | 58 | ;; |
| 59 | esac |
Andy Parkins | 0a0d080 | 2007-03-20 10:58:32 +0000 | [diff] [blame] | 60 | |
Andy Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 61 | # --- Check types |
Gerrit Pape | ad7638b | 2007-11-08 09:47:39 +0000 | [diff] [blame] | 62 | # if $newrev is 0000...0000, it's a commit to delete a ref. |
Denton Liu | d8d3d63 | 2020-09-23 02:38:45 -0700 | [diff] [blame] | 63 | zero=$(git hash-object --stdin </dev/null | tr '[0-9a-f]' '0') |
Pierre Habouzit | aed97c6 | 2009-04-16 22:00:44 +0200 | [diff] [blame] | 64 | if [ "$newrev" = "$zero" ]; then |
Gerrit Pape | ad7638b | 2007-11-08 09:47:39 +0000 | [diff] [blame] | 65 | newrev_type=delete |
Gerrit Pape | 9177649 | 2007-04-16 08:31:35 +0000 | [diff] [blame] | 66 | else |
Ben Walton | 100e762 | 2010-03-20 10:48:09 -0400 | [diff] [blame] | 67 | newrev_type=$(git cat-file -t $newrev) |
Gerrit Pape | 9177649 | 2007-04-16 08:31:35 +0000 | [diff] [blame] | 68 | fi |
Andy Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 69 | |
| 70 | case "$refname","$newrev_type" in |
| 71 | refs/tags/*,commit) |
Pavel Roskin | 3dff537 | 2007-02-03 23:49:16 -0500 | [diff] [blame] | 72 | # un-annotated tag |
Andy Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 73 | short_refname=${refname##refs/tags/} |
Gerrit Pape | b8ac23b | 2007-03-02 12:20:10 +0000 | [diff] [blame] | 74 | if [ "$allowunannotated" != "true" ]; then |
Andy Parkins | 46d409d | 2007-03-23 10:21:59 +0000 | [diff] [blame] | 75 | echo "*** The un-annotated tag, $short_refname, is not allowed in this repository" >&2 |
Andy Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 76 | echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2 |
| 77 | exit 1 |
Andreas Ericsson | 8a3ee7c | 2006-01-04 16:17:59 +0000 | [diff] [blame] | 78 | fi |
| 79 | ;; |
Gerrit Pape | ad7638b | 2007-11-08 09:47:39 +0000 | [diff] [blame] | 80 | 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 Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 87 | refs/tags/*,tag) |
| 88 | # annotated tag |
Heiko Voigt | 7742c65 | 2009-05-08 17:22:30 +0200 | [diff] [blame] | 89 | 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 Ericsson | 8a3ee7c | 2006-01-04 16:17:59 +0000 | [diff] [blame] | 95 | ;; |
Andy Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 96 | refs/heads/*,commit) |
| 97 | # branch |
Pierre Habouzit | aed97c6 | 2009-04-16 22:00:44 +0200 | [diff] [blame] | 98 | 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 Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 102 | ;; |
Gerrit Pape | ad7638b | 2007-11-08 09:47:39 +0000 | [diff] [blame] | 103 | 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 Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 110 | refs/remotes/*,commit) |
| 111 | # tracking branch |
Junio C Hamano | 86b13da | 2005-09-02 10:53:15 -0700 | [diff] [blame] | 112 | ;; |
Gerrit Pape | ad7638b | 2007-11-08 09:47:39 +0000 | [diff] [blame] | 113 | 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 Hamano | 86b13da | 2005-09-02 10:53:15 -0700 | [diff] [blame] | 120 | *) |
Andy Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 121 | # Anything else (is there anything else?) |
Andy Parkins | 46d409d | 2007-03-23 10:21:59 +0000 | [diff] [blame] | 122 | echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2 |
Andy Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 123 | exit 1 |
Junio C Hamano | 86b13da | 2005-09-02 10:53:15 -0700 | [diff] [blame] | 124 | ;; |
Andy Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 125 | esac |
| 126 | |
Andy Parkins | 829a686 | 2007-01-26 09:01:04 +0000 | [diff] [blame] | 127 | # --- Finished |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 128 | exit 0 |