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

从Go';调用'git shortlog'有什么问题;exec()';?,git,go,exec,Git,Go,Exec,我试图从Go调用git shortlog来获取输出,但我遇到了麻烦 下面是一个工作示例,说明了如何使用git log: package main import ( "fmt" "os" "os/exec" ) func main() { runBasicExample() } func runBasicExample() { cmdOut, err := exec.Command("

我试图从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%s\n", output)
}
其中给出了预期输出:

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

    Updating readme
但是我真的很想用
gitshortlog
来实现这一点。 出于某种原因。。。我就是不能让它和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%s\n", output)
}
输出为空:

$>  go run show-commits.go 
Output: 
我可以直接从命令行运行
git shortlog
,它似乎工作得很好。通过检查,我相信'shortlog'命令是git本身的一部分

有人能帮我指出我可以做什么不同吗


谢谢

事实证明,我能够通过重新阅读

答案是这样的:

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

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

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

cmdOut, err := exec.Command("git", "shortlog", "master").Output()
一切都按预期进行