[英]How to parse git commit id in android .go
As title, I try to call作为标题,我尝试调用
cmd := exec.Command(commit_id, "=", "$(git rev-parse --short HEAD)")
cmd.Run()
fmt.Println("commit_id = ", commit_id);
But the result is null.但结果是 null。 Does somebody know how to parse git commit-id?
有人知道如何解析 git commit-id 吗? Thanks!
谢谢!
Here is a sample.这是一个示例。
package main
import (
"bytes"
"fmt"
"os/exec"
)
func main() {
var commitid string
var sout, serr bytes.Buffer
cmd := exec.Command("git", "rev-parse", "--short", "HEAD")
cmd.Stdout = &sout
cmd.Stderr = &serr
err := cmd.Run()
if err != nil {
fmt.Println(serr.String())
} else {
commitid = sout.String()
fmt.Println("commit_id =", commitid)
}
}
I am new to Golang.我是 Golang 的新手。 The code could be better.
代码可能会更好。 I followed the example in Command and it seems that it works this way.
我按照命令中的示例进行操作,似乎它是这样工作的。
Save the below code as commit_hash.go
将以下代码另存为
commit_hash.go
import (
"bytes"
"fmt"
"os"
"os/exec"
)
func main() {
commit, err := commitHash()
if err != nil {
fmt.Printf("\x1b[31;1m%s\x1b[0m\n", fmt.Sprintf("error: %s", err))
os.Exit(1)
}
fmt.Printf("commit_id=%s", commit)
}
func commitHash() (string, error) {
cmd := exec.Command("git", "rev-parse", "--short", "HEAD")
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("%w; %s", err, stderr.String())
}
return stdout.String(), nil
}
then run it from your git repository:然后从您的 git 存储库运行它:
go run commit_hash.go
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.