简体   繁体   English

如何正确获取go build的返回码?

[英]How to properly get return code of go build?

I want to add go build into a precommit hook so that not to ship unbuildable code. 我想将go build添加到一个预提交的钩子中,以免发布无法构建的代码。

If the build succeeds, I want to continue commit, otherwise fail and reject commit. 如果构建成功,则我想继续提交,否则将失败并拒绝提交。

How do I do it properly? 如何正确执行?

Any pre-commit hook will be executed by a git bash (even on Windows), so you can script it through a regular bash script. 任何pre-commit钩子都将由git bash执行(即使在Windows上也是如此),因此您可以通过常规bash脚本对其进行脚本编写。

See Git Hooks 钩钩

Exiting non-zero from this hook aborts the commit, although you can bypass it with git commit --no-verify . 尽管可以使用git commit --no-verify绕过它,但从该挂钩中退出非零值会中止提交。

#!/bin/bash
set -e
go build

(from " Checking Bash exit status of several commands efficiently ") (摘自“有效检查多个命令的Bash退出状态 ”)
That way, you can chain multiple commands (like go vet , other go linters ). 这样,您可以链接多个命令(例如go vet ,其他go linters )。 If any of them fail, the pre-commit hook will prevent the commit. 如果它们中的任何一个失败,则pre-commit挂钩将阻止提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM