问题
Does anyone know if there's a githook that will hook into the creation of a branch in order to reject the branch name before it's actually created?
I currently use commit-msg to validate the branch name upon commits, but i'm looking for a way to avoid having to rename a branch if it doesn't match our policy (regex).
回答1:
There is not (and to some extent it can't be controlled: branch names are transferred across the network during fetch and push, via refspecs, and refspecs are somewhat limited).
There is a git command, git check-ref-format
; its documentation describes what Git allows. (Branch names are simply any reference that starts with refs/heads/
.)
回答2:
Does anyone know if there's a githook that will hook into the creation of a branch in order to reject the branch name before it's actually created?
The only place you can mandate anything at all is in your own repositories. In your pre-receive exit,
while read old new ref; do
yourtesthere "$ref" || { rc=1; echo "$ref failed yourtesthere's validation"; }
done
exit $rc
and no one can push a bad refname to your repository. A command to check a name before passing it along to git is down in alias / oneliner territory.
You can distribute the yourtesthere
command, of course, and anyone that wants can use it to test branch names in their own repositories, but just as your repositories are yours, their repositories are theirs.
回答3:
Seems like you are referring to placing a policy for commits or branch creation. There is a git config command which helps in putting some template in place eg:
$ git config --global commit.template ~/.template.txt $ git commit
subject line
what happened
[ticket: X]
# Please enter the commit message .......
#
# modified: lib/test.rb
#
~
~
".git/COMMIT_EDITMSG"
Sorry if this did not help. Thanks.
来源:https://stackoverflow.com/questions/38165711/githook-to-validate-newly-created-branch-names