如何在自定义预提交挂钩中返回状态“Skipped”?

发布于 2025-01-09 20:22:09 字数 1345 浏览 1 评论 0原文

我有一个自定义的预提交挂钩,如果较早的挂钩失败,我想跳过该挂钩。如果较早的挂钩失败,则会生成名为 pre-commit.log 的日志文件。

.pre-commit-config.yaml 可能如下所示:

repos:
-   repo: https://gitlab.com/pycqa/flake8
    rev: 3.9.2
    hooks:
    - id: flake8
      log_file: pre-commit.log
-   repo: local
    hooks:
    - id: skript
      name: skript
      entry: bash skript.sh
      language: system
      log_file: pre-commit.log

我的 bash skript.sh 如下所示:

if [ -f "pre-commit.log" ]; then
  echo "Error in earlier pre-commit! We skip this script."
  echo "See 'pre-commit.log' for detailed information."
  exit 1
else
  echo "All pre-commits successful."
  # some other code
fi

现在我检查“pre-commit.log”是否存在,如果存在,我返回exit 1。因此,该挂钩的输出为 Failed。但我更喜欢 Skipped 状态,因为没有失败。

我的目标是在控制台或日志文件中看到文本 Skipped,就像此图中的 jupyter-notebook-cleanup 一样。 如果我写 exit 0 我会得到 Passed 就像 isortblack 一样,如果我返回 exit 1 或其他大于 1 的数字,我会得到 Failed,就像 flake8 一样。

控制台输出

如何获取消息已跳过

I have a self defined pre-commit hook which I want to skip if an earlier hook fails. If an earlier hook fails, a logfile named pre-commit.log is generated.

A .pre-commit-config.yaml could look like this:

repos:
-   repo: https://gitlab.com/pycqa/flake8
    rev: 3.9.2
    hooks:
    - id: flake8
      log_file: pre-commit.log
-   repo: local
    hooks:
    - id: skript
      name: skript
      entry: bash skript.sh
      language: system
      log_file: pre-commit.log

My bash skript.sh looks like this:

if [ -f "pre-commit.log" ]; then
  echo "Error in earlier pre-commit! We skip this script."
  echo "See 'pre-commit.log' for detailed information."
  exit 1
else
  echo "All pre-commits successful."
  # some other code
fi

Right now I check if the "pre-commit.log" exists and if so I return exit 1. Because of this, the output for this hook is Failed. But I would prefer the status Skipped because there was no failure.

My goal is to see in the console or logfile the text Skipped, like in this image for jupyter-notebook-cleanup.
If I write exit 0 I get Passed like for isort and black and if I return exit 1 or another number greater 1 I get Failed like for flake8.

console output

How can I get the message Skipped?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

或十年 2025-01-16 20:22:09

无法影响文本被 Skipped

Skipped 仅在以下情况下出现:

  • 钩子未运行,因为其 hookid 出现在 SKIP 中> 环境变量
  • 没有与 files / types / types_orpass_filenames 匹配的文件 true< /code> (默认)和always_runfalse(默认值),

您最好的选择是在非失败情况下 exit 0,这将显示为 Passed输出


免责声明中的 :我编写了您正在使用的预提交工具

there is no way to influence that text be Skipped

Skipped only appears in the following cases:

  • the hook was not run because its hookid appeared inside the SKIP environment variable
  • there were no files which matched files / types / types_or and pass_filenames was true (the default) and always_run was false (the default)

your best bet is to exit 0 in the non-failure case, this will appear as Passed in the output


disclaimer: I wrote the pre-commit tool you're using

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文