Git hooks are not as widely used as they could be, and I believe is because people new to Git don't want to be messing around with Bash scripts or haven't yet discovered how to use them.
I'd like to propose a new tool for creating and managing Git hooks, and versioning them with the project they reside in. This can benefit everyone on a given project as they can implement the same workflow which Git hooks allow. If a hook is related only to a single developer, simply do not check it in.
A command line utility will be provided to manage and test hooks. The proposed API is as follows:
hooks install all
This will write the setup hooks which will trigger the main hooks.
Basically writes each hook (e.g. .git/hooks/pre-commit
) to execute a file residing in the project root (e.g. config/hooks/pre_commit.rb
). The language of the hook can be defined during installation:
hooks install all --language=ruby
If you only require a single hook, you can request only that hook be created:
hooks install pre-commit
Following an installation, simply edit the hook file with the code you want to run in it:
# config/hooks/pre_commit.rb
puts "[hook] Running some Ruby code..."
A hook will be triggered like any other Git hook. Given the hook we just wrote, if we were to commit, we'd see a [hook] Running some Ruby code...
message output during the commit process. As with any Git hook, the exit code of the script is taken into account when deciding whether to abort the current action or not.
To remove all hooks and integration simply run the following:
hooks remove all
Or to remove a single hook:
hooks remove pre-commit
As with any contents of a Git repository, hooks can be versioned and shared between contributors. This can be especially useful if many developers rely on similar hooks to validate the contents of a commit before committing, such as ensure there are no console.log
statements etc.
Worth implementing or pointless fluff around Git hooks?