Skip to content

Commit c223a8e

Browse files
committed
Move pre-commit to more general section
1 parent 348d2bf commit c223a8e

File tree

6 files changed

+99
-48
lines changed

6 files changed

+99
-48
lines changed

doc/user_guide/features/formatting_code/index.rst

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,6 @@ deterministic manner.
4040
| | | need to be re-formatted |
4141
+--------------------+------------------+------------------------------------+
4242

43-
.. _pre_commit:
44-
45-
pre-commit
46-
++++++++++++
47-
48-
`Pre-commit <https://pre-commit.com/>`__ is a framework that automatically runs
49-
configurable checks and fixes on code before Git commits and pushes.
50-
Included in the cookiecutter project template is a ``.pre-commit-config.yaml``, which
51-
uses the formatting :ref:`formatting_sessions`. For information on configuring and
52-
setting it up see :ref:`pre-commit_configuration`.
53-
54-
Once it's ready to use, the process is:
55-
56-
#. Make your code changes
57-
#. ``git add -u`` and ``git commit -m "<message>"``
58-
#. pre-commit performs checks on the changed files and produces an output like
59-
60-
.. code-block:: bash
61-
62-
code-format..........................................(no files to check)Skipped
63-
check yaml...........................................(no files to check)Skipped
64-
fix end of files.........................................................Passed
65-
trim trailing whitespace.................................................Passed
66-
67-
* If all steps pass, then no action is needed.
68-
* If a step fails, then check the output further. If it was an automatic fix, then
69-
just add the altered file to your commit and execute your ``git commit`` line again.
70-
Otherwise, you will need to take on manual intervention.
71-
7243
.. _formatting_configuration:
7344

7445
Configuration
@@ -98,23 +69,6 @@ Ensure ``isort`` is configured with compatibility for ``black``:
9869
For further configuration options, see
9970
`isort options <https://pycqa.github.io/isort/docs/configuration/options.html>`__.
10071

101-
.. _pre-commit_configuration:
102-
103-
pre-commit
104-
^^^^^^^^^^
105-
#. Add a :code:`.pre-commit-config.yaml` file to your project root
106-
107-
Feel free to get some inspiration from the Python toolbox itself:
108-
109-
.. literalinclude:: ../../../../project-template/{{cookiecutter.repo_name}}/.pre-commit-config.yaml
110-
:language: yaml
111-
112-
#. Enable pre-commit hooks for your workspace
113-
114-
.. code-block:: shell
115-
116-
poetry run -- pre-commit install --hook-type pre-commit --hook-type pre-push
117-
11872

11973
pyupgrade
12074
^^^^^^^^^

doc/user_guide/features/formatting_code/troubleshooting.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _formatting_troubleshooting:
2+
13
Troubleshooting
24
===============
35

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
.. _github_hooks:
2+
3+
Enabling GitHub Hooks
4+
=====================
5+
6+
.. toctree::
7+
:maxdepth: 2
8+
9+
troubleshooting
10+
11+
GitHub hooks are automated scripts that run at specific points during Git's execution,
12+
allowing developers to enforce quality standards, automate tasks, and customize Git's
13+
behavior. The PTB uses the `pre-commit <https://pre-commit.com/>`__ framework to
14+
define common GitHub hooks for Git commits and pushes. The configuration for
15+
``pre-commit`` is provided in a ``.pre-commit-config.yaml`` file and described in
16+
:ref:`pre-commit_configuration`.
17+
18+
.. _pre-commit_configuration:
19+
20+
Configuration
21+
+++++++++++++
22+
23+
#. Add a ``.pre-commit-config.yaml`` file to your project's root directory. Feel free to
24+
take inspiration from the example ``.pre-commit-config.yaml``:
25+
26+
.. collapse:: .pre-commit-config.yaml
27+
28+
.. literalinclude:: ../../../../project-template/{{cookiecutter.repo_name}}/.pre-commit-config.yaml
29+
:language: yaml
30+
31+
32+
#. Enable pre-commit hooks for your workspace:
33+
34+
.. code-block:: shell
35+
36+
poetry run -- pre-commit install --hook-type pre-commit --hook-type pre-push
37+
38+
39+
Working with ``pre-commit``
40+
+++++++++++++++++++++++++++
41+
42+
.. _committing:
43+
44+
Committing
45+
----------
46+
47+
Once ``pre-commit`` has been configured, the process for performing a ``git commit`` is:
48+
49+
#. Make your code changes
50+
#. ``git add`` changed files and ``git commit -m "<message>"``
51+
#. ``pre-commit`` performs checks on the changed files and produces an output like
52+
53+
.. code-block:: bash
54+
55+
code-format..........................................(no files to check)Skipped
56+
check yaml...........................................(no files to check)Skipped
57+
fix end of files.........................................................Passed
58+
trim trailing whitespace.................................................Passed
59+
60+
* If all steps pass, then no action is needed.
61+
* If a step fails, then check the output further. If it was an automatic fix, then
62+
just add the altered file to your commit and execute your ``git commit`` line again.
63+
Otherwise, manual intervention is needed.
64+
65+
Pushing
66+
-------
67+
68+
Once ``pre-commit`` has been configured, the process for performing a ``git push`` is:
69+
70+
#. Perform one or more iterations of :ref:`committing`.
71+
#. ``git push``
72+
#. ``pre-commit`` performs checks on the changed files and produces an output like
73+
74+
.. code-block:: bash
75+
76+
type-check...............................................................Passed
77+
lint.....................................................................Passed
78+
79+
* If all steps pass, then no action is needed.
80+
* If a step fails, then check the output further. The suggested ``pre-push`` actions
81+
given in the :ref:`pre-commit_configuration` require manual intervention. Create
82+
a new commit to resolve the issue & try ``git push`` again.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Troubleshooting
2+
===============
3+
4+
The checks for ``git commit`` or ``git push`` keep failing
5+
----------------------------------------------------------
6+
7+
The ``pre-commit`` hooks, as defined in the ``.pre-commit-config.yaml``
8+
use multiple nox sessions. If a step fails multiple times, despite a user adding fixes
9+
or manually resolving the error, then please check which nox session is failing
10+
and refer to its troubleshooting documentation for help:
11+
12+
* :ref:`Formatting Troubleshooting <formatting_troubleshooting>`

doc/user_guide/features/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Features
99
metrics/collecting_metrics
1010
creating_a_release
1111
documentation/index
12+
github_hooks/index
1213
formatting_code/index
1314
managing_dependencies
1415

doc/user_guide/getting_started.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ straightforward, and you just can use the example ``noxfile.py`` below.
131131

132132
For additional information on resolving this issue, please :ref:`refer to <faq_no_module_noxconfig>`.
133133

134-
5. Set up the pre-commit hooks [optional]
135-
+++++++++++++++++++++++++++++++++++++++++
134+
5. Set up the GitHub ``pre-commit`` hooks [optional]
135+
++++++++++++++++++++++++++++++++++++++++++++++++++++
136136

137137
See :ref:`pre-commit_configuration` for the required steps.
138138

0 commit comments

Comments
 (0)