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

huangapple go评论104阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2017年1月21日 16:01:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/41777068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定