使用go-github创建提交失败,返回422错误 – 更新不是快进式提交。

huangapple go评论123阅读模式
英文:

Failing to create a commit using go-github with 422 - Update is not a fast forward

问题

我正在使用以下函数来使用go-github库在分支中简单地创建一个新的提交:

func GHAPICreateCommit(ctx context.Context, client *github.Client, commitOpts *CommitOptions) error {
	// 获取分支的引用
	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)

	// 使用更新后的提交消息创建一个新的提交
	newCommit, _, err := client.Git.CreateCommit(ctx, repoOwner, commitOpts.Repo, commit)
	if err != nil {
		return err
	}
	// 将新的提交附加到引用上
	ref.Object.SHA = newCommit.SHA

	// 更新分支引用以指向新的提交
	_, _, 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 []

为什么不是快进(fast-forward)呢?它只是从现有的分支/提交创建了一个新的提交。

PS:我明确地不想在我的提交中创建新文件。

英文:

I am using the following function to simply create a new commit in a branch using go-github library

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
}

This fails with:

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 []

Why isn't a fast-forward? It is simply a new commit created from an existing branch/commit.

PS: I EXPLICITLY DO NOT want to create new files on my commit.

答案1

得分: 2

func (s *GitService) CreateCommit(ctx context.Context, owner string, repo string, commit *Commit) (*Commit, *Response, error)

参数commit用于指定新提交的一些信息,包括新提交的parents(参见实现)。

在你的代码中,新提交和旧提交具有相同的parents,因此这不是一个快进推送到分支。为了使其成为分支的快进推送,新提交的parents应该指向旧提交。

我猜以下更改将使其成为快进:

+ commit.Parents = []*github.Commit{commit}
  newCommit, _, err := client.Git.CreateCommit(ctx, repoOwner, commitOpts.Repo, commit)
英文:
func (s *GitService) CreateCommit(ctx context.Context, owner string, repo string, commit *Commit) (*Commit, *Response, error)

The parameter commit is used to specify some of the information of the new commit, including the parents of the new commit (see the implementation).

In your code, the new commit and the old commit has the same parents, so this is not a fast-forward push to the branch. To make it a fast-forward push to the branch, the parents of the new commit should point to the old commit.

I guess the following change would make it a fast-forward:

+ commit.Parents = []*github.Commit{commit}
  newCommit, _, err := client.Git.CreateCommit(ctx, repoOwner, commitOpts.Repo, commit)

huangapple
  • 本文由 发表于 2023年4月30日 19:29:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76141008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定