从 Go 'exec()' 调用 'git shortlog' 有什么问题?



我试图从 Go 调用git shortlog来获取输出,但我遇到了一堵墙。

这是我如何使用git log做到这一点的工作示例:

package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
runBasicExample()
}
func runBasicExample() {
cmdOut, err := exec.Command("git", "log").Output()
if err != nil {
fmt.Fprintln(os.Stderr, "There was an error running the git command: ", err)
os.Exit(1)
}
output := string(cmdOut)
fmt.Printf("Output: n%sn", output)
}

这给出了预期的输出:

$>  go run show-commits.go 
Output: 
commit 4abb96396c69fa4e604c9739abe338e03705f9d4
Author: TheAndruu
Date:   Tue Aug 21 21:55:07 2018 -0400
Updating readme

但我真的很想用git shortlog来做这件事.

出于某种原因...我只是无法让它与短日志一起使用。 这又是程序,唯一的变化是 git 命令行:

package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
runBasicExample()
}
func runBasicExample() {
cmdOut, err := exec.Command("git", "shortlog").Output()
if err != nil {
fmt.Fprintln(os.Stderr, "There was an error running the git command: ", err)
os.Exit(1)
}
output := string(cmdOut)
fmt.Printf("Output: n%sn", output)
}

空输出:

$>  go run show-commits.go 
Output: 

我可以直接从命令行运行git shortlog,它似乎工作正常。 检查文档时,我被引导相信"shortlog"命令是 git 本身的一部分。

谁能帮忙指出我可以做些什么不同的事情?

谢谢

事实证明,我能够通过重新阅读 git 文档找到答案

答案是这一行:

如果在命令行上没有传递修订,并且标准输入不是终端或没有当前分支,git shortlog 将输出从标准输入读取的日志摘要,而不引用当前存储库。

尽管我可以从终端运行git shortlog并查看预期的输出,但在通过exec()命令运行时,我需要指定分支。

所以在上面的例子中,我在命令参数中添加了"master",如下所示:

cmdOut, err := exec.Command("git", "shortlog", "master").Output()

一切都按预期工作。

最新更新