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

Cug*_*uga 3 git exec go

我正在尝试git shortlog从Go 调用以获取输出,但是我遇到了麻烦。

这是一个如何使用以下方法的有效示例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)
}
Run Code Online (Sandbox Code Playgroud)

给出预期的输出:

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

    Updating readme
Run Code Online (Sandbox Code Playgroud)

但是我真的很想用git shortlog

由于某种原因...我只是无法使其与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)
}
Run Code Online (Sandbox Code Playgroud)

空输出:

$>  go run show-commits.go 
Output: 
Run Code Online (Sandbox Code Playgroud)

我可以git shortlog直接从命令行运行,它似乎可以正常运行。检查文档,我被认为是'shortlog'命令是git本身的一部分。

谁能帮我指出我可以做些什么?

谢谢

Cug*_*uga 6

原来,我能够通过重新阅读git docs找到答案

答案在这一行:

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

尽管事实上我可以git shortlog通过终端运行,并通过exec()命令,我仍需要指定分支。

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

cmdOut, err := exec.Command("git", "shortlog", "master").Output()
Run Code Online (Sandbox Code Playgroud)

一切都按预期进行。