Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...