gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:54:57 27 4
gpt4 key购买 nike

我试图从 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() 命令运行时,我需要指定分支。

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

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

一切都按预期进行。

关于git - 从 Go `git shortlog` 调用 'exec()' 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51966053/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com