Skip to content

Commit

Permalink
Merge 2fdb4a0 into 5258111
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaibivol committed May 17, 2016
2 parents 5258111 + 2fdb4a0 commit 8a014a2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
18 changes: 11 additions & 7 deletions dictdiffer/unify.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,23 @@ def unify(self, first_patches, second_patches, conflicts):
:param conflicts: list of Conflict objects
"""
self.unified_patches = []
sorted_patches = sorted(first_patches + second_patches, key=get_path)

self._build_index(conflicts)

sorted_patches = sorted(first_patches + second_patches, key=get_path)

for patch in sorted_patches:
conflict = self._index.get(nested_hash(patch))

# Apply only the patches that were taken as part of conflict
# resolution.
if conflict:
if not conflict.handled:
conflict.handled = True
self.unified_patches.append(conflict.take_patch())
else:
self.unified_patches.append(patch)
if conflict.take_patch() != patch:
continue
if conflict.handled:
continue
conflict.handled = True

self.unified_patches.append(patch)

return self.unified_patches

Expand Down
36 changes: 36 additions & 0 deletions tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import unittest

from dictdiffer import patch
from dictdiffer.merge import Merger, UnresolvedConflictsException


Expand Down Expand Up @@ -43,6 +44,41 @@ def take_first(conflict, _, __, ___):
self.assertEqual(m.unified_patches,
[('change', 'changeme', ('Jo', 'Joe'))])

def test_continue_run_multiple_conflicts_per_patch(self):
lca = {'foo': [{'x': [1, 2, 3]}, {'y': [2, 3, 4]}]}
first = {'foo': [{'x': [1, 2, 3]},
{'y': [2, 3, 4]},
{'z': [4, 5, 6]}]}
second = {'bar': 'baz'}
expected = {'foo': [{'x': [1, 2, 3]},
{'y': [2, 3, 4]},
{'z': [4, 5, 6]}],
'bar': 'baz'}

m = Merger(lca, first, second, {})
try:
m.run()
except UnresolvedConflictsException as e:
m.continue_run(['f' for c in e.content])

self.assertEqual(patch(m.unified_patches, lca), expected)

def test_continue_run_multiple_conflicts_per_patch_reverse(self):
lca = {'foo': [{'x': [1, 2, 3]}, {'y': [2, 3, 4]}]}
first = {'foo': [{'x': [1, 2, 3]},
{'y': [2, 3, 4]},
{'z': [4, 5, 6]}]}
second = {'bar': 'baz'}
expected = {'bar': 'baz'}

m = Merger(lca, first, second, {})
try:
m.run()
except UnresolvedConflictsException as e:
m.continue_run(['s' for c in e.content])

self.assertEqual(patch(m.unified_patches, lca), expected)


if __name__ == '__main__':
unittest.main()

0 comments on commit 8a014a2

Please sign in to comment.