亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 Go 'exec()' 調用 `git shortlog` 有什么問題?

從 Go 'exec()' 調用 `git shortlog` 有什么問題?

Go
POPMUISE 2023-03-21 17:12:34
我試圖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()

一切都按預期進行。


查看完整回答
反對 回復 2023-03-21
  • 1 回答
  • 0 關注
  • 103 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號