将当前分支与上游分支(或任何其他引用)在git2go中合并。

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

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&#39;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}, &amp;mergeOpts, &amp;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.

huangapple
  • 本文由 发表于 2015年7月8日 02:39:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/31276740.html
匿名

发表评论

匿名网友

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

确定