2
2
from changeset import Inventory, apply_changeset, invert_dict
4
from merge3 import Merge3
7
"""Contents-change wrapper around merge3.Merge3"""
8
def __init__(self, base_file, other_file):
9
self.base_file = base_file
10
self.other_file = other_file
12
def __eq__(self, other):
13
if not isinstance(other, ApplyMerge3):
15
return (self.base_file == other.base_file and
16
self.other_file == other.other_file)
18
def __ne__(self, other):
19
return not (self == other)
22
def apply(self, filename, conflict_handler, reverse=False):
23
new_file = filename+".new"
26
other = self.other_file
28
base = self.other_file
29
other = self.base_file
30
m3 = Merge3(file(base, "rb").readlines(),
31
file(filename, "rb").readlines(),
32
file(other, "rb").readlines())
35
output_file = file(new_file, "wb")
36
start_marker = "!START OF MERGE CONFLICT!" + "I HOPE THIS IS UNIQUE"
37
for line in m3.merge_lines(name_a = "TREE", name_b = "MERGE-SOURCE",
38
start_marker=start_marker):
39
if line.startswith(start_marker):
41
output_file.write(line.replace(start_marker, '<<<<<<<<'))
43
output_file.write(line)
46
os.chmod(new_file, os.stat(filename).st_mode)
47
os.rename(new_file, filename)
50
conflict_handler.merge_conflict(new_file, filename, base, other)
5
52
class ThreewayInventory(object):
6
53
def __init__(self, this_inventory, base_inventory, other_inventory):
371
418
conflict_handler = changeset.ExceptionConflictHandler(self.this.dir)
372
419
return make_merge_changeset(self.cset, all_inventory, self.this,
373
420
self.base, self.other, conflict_handler,
374
changeset.Diff3Merge)
376
423
def apply_inv_change(self, inventory_change, orig_inventory):
377
424
orig_inventory_by_path = {}
494
541
builder.cleanup()
496
543
def test_contents_merge(self):
544
"""Test merge3 merging"""
545
self.do_contents_test(ApplyMerge3)
547
def test_contents_merge2(self):
497
548
"""Test diff3 merging"""
549
self.do_contents_test(changeset.Diff3Merge)
551
def do_contents_test(self, merge_factory):
552
"""Test merging with specified ContentsChange factory"""
498
553
builder = MergeBuilder()
499
554
builder.add_file("1", "0", "name1", "text1", 0755)
500
555
builder.change_contents("1", other="text4")
505
560
cset = builder.merge_changeset()
506
561
assert(cset.entries["1"].contents_change is not None)
507
562
assert(isinstance(cset.entries["1"].contents_change,
508
changeset.Diff3Merge))
509
564
assert(isinstance(cset.entries["2"].contents_change,
510
changeset.Diff3Merge))
511
566
assert(cset.entries["3"].is_boring())
512
567
builder.apply_changeset(cset)
513
568
assert(file(builder.this.full_path("1"), "rb").read() == "text4" )