我試圖git shortlog從 Go 中調用以獲取輸出,但我遇到了麻煩。這是我如何使用以下方法執行此操作的工作示例git log:package mainimport ( "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 4abb96396c69fa4e604c9739abe338e03705f9d4Author: TheAndruuDate: Tue Aug 21 21:55:07 2018 -0400 Updating readme但我真的很想用git shortlog.出于某種原因......我無法讓它與 shortlog 一起工作。又是這個程序,唯一的變化是 git 命令行:package mainimport ( "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直接從命令行運行,它似乎工作正常。檢查文檔后,我相信“shortlog”命令是 git 本身的一部分。任何人都可以幫助指出我可以做些什么不同嗎?
1 回答

www說
TA貢獻1775條經驗 獲得超8個贊
事實證明,我能夠通過重新閱讀git 文檔找到答案
答案是在這一行:
如果沒有在命令行上傳遞任何修訂,并且標準輸入不是終端或沒有當前分支,則 git shortlog 將輸出從標準輸入讀取的日志摘要,而不引用當前存儲庫。
盡管我可以git shortlog
從終端運行并看到預期的輸出,但在通過exec()
命令運行時,我需要指定分支。
所以在上面的示例中,我將“master”添加到命令參數中,如下所示:
cmdOut, err := exec.Command("git", "shortlog", "master").Output()
一切都按預期進行。
- 1 回答
- 0 關注
- 103 瀏覽
添加回答
舉報
0/150
提交
取消