目錄
問題內容
解決方法
首頁 後端開發 Golang 無法使用 go-github 和 422 建立提交 - 更新不是快轉

無法使用 go-github 和 422 建立提交 - 更新不是快轉

Feb 09, 2024 pm 05:12 PM

无法使用 go-github 和 422 创建提交 - 更新不是快进

php小編小新在使用go-github時,遇到了一個問題:在建立提交時,出現了422錯誤,並且提示「更新不是快轉」。這個問題的具體原因是什麼呢?該如何解決呢?接下來,我們將為大家詳細解答。

問題內容

我使用以下函數簡單地使用 go-github 函式庫在分支中建立一個新提交

func ghapicreatecommit(ctx context.context, client *github.client, commitopts *commitoptions) error {
    // get the reference of the branch
    ref, _, err := client.git.getref(ctx, repoowner, commitopts.repo, "refs/heads/"+commitopts.branch)
    if err != nil {
        return err
    }
    commit, _, err := client.git.getcommit(ctx, repoowner, commitopts.repo, *ref.object.sha)
    if err != nil {
        return err
    }

    commit.message = github.string(commitopts.commitmessage)

    // create a new commit with the updated commit message
    newcommit, _, err := client.git.createcommit(ctx, repoowner, commitopts.repo, commit)
    if err != nil {
        return err
    }
    // attach the new commit to the reference
    ref.object.sha = newcommit.sha

    // update the branch reference to point to the new commit
    _, _, err = client.git.updateref(ctx, repoowner, commitopts.repo, ref, false)
    if err != nil {
        return err
    }

    return nil
}
登入後複製

此操作失敗:

PATCH https://api.github.com/repos/MyOrg/myrepo/git/refs/heads/the-branch-I-am-creating-the-new-commit-to: 422 Update is not a fast forward []
登入後複製

為什麼不快轉?它只是從現有分支/提交創建的新提交。

ps:我明確不想想要在提交時建立新檔案。

解決方法

func (s *gitservice) createcommit(ctx context.context, owner string, repo string, commit *commit) (*commit, *response, error)
登入後複製

參數commit用於指定新commit的一些信息,包括新commit的parents(請參閱實作)。

在您的程式碼中,新提交和舊提交具有相同的 parents,因此這不是快轉推送到分支。為了使其快速推送到分支,新提交的 parents 應指向舊提交。

我想以下更改會使其快轉:

+ commit.Parents = []*github.Commit{commit}
  newCommit, _, err := client.Git.CreateCommit(ctx, repoOwner, commitOpts.Repo, commit)
登入後複製

以上是無法使用 go-github 和 422 建立提交 - 更新不是快轉的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Go語言包導入:帶下劃線和不帶下劃線的區別是什麼? Go語言包導入:帶下劃線和不帶下劃線的區別是什麼? Mar 03, 2025 pm 05:17 PM

Go語言包導入:帶下劃線和不帶下劃線的區別是什麼?

Beego框架中NewFlash()函數如何實現頁面間短暫信息傳遞? Beego框架中NewFlash()函數如何實現頁面間短暫信息傳遞? Mar 03, 2025 pm 05:22 PM

Beego框架中NewFlash()函數如何實現頁面間短暫信息傳遞?

Go語言中如何將MySQL查詢結果List轉換為自定義結構體切片? Go語言中如何將MySQL查詢結果List轉換為自定義結構體切片? Mar 03, 2025 pm 05:18 PM

Go語言中如何將MySQL查詢結果List轉換為自定義結構體切片?

如何編寫模擬對象和存根以進行測試? 如何編寫模擬對象和存根以進行測試? Mar 10, 2025 pm 05:38 PM

如何編寫模擬對象和存根以進行測試?

如何定義GO中仿製藥的自定義類型約束? 如何定義GO中仿製藥的自定義類型約束? Mar 10, 2025 pm 03:20 PM

如何定義GO中仿製藥的自定義類型約束?

您如何在GO中編寫單元測試? 您如何在GO中編寫單元測試? Mar 21, 2025 pm 06:34 PM

您如何在GO中編寫單元測試?

Go語言如何便捷地寫入文件? Go語言如何便捷地寫入文件? Mar 03, 2025 pm 05:15 PM

Go語言如何便捷地寫入文件?

如何使用跟踪工具了解GO應用程序的執行流? 如何使用跟踪工具了解GO應用程序的執行流? Mar 10, 2025 pm 05:36 PM

如何使用跟踪工具了解GO應用程序的執行流?

See all articles