英文:
Merge current branch with upstream (or any other ref) in git2go
问题
我正在尝试在git2go中复制以下命令的结果:
git merge -X theirs --no-ff -m "Commit msg"
我能够成功使用remote.Fetch(nil, nil, "")
获取上游远程内容,但我无法执行实际的合并操作。以下是我用于合并的代码:
// 获取要合并的上游引用的注释提交
head, _ := repo.Head()
upstream, _ := head.Branch().Upstream()
annotatedCommit, _ := repo.AnnotatedCommitFromRef(upstream)
// 准备合并和检出选项
mergeOpts, _ := git.DefaultMergeOptions()
mergeOpts.FileFavor = git.MergeFileFavorTheirs
checkoutOpts := git.CheckoutOpts{
Strategy: git.CheckoutUseTheirs,
}
// 执行合并
err := repo.Merge([]*git.AnnotatedCommit{annotatedCommit}, &mergeOpts, &checkoutOpts)
// 这里没有错误
我理解在此之后我需要检查索引是否存在冲突,并使用两个父提交进行实际提交,但是我目前在这里卡住了,因为`Merge()`似乎没有执行任何操作(没有合并,没有检出)。
请注意,这只是代码的翻译部分,不包括任何其他内容。
英文:
I'm trying to replicate the outcome of the following command in git2go:
git merge -X theirs --no-ff -m "Commit msg" <commit>
I'm able to successfully fetch the upstream remote using remote.Fetch(nil, nil, "")
, but I cannot do the actual merge. This is the code I'm using for merging:
<!-- language-all:go -->
// get the upstream ref's annotated commit to merge in
head, _ := repo.Head()
upstream, _ := head.Branch().Upstream()
annotatedCommit, _ := repo.AnnotatedCommitFromRef(upstream)
// prepare merge and checkout options
mergeOpts, _ := git.DefaultMergeOptions()
mergeOpts.FileFavor = git.MergeFileFavorTheirs
checkoutOpts := git.CheckoutOpts{
Strategy: git.CheckoutUseTheirs,
}
// do the merge
err := repo.Merge([]*git.AnnotatedCommit{annotatedCommit}, &mergeOpts, &checkoutOpts)
// no error here
I understand that after this I need to check the index for any conflicts, and actually do the commit with the two parents, however I'm stuck here at the moment because the Merge()
doesn't appear to be doing anything (no merge, no checkout).
答案1
得分: 1
默认情况下,检出操作会执行干运行,所以为了能够成功合并,我必须使用类似以下的代码:
checkoutOpts := git.CheckoutOpts{
Strategy: git.CheckoutSafe | git.CheckoutRecreateMissing | git.CheckoutAllowConflicts | git.CheckoutUseTheirs,
}
唯一可用的文档在checkout.go源代码中。
英文:
Checkout is performing a dry run by default, so to be able to successfully merge I had to use something like this:
<!-- language-all: go -->
checkoutOpts := git.CheckoutOpts{
Strategy: git.CheckoutSafe | git.CheckoutRecreateMissing | git.CheckoutAllowConflicts | git.CheckoutUseTheirs,
}
The only documentation available is in the checkout.go source.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论