Ideally, hooks are written in Bash, if you follow the sample files. But you can write it in any language available, and just make sure it has the executable flag.
So, you can write a Python or Go code to achieve your goals, and place it under the hooks folder. It will work, but it will not be managed along with the repository.
Two Options
a) Multi Scripts
You can code your hooks inside your help, and add a small fragment of code to hooks, to call your perfect script, like this:
$ cat .git/hooks/pre-commit
#!/bin/bash
../../hooks/myprecommit.js
b) Single Script
A cooler option is to add just one script to rule them all, instead of several ones. So, you create a hooks/mysuperhook.go file and point every hook you want to have to it.
$ cat .git/hooks/pre-commit
#!/bin/bash
../../hooks/mysuperhook.go $(basename $0)
The parameter will provide your script which hook was triggered, and you can differentiate it inside your code. Why? Sometimes you might want to run the same check for commit and push, for instance.
And then?
Then, you might want to have further functionalities, like:
- Trigger the hook manually to check if everything is OK even prior to a commit or push. If you just call your script (option a or b) would do the trick.
- Trigger the hooks on CI, so you don't need to rewrite the same checks for CI. It would be just calling the commit and push triggers, for instance. The same as the above should solve it.
- Call external tools, like a markdown validator, or a YAML validator. You can make syscalls and need to handle standard output and standard error.
- Make sure all developers have a simple way to install the hooks, so a nice script needs to be added to the repository to replace default hooks with the correct ones
- Have some global helpers, like a check to block commits to develop and master branches, not having to add it to every repository. You can solve it by having another repository with global scripts.
Can this be simpler?
Yes, there are several tools to help you manage Git hooks. Each of them is tailored to tackle the problem from a different perspective, and you might need to understand all of them to get the one that is best for you or your team. GitHooks.com offers a lot of reading about hooking, and several tools available today.
As of today, there are 21 projects listed there with different strategies to manage Git hooks. Some only do it for a single hook, some for a specific language, and so on.
One of those tools, written by me and offered for free as an open-source project, is called hooks4git. It is written in Python (because I like it), but the idea is to handle all items listed above in a single configuration file called .hooks4git.ini, which lives inside your repository and can call any script you want to call, in any language.
Using Git hooks is absolutely fantastic, but the way they are offered usually only gets people away from it.
pre-commit
makes this easy for pre-commit hooks. Doesn't answer the OP's question about managing any arbitrary git hook, but pre-commit hooks are probably the most frequently used for code quality purposes.