我使用以下函数在一个使用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:我EXPLICITLY DO NOT想在提交时创建新文件.