Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conflict reason does not match the result of the "git merge" command. #850

Open
laujinkai opened this issue Nov 4, 2021 · 0 comments
Open

Comments

@laujinkai
Copy link

laujinkai commented Nov 4, 2021

I am trying to use git2go to write an utility to do automatic merge, in the process of development I encountered a strange phenomenon and do not know how to solve it.

One of my projects will get different conflict reasons than "git merge --no-ff ..." if I merge it with git2go.

The output of "git status" after running "git merge --no-ff ..." shows some unmerged files are "both added", but those same files
are shown as "modified" and merged if I run "git status" after merging branch using git2go.

Below is the code snippet I used to do the merge:

func Merge(projectPath, target, message  string) (MergeStatus, error) {
	repo, err := git.OpenRepository(projectPath)
	if err != nil {
		panic(err)
	}
	defer repo.Free()

	commit, err := resolveRefish(repo, target)
	if err != nil {
		panic(err)
	}
	defer commit.Free()

	analysis, _, err := repo.MergeAnalysis([]*git.AnnotatedCommit{commit})
	if err != nil {
		panic(err)
	}

	switch analysis {
	case git.MergeAnalysisNormal, git.MergeAnalysisFastForward:
		break // can merge

	case git.MergeAnalysisUpToDate:
		return AlreadyUpToDate, nil

	case git.MergeAnalysisNone:
		return NothingToMerge, nil

	default:
		panic(fmt.Sprintf("unhandled analysis: %v", analysis))
	}

	mergeOpts := git.MergeOptions{
		TreeFlags: git.MergeTreeFindRenames,
	}
	checkoutOpts := git.CheckoutOptions{
		Strategy: git.CheckoutForce,
	}
	err = repo.Merge([]*git.AnnotatedCommit{commit}, &mergeOpts, &checkoutOpts)
	if err != nil {
		panic(err)
	}

	index, err := repo.Index()
	if err != nil {
		panic(err)
	}
	defer index.Free()

	if index.HasConflicts() {
		return Conflicted, nil
	}

	createMergeCommit(repo, target, message)

	return Merged, nil
}

Is there something wrong with my code that causes this phenomenon?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant