How to prohibit commits if they do not contain some Issue(s)

Tristan :

Good morning, everyone, For a project in Java on github, I have to use github functions like "issues". For this purpose all "commits" must contain at least one exit. To prevent my team from making dead-end commits I must therefore check before each commits. GitHooks is the perfect tool for this work. Only, to implement a Windows compatible script.

On a linux environment, I know how to interrupt the commit but I don't know how to get the content of the commit that will be able to check the condition.

bk2204 :

In general, Git hooks are not a good tool for this case because the user can bypass them with git commit --no-verify (along with a variety of other techniques). Anything on the user's system is not an effective control, since the user can modify or delete it.

Typically, this behavior is done on the server side. In some cases, you can run a pre-receive hook to check these things, but this isn't possible on GitHub. Instead, you should check this in your CI system in addition to other checks you do.

You can write a script to do that in shell like so, where the two arguments are the main branch (usually master) and the commit HEAD for the pull request:

#!/bin/sh -e

git rev-list "$1".."$2" | xargs -L1 sh -c 'git log -1 --format=%b "$@" | grep -E "#[0-9]+"'

That will check for the pattern #[0-9]+ in the commit message and exit nonzero if it doesn't match. Such a script will work on Windows as well if you invoke it with bash or sh, since all versions of Git on Windows come with some shell implementation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=305328&siteId=1