1

我正在使用这个库来尝试向我的项目添加一个预提交挂钩。需要考虑的内容很多,我认为我想要的预提交检查不需要他们的大多数示例使用的大范围。我想做的就是运行一个 python 文件。根据该文件是否退出/完成而没有问题(IE 没有引发异常)是我想要允许或禁止提交的内容。我做了一个.pre-commit-config.yaml,但我不知道如何让它只运行一个文件。

我基本上希望键入git commit -m "whatever"自动运行python myfile.py<- 并基于此退出代码,允许或阻止提交。关于我的 yaml 应该是什么样子的任何想法?

这是我到目前为止所拥有的:

repos:
-   repo: local
    hooks:
    - id: translation-file-check
      name: Check Translation Files Are Aligned
      description: This hook ensures that all translation files share the same set of keys and generates a CSV if there are no issues
      language: python
      entry: "./dir/subdir/myfile.py"

但我收到以下错误:.An unexpected error has occurred: OSError: [WinError 193] %1 is not a valid Win32 application 我认为因为它期望这个 .py 文件是一个 .exe 或其他东西,即使我设置language为 python ...

4

1 回答 1

5

很接近了!有两种方法可以使这项工作:

  1. 将“shebang”(#!/usr/bin/env python)添加到您的文件中
    • 尽管 shebangs 是一个 posix 的东西,但pre-commit它包含规范化平台并使它们也可以在 Windows 上工作的代码!
  2. 利用entry: python ./dir/subdir/myfile.py

    repos:
    -   repo: local
        hooks:
        - id: translation-file-check
          name: Check Translation Files Are Aligned
          description: This hook ensures that all translation files share the same set of keys and generates a CSV if there are no issues
          language: python
          entry: python ./dir/subdir/myfile.py
    

(免责声明:我是作者pre-commit

于 2019-11-18T19:54:56.133 回答