本文整理汇总了Golang中github.com/libgit2/git2go.Repository.MergeCommits方法的典型用法代码示例。如果您正苦于以下问题:Golang Repository.MergeCommits方法的具体用法?Golang Repository.MergeCommits怎么用?Golang Repository.MergeCommits使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/libgit2/git2go.Repository
的用法示例。
在下文中一共展示了Repository.MergeCommits方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CommitToRef
func CommitToRef(r *git.Repository, tree *git.Tree, parent *git.Commit, refname, msg string) (*git.Commit, error) {
// Retry loop in case of conflict
// FIXME: use a custom inter-process lock as a first attempt for performance
var (
needMerge bool
tmpCommit *git.Commit
)
for {
if !needMerge {
// Create simple commit
commit, err := mkCommit(r, refname, msg, tree, parent)
if isGitConcurrencyErr(err) {
needMerge = true
continue
}
return commit, err
} else {
if tmpCommit == nil {
var err error
// Create a temporary intermediary commit, to pass to MergeCommits
// NOTE: this commit will not be part of the final history.
tmpCommit, err = mkCommit(r, "", msg, tree, parent)
if err != nil {
return nil, err
}
defer tmpCommit.Free()
}
// Lookup tip from ref
tip := lookupTip(r, refname)
if tip == nil {
// Ref may have been deleted after previous merge error
needMerge = false
continue
}
// Merge simple commit with the tip
opts, err := git.DefaultMergeOptions()
if err != nil {
return nil, err
}
idx, err := r.MergeCommits(tmpCommit, tip, &opts)
if err != nil {
return nil, err
}
conflicts, err := idx.ConflictIterator()
if err != nil {
return nil, err
}
defer conflicts.Free()
for {
c, err := conflicts.Next()
if isGitIterOver(err) {
break
} else if err != nil {
return nil, err
}
if c.Our != nil {
idx.RemoveConflict(c.Our.Path)
if err := idx.Add(c.Our); err != nil {
return nil, fmt.Errorf("error resolving merge conflict for '%s': %v", c.Our.Path, err)
}
}
}
mergedId, err := idx.WriteTreeTo(r)
if err != nil {
return nil, fmt.Errorf("WriteTree: %v", err)
}
mergedTree, err := lookupTree(r, mergedId)
if err != nil {
return nil, err
}
// Create new commit from merged tree (discarding simple commit)
commit, err := mkCommit(r, refname, msg, mergedTree, parent, tip)
if isGitConcurrencyErr(err) {
// FIXME: enforce a maximum number of retries to avoid infinite loops
continue
}
return commit, err
}
}
return nil, fmt.Errorf("too many failed merge attempts, giving up")
}