forked from pre-commit/pre-commit.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mako
1886 lines (1457 loc) · 65.2 KB
/
index.mako
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## -*- coding: utf-8 -*-
<%!
from template_lib import md
%>
<%inherit file="base.mako" />
<div class="row">
<div class="col-sm-3 d-none d-lg-block">
<nav class="nav flex-column nav-pills sticky-top" aria-orientation="vertical" id="content-navigation">
<a class="nav-link active" href="#intro" role="tab">Introduction</a>
<a class="nav-link" href="#install" role="tab">Installation</a>
<a class="nav-link" href="#plugins" role="tab">Adding plugins</a>
<a class="nav-link" href="#usage" role="tab">Usage</a>
<a class="nav-link" href="#new-hooks" role="tab">Creating new hooks</a>
<a class="nav-link" href="#cli" role="tab">Command line interface</a>
<a class="nav-link" href="#advanced" role="tab">Advanced features</a>
<a class="nav-link" href="#contributing" role="tab">Contributing</a>
</nav>
</div>
<div class="col-lg-9 col-12">
<div id="intro">
<div class="page-header">${md('# Introduction')}</div>
${md('''
Git hook scripts are useful for identifying simple issues before submission to
code review. We run our hooks on every commit to automatically point out
issues in code such as missing semicolons, trailing whitespace, and debug
statements. By pointing these issues out before code review, this allows a
code reviewer to focus on the architecture of a change while not wasting time
with trivial style nitpicks.
As we created more libraries and projects we recognized that sharing our
pre-commit hooks across projects is painful. We copied and pasted unwieldy
bash scripts from project to project and had to manually change the hooks to
work for different project structures.
We believe that you should always use the best industry standard linters.
Some of the best linters are written in languages that you do not use in your
project or have installed on your machine. For example scss-lint is a linter
for SCSS written in Ruby. If you’re writing a project in node you should be
able to use scss-lint as a pre-commit hook without adding a Gemfile to your
project or understanding how to get scss-lint installed.
We built pre-commit to solve our hook issues. It is a multi-language package
manager for pre-commit hooks. You specify a list of hooks you want and
pre-commit manages the installation and execution of any hook written in any
language before every commit. pre-commit is specifically designed to not
require root access. If one of your developers doesn’t have node installed
but modifies a JavaScript file, pre-commit automatically handles downloading
and building node to run eslint without root.
''')}
</div>
<div id="install">
<div class="page-header">${md('# Installation')}</div>
${md('''
Before you can run hooks, you need to have the pre-commit package manager
installed.
Using pip:
```bash
pip install pre-commit
```
Non-administrative installation:
- _to upgrade: run again, to uninstall: pass `uninstall` to python_
- _does not work on platforms without symlink support (windows)_
```bash
curl https://pre-commit.com/install-local.py | python -
```
In a python project, add the following to your requirements.txt (or
requirements-dev.txt):
```
pre-commit
```
As a 0-dependency [zipapp]:
- locate and download the `.pyz` file from the [github releases]
- run `python pre-commit-#.#.#.pyz ...` in place of `pre-commit ...`
[zipapp]: https://docs.python.org/3/library/zipapp.html
[github releases]: https://github.com/pre-commit/pre-commit/releases
Using [homebrew](https://brew.sh):
```bash
brew install pre-commit
```
Using [conda](https://conda.io) (via [conda-forge](https://conda-forge.org)):
```bash
conda install -c conda-forge pre-commit
```
## Quick start
### 1. Install pre-commit
- follow the [install](#install) instructions above
- `pre-commit --version` should show you what version you're using
```cmd
pre-commit --version
```
### 2. Add a pre-commit configuration
- create a file named `.pre-commit-config.yaml`
- you can generate a very basic configuration using
[`pre-commit sample-config`](#pre-commit-sample-config)
- the full set of options for the configuration are listed [below](#plugins)
- this example uses a formatter for python code, however `pre-commit` works for
any programming language
- other [supported hooks](hooks.html) are available
```yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 19.3b0
hooks:
- id: black
```
### 3. Install the git hook scripts
- run `pre-commit install` to set up the git hook scripts
```console
$ pre-commit install
pre-commit installed at .git/hooks/pre-commit
```
- now `pre-commit` will run automatically on `git commit`!
### 4. (optional) Run against all the files
- it's usually a good idea to run the hooks against all of the files when adding
new hooks (usually `pre-commit` will only run on the changed files during
git hooks)
```pre-commit
$ pre-commit run --all-files
[INFO] Initializing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Initializing environment for https://github.com/psf/black.
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
[INFO] Installing environment for https://github.com/psf/black.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
Check Yaml...............................................................Passed
Fix End of Files.........................................................Passed
Trim Trailing Whitespace.................................................Failed
- hook id: trailing-whitespace
- exit code: 1
Files were modified by this hook. Additional output:
Fixing sample.py
black....................................................................Passed
```
- oops! looks like I had some trailing whitespace
- consider running that in [CI](#usage-in-continuous-integration) too
''')}
</div>
<div id="plugins">
<div class="page-header">${md('# Adding pre-commit plugins to your project')}</div>
${md('''
Once you have pre-commit installed, adding pre-commit plugins to your project
is done with the `.pre-commit-config.yaml` configuration file.
Add a file called `.pre-commit-config.yaml` to the root of your project. The
pre-commit config file describes what repositories and hooks are installed.
## .pre-commit-config.yaml - top level
_new in 1.0.0_: The default configuration file top-level was changed from a
list to a map. If you're using an old version of pre-commit, the top-level
list is the same as the value of [`repos`](#pre-commit-configyaml---repos).
If you'd like to migrate to the new configuration format, run
[`pre-commit migrate-config`](#pre-commit-migrate-config) to automatically
migrate your configuration.
```table
=r=
=c= [`repos`](_#top_level-repos)
=c= A list of [repository mappings](#pre-commit-configyaml---repos).
=r=
=c= [`default_language_version`](_#top_level-default_language_version)
=c= (optional: default `{}`) a mapping from language to the default
[`language_version`](#config-language_version) that should be used for that language. This will
only override individual hooks that do not set [`language_version`](#config-language_version).
For example to use `python3.7` for `language: python` hooks:
```yaml
default_language_version:
python: python3.7
```
_new in 1.14.0_
=r=
=c= [`default_stages`](_#top_level-default_stages)
=c= (optional: default (all stages)) a configuration-wide default for
the [`stages`](#config-stages) property of hooks. This will only override individual
hooks that do not set [`stages`](#config-stages).
For example:
```yaml
default_stages: [commit, push]
```
_new in 1.14.0_
=r=
=c= [`files`](_#top_level-files)
=c= (optional: default `''`) global file include pattern. _new in 1.21.0_.
=r=
=c= [`exclude`](_#top_level-exclude)
=c= (optional: default `^$`) global file exclude pattern. _new in 1.1.0_.
=r=
=c= [`fail_fast`](_#top_level-fail_fast)
=c= (optional: default `false`) set to `true` to have pre-commit stop
running hooks after the first failure. _new in 1.1.0_.
=r=
=c= [`minimum_pre_commit_version`](_#top_level-minimum_pre_commit_version)
=c= (optional: default `'0'`) require a minimum version of pre-commit.
_new in 1.15.0_.
```
A sample top-level:
```yaml
exclude: '^$'
fail_fast: false
repos:
- ...
```
## .pre-commit-config.yaml - repos
The repository mapping tells pre-commit where to get the code for the hook
from.
```table
=r=
=c= [`repo`](_#repos-repo)
=c= the repository url to `git clone` from
=r=
=c= [`rev`](_#repos-rev)
=c= the revision or tag to clone at. _new in 1.7.0_: previously `sha`
=r=
=c= [`hooks`](_#repos-hooks)
=c= A list of [hook mappings](#pre-commit-configyaml---hooks).
```
A sample repository:
```yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v1.2.3
hooks:
- ...
```
## .pre-commit-config.yaml - hooks
The hook mapping configures which hook from the repository is used and allows
for customization. All optional keys will receive their default from the
repository's configuration.
```table
=r=
=c= [`id`](_#config-id)
=c= which hook from the repository to use.
=r=
=c= [`alias`](_#config-alias)
=c= (optional) allows the hook to be referenced using an additional id when
using `pre-commit run <hookid>`.
_new in 1.14.0_.
=r=
=c= [`name`](_#config-name)
=c= (optional) override the name of the hook - shown during hook execution.
=r=
=c= [`language_version`](_#config-language_version)
=c= (optional) override the language version for the
hook. See [Overriding Language Version](#overriding-language-version).
=r=
=c= [`files`](_#config-files)
=c= (optional) override the default pattern for files to run on.
=r=
=c= [`exclude`](_#config-exclude)
=c= (optional) file exclude pattern.
=r=
=c= [`types`](_#config-types)
=c= (optional) override the default file types to run on (AND). See
[Filtering files with types](#filtering-files-with-types).
=r=
=c= [`types_or`](_#config-types_or)
=c= (optional) override the default file types to run on (OR). See
[Filtering files with types](#filtering-files-with-types).
_new in 2.9.0_.
=r=
=c= [`exclude_types`](_#config-exclude_types)
=c= (optional) file types to exclude.
=r=
=c= [`args`](_#config-args)
=c= (optional) list of additional parameters to pass to the hook.
=r=
=c= [`stages`](_#config-stages)
=c= (optional) confines the hook to the `commit`, `merge-commit`, `push`,
`prepare-commit-msg`, `commit-msg`, `post-checkout`, `post-commit`, or
`manual` stage. See
[Confining hooks to run at certain stages](#confining-hooks-to-run-at-certain-stages).
=r=
=c= [`additional_dependencies`](_#config-additional_dependencies)
=c= (optional) a list of dependencies that will be installed in the
environment where this hook gets run. One useful application is to
install plugins for hooks such as `eslint`.
=r=
=c= [`always_run`](_#config-always_run)
=c= (optional) if `true`, this hook will run even if there are no matching
files.
=r=
=c= [`verbose`](_#config-verbose)
=c= (optional) if `true`, forces the output of the hook to be printed even when
the hook passes. _new in 1.6.0_.
=r=
=c= [`log_file`](_#config-log_file)
=c= (optional) if present, the hook output will additionally be written
to a file.
```
One example of a complete configuration:
```yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v1.2.3
hooks:
- id: trailing-whitespace
```
This configuration says to download the pre-commit-hooks project and run its
trailing-whitespace hook.
## Updating hooks automatically
You can update your hooks to the latest version automatically by running
[`pre-commit autoupdate`](#pre-commit-autoupdate). By default, this will
bring the hooks to the latest tag on the default branch.
''')}
</div>
<div id="usage">
<div class="page-header">${md('# Usage')}</div>
${md('''
Run `pre-commit install` to install pre-commit into your git hooks. pre-commit
will now run on every commit. Every time you clone a project using pre-commit
running `pre-commit install` should always be the first thing you do.
If you want to manually run all pre-commit hooks on a repository, run
`pre-commit run --all-files`. To run individual hooks use
`pre-commit run <hook_id>`.
The first time pre-commit runs on a file it will automatically download,
install, and run the hook. Note that running a hook for the first time may be
slow. For example: If the machine does not have node installed, pre-commit
will download and build a copy of node.
```pre-commit
$ pre-commit install
pre-commit installed at /home/asottile/workspace/pytest/.git/hooks/pre-commit
$ git commit -m "Add super awesome feature"
black....................................................................Passed
blacken-docs.........................................(no files to check)Skipped
Trim Trailing Whitespace.................................................Passed
Fix End of Files.........................................................Passed
Check Yaml...........................................(no files to check)Skipped
Debug Statements (Python)................................................Passed
Flake8...................................................................Passed
Reorder python imports...................................................Passed
pyupgrade................................................................Passed
rst ``code`` is two backticks........................(no files to check)Skipped
rst..................................................(no files to check)Skipped
changelog filenames..................................(no files to check)Skipped
[master 146c6c2c] Add super awesome feature
1 file changed, 1 insertion(+)
```
''')}
</div>
<div id="new-hooks">
<div class="page-header">${md('# Creating new hooks')}</div>
${md('''
pre-commit currently supports hooks written in
[many languages](#supported-languages). As long as your git repo is an
installable package (gem, npm, pypi, etc.) or exposes an executable, it can be
used with pre-commit. Each git repo can support as many languages/hooks as you
want.
The hook must exit nonzero on failure or modify files.
A git repo containing pre-commit plugins must contain a .pre-commit-hooks.yaml
file that tells pre-commit:
```table
=r=
=c= [`id`](_#hooks-id)
=c= the id of the hook - used in pre-commit-config.yaml.
=r=
=c= [`name`](_#hooks-name)
=c= the name of the hook - shown during hook execution.
=r=
=c= [`entry`](_#hooks-entry)
=c= the entry point - the executable to run. `entry` can also contain
arguments that will not be overridden such as `entry: autopep8 -i`.
=r=
=c= [`language`](_#hooks-language)
=c= the language of the hook - tells pre-commit how to install the hook.
=r=
=c= [`files`](_#hooks-files)
=c= (optional: default `''`) the pattern of files to run on.
=r=
=c= [`exclude`](_#hooks-exclude)
=c= (optional: default `^$`) exclude files that were matched by [`files`](#hooks-files).
=r=
=c= [`types`](_#hooks-types)
=c= (optional: default `[file]`) list of file types to run on (AND). See
[Filtering files with types](#filtering-files-with-types).
=r=
=c= [`types_or`](_#hooks-types_or)
=c= (optional: default `[]`) list of file types to run on (OR). See
[Filtering files with types](#filtering-files-with-types).
_new in 2.9.0_.
=r=
=c= [`exclude_types`](_#hooks-exclude_types)
=c= (optional: default `[]`) exclude files that were matched by [`types`](#hooks-types).
=r=
=c= [`always_run`](_#hooks-always_run)
=c= (optional: default `false`) if `true` this hook will run even if there
are no matching files.
=r=
=c= [`verbose`](_#hooks-verbose)
=c= (optional) if `true`, forces the output of the hook to be printed even when
the hook passes. _new in 1.6.0_.
=r=
=c= [`pass_filenames`](_#hooks-pass_filenames)
=c= (optional: default `true`) if `false` no filenames will be passed to
the hook.
=r=
=c= [`require_serial`](_#hooks-require_serial)
=c= (optional: default `false`) if `true` this hook will execute using a
single process instead of in parallel. _new in 1.13.0_.
=r=
=c= [`description`](_#hooks-description)
=c= (optional: default `''`) description of the hook. used for metadata
purposes only.
=r=
=c= [`language_version`](_#hooks-language_version)
=c= (optional: default `default`) see
[Overriding language version](#overriding-language-version).
=r=
=c= [`minimum_pre_commit_version`](_#hooks-minimum_pre_commit_version)
=c= (optional: default `'0'`) allows one to indicate a minimum
compatible pre-commit version.
=r=
=c= [`args`](_#hooks-args)
=c= (optional: default `[]`) list of additional parameters to pass to the hook.
=r=
=c= [`stages`](_#hooks-stages)
=c= (optional: default (all stages)) confines the hook to the `commit`, `merge-commit`,
`push`, `prepare-commit-msg`, `commit-msg`, `post-checkout`, `post-commit`, or
`manual` stage. See
[Confining hooks to run at certain stages](#confining-hooks-to-run-at-certain-stages).
```
For example:
```yaml
- id: trailing-whitespace
name: Trim Trailing Whitespace
description: This hook trims trailing whitespace.
entry: trailing-whitespace-fixer
language: python
types: [text]
```
## Developing hooks interactively
Since the [`repo`](#repos-repo) property of `.pre-commit-config.yaml` can refer to anything
that `git clone ...` understands, it's often useful to point it at a local
directory while developing hooks.
[`pre-commit try-repo`](#pre-commit-try-repo) streamlines this process by
enabling a quick way to try out a repository. Here's how one might work
interactively:
_note_: you may need to provide `--commit-msg-filename` when using this
command with hook types `prepare-commit-msg` and `commit-msg`.
_new in 1.14.0_: a commit is no longer necessary to `try-repo` on a local
directory. `pre-commit` will clone any tracked uncommitted changes.
```pre-commit
~/work/hook-repo $ git checkout origin/master -b feature
# ... make some changes
# new in 1.14.0: a commit is no longer necessary for `try-repo`
# In another terminal or tab
~/work/other-repo $ pre-commit try-repo ../hook-repo foo --verbose --all-files
===============================================================================
Using config:
===============================================================================
repos:
- repo: ../hook-repo
rev: 84f01ac09fcd8610824f9626a590b83cfae9bcbd
hooks:
- id: foo
===============================================================================
[INFO] Initializing environment for ../hook-repo.
Foo......................................................................Passed
- hook id: foo
- duration: 0.02s
Hello from foo hook!
```
## Supported languages
- [conda](#conda)
- [coursier](#coursier)
- [docker](#docker)
- [docker_image](#docker_image)
- [dotnet](#dotnet)
- [fail](#fail)
- [golang](#golang)
- [node](#node)
- [perl](#perl)
- [python](#python)
- [python_venv](#python_venv)
- [ruby](#ruby)
- [rust](#rust)
- [swift](#swift)
- [pygrep](#pygrep)
- [script](#script)
- [system](#system)
### conda
_new in 1.21.0_
The hook repository must contain an `environment.yml` file which will be used
via `conda env create --file environment.yml ...` to create the environment.
The `conda` language also supports [`additional_dependencies`](#config-additional_dependencies)
and will pass any of the values directly into `conda install`. This language can therefore be
used with [local](#repository-local-hooks) hooks.
__Support:__ `conda` hooks work as long as there is a system-installed `conda`
binary (such as [`miniconda`](https://docs.conda.io/en/latest/miniconda.html)).
It has been tested on linux, macOS, and windows.
### coursier
_new in 2.8.0_
The hook repository must have a `.pre-commit-channel` folder and that folder must contain
the coursier
[application descriptors](https://get-coursier.io/docs/2.0.0-RC6-10/cli-install.html#application-descriptor-reference)
for the hook to install. For configuring coursier hooks, your
[`entry`](#hooks-entry) should correspond to an executable installed from the
repository's `.pre-commit-channel` folder.
__Support:__ `coursier` hooks are known to work on any system which has the `cs`
package manager installed. The specific coursier applications you install may depend
on various versions of the JVM, consult the hooks' documentation for clarification.
It has been tested on linux.
### docker
The hook repository must have a `Dockerfile`. It will be installed via
`docker build .`.
Running Docker hooks requires a running Docker engine on your host. For
configuring Docker hooks, your [`entry`](#hooks-entry) should correspond to an executable
inside the Docker container, and will be used to override the default container
entrypoint. Your Docker `CMD` will not run when pre-commit passes a file list
as arguments to the run container command. Docker allows you to use any
language that's not supported by pre-commit as a builtin.
pre-commit will automatically mount the repository source as a volume using
`-v $PWD:/src:rw,Z` and set the working directory using `--workdir /src`.
__Support:__ docker hooks are known to work on any system which has a working
`docker` executable. It has been tested on linux and macOS. Hooks that are
run via `boot2docker` are known to be unable to make modifications to files.
See [this repository](https://github.com/pre-commit/pre-commit-docker-flake8)
for an example Docker-based hook.
### docker_image
A more lightweight approach to `docker` hooks. The `docker_image`
"language" uses existing docker images to provide hook executables.
`docker_image` hooks can be conveniently configured as [local](#repository-local-hooks)
hooks.
The [`entry`](#hooks-entry) specifies the docker tag to use. If an image has an
`ENTRYPOINT` defined, nothing special is needed to hook up the executable.
If the container does not specify an `ENTRYPOINT` or you want to change the
entrypoint you can specify it as well in your [`entry`](#hooks-entry).
For example:
```yaml
- id: dockerfile-provides-entrypoint
name: ...
language: docker_image
entry: my.registry.example.com/docker-image-1:latest
- id: dockerfile-no-entrypoint-1
name: ...
language: docker_image
entry: --entrypoint my-exe my.registry.example.com/docker-image-2:latest
# Alternative equivalent solution
- id: dockerfile-no-entrypoint-2
name: ...
language: docker_image
entry: my.registry.example.com/docker-image-3:latest my-exe
```
### dotnet
_new in 2.8.0_
dotnet hooks are installed using the system installation of the dotnet CLI.
Hook repositories must contain a dotnet CLI tool which can be `pack`ed and
`install`ed as per [this](https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools-how-to-create)
example. The `entry` should match an executable created by building the
repository. Additional dependencies are not currently supported.
__Support:__ dotnet hooks are known to work on any system which has the dotnet
CLI installed. It has been tested on linux and windows.
### fail
_new in 1.11.0_
A lightweight [`language`](#hooks-language) to forbid files by filename. The `fail` language is
especially useful for [local](#repository-local-hooks) hooks.
The [`entry`](#hooks-entry) will be printed when the hook fails. It is suggested to provide
a brief description for [`name`](#hooks-name) and more verbose fix instructions in [`entry`](#hooks-entry).
Here's an example which prevents any file except those ending with `.rst` from
being added to the `changelog` directory:
```yaml
- repo: local
hooks:
- id: changelogs-rst
name: changelogs must be rst
entry: changelog filenames must end in .rst
language: fail
files: 'changelog/.*(?<!\.rst)$'
```
### golang
The hook repository must contain go source code. It will be installed via
`go get ./...`. pre-commit will create an isolated `GOPATH` for each hook and
the [`entry`](#hooks-entry) should match an executable which will get installed into the
`GOPATH`'s `bin` directory.
__Support:__ golang hooks are known to work on any system which has go
installed. It has been tested on linux, macOS, and windows.
### node
The hook repository must have a `package.json`. It will be installed via
`npm install .`. The installed package will provide an executable that will
match the [`entry`](#hooks-entry) – usually through `bin` in package.json.
__Support:__ node hooks work without any system-level dependencies. It has
been tested on linux and macOS and _may_ work under cygwin.
_new in 1.5.0_: windows is now supported for node hooks. Currently python3
only due to [a bug in cpython](https://bugs.python.org/issue32539).
### perl
_new in 2.1.0_
Perl hooks are installed using the system installation of
[cpan](https://perldoc.perl.org/5.30.0/cpan.html), the CPAN package installer
that comes with Perl.
Hook repositories must have something that `cpan` supports, typically
`Makefile.PL` or `Build.PL`, which it uses to install an executable to
use in the [`entry`](#hooks-entry) definition for your hook. The repository will be installed
via `cpan -T .` (with the installed files stored in your pre-commit cache,
not polluting other Perl installations).
When specifying [`additional_dependencies`](#config-additional_dependencies) for Perl, you can use any of the
[install argument formats understood by `cpan`](https://perldoc.perl.org/5.30.0/CPAN.html#get%2c-make%2c-test%2c-install%2c-clean-modules-or-distributions).
__Support:__ Perl hooks currently require a pre-existing Perl installation,
including the `cpan` tool in `PATH`. It has been tested on linux, macOS, and
Windows.
### python
The hook repository must be installable via `pip install .` (usually by either
`setup.py` or `pyproject.toml`). The installed package will provide an
executable that will match the [`entry`](#hooks-entry) – usually through `console_scripts` or
`scripts` in setup.py.
__Support:__ python hooks work without any system-level dependencies. It
has been tested on linux, macOS, windows, and cygwin.
### python_venv
_new in 1.9.0_
_new in 2.4.0_: The `python_venv` language is now an alias to `python` since
`virtualenv>=20` creates equivalently structured environments. Previously,
this [`language`](#hooks-language) created environments using the [venv] module.
This [`language`](#hooks-language) will be removed eventually so it is suggested to use `python`
instead.
[venv]: https://docs.python.org/3/library/venv.html
__Support:__ python hooks work without any system-level dependencies. It
has been tested on linux, macOS, windows, and cygwin.
### ruby
The hook repository must have a `*.gemspec`. It will be installed via
`gem build *.gemspec && gem install *.gem`. The installed package will
produce an executable that will match the [`entry`](#hooks-entry) – usually through
`executables` in your gemspec.
__Support:__ ruby hooks work without any system-level dependencies. It has
been tested on linux and macOS and _may_ work under cygwin.
### rust
_new in 1.10.0_
Rust hooks are installed using the system installation of
[Cargo](https://github.com/rust-lang/cargo), Rust's official package manager.
Hook repositories must have a `Cargo.toml` file which produces at least one
binary ([example](https://github.com/chriskuehl/example-rust-pre-commit-hook)),
whose name should match the [`entry`](#hooks-entry) definition for your hook. The repo will be
installed via `cargo install --bins` (with the binaries stored in your
pre-commit cache, not polluting your user-level Cargo installations).
When specifying [`additional_dependencies`](#config-additional_dependencies) for Rust, you can use the syntax
`{package_name}:{package_version}` to specify a new library dependency (used to
build _your_ hook repo), or the special syntax
`cli:{package_name}:{package_version}` for a CLI dependency (built separately,
with binaries made available for use by hooks).
__Support:__ Rust hooks currently require a pre-existing Rust installation. It
has been tested on linux, Windows, and macOS.
### swift
The hook repository must have a `Package.swift`. It will be installed via
`swift build -c release`. The [`entry`](#hooks-entry) should match an executable created by
building the repository.
__Support:__ swift hooks are known to work on any system which has swift
installed. It has been tested on linux and macOS.
### pygrep
_new in 1.2.0_
A cross-platform python implementation of `grep` – pygrep hooks are a quick
way to write a simple hook which prevents commits by file matching. Specify
the regex as the [`entry`](#hooks-entry). The [`entry`](#hooks-entry) may be any python
[regular expression](#regular-expressions). For case insensitive regexes you
can apply the `(?i)` flag as the start of your entry, or use `args: [-i]`.
_new in 1.8.0_: For multiline matches, use `args: [--multiline]`.
_new in 2.8.0_: To require all files to match, use `args: [--negate]`.
__Support:__ pygrep hooks are supported on all platforms which pre-commit runs
on.
### script
Script hooks provide a way to write simple scripts which validate files. The
[`entry`](#hooks-entry) should be a path relative to the root of the hook repository.
This hook type will not be given a virtual environment to work with – if it
needs additional dependencies the consumer must install them manually.
__Support:__ the support of script hooks depend on the scripts themselves.
### system
System hooks provide a way to write hooks for system-level executables which
don't have a supported language above (or have special environment
requirements that don't allow them to run in isolation such as pylint).
This hook type will not be given a virtual environment to work with – if it
needs additional dependencies the consumer must install them manually.
__Support:__ the support of system hooks depend on the executables.
''')}
</div>
<div id="cli">
<div class="page-header">${md('# Command line interface')}</div>
${md('''
All pre-commit commands take the following options:
- `--color {auto,always,never}`: whether to use color in output.
Defaults to `auto`. _new in 1.18.0_: can be overridden by using
`PRE_COMMIT_COLOR={auto,always,never}` or disabled using `TERM=dumb`.
- `-c CONFIG`, `--config CONFIG`: path to alternate config file
- `-h`, `--help`: show help and available options.
_new in 2.8.0_: `pre-commit` now exits with more specific codes:
- `1`: a detected / expected error
- `3`: an unexpected error
- `130`: the process was interrupted by `^C`
## pre-commit autoupdate [options] #pre-commit-autoupdate
Auto-update pre-commit config to the latest repos' versions.
Options:
- `--bleeding-edge`: update to the bleeding edge of the default branch instead
of the latest tagged version (the default behaviour).
- `--freeze`: _new in 1.21.0_: Store "frozen" hashes in [`rev`](#repos-rev)
instead of tag names.
- `--repo REPO`: _new in 1.4.1_: Only update this repository. _new in 1.7.0_:
This option may be specified multiple times.
Here are some sample invocations using this `.pre-commit-config.yaml`:
```yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.1.0
hooks:
- id: trailing-whitespace
- repo: https://github.com/asottile/pyupgrade
rev: v1.25.0
hooks:
- id: pyupgrade
args: [--py36-plus]
```
```console
$ : default: update to latest tag on default branch
$ pre-commit autoupdate # by default: pick tags
Updating https://github.com/pre-commit/pre-commit-hooks ... updating v2.1.0 -> v2.4.0.
Updating https://github.com/asottile/pyupgrade ... updating v1.25.0 -> v1.25.2.
$ grep rev: .pre-commit-config.yaml
rev: v2.4.0
rev: v1.25.2
```
```console
$ : update a specific repository to the latest revision of the default branch
$ pre-commit autoupdate --bleeding-edge --repo https://github.com/pre-commit/pre-commit-hooks
Updating https://github.com/pre-commit/pre-commit-hooks ... updating v2.1.0 -> 5df1a4bf6f04a1ed3a643167b38d502575e29aef.
$ grep rev: .pre-commit-config.yaml
rev: 5df1a4bf6f04a1ed3a643167b38d502575e29aef
rev: v1.25.0
```
```console
$ : update to frozen versions
$ pre-commit autoupdate --freeze
Updating https://github.com/pre-commit/pre-commit-hooks ... updating v2.1.0 -> v2.4.0 (frozen).
Updating https://github.com/asottile/pyupgrade ... updating v1.25.0 -> v1.25.2 (frozen).
$ grep rev: .pre-commit-config.yaml
rev: 0161422b4e09b47536ea13f49e786eb3616fe0d7 # frozen: v2.4.0
rev: 34a269fd7650d264e4de7603157c10d0a9bb8211 # frozen: v1.25.2
```
## pre-commit clean [options] #pre-commit-clean
Clean out cached pre-commit files.
Options: (no additional options)
## pre-commit gc [options] #pre-commit-gc
_new in 1.14.0_
Clean unused cached repos.
`pre-commit` keeps a cache of installed hook repositories which grows over
time. This command can be run periodically to clean out unused repos from
the cache directory.
Options: (no additional options)
## pre-commit init-templatedir DIRECTORY [options] #pre-commit-init-templatedir
_new in 1.18.0_
Install hook script in a directory intended for use with
`git config init.templateDir`.
Options:
- `-t {pre-commit,pre-merge-commit,pre-push,prepare-commit-msg,commit-msg,post-checkout,post-commit}`,
`--hook-type {pre-commit,pre-merge-commit,pre-push,prepare-commit-msg,commit-msg,post-checkout,post-commit}`:
which hook type to install.
Some example useful invocations:
```bash
git config --global init.templateDir ~/.git-template
pre-commit init-templatedir ~/.git-template
```
For Windows cmd.exe use `%HOMEPATH%` instead of `~`:
```batch
pre-commit init-templatedir %HOMEPATH%\.git-template
```
For Windows PowerShell use `$HOME` instead of `~`:
```powershell
pre-commit init-templatedir $HOME\.git-template
```
Now whenever a repository is cloned or created, it will have the hooks set up
already!
## pre-commit install [options] #pre-commit-install
Install the pre-commit script.
Options:
- `-f`, `--overwrite`: Replace any existing git hooks with the pre-commit
script.
- `--install-hooks`: Also install environments for all available hooks now
(rather than when they are first executed). See [`pre-commit
install-hooks`](#pre-commit-install-hooks).
- `-t {pre-commit,pre-merge-commit,pre-push,prepare-commit-msg,commit-msg,post-checkout,post-commit}`,
`--hook-type {pre-commit,pre-merge-commit,pre-push,prepare-commit-msg,commit-msg,post-checkout,post-commit}`:
Specify which hook type to install.
- `--allow-missing-config`: Hook scripts will permit a missing configuration
file.
Some example useful invocations:
- `pre-commit install`: Default invocation. Installs the pre-commit script
alongside any existing git hooks.
- `pre-commit install --install-hooks --overwrite`: Idempotently replaces
existing git hook scripts with pre-commit, and also installs hook
environments.
## pre-commit install-hooks [options] #pre-commit-install-hooks
Install all missing environments for the available hooks. Unless this command or
`install --install-hooks` is executed, each hook's environment is created the
first time the hook is called.
Each hook is initialized in a separate environment appropriate to the language
the hook is written in. See [supported languages](#supported-languages).