首頁  >  文章  >  後端開發  >  go-github 函式庫中的 Tree SHA is not a tree object 錯誤

go-github 函式庫中的 Tree SHA is not a tree object 錯誤

王林
王林轉載
2024-02-09 14:00:11458瀏覽

go-github 库中的 Tree SHA is not a tree object 错误

在使用 go-github 函式庫時,您可能會遇到一個名為 "Tree SHA is not a tree object" 的錯誤。這個錯誤的產生是因為您提供的 SHA 值不是一個有效的樹物件。通常,這個錯誤可能是由於檔案或目錄不存在、SHA 值錯誤或其他一些問題導致的。為了解決這個問題,您可以檢查您提供的 SHA 值是否正確,並確保相關的檔案或目錄存在。如果問題仍然存在,您可以查閱 go-github 庫的文檔或尋求協助,以獲得進一步的解決方案。 php小編柚子希望這個簡短的指南能對您有所幫助!

問題內容

我正在嘗試使用 go-github 在 github 中建立一個空提交。

以下程式碼:

func createheadbranchforpr(ctx context.context, basebranch, repo, owner string,
    client *github.client) (newbranch string, err error) {
    newbranch = createrandombranchname()
    basebranchref, _, err := client.git.getref(ctx, owner, repo, "heads/"+basebranch)
    if err != nil {
        return "", err
    }
    latestcommitsha := basebranchref.object.getsha()
    // create a new tree with no changes from the latest commit on the base branch
    newtree := &github.tree{
        sha: &latestcommitsha,
    }
    currenttime := time.now()
    newcommit := &github.commit{
        message: github.string("test commit"),
        tree:    newtree,
        parents: []github.commit{
            {
                sha: github.string(latestcommitsha),
            },
        },
        author: &github.commitauthor{
            name:  github.string(prcommitterauthorname),
            email: github.string(prcommitterauthoremail),
            date:  &currenttime,
        },
        committer: &github.commitauthor{
            name:  github.string(prcommitterauthorname),
            email: github.string(prcommitterauthorname),
            date:  &currenttime,
        },
        sha: &latestcommitsha,
    }
    newcommitresponse, _, err := client.git.createcommit(ctx, owner, repo, newcommit)
    if err != nil {
        return "", err
    }
    // create a new branch based on the new commit
    newbranchref := &github.reference{
        ref:    github.string("refs/heads/" + newbranch),
        object: &github.gitobject{sha: newcommitresponse.sha},
    }
    _, _, err = client.git.createref(ctx, owner, repo, newbranchref)
    if err != nil {
        return "", err
    }
    return newbranch, nil
}

失敗

newcommitresponse, _, err := client.git.createcommit(ctx, owner, repo, newcommit)

422 Tree SHA is not a tree object []

我在任何地方都找不到有關此錯誤的任何相關資訊。

有什麼想法嗎?

解決方法

當你使用 git cli 時,git 本身會執行「有意義」的翻譯-例如:用相關的樹的 sha 替換提交。

使用這個較低層級的 api,您必須明確地進行此轉換。

使用 go-github,您可以透過一個額外的查詢來完成此操作:

commit, _, err := client.Git.GetCommit(ctx, owner, repo, latestCommitSHA)
if err != nil {
   return "", err
}

treeSHA := commit.GetTree().GetSHA()

以上是go-github 函式庫中的 Tree SHA is not a tree object 錯誤的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除