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

推荐答案

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

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

在您的代码中,新提交和旧提交具有相同的parents,因此这不是对分支的快进推送.要使其成为对分支的快进推送,新提交的parents应该指向旧提交.

我猜下面的变化会让它变得快进:

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

Go相关问答推荐

如何使用Docker Compose配置Go,使main. go文件位于/CMD文件夹中

具有GRPC的RBAC(基于角色的访问控制)-网关生成的REST风格的API

golang 的通用 map 功能

错误.如果它包含切片,则返回FALSE

有没有办法通过Go cmdline或IDE(IntelliJ)找出我的 struct 实现了什么接口?

在nixos上找不到XInput2.h头文件的包

如何模拟go的Elastic search SDK?

如何使用 AWS sdk 在 Go 中正确解组 PartiQL 查询的结果?

Docker 执行失败并显示cmd/ENTRYPOINT 中的命令未找到

Golang校验器包:重命名字段错误处理

如何在 Go 中将 int 转换为包含 complex128 的泛型类型?

为什么不同的 Wireguard 私钥会产生相同的公钥?

go version -m 输出中的箭头符号=>是什么意思?

在 Golang 模板中计算时间/持续时间

查找、解析和验证邮箱地址

Golang invopop jsonschema 使用 if/then/else

NaN 是 golang 中的可比类型吗?

在我的情况下,如何以正确的方式测试方法?

Go 使用 struct 作为接口而不实现所有方法

Golang - 使用正则表达式提取链接