pre-commit / pre-commit-hooks

Some out-of-the-box hooks for pre-commit
MIT License
5.4k stars 710 forks source link

Question: Running a hook when a submodule's state changes #1079

Closed jvacek closed 4 months ago

jvacek commented 4 months ago

Heya!

I'm trying to have a hook run whenever one of the files that changes is a submodule's reference. I was assuming that the change would count as a "file change" to pre-commit (as it does to git), however when I'm making a commit, I'm told there are no files that changed.

I am now running this with always_run: true, as I cannot find any way to make a condition about the subproject commit of a submodule changing.

Would you have any advice for a sane way to do this? Many thanks!


here's some output to maybe explain better:

usermanagement ❯ git status
On branch ruff
Your branch is up to date with 'origin/ruff'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   .python-shared-config

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
  (commit or discard the untracked or modified content in submodules)
        modified:   .python-shared-config (modified content)

usermanagement ❯ git diff --cached
diff --git a/.python-shared-config b/.python-shared-config
index e53dc17ddb..3aa9c783b8 160000
--- a/.python-shared-config
+++ b/.python-shared-config
@@ -1 +1 @@
-Subproject commit e53dc17ddb89df93476c52d59d904c2d5a1f742b
+Subproject commit 3aa9c783b8c04346df58e695e42bf05e7a2dec2a

usermanagement ❯ pre-commit run
trim trailing whitespace.........................................................(no files to check)Skipped
fix end of files.................................................................(no files to check)Skipped
check for merge conflicts........................................................(no files to check)Skipped
check for added large files......................................................(no files to check)Skipped
mixed line ending................................................................(no files to check)Skipped
pip-compile requirements.in......................................................(no files to check)Skipped
pip-compile requirements-dev.in..................................................(no files to check)Skipped
Lint YAML........................................................................(no files to check)Skipped
Lint Python......................................................................(no files to check)Skipped
Format Python....................................................................(no files to check)Skipped
Python imports order.............................................................(no files to check)Skipped
terraform fmt....................................................................(no files to check)Skipped
Compile API......................................................................(no files to check)Skipped
Ruff version align check.........................................................(no files to check)Skipped
ruff.............................................................................(no files to check)Skipped
shfmt............................................................................(no files to check)Skipped
shellcheck.......................................................................(no files to check)Skipped
Check migrations safety..........................................................(no files to check)Skipped

~/repositories/otainsight/usermanagement ruff*
usermanagement ❯ touch hello

~/repositories/otainsight/usermanagement ruff*
usermanagement ❯ git add hello

~/repositories/otainsight/usermanagement ruff*
usermanagement ❯ pre-commit run
trim trailing whitespace.........................................................(no files to check)Skipped
fix end of files.................................................................(no files to check)Skipped
check for merge conflicts............................................................................Passed
check for added large files..........................................................................Passed
mixed line ending....................................................................................Passed
pip-compile requirements.in......................................................(no files to check)Skipped
pip-compile requirements-dev.in..................................................(no files to check)Skipped
Lint YAML........................................................................(no files to check)Skipped
Lint Python......................................................................(no files to check)Skipped
Format Python....................................................................(no files to check)Skipped
Python imports order.............................................................(no files to check)Skipped
terraform fmt....................................................................(no files to check)Skipped
Compile API......................................................................(no files to check)Skipped
Ruff version align check.............................................................................Failed
- hook id: ruff-version-align-check
- exit code: 1

Submodule in: ./.python-shared-config
ruff versions:
  0.5.5  | ./.python-shared-config/ruff/requirements.in
  asdasd         | ./.python-shared-config/ruff/ruff-ci.yml
  0.5.5  | requirements-dev.txt
  0.5.5  | .pre-commit-config.yaml

ERROR: ruff versions are not aligned

ruff.............................................................................(no files to check)Skipped
shfmt............................................................................(no files to check)Skipped
shellcheck.......................................................................(no files to check)Skipped
Check migrations safety..........................................................(no files to check)Skipped

as for the hook I'm trying to run:

  - repo: .python-shared-config
    rev: v0.2.0
    hooks:
      - id: ruff-version-align-check

- id: ruff-version-align-check
  name: Ruff version align check
  entry: ./ruff/check_versions_aligned.sh
  language: script
  # always_run: true # A submodule bump does not count as a "matching file"
asottile commented 4 months ago

this repo is for a few out of the box hooks for pre-commit and not a support forum. your best bet is to first read the docs on filtering and then if you still don't know the answer ask with the correct tags on stackoverflow (also listed in the docs)