0

I am 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
}

Fails in

newCommitResponse, _, err := client.Git.CreateCommit(ctx, owner, repo, newCommit)

with

422 Tree SHA is not a tree object []

I cannot find any relevant info anywhere about this error.

Any ideas?

pkaramol
  • 16,451
  • 43
  • 149
  • 324
  • Sounds like you are providing a non-tree as the tree to the new commit. – eftshift0 Apr 05 '23 at 12:24
  • Yes but why is that? Can't I construct a tree based on the sha that I am providing? – pkaramol Apr 05 '23 at 12:25
  • I don't know that API _but_ a commit _needs_ a tree.... and the tree _has_ to be a valid tree. Check `git help commit-tree` – eftshift0 Apr 05 '23 at 12:47
  • 1
    When you go through `git` cli, `git` itself runs the translations that "make sense" -- e.g: replace a commit with the sha of its tree where relevant. Using this lower level API, you will have to do something similar by hand: check the function that allows to get the type for a sha, and the details for a commit, and extract the tree sha from a commit sha. – LeGEC Apr 05 '23 at 13:49

1 Answers1

2

When you go through git cli, git itself runs the translations that "make sense" -- e.g: replace a commit with the sha of its tree where relevant.


Using this lower level API, you have to do this translation explicitly.

Using go-github, you can do this with one extra query:

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

treeSHA := commit.GetTree().GetSHA()
LeGEC
  • 46,477
  • 5
  • 57
  • 104