英文:
How to properly get return code of go build?
问题
我想将go build
添加到预提交钩子中,以便不会提交无法构建的代码。
如果构建成功,我想继续提交,否则失败并拒绝提交。
如何正确实现这个功能?
英文:
I want to add go build
into a precommit hook so that not to ship unbuildable code.
If the build succeeds, I want to continue commit, otherwise fail and reject commit.
How do I do it properly?
答案1
得分: 1
任何pre-commit
钩子都将由git bash执行(即使在Windows上),因此您可以通过常规的bash脚本来编写它。
请参阅Git Hooks
> 如果从此钩子退出时返回非零值,将中止提交,尽管您可以使用git commit --no-verify
绕过它。
#!/bin/bash
set -e
go build
(来自“Checking Bash exit status of several commands efficiently”)
这样,您可以链接多个命令(例如go vet
,其他go linters)。如果其中任何一个失败,pre-commit
钩子将阻止提交。
英文:
Any pre-commit
hook will be executed by a git bash (even on Windows), so you can script it through a regular bash script.
See Git Hooks
> Exiting non-zero from this hook aborts the commit, although you can bypass it with git commit --no-verify
.
#!/bin/bash
set -e
go build
(from "Checking Bash exit status of several commands efficiently")
That way, you can chain multiple commands (like go vet
, other go linters). If any of them fail, the pre-commit
hook will prevent the commit.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论