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

What is wrong with invoking `git shortlog` from Go 'exec()'?

我正在尝试从 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

但我真的很想用 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)
}

空输出:

$>  go run show-commits.go 
Output: 

我可以直接从命令行 运行 git shortlog 并且似乎工作正常。检查 docs,我相信 'shortlog' 命令是 git 本身的一部分。

任何人都可以帮助指出我可以做些什么吗?

谢谢

事实证明,我通过重新阅读 git docs

找到了答案

答案在这一行:

If no revisions are passed on the command line and either standard input is not a terminal or there is no current branch, git shortlog will output a summary of the log read from standard input, without reference to the current repository.

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

所以在上面的示例中,我将 'master' 添加到命令参数中,如下所示:

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

一切都按预期进行。