That would be a pre-commit hook, like the one coming with evey git installation: templates/hooks--pre-commit.sample
.
A more complex example would be the project git-pylint-commit-hook for instance, for hecking Python code quality. The hook will check files ending with .py or that has a she bang (#!) containing python.
The pre-commit
hook is run first, before you even type in a commit message.
It’s used to inspect the snapshot that’s about to be committed, to see if you’ve forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.
Exiting non-zero from this hook aborts the commit, although you can bypass it with git commit --no-verify
.
You can do things like check for code style (run lint or something equivalent), check for trailing whitespace (the default hook does exactly that), or check for appropriate documentation on new methods.
See more examples at "Customizing Git - An Example Git-Enforced Policy", in ruby:
files_modified = `git diff-index --cached --name-only HEAD`.split("\n")
files_modified.each do |path|
# do your checks
end
end