update.sample 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/bin/sh
  2. #
  3. # An example hook script to block unannotated tags from entering.
  4. # Called by "git receive-pack" with arguments: refname sha1-old sha1-new
  5. #
  6. # To enable this hook, rename this file to "update".
  7. #
  8. # Config
  9. # ------
  10. # hooks.allowunannotated
  11. # This boolean sets whether unannotated tags will be allowed into the
  12. # repository. By default they won't be.
  13. # hooks.allowdeletetag
  14. # This boolean sets whether deleting tags will be allowed in the
  15. # repository. By default they won't be.
  16. # hooks.allowmodifytag
  17. # This boolean sets whether a tag may be modified after creation. By default
  18. # it won't be.
  19. # hooks.allowdeletebranch
  20. # This boolean sets whether deleting branches will be allowed in the
  21. # repository. By default they won't be.
  22. # hooks.denycreatebranch
  23. # This boolean sets whether remotely creating branches will be denied
  24. # in the repository. By default this is allowed.
  25. #
  26. # --- Command line
  27. refname="$1"
  28. oldrev="$2"
  29. newrev="$3"
  30. # --- Safety check
  31. if [ -z "$GIT_DIR" ]; then
  32. echo "Don't run this script from the command line." >&2
  33. echo " (if you want, you could supply GIT_DIR then run" >&2
  34. echo " $0 <ref> <oldrev> <newrev>)" >&2
  35. exit 1
  36. fi
  37. if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
  38. echo "usage: $0 <ref> <oldrev> <newrev>" >&2
  39. exit 1
  40. fi
  41. # --- Config
  42. allowunannotated=$(git config --bool hooks.allowunannotated)
  43. allowdeletebranch=$(git config --bool hooks.allowdeletebranch)
  44. denycreatebranch=$(git config --bool hooks.denycreatebranch)
  45. allowdeletetag=$(git config --bool hooks.allowdeletetag)
  46. allowmodifytag=$(git config --bool hooks.allowmodifytag)
  47. # check for no description
  48. projectdesc=$(sed -e '1q' "$GIT_DIR/description")
  49. case "$projectdesc" in
  50. "Unnamed repository"* | "")
  51. echo "*** Project description file hasn't been set" >&2
  52. exit 1
  53. ;;
  54. esac
  55. # --- Check types
  56. # if $newrev is 0000...0000, it's a commit to delete a ref.
  57. zero="0000000000000000000000000000000000000000"
  58. if [ "$newrev" = "$zero" ]; then
  59. newrev_type=delete
  60. else
  61. newrev_type=$(git cat-file -t $newrev)
  62. fi
  63. case "$refname","$newrev_type" in
  64. refs/tags/*,commit)
  65. # un-annotated tag
  66. short_refname=${refname##refs/tags/}
  67. if [ "$allowunannotated" != "true" ]; then
  68. echo "*** The un-annotated tag, $short_refname, is not allowed in this repository" >&2
  69. echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2
  70. exit 1
  71. fi
  72. ;;
  73. refs/tags/*,delete)
  74. # delete tag
  75. if [ "$allowdeletetag" != "true" ]; then
  76. echo "*** Deleting a tag is not allowed in this repository" >&2
  77. exit 1
  78. fi
  79. ;;
  80. refs/tags/*,tag)
  81. # annotated tag
  82. if [ "$allowmodifytag" != "true" ] && git rev-parse $refname > /dev/null 2>&1
  83. then
  84. echo "*** Tag '$refname' already exists." >&2
  85. echo "*** Modifying a tag is not allowed in this repository." >&2
  86. exit 1
  87. fi
  88. ;;
  89. refs/heads/*,commit)
  90. # branch
  91. if [ "$oldrev" = "$zero" -a "$denycreatebranch" = "true" ]; then
  92. echo "*** Creating a branch is not allowed in this repository" >&2
  93. exit 1
  94. fi
  95. ;;
  96. refs/heads/*,delete)
  97. # delete branch
  98. if [ "$allowdeletebranch" != "true" ]; then
  99. echo "*** Deleting a branch is not allowed in this repository" >&2
  100. exit 1
  101. fi
  102. ;;
  103. refs/remotes/*,commit)
  104. # tracking branch
  105. ;;
  106. refs/remotes/*,delete)
  107. # delete tracking branch
  108. if [ "$allowdeletebranch" != "true" ]; then
  109. echo "*** Deleting a tracking branch is not allowed in this repository" >&2
  110. exit 1
  111. fi
  112. ;;
  113. *)
  114. # Anything else (is there anything else?)
  115. echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2
  116. exit 1
  117. ;;
  118. esac
  119. # --- Finished
  120. exit 0