0

Summary of the problem

I've setup a repository with the checks I developed, designed to be used exclusively in an another repository. The problem I've been facing is that when I run pre-commit run -a -v the checks don't go through every file, and time to time they even change!

A little more details

  • I've run the [identity][1] check and it prints every file in the repo, meaning the files are read by pre-commit (per my understanding)
  • When I execute pre-commit run the file in staging are correctly parsed

Why do i think that the checks don't run on every file?

  • I always print something to the standard output for every check, and when I run it verbosely it only prints 5 to 7 lines, meaning it checked only those.
  • If I try and edit a file (breaking a check) whose not on the short list, the check still passes through

Some code

The structure of the folder containing the files to be checked

./
    articles/
             some_name/
                       file.md
                       file.png
                       and_so_on.jpeg
    team/
         some_other_name/
                       file.md
                       propic.jpg

Summary of the .pre-commit-hooks.yaml

-   id: team-check-name
    name: blabla
    description: blabla
    entry: team-checkname
    files: 'team/.*/'
    types: [markdown]
    language: python

-   id: article-check-name
    name: blabla
    description: blabla
    entry: article-checkname
    files: 'articles/.*/'
    types: [markdown]
    language: python

Essentially the checks supposed to run on articles/.*/ start with article- and they all have this property: files: 'articles/.*/'. Similarly every check supposed to run on team/.*/ starts with team- and they all have files: 'team/.*/'

Summary of the .pre-commit-config.yaml

repos:
-   repo: https://github.com/repourl
    rev: commit_hash
    hooks:
    -   id: team-check-name
    -   id: article-check-name

But as you can see, I don't override any settings so it shouldn't interfere

ExalFabu
  • 77
  • 7

1 Answers1

2

the hook tools you've written are incorrect -- they only process sys.argv[1] rather than positional arguments

your code uses a lot of global variables so it's not a straightforward refactor -- typically you'd either use argparse to collect nargs='*' or loop over sys.argv[1:] (if you don't have any options)

as to why this is the convention -- it's very wasteful to start a linter process over and over to lint a single file (often times executable startup cost dwarfs the actual linting / formatting process)


disclaimer: I wrote pre-commit

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • Oh, got it thanks! Basically i was checking the first file of every "batch". That makes sense. Sadly i missed that part in the documentation, would've wasted a lot less time. Thanks again :) – ExalFabu Oct 17 '22 at 19:33