Debian Codegraph @

docs/integrations/source_version_control.md

raw
1 # Version control integration
2
3 Use [pre-commit](https://pre-commit.com/). Once you
4 [have it installed](https://pre-commit.com/#install), add this to the
5 `.pre-commit-config.yaml` in your repository:
6
7 ```yaml
8 repos:
9 # Using this mirror lets us use mypyc-compiled black, which is about 2x faster
10 - repo: https://github.com/psf/black-pre-commit-mirror
11 rev: 26.5.1
12 hooks:
13 - id: black
14 # It is recommended to specify the latest version of Python
15 # supported by your project here, or alternatively use
16 # pre-commit's default_language_version, see
17 # https://pre-commit.com/#top_level-default_language_version
18 language_version: python3.11
19 ```
20
21 Feel free to switch out the `rev` value to a different version of Black.
22
23 Note if you'd like to use a specific commit in `rev`, you'll need to swap the repo
24 specified from the mirror to https://github.com/psf/black. We discourage the use of
25 branches or other mutable refs since the hook [won't auto update as you may
26 expect][pre-commit-mutable-rev].
27
28 ## Jupyter Notebooks
29
30 There is an alternate hook `black-jupyter` that expands the targets of `black` to
31 include Jupyter Notebooks. To use this hook, simply replace the hook's `id: black` with
32 `id: black-jupyter` in the `.pre-commit-config.yaml`:
33
34 ```yaml
35 repos:
36 - repo: https://github.com/psf/black-pre-commit-mirror
37 rev: 26.5.1
38 hooks:
39 - id: black-jupyter
40 language_version: python3.11
41 ```
42
43 ```{note}
44 The `black-jupyter` hook became available in version 21.8b0.
45 ```
46
47 See the [Jupyter Notebooks guide](../guides/using_black_with_jupyter_notebooks.md) for
48 more details.
49
50 ## Excluding files with pre-commit
51
52 When using pre-commit, there's an important distinction in how file exclusions work.
53 Pre-commit passes files directly to Black via the command line, rather than letting
54 Black discover files recursively. This means Black's `--exclude` option won't work as
55 expected because it only applies to files discovered during recursive directory
56 traversal.
57
58 To exclude files when using pre-commit, you have two options:
59
60 ### Option 1: Use pre-commit's exclude (Recommended)
61
62 Configure exclusions directly in `.pre-commit-config.yaml`:
63
64 ```yaml
65 repos:
66 - repo: https://github.com/psf/black-pre-commit-mirror
67 rev: 26.5.1
68 hooks:
69 - id: black
70 exclude: ^migrations/|^generated/
71 ```
72
73 This is the recommended approach because pre-commit filters files before passing them to
74 Black, avoiding unnecessary processing.
75
76 ### Option 2: Use Black's force-exclude in pyproject.toml
77
78 Black's `force-exclude` configuration option excludes files even when they are passed
79 explicitly as command-line arguments (which is how pre-commit invokes Black). Simply add
80 the pattern to your `pyproject.toml`:
81
82 ```toml
83 [tool.black]
84 force-exclude = '''
85 (
86 ^migrations/
87 | ^generated/
88 )
89 '''
90 ```
91
92 Black automatically reads this configuration—no additional CLI arguments are needed in
93 your `.pre-commit-config.yaml`.
94
95 ```{note}
96 The `force-exclude` option was added in version 20.8b0 specifically to support
97 workflows where files are passed directly via CLI, such as pre-commit hooks and
98 editor plugins.
99 ```
100
101 [pre-commit-mutable-rev]:
102 https://pre-commit.com/#using-the-latest-version-for-a-repository