预提交(pre-commit)运行正常,但是git提交失败。

4
在我的项目中,有几个预提交钩子来确保我们的代码符合团队标准,但每当我试图提交更改时,总是会出现一个错误,指出找不到可执行文件。通常情况下,我首先认为路径环境变量值可能存在问题,但实际上只是git提交命令引起了这个问题。
yunu@LAPTOP-8FK6RMJ8:/mnt/d/Projects/xyz/idl$ pre-commit
Fix End of Files.........................................................Passed
Trim Trailing Whitespace.................................................Passed
yamllint.............................................(no files to check)Skipped
ensure buf...............................................................Passed
buf check lint...........................................................Passed
buf check breaking.......................................................Passed
yunu@LAPTOP-8FK6RMJ8:/mnt/d/Projects/xyz/idl$ git commit
Fix End of Files.........................................................Passed
Trim Trailing Whitespace.................................................Passed
yamllint.............................................(no files to check)Skipped
ensure buf...............................................................Failed
- hook id: ensure-buf
- exit code: 1

Executable `task` not found

buf check lint...........................................................Failed
- hook id: buf-lint
- exit code: 1

Executable `buf` not found

buf check breaking.......................................................Failed
- hook id: buf-breaking
- exit code: 1

Executable `buf` not found

接着这个操作,我可以运行之前被标为“未找到”的命令。
我可以看到Bash已经正确识别了这些命令。

yunu@LAPTOP-8FK6RMJ8:/mnt/d/Projects/xyz/idl$ buf
Usage:
  buf [command]

Available Commands:
  check           Run lint or breaking change checks.
  experimental    Experimental commands. Unstable and will likely change.
  help            Help about any command
  image           Work with Images and FileDescriptorSets.
  ls-files        List all Protobuf files for the input location.
  protoc          High-performance protoc replacement.

Flags:
  -h, --help                help for buf
      --log-format string   The log format [text,color,json]. (default "color")
      --log-level string    The log level [debug,info,warn,error]. (default "info")
      --timeout duration    The duration until timing out. (default 2m0s)
  -v, --version             version for buf

Use "buf [command] --help" for more information about a command.
yunu@LAPTOP-8FK6RMJ8:/mnt/d/Projects/xyz/idl$

那么问题出在哪里呢?是git命令使用了不同的路径吗?我是在Windows 10上的Ubuntu WSL上运行的。

你能否无问题地运行.git/hooks/pre-commit - LeGEC
感谢您的帮助,结果出现了与git提交时相同的问题,错误消息显示多个命令未找到。我一直在运行错误的命令吗? - yunu
如果在 pre-commit 中使用它们的完整路径替换 buftask,它能正常工作吗? - ElpieKay
3个回答

4

我移除了.git/hooks/pre-commit,然后再次运行pre-commit install,问题就解决了。现在我可以顺利使用git commit命令。


0
对我来说,我所需要做的就是重新启动PyCharm。

0
我在VScode上一遍又一遍地遇到同样的错误; 最后我决定使用hadolint dockerfile代替。
所以在我的pre-commit-config.yaml文件中,我有以下内容:
  - repo: local
    hooks:
    - id: hadolint
      name: hadolint
      entry: hadolint/hadolint:v2.12.1-beta hadolint --ignore DL3008 --no-color
      language: docker_image
      types: [file, dockerfile]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接