Skip to content

Commit 756f7ab

Browse files
committed
document support for git 2.54+ hooks
1 parent eed4a9c commit 756f7ab

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

sections/advanced.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,79 @@ repos:
445445
args: []
446446
```
447447
448+
## usage with git 2.54+ hook configuration
449+
450+
_new in 4.6.0_: pre-commit improved support for `git config`-based hooks.
451+
a later version will change `pre-commit install` to use this approach.
452+
453+
[git 2.54] introduced a new way to install git hook tools via `git config`.
454+
455+
the basic gist is the following enables a hook in a git repo:
456+
457+
```bash
458+
git config set hook.<name>.event pre-push
459+
git config set hook.<name>.command 'some command here'
460+
```
461+
462+
an example setup with `pre-commit` might look like:
463+
464+
```bash
465+
# note, the "hook" name here is `pre-commit.pre-commit`
466+
# for the `pre-commit` "tool" and the `pre-commit` "event"
467+
git config set hook.pre-commit.pre-commit.event pre-commit
468+
git config set hook.pre-commit.pre-commit.command 'pre-commit hook-impl --hook-type pre-commit --'
469+
470+
# please follow that naming scheme for future compatibility with `pre-commit install`
471+
472+
# an example with pre-push:
473+
#
474+
# git config set hook.pre-commit.pre-push.event pre-push
475+
# git config set hook.pre-commit.pre-push.command 'pre-commit hook-impl --hook-type pre-push --'
476+
```
477+
478+
`pre-commit hook-impl` is a "hidden" implementation command with these options:
479+
- `--hook-type ...`: the [hook type](#supported-git-hooks) to use
480+
- `--skip-on-missing-config`: silently pass when a config is missing
481+
482+
some interesting applications of this:
483+
484+
### "global" installation of pre-commit
485+
486+
with `git config set --global ...` this can automatically enable pre-commit
487+
for all repositories:
488+
489+
```bash
490+
git config set --global hook.pre-commit.pre-commit.event pre-commit
491+
git config set --global hook.pre-commit.pre-commit.command 'pre-commit hook-impl --hook-type pre-commit --skip-on-missing-config --'
492+
```
493+
494+
- this setup **not recommended** as it can lead to accidentally running hooks
495+
when interacting with an untrusted repository.
496+
- `--skip-on-missing-config` is recommended here as arbitrary git repositories
497+
may not have a `.pre-commit-config.yaml`.
498+
499+
### always running a hook on all files
500+
501+
since you can configure pre-commit as many times as you want you *could* invoke
502+
pre-commit to run a particular hook always and on all files
503+
504+
```bash
505+
git config set hook.pre-commit.pre-commit-always.event pre-commit
506+
git config set hook.pre-commit.pre-commit-always.command 'pre-commit run hookid --hook-stage pre-commit --all-files'
507+
```
508+
509+
*note*: this is not recommended as it has the tendancy to be slow and deviates
510+
from the normal expectations of pre-commit.
511+
512+
[git 2.54]: https://github.blog/open-source/git/highlights-from-git-2-54/#h-config-based-hooks
513+
448514
## automatically enabling pre-commit on repositories
449515

516+
*note*: if you are on a new-enough version of `git` you may want to use
517+
[this approach](#global-installation-of-pre-commit) instead.
518+
519+
___
520+
450521
`pre-commit init-templatedir` can be used to set up a skeleton for `git`'s
451522
`init.templateDir` option. This means that any newly cloned repository will
452523
automatically have the hooks set up without the need to run

template_lib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
ID_RE = re.compile(r' #([a-z0-9-]+)$')
15-
SPECIAL_CHARS_RE = re.compile('[^a-z0-9 _-]')
15+
SPECIAL_CHARS_RE = re.compile('(&[a-z]+;|[^a-z0-9 _-])')
1616

1717

1818
ROW = '=r='

0 commit comments

Comments
 (0)