
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:我明确不想想要在提交时创建新文件。
采用 php+mysql 数据库方式运行的强大网上商店系统,执行效率高速度快,支持多语言,模板和代码分离,轻松创建属于自己的个性化用户界面 v3.5更新: 1).进一步静态化了活动商品. 2).提供了一些重要UFT-8转换文件 3).修复了除了网银在线支付其它支付显示错误的问题. 4).修改了LOGO广告管理,增加LOGO链接后主页LOGO路径错误的问题 5).修改了公告无法发布的问题,可能是打压
解决方法
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)










