我正在寻找在
谢谢!
.pre-commit-config.yaml
文件中的配置,以便排除某些分支上的 pre-commit 运行,或者仅在某些分支上运行它。我不知道这个功能是否已经实现,还是我在文档中漏掉了它。谢谢!
.pre-commit-config.yaml
文件中的配置,以便排除某些分支上的 pre-commit 运行,或者仅在某些分支上运行它。我不知道这个功能是否已经实现,还是我在文档中漏掉了它。Git 钩子只是被执行的 shell 脚本。
所以在脚本中,你可以做类似于这样的事情:
if [ $(git branch --no-color | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/') = "main" ]; then
# Do checks for main here
fi
git branch
命令中添加 --show-current
参数来避免使用 sed
。 - Ginger Jesus有几种不同的方法可以实现这个目标:
$ git switch -c no-pre-commit
Switched to a new branch 'no-pre-commit'
$ git rm .pre-commit-config.yaml
rm '.pre-commit-config.yaml'
$ git commit -m 'Remove pre-commit config'
No .pre-commit-config.yaml file was found
- To temporarily silence this, run `PRE_COMMIT_ALLOW_NO_CONFIG=1 git ...`
- To permanently silence this, install pre-commit with the --allow-missing-config option
$ pre-commit install --allow-missing-config
pre-commit installed at .git/hooks/pre-commit
$ git commit -m 'Remove pre-commit config'
`.pre-commit-config.yaml` config file not found. Skipping `pre-commit`.
[no-pre-commit 948c59a] Remove pre-commit config
1 file changed, 159 deletions(-)
delete mode 100644 .pre-commit-config.yaml
如果您已经为不同的阶段安装了pre-commit,则还必须运行
pre-commit install -t <stage> --allow-missing-config
从技术上讲,使得预提交配置有效的唯一必要条件是repos
列表存在,而该列表可以为空。如果您将一个提交添加到更改其.pre-commit-config.yaml
的某些分支中,则会变成这样:
repos: []
然后,当您提交到这些分支时,pre-commit将不会执行任何操作。
根据您的使用情况,您可以使用--no-verify
标志来绕过钩子,例如:
git commit --no-verify -m "WIP Commit message"
或者
git commit -n -m "WIP Commit message"
.pre-commit-config.yaml
不仅仅是git
的一部分,它必须是额外工具的一部分:gitlab
或jenkins
或redmine
等等。你能指定是哪一个吗? - LeGEC.pre-commit-config.yaml
只在本地工作(当我添加或删除钩子时确实如此)。@LasseV.Karlsen 这就是我的问题所涉及的内容。我希望在某些分支上不运行pre-commit。这个配置在overcommit中是可用的,但我在pre-commit中找不到它。 - Alex Martínez Ascensión