代码之家  ›  专栏  ›  技术社区  ›  Cuga

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

  •  3
  • Cuga  · 技术社区  · 6 年前

    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短日志

    不知为什么。。。我只是不能让它和短日志一起工作。程序又来了,唯一的变化是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短日志 直接从命令行运行,看起来运行良好。检查 docs

    有谁能帮我指出我能做什么不同的吗?

    谢谢

    1 回复  |  直到 6 年前
        1
  •  7
  •   Cuga    6 年前

    结果,我通过重新阅读 git docs

    答案是这样的:

    如果没有在命令行上传递修订,则 标准输入不是终端

    尽管我能跑 git shortlog 当通过终端运行 exec() 命令,我需要指定分支。

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

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

    一切如期而至。