0

I'm working on a Ruby script that needs to do some merging. What I'd like to do is, given a target reference and a commit to merge attempt to merge them, and if there are merge conflicts give control back to the user to resolve them, basically exactly like git merge does, so the user can deal with conflicts, then call git my-merge --continue and continue where we left off.

What I have so far is this:

merge_index = @repo.merge_commits(@commit, target_tip, options)
unless merge_index.conflicts?
options = {
    :committer => @commit.committer,
    :author => @commit.author,
    :parents => [target_tip, @commit],
    :message => merge_message,
    :update_ref => @target.canonical_name,
    :tree => merge_index.write_tree(@repo)
}

commit = Rugged::Commit.create(@repo, options)
else
    # Here's where my unwritten code goes 
end

Here comes the question: Given the merge_index that I have with some conflicts, how do I get my working directory into the state represented by it so I can return to shell and let the user resolve them? It seems I cannot write it out (it will complain that it cannot write out an index that isn't fully merged) and I don't see an obvious way to set my current index to it.

Anton
  • 27
  • 4

1 Answers1

0

The merge index is the result of the merge. If you want that to be the status of the repository, you need to write that index to be the index of the repository, like with

repo.index = merge_index
merge_index.write()

you can then use checkout as usual, which will create the conflict markers on the files in the worktree.

libgit2 itself has git_merge() does does this for you; you might want to look into making that available through rugged if you do not want to perform the steps in your code.

Carlos Martín Nieto
  • 5,207
  • 1
  • 15
  • 16
  • When I try `repo.index = merge_index` I get RuntimeError: The given object is already owned by another repository – Anton Mar 06 '15 at 19:27
  • The index you get back from `git_merge_commits()` is not owned by a repository, so that would indicate a bug at some layer. – Carlos Martín Nieto Mar 08 '15 at 14:48
  • Ok, that would explain why I'm having trouble :). I created a very simple repo and wrote a very simple Ruby script to test this and it most certainly fails with "object is already owned" – Anton Mar 09 '15 at 19:48