Skip to content

Instantly share code, notes, and snippets.

@joshnesbitt
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshnesbitt/f6de47766cf101b9e320 to your computer and use it in GitHub Desktop.
Save joshnesbitt/f6de47766cf101b9e320 to your computer and use it in GitHub Desktop.

Better Git Hooks

Intro

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.

Suggested Usage

A command line utility will be provided to manage and test hooks. The proposed API is as follows:

Installation

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

Writing a hook

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..."

Triggering a hook

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.

Uninstalling

To remove all hooks and integration simply run the following:

hooks remove all

Or to remove a single hook:

hooks remove pre-commit

Version Control

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.

Thoughts?

Worth implementing or pointless fluff around Git hooks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment