Home > Back-end >  Is there a way to prevent git push if the code convention not right?
Is there a way to prevent git push if the code convention not right?

Time:12-14

I've heard I can protect the branch and set some rules to allow users push their codes, but I cannot see what I wanted.

What I want is when I set some code convention, if the user did not follow it, user's code will not pushed (with some warning or alert which part of code he did not follow the convention).

The main language to use will be python, but more languages will be included in future.

CodePudding user response:

The answer is maybe. The server side of a connection can run a pre-receive hook to check the contents that are being pushed and either accept or reject it. However, GitHub only supports custom pre-receive hooks on GitHub Enterprise Server (which is the on-premises solution) and not github.com. Even in GHES, the timeout is limited to 5 seconds, which may not be enough to lint your code completely.

The typical way to handle this problem is to add a check in CI that fails if the style is unsuitable or other policy (e.g., lack of trailing whitespace) is not met. This will allow the push, but it will prevent a pull request from being merged if it fails.

Note that using the pre-push or pre-commit hooks on the client side is not a good idea because those can be bypassed trivially with no evidence left of it. Therefore, they are not an effective control and shouldn't be used for this.

  • Related