dsla94915 2018-08-22 11:40
浏览 50
已采纳

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

I'm trying to invoke git shortlog from Go to grab the output, but I'm running into a wall.

Here's a working example of how I'm able to do this with 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: 
%s
", output)
}

Which gives expected output:

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

    Updating readme

But I really want to do this with git shortlog.

For some reason... I just can't get it to work with shortlog. Here's the program again, with the only change being the git command line:

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: 
%s
", output)
}

With empty output:

$>  go run show-commits.go 
Output: 

I can run git shortlog directly from the command line and it seems to work fine. Checking the docs, I'm led to believe the 'shortlog' command is part of git itself.

Can anyone help point out what I could do different?

Thanks

  • 写回答

1条回答 默认 最新

  • duanjieyi6582 2018-08-22 11:45
    关注

    Turns out, I was able to find the answer by re-reading the git docs

    The answer was in this line:

    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.

    Despite the fact that I can run git shortlog from the terminal and see expected output, when running via the exec() command, I need to specify the branch.

    So in the above examples, I added 'master' to the command arguments like so:

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

    And everything works as expected.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧