dongwei9771 2016-05-06 12:35
浏览 63
已采纳

Git2go:如何处理简单的合并冲突

For a simple live coding environment I'm writing a git add, commit and push function, which is working fine, except of the case of conflicts, when two users code in the same part of a file. It performs the conflict analysis correctly and loads the result:

// Live-Editor, Processing / Javascript
fill(130, 52, 130);
<<<<<<< HEAD

textSize(130);

textSize(120);
>>>>>>> master
text( "

  • 写回答

1条回答 默认 最新

  • dpgkg42484 2016-05-10 05:53
    关注

    I solved the problem. Collision detection worked fine. In case of a not automatically resolvable conflict, the control was given back to the user. He made changes, and those changes weren'T committed before the conflict analysis happened again.

    Here is the complete code (the section after "Merge commit (in case of -- now hopefully resolved -- conflicts) is crucial."):

    //////////////////////////////////////////////////////////
    // GitAddCommitPush
    func GitAddCommitPush(userName string, dir string, message string, firstCommit bool) error {
    
        ///////////////////////////////////////////////////////////////////////
        // Add
        //
        // 1 Open repository
        repo, err := git.OpenRepository(dir)
        if err != nil {
            beego.Error("OpenRepository - ", err)
        }
    
        // 2 Retrieve index
        index, err := repo.Index()
        if err != nil {
            beego.Error("Index - ", err)
        }
    
        // 3 Remember if we had conflicts before we added everything to the index
        indexHadConflicts := index.HasConflicts()
    
        // 4 Add everything to the index
        err = index.AddAll([]string{}, git.IndexAddDefault, nil)
        if err != nil {
            beego.Error("AddAll - ", err)
        }
    
        // 5 Write the index (so git knows about it)
        err = index.Write()
        if err != nil {
            beego.Error("Write - ", err)
        }
    
        // 6 Write the current index tree to the repo
        treeId, err := index.WriteTreeTo(repo)
        if err != nil {
            beego.Error("WriteTreeTo - ", err)
        }
    
        /////////////////////////////////////////////////////////////////////////////////////////////
        // Commit
        //
        // 1 Retrieve the tree we just wrote (git's reference of it that it made in the last step)
        tree, err := repo.LookupTree(treeId)
        if err != nil {
            beego.Error("LookupTree - ", err)
        }
    
        // 2 Create a signature
        sig := &git.Signature{
            Name:  userName,
            Email: userName + "@" + beego.AppConfig.String("userdata::emailserver"),
            When:  time.Now(),
        }
    
        // 3 Get remote now (as we need it for both, fetch and later push )
        remote, err := repo.LookupRemote("origin")
        if err != nil {
            remote, err = repo.CreateRemote("origin", repo.Path())
            if err != nil {
                beego.Error("CreateRemote - ", err)
            }
        }
    
        // 4 Read the remote branch
        remoteBranch, err := repo.LookupReference("refs/remotes/origin/master")
        if err != nil {
            beego.Error("Fetch 2 - ", err)
        }
    
        // 5 Determine if this is a first commit ...
        if firstCommit == true {
    
            // 5a ... then create a new one
            _, err = repo.CreateCommit("HEAD", sig, sig, message, tree)
    
        } else {
    
            // 5b ... or retrieve current head
            currentBranch, err := repo.Head()
            if err != nil {
                beego.Error("Head - ", err)
            }
    
            // 6 Retrieve current commit
            currentTip, err := repo.LookupCommit(currentBranch.Target())
            if err != nil {
                beego.Error("LookupCommit - ", err)
            }
    
            // 7 Create a new one on top
            currentCommit, err := repo.CreateCommit("HEAD", sig, sig, message, tree, currentTip)
            if err != nil {
                beego.Error("CreateCommit - ", err)
            }
    
            ////////////////////////////////////////////////////////////////////////////////////
            // Merge commit (in case of -- now hopefully resolved -- conflicts)
            //
            // 1 If there were conflicts, do the merge commit
            if indexHadConflicts == true {
    
                // 2 Retrieve the local commit
                localCommit, err := repo.LookupCommit(currentCommit)
                if err != nil {
                    beego.Error("Fetch 11 - ", err)
                }
    
                // 3 Retrieve the remote commit
                remoteCommit, err := repo.LookupCommit(remoteBranch.Target())
                if err != nil {
                    beego.Error("Fetch 12 - ", err)
                }
    
                // 4 Create a new one
                repo.CreateCommit("HEAD", sig, sig, "Merge commit", tree, localCommit, remoteCommit)
    
                // 5 Clean up
                repo.StateCleanup()
            }
    
            ///////////////////////////////////////////////////////////////////////////////////
            // Pull (Fetch and Commit)
            //
            // 1 Fetch it (pull without commit)
            err = remote.Fetch([]string{}, nil, "")
            if err != nil {
                beego.Error("Fetch 1 - ", err)
            }
    
            // 2 Perform an annotated commit
            annotatedCommit, err := repo.AnnotatedCommitFromRef(remoteBranch)
            if err != nil {
                beego.Error("Fetch 3 - ", err)
            }
    
            // 3 Do the merge analysis
            mergeHeads := make([]*git.AnnotatedCommit, 1)
            mergeHeads[0] = annotatedCommit
            analysis, _, err := repo.MergeAnalysis(mergeHeads)
            if err != nil {
                beego.Error("Fetch 4 - ", err)
            }
    
            // 4 Check if something happend
            if analysis&git.MergeAnalysisUpToDate == 0 && analysis&git.MergeAnalysisNormal != 0 {
    
                // 5 Yes! First just merge changes
                if err := repo.Merge([]*git.AnnotatedCommit{annotatedCommit}, nil, nil); err != nil {
                    beego.Error("Fetch 5 - ", err)
                }
    
                // 6 Retrieve the index after that treatment
                index, err := repo.Index()
                if err != nil {
                    beego.Error("Fetch 6 - ", err)
                }
    
                // 7 Check for conflicts
                if index.HasConflicts() {
    
                    // 7a There are not automaticly solvable conflicts ... give them back to the user
                    beego.Trace("Conflicts! Write new index and return.", index)
                    err = index.Write()
                    if err != nil {
                        beego.Error("Write - ", err)
                    }
    
                    return errors.New("Conflicts")
                }
    
                // 8 Write the new tree
                treeId, err := index.WriteTree()
                if err != nil {
                    beego.Error("Fetch 9 - ", err)
                }
    
                // 9 Retrieve the new tree
                tree, err := repo.LookupTree(treeId)
                if err != nil {
                    beego.Error("Fetch 10 - ", err)
                }
    
                // 10 Retrieve the local commit
                localCommit, err := repo.LookupCommit(currentCommit)
                if err != nil {
                    beego.Error("Fetch 11 - ", err)
                }
    
                // 11 Retrieve the remote commit
                remoteCommit, err := repo.LookupCommit(remoteBranch.Target())
                if err != nil {
                    beego.Error("Fetch 12 - ", err)
                }
    
                // 12 Create a new one
                repo.CreateCommit("HEAD", sig, sig, "Merge commit", tree, localCommit, remoteCommit)
    
                // 13 Clean up
                repo.StateCleanup()
            }
        }
    
        //////////////////////////////////////////////////////////////////////////////////////////////////////
        // Push
        err = remote.Push([]string{"refs/heads/master"}, nil, sig, message)
        if err != nil {
            beego.Error("Push - ", err)
        }
    
        return err
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作