githook to validate newly created branch names

若如初见. 提交于 2019-12-31 05:06:33

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!