每个目录的 Git 预提交挂钩

问题描述 投票:0回答:1

如何格式化

.pre-commit-config.yaml
,使其根据我的目录而有所不同?

我的项目有不同的目录。其中一个目录是包含

package.json
和 Django 文件的 UI 目录。另一种是 Python 包,它没有
package.json

我想仅在 UI 目录上运行

eslint
,而不是在 Python 包目录上运行,因为它没有 Javascript 文件。

目前,当我在根目录运行它时,它显示:

npm ERR! enoent ENOENT: no such file or directory, open '/xxx/package.json'

我的

.pre-commit-config.yaml
文件看起来像:

fail_fast: true
repos:
- repo: git://github.com/pre-commit/pre-commit-hooks
  rev: v0.9.2
  hooks:
    ...

- repo: local
  hooks:
  - id: eslint
    name: eslint-local
    entry: npm run lint
    language: system
    types: [javascript]
    exclude: >
            (?x)^(
                .+\.config\.js|
                server\.js|
                \.eslintrc\.js
            )$
    pass_filenames: true
git hook pre-commit pre-commit.com
1个回答
0
投票

您可以在 bash/other 中执行命令并自行更改目录!这对我来说最有效。

例如:

-   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/

或者如果您正在运行 Python 测试:

  - repo: local
    hooks:
      - id: pytest
        name: pytest
        entry: sh -c 'cd text2sql-backend && PYTHONPATH=. poetry run pytest .'
        language: system
        types: [python]
        pass_filenames: false
        stages: [commit]
        exclude: ".*pb2(_grpc)?.py(i)?$|__init__.py"
© www.soinside.com 2019 - 2024. All rights reserved.