~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge_core.py

  • Committer: Aaron Bentley
  • Date: 2005-07-26 17:44:20 UTC
  • mto: (1092.1.41) (1185.3.4) (974.1.47)
  • mto: This revision was merged to the branch mainline in revision 1020.
  • Revision ID: abentley@panoramicfeedback.com-20050726174420-ff08fb945ea9cf6b
Implemented merge3 as the default text merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import changeset
2
2
from changeset import Inventory, apply_changeset, invert_dict
3
3
import os.path
 
4
from merge3 import Merge3
 
5
 
 
6
class ApplyMerge3:
 
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
 
11
 
 
12
    def __eq__(self, other):
 
13
        if not isinstance(other, ApplyMerge3):
 
14
            return False
 
15
        return (self.base_file == other.base_file and 
 
16
                self.other_file == other.other_file)
 
17
 
 
18
    def __ne__(self, other):
 
19
        return not (self == other)
 
20
 
 
21
 
 
22
    def apply(self, filename, conflict_handler, reverse=False):
 
23
        new_file = filename+".new" 
 
24
        if not reverse:
 
25
            base = self.base_file
 
26
            other = self.other_file
 
27
        else:
 
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())
 
33
 
 
34
        new_conflicts = False
 
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):
 
40
                new_conflicts = True
 
41
                output_file.write(line.replace(start_marker, '<<<<<<<<'))
 
42
            else:
 
43
                output_file.write(line)
 
44
        output_file.close()
 
45
        if not new_conflicts:
 
46
            os.chmod(new_file, os.stat(filename).st_mode)
 
47
            os.rename(new_file, filename)
 
48
            return
 
49
        else:
 
50
            conflict_handler.merge_conflict(new_file, filename, base, other)
4
51
 
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)
 
421
                                    ApplyMerge3)
375
422
 
376
423
    def apply_inv_change(self, inventory_change, orig_inventory):
377
424
        orig_inventory_by_path = {}
494
541
        builder.cleanup()
495
542
 
496
543
    def test_contents_merge(self):
 
544
        """Test merge3 merging"""
 
545
        self.do_contents_test(ApplyMerge3)
 
546
 
 
547
    def test_contents_merge2(self):
497
548
        """Test diff3 merging"""
 
549
        self.do_contents_test(changeset.Diff3Merge)
 
550
 
 
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))
 
563
                          ApplyMerge3))
509
564
        assert(isinstance(cset.entries["2"].contents_change,
510
 
                          changeset.Diff3Merge))
 
565
                          ApplyMerge3))
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" )