gitlab - 如何为 Gitlab 指定 pre-commit --from-ref?

标签 gitlab gitlab-ci pre-commit.com

我想检查在 Gitlab CI 的 git 分支中更改的预提交文件。 --from-ref 我需要使用什么参数?

pre-commit run --from-ref=? --to-ref=HEAD

pre-commit 文档中的示例会引发错误。

$ pre-commit run --from-ref=origin/HEAD --to-ref HEAD
An unexpected error has occurred: CalledProcessError: command: ('/usr/bin/git', 'diff', '--name-only', '--no-ext-diff', '-z', 'origin/HEAD...HEAD')
return code: 128
expected return code: 0
stdout: (none)
stderr:
    fatal: ambiguous argument 'origin/HEAD...HEAD': unknown revision or path not in the working tree.
    Use '--' to separate paths from revisions, like this:
    'git <command> [<revision>...] -- [<file>...]'

最佳答案

我在为 GitLab CI/CD 管道开发预提交阶段时遇到了同样的问题。似乎 GitLab CI/CD 运行器不会自动获取 origin/master 分支,导致 fatal: ambiguous argument 'origin/HEAD...HEAD': unknown revision or path不在工作树中。 错误。

我的解决方法是手动获取 origin:

git fetch origin;
pre-commit run --from-ref "origin/$CI_DEFAULT_BRANCH" --to-ref "$CI_COMMIT_SHA";

关于gitlab - 如何为 Gitlab 指定 pre-commit --from-ref?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68818854/

相关文章:

gitlab - [Gitlab]在哪里可以找到托管在我的私有(private) Gitlab 实例上的 Gitlab 页面?

.net-core - gitlab CI 上的 dotnet pack 或 nuget pack

docker - gitlab kaniko - 没有找到匹配的凭据,依靠匿名

linux - 通过文件系统将 Gitlab 存储库复制到另一个 Gitlab 服务器

python - python pre-commit 工具是否可以将子目录视为项目的基础?

python - 如何忽略预提交配置中的 `rev`?

python - 将 pytest 作为预提交钩子(Hook)运行//没有这样的文件或目录问题

git - 是否应该将 "merge commit" merge 回 dev 分支?

gitlab - 如何在 gitlab 中创建存储库徽章?

gitlab-ci:动态工件名称