Skip to content

Commit

Permalink
Use the diff3 merge function in the merge command
Browse files Browse the repository at this point in the history
Having introduced the `Merge::Diff3` module in the previous commit, we
now use it in `Merge::Resolve` to merge the blobs of conflicting edits
to the same file, replacing the simplistic method of concatenating the
two versions and letting the user figure it out.
  • Loading branch information
jcoglan committed Jul 27, 2018
1 parent 205f040 commit f9719f0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
23 changes: 9 additions & 14 deletions lib/merge/resolve.rb
@@ -1,3 +1,5 @@
require_relative "./diff3"

module Merge
class Resolve

Expand Down Expand Up @@ -69,22 +71,15 @@ def merge_blobs(base_oid, left_oid, right_oid)
result = merge3(base_oid, left_oid, right_oid)
return result if result

blob = Database::Blob.new(merged_data(left_oid, right_oid))
@repo.database.store(blob)
[false, blob.oid]
end
oids = [base_oid, left_oid, right_oid]
blobs = oids.map { |oid| oid ? @repo.database.load(oid).data : "" }
merge = Diff3.merge(*blobs)

def merged_data(left_oid, right_oid)
left_blob = @repo.database.load(left_oid)
right_blob = @repo.database.load(right_oid)
data = merge.to_s(@inputs.left_name, @inputs.right_name)
blob = Database::Blob.new(data)
@repo.database.store(blob)

[
"<<<<<<< #{ @inputs.left_name }\n",
left_blob.data,
"=======\n",
right_blob.data,
">>>>>>> #{ @inputs.right_name }\n"
].join("")
[merge.clean?, blob.oid]
end

def merge_modes(base_mode, left_mode, right_mode)
Expand Down
17 changes: 17 additions & 0 deletions test/command/merge_test.rb
Expand Up @@ -187,6 +187,23 @@ def assert_index(*entries)
end
end

describe "unconflicted merge: in-file merge possible" do
before do
merge3(
{ "f.txt" => "1\n2\n3\n" },
{ "f.txt" => "4\n2\n3\n" },
{ "f.txt" => "1\n2\n5\n" })
end

it "puts the combined changes in the workspace" do
assert_workspace "f.txt" => "4\n2\n5\n"
end

it "creates a clean merge" do
assert_clean_merge
end
end

describe "unconflicted merge: edit and mode-change" do
before do
merge3(
Expand Down

0 comments on commit f9719f0

Please sign in to comment.