首页 > 后端开发 > Golang > 正文

无法使用 go-github 和 422 创建提交 - 更新不是快进

PHPz
发布: 2024-02-09 17:12:19
转载
731 人浏览过

无法使用 go-github 和 422 创建提交 - 更新不是快进

php小编小新在使用go-github时,遇到了一个问题:在创建提交时,出现了422错误,并且提示“更新不是快进”。这个问题的具体原因是什么呢?该如何解决呢?接下来,我们将为大家详细解答。

问题内容

我使用以下函数简单地使用 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:我明确不想想要在提交时创建新文件。

解决方法

func (s *gitservice) createcommit(ctx context.context, owner string, repo string, commit *commit) (*commit, *response, error)
登录后复制

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

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

我想以下更改会使其快进:

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

以上是无法使用 go-github 和 422 创建提交 - 更新不是快进的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:stackoverflow.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!