Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

pre commit.com - Can the python pre-commit tool consider a subdirectory as a base of the project?

I have a repo like this:

myrepo/
      .git
      tool1/  
           src/main.py
           tests/test.py
           .pre-commit-config.yaml

Where the config is:

repos:
- repo: local
  hooks:
    - id: pytest-cov
      name: pytest coverage
      stages: [push]
      language: system
      entry: pwd  # originally: pytest testtool --cov --cov-fail-under=0 
      types: [python]
      pass_filenames: false

Changed the entry to 'pwd' just to see what the basedir is in pre-commit's context.. Now, when I run this from the myrepo/tool1 dir, by pre-commit run --hook-stage=push --verbose, pwd will say "myrepo", rather than "myrepo/tool1". I would need to run in the tool1 as all paths etc are relative to that.

Is there some setting or another way to run precommit so that the base directory would be myrepo/tool1? or does the project really have to live in the root of the repo?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you can put the configuration anywhere as you've seen but git hooks themselves always run from the root of the repository (this is how git works). there's also only one set of git hooks per repository so functionality for a monorepo doesn't really make sense for git hooks

there's a few issues on the tracker that are of interest: pre-commit/pre-commit/issues search:monorepo

the suggestion is to write a wrapper around whatever tool in question and use cd if you need to change the working directory -- for instance from #1417:

-   repo: local
    hooks:
    -   id: go-unit-tests
        name: run go test s(go test)
        language: system
        entry: bash -c 'cd subdir && exec go test ./...'
        pass_filenames: false
        types: [go]
        files: ^subdir/

disclaimer: I'm the author of pre-commit


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...