[Python] Make pre-commit work to check the commit message on GitHub

Discussão em 'Python' iniciado por Stack, Julho 16, 2025.

  1. Stack

    Stack Membro Participativo

    I am wondering how I can make a commit message pre-commit hook work in both locally and on GitHub using pre-commit.

    We use pre-commit in two projects I am involved in to locally check that the commit message adheres to some standards through two Python scripts:

    https://github.com/InsightSoftwareConsortium/ITK/blob/main/Utilities/Hooks/kw-commit-msg.py

    and

    https://github.com/dipy/dipy/pull/3589/files (still a PR)

    And the corresponding .pre-commit-config.yaml hooks:

    - id: kw-commit-msg
    name: 'kw commit-msg'
    entry: 'Utilities/Hooks/run-pixi-python.sh Utilities/Hooks/kw-commit-msg.py'
    language: system
    stages: [commit-msg]


    and

    - repo: local
    hooks:
    - id: dipy-commit-msg
    name: 'dipy-commit-msg'
    entry: 'tools/hooks/commit-msg.py'
    language: python


    I have realized that although the hook works well locally (as long as the stages: [commit-msg] is there), the check is not run on GitHub: https://github.com/dipy/dipy/actions/runs/16013005246/job/45174358635?pr=3589#step:4:110

    I have done an additional quick test submitting a non-compliant message to a fork of mine on the other project as well and pre-commit checks pass: https://github.com/jhlegarreta/ITK/actions/runs/16332297568/job/46137422968?pr=1#step:5:150

    As suggested by a colleague, if I remove stages: [commit-msg] from the .pre-commit-config.yaml file, the check is triggered on GitHub, but it fails even if the commit message is compliant:

    https://github.com/dipy/dipy/actions/runs/16331696031/job/46135555136?pr=3589#step:4:119

    and shows the error:

    To continue editing a message, run the command:
    git commit -e -F "/home/runner/work/dipy/dipy/.git/COMMIT_MSG"
    (assuming your working directory is at the top).
    Traceback (most recent call last):
    File "/home/runner/work/dipy/dipy/tools/hooks/commit-msg.py", line 147, in <module>
    main()
    File "/home/runner/work/dipy/dipy/tools/hooks/commit-msg.py", line 81, in main
    subject_line: str = input_file_lines[0]
    IndexError: list index out of range


    (the die message about how the commit should be formatted is also shown 3 times; not sure why)

    It looks like the commit message file is not found by the CI runner because the commit is already made at that point.

    So how can I make work the hook both locally and on GitHub?

    Continue reading...

Compartilhe esta Página