Home > Backend Development > Golang > Tree SHA is not a tree object error in go-github library

Tree SHA is not a tree object error in go-github library

王林
Release: 2024-02-09 14:00:11
forward
503 people have browsed it

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

When using the go-github library, you may encounter an error called "Tree SHA is not a tree object". This error occurs because the SHA value you supplied is not a valid tree object. Typically, this error can be caused by a file or directory that doesn't exist, a wrong SHA value, or some other issue. To resolve this issue, you can check that the SHA value you provided is correct and ensure that the relevant file or directory exists. If the problem persists, you can check the go-github repository's documentation or ask for help for further solutions. PHP editor Yuzi hopes this short guide will be helpful to you!

Question content

I'm trying to create an empty commit in github using go-github.

The following code:

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
}
Copy after login

fail

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

and

422 Tree SHA is not a tree object []
Copy after login

I can't find any relevant information about this error anywhere.

Any ideas?

Workaround

When you use git cli, git itself will run a "meaningful" translation - for example: replace the commit with the sha of the relevant tree.

Using this lower-level API, you must do this conversion explicitly.

Using go-github, you can do this with an additional query:

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

treeSHA := commit.GetTree().GetSHA()
Copy after login

The above is the detailed content of Tree SHA is not a tree object error in go-github library. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template