Git hooks
Use git hooks to automatically run oneRepo tasks and other commands before, during, and after git commands using git hooks.
Git hooks can be a divisive topic. Because of that, managing the hooks through oneRepo is an opt-in behavior with multiple options.
Enabling hooks
Section titled Enabling hooksAutomatically
Section titled AutomaticallySet vcs.autoSyncHooks: true
in your root config. This will ensure git is configured to use your hooks after any successful command run.
Manually
Section titled ManuallyIf you and your team do not want to always require git hooks to be configured in dev environments, any individual developer can run the commands to initialize and create hooks at will:
Then you may create hooks either by using the oneRepo recommended hooks or add & edit them manually:
All hooks will be created within the vcs.hooksPath
config (./.hooks
by default) unless otherwise modified. Once created, make sure the to commit the hook files to git so they can be shared.
Disabling hooks
Section titled Disabling hooksBypassing hooks
Section titled Bypassing hooksYou can bypass pre-commit and commit-msg hooks using Git --no-verify
/-n
option:
For Git commands that donβt have a --no-verify
option, you can set the ONEREPO_USE_HOOKS
environment variable to "0"
and hooks will exit before executing any configured tasks:
Preventing hooks sync
Section titled Preventing hooks syncFor one-off uses or as an individual setting not tracked in your config, set an environment variable ONEREPO_SYNC_HOOKS
to 0
. This will prevent the background task that automatically updates git with knowledge of the hooks:
Uninstalling
Section titled UninstallingTo uninstall the git hooks, unset the local git config value. Ensure you also disable auto-sync by setting vcs.autoSyncHooks
to false
.
Commands
Section titled Commandsone hooks
Section titled one hooksManage git repository hooks
one hooks create
Section titled one hooks createCreate git hooks
Option | Type | Description |
---|---|---|
--add | boolean , default: true | Add the hooks to the git stage for committing. |
--hook | "applypatch-msg" , "pre-applypatch" , "post-applypatch" , "pre-commit" , "pre-merge-commit" , "prepare-commit-msg" , "commit-msg" , "post-commit" , "pre-rebase" , "post-checkout" , "post-merge" , "pre-receive" , "update" , "post-receive" , "post-update" , "pre-auto-gc" , "post-rewrite" , "pre-push" , "proc-receive" , "push-to-checkout" , default: ["pre-commit","post-checkout","post-merge","post-rewrite"] | The git hook to create. Omit this option to auto-create recommended hooks. |
--overwrite | boolean | Overwrite existing hooks |
Advanced options
Option | Type | Description | Required |
---|---|---|---|
--hooks-path | string | Tracked path to use for git hooks. This option is defaulted via the oneRepo root config. | β |
one hooks init
Section titled one hooks initAliases: one hooks sync
Initialize and sync git hook settings for this repository.
Advanced options
Option | Type | Description | Required |
---|---|---|---|
--hooks-path | string | Tracked path to use for git hooks. | β |
Acknowledgements
Section titled AcknowledgementsMany of the ideas and code patterns for this module were inspired by Husky, which has been a long time standard utility for JavaScript based repos.