Smee is a Composer package designed to make it easier to share Git hooks with a project.
Git hooks are a useful way to automatically perform actions at different points throughout the Git lifecycle. These actions could include verifying coding standards, running unit tests, enforcing commit message formats, and more.
The downside to Git hooks is that they're not distributed with the Git repository, meaning it's difficult to ensure that all developers on a project are using the same hooks.
Smee aims to change that, by introducing the .githooks
directory and providing an easy way to automatically install the hooks on composer install
.
Smee should be added to your projects via Composer:
$ composer require --dev stevegrunwell/smee
To ensure Smee is automatically run for other users, add Smee to the "post-install-cmd" Composer event:
{
"scripts": {
"post-install-cmd": [
"smee install"
]
}
}
Finally, create a .githooks
directory in the root of your project, and add the hooks you'd like to use in your project. These files should be committed to the git repository, and will automatically be copied into other developers' local repositories the next time they run composer install
.
Git supports a number of hooks, which each fire at different points throughout the lifecycle. Each Git hook exists in a separate file, named after the hook it corresponds to (e.g. .git/hooks/pre-commit
will be executed on the "pre-commit" hook).
These are some of the most common Git hooks, firing at different points as a developer attempts to commit code:
- pre-commit
- Runs before a commit message is created.
- prepare-commit-msg
- Run before the commit message editor is opened but after the default message is created.
- commit-msg
- Runs before saving the commit message, often used for verifying commit message format.
- post-commit
- Runs after a commit has been made. This hook is typically used for notifications.
Git also supports an email-based workflow via git am
, which applies patches in sequence from a mailbox. If you're using this workflow, a few additional hooks are available.
- pre-rebase
- Runs before allowing any code to be rebased.
- post-rewrite
- Runs after rewriting a commit, e.g.
git commit --amend
. - post-checkout
- Runs after a successful
git checkout
command. - post-merge
- Runs after a successful
git merge
command. - pre-push
- Runs before a
git push
command. - pre-auto-gc
- Runs before Git's periodic garbage collection processes.