搜尋
首頁後端開發Golang無法使用 go-github 和 422 建立提交 - 更新不是快轉

无法使用 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中文網其他相關文章!

陳述
本文轉載於:stackoverflow。如有侵權,請聯絡admin@php.cn刪除
了解Goroutines:深入研究GO的並發了解Goroutines:深入研究GO的並發May 01, 2025 am 12:18 AM

goroutinesarefunctionsormethodsthatruncurranceingo,啟用效率和燈威量。 1)shememanagedbodo'sruntimemultimusingmultiplexing,允許千sstorunonfewerosthreads.2)goroutinessimproverentimensImproutinesImproutinesImproveranceThroutinesImproveranceThrountinesimproveranceThroundinesImproveranceThroughEasySytaskParallowalizationAndeff

了解GO中的初始功能:目的和用法了解GO中的初始功能:目的和用法May 01, 2025 am 12:16 AM

purposeoftheInitfunctionoIsistoInitializeVariables,setUpConfigurations,orperformneccesSetarySetupBeforEtheMainFunctionExeCutes.useInitby.UseInitby:1)placingitinyourcodetorunautoamenationally oneraty oneraty oneraty on inity in ofideShortAndAndAndAndForemain,2)keepitiTshortAntAndFocusedonSimImimpletasks,3)

了解GO界面:綜合指南了解GO界面:綜合指南May 01, 2025 am 12:13 AM

Gointerfacesaremethodsignaturesetsthattypesmustimplement,enablingpolymorphismwithoutinheritanceforcleaner,modularcode.Theyareimplicitlysatisfied,usefulforflexibleAPIsanddecoupling,butrequirecarefulusetoavoidruntimeerrorsandmaintaintypesafety.

從恐慌中恢復:何時以及如何使用recover()從恐慌中恢復:何時以及如何使用recover()May 01, 2025 am 12:04 AM

在Go中使用recover()函數可以從panic中恢復。具體方法是:1)在defer函數中使用recover()捕獲panic,避免程序崩潰;2)記錄詳細的錯誤信息以便調試;3)根據具體情況決定是否恢復程序執行;4)謹慎使用,以免影響性能。

您如何使用'字符串”包裝操縱串中的琴弦?您如何使用'字符串”包裝操縱串中的琴弦?Apr 30, 2025 pm 02:34 PM

本文討論了使用GO的“字符串”軟件包進行字符串操作,詳細介紹了共同的功能和最佳實踐,以提高效率並有效地處理Unicode。

您如何使用'加密”在Go中執行加密操作的軟件包?您如何使用'加密”在Go中執行加密操作的軟件包?Apr 30, 2025 pm 02:33 PM

本文使用GO的“加密”軟件包詳細介紹了加密操作,討論了安全實施的關鍵生成,管理和最佳實踐。

您如何使用'時間”處理日期和時間的包裝?您如何使用'時間”處理日期和時間的包裝?Apr 30, 2025 pm 02:32 PM

本文詳細介紹了GO的“時間”包用於處理日期,時間和時區,包括獲得當前時間,創建特定時間,解析字符串以及測量經過的時間。

您如何使用'反映”包裹檢查GO中變量的類型和值?您如何使用'反映”包裹檢查GO中變量的類型和值?Apr 30, 2025 pm 02:29 PM

文章討論了使用GO的“反射”軟件包進行可變檢查和修改,突出顯示方法和性能注意事項。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器