~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

(andrew) Replace several fragile try/finally blocks in merge.py using
        bzrlib.cleanup. (#517275)

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
    ui,
37
37
    versionedfile
38
38
    )
 
39
from bzrlib.cleanup import OperationWithCleanups
39
40
from bzrlib.symbol_versioning import (
40
41
    deprecated_in,
41
42
    deprecated_method,
45
46
 
46
47
def transform_tree(from_tree, to_tree, interesting_ids=None):
47
48
    from_tree.lock_tree_write()
48
 
    try:
49
 
        merge_inner(from_tree.branch, to_tree, from_tree, ignore_zero=True,
50
 
                    interesting_ids=interesting_ids, this_tree=from_tree)
51
 
    finally:
52
 
        from_tree.unlock()
 
49
    operation = OperationWithCleanups(merge_inner)
 
50
    operation.add_cleanup(from_tree.unlock)
 
51
    operation.run_simple(from_tree.branch, to_tree, from_tree,
 
52
        ignore_zero=True, interesting_ids=interesting_ids, this_tree=from_tree)
53
53
 
54
54
 
55
55
class MergeHooks(hooks.Hooks):
455
455
    def _add_parent(self):
456
456
        new_parents = self.this_tree.get_parent_ids() + [self.other_rev_id]
457
457
        new_parent_trees = []
 
458
        operation = OperationWithCleanups(self.this_tree.set_parent_trees)
458
459
        for revision_id in new_parents:
459
460
            try:
460
461
                tree = self.revision_tree(revision_id)
462
463
                tree = None
463
464
            else:
464
465
                tree.lock_read()
 
466
                operation.add_cleanup(tree.unlock)
465
467
            new_parent_trees.append((revision_id, tree))
466
 
        try:
467
 
            self.this_tree.set_parent_trees(new_parent_trees,
468
 
                                            allow_leftmost_as_ghost=True)
469
 
        finally:
470
 
            for _revision_id, tree in new_parent_trees:
471
 
                if tree is not None:
472
 
                    tree.unlock()
 
468
        operation.run_simple(new_parent_trees, allow_leftmost_as_ghost=True)
473
469
 
474
470
    def set_other(self, other_revision, possible_transports=None):
475
471
        """Set the revision and tree to merge from.
626
622
                               change_reporter=self.change_reporter,
627
623
                               **kwargs)
628
624
 
629
 
    def _do_merge_to(self, merge):
 
625
    def _do_merge_to(self):
 
626
        merge = self.make_merger()
630
627
        if self.other_branch is not None:
631
628
            self.other_branch.update_references(self.this_branch)
632
629
        merge.do_merge()
646
643
                    sub_tree.branch.repository.revision_tree(base_revision)
647
644
                sub_merge.base_rev_id = base_revision
648
645
                sub_merge.do_merge()
 
646
        return merge
649
647
 
650
648
    def do_merge(self):
 
649
        operation = OperationWithCleanups(self._do_merge_to)
651
650
        self.this_tree.lock_tree_write()
652
 
        try:
653
 
            if self.base_tree is not None:
654
 
                self.base_tree.lock_read()
655
 
            try:
656
 
                if self.other_tree is not None:
657
 
                    self.other_tree.lock_read()
658
 
                try:
659
 
                    merge = self.make_merger()
660
 
                    self._do_merge_to(merge)
661
 
                finally:
662
 
                    if self.other_tree is not None:
663
 
                        self.other_tree.unlock()
664
 
            finally:
665
 
                if self.base_tree is not None:
666
 
                    self.base_tree.unlock()
667
 
        finally:
668
 
            self.this_tree.unlock()
 
651
        operation.add_cleanup(self.this_tree.unlock)
 
652
        if self.base_tree is not None:
 
653
            self.base_tree.lock_read()
 
654
            operation.add_cleanup(self.base_tree.unlock)
 
655
        if self.other_tree is not None:
 
656
            self.other_tree.lock_read()
 
657
            operation.add_cleanup(self.other_tree.unlock)
 
658
        merge = operation.run_simple()
669
659
        if len(merge.cooked_conflicts) == 0:
670
660
            if not self.ignore_zero and not trace.is_quiet():
671
661
                trace.note("All changes applied successfully.")
765
755
            self.do_merge()
766
756
 
767
757
    def do_merge(self):
 
758
        operation = OperationWithCleanups(self._do_merge)
 
759
        operation.add_cleanup(self.pb.clear)
768
760
        self.this_tree.lock_tree_write()
 
761
        operation.add_cleanup(self.this_tree.unlock)
769
762
        self.base_tree.lock_read()
 
763
        operation.add_cleanup(self.base_tree.unlock)
770
764
        self.other_tree.lock_read()
 
765
        operation.add_cleanup(self.other_tree.unlock)
 
766
        operation.run()
 
767
 
 
768
    def _do_merge(self, operation):
 
769
        self.tt = transform.TreeTransform(self.this_tree, self.pb)
 
770
        operation.add_cleanup(self.tt.finalize)
 
771
        self.pp.next_phase()
 
772
        self._compute_transform()
 
773
        self.pp.next_phase()
 
774
        results = self.tt.apply(no_conflicts=True)
 
775
        self.write_modified(results)
771
776
        try:
772
 
            self.tt = transform.TreeTransform(self.this_tree, self.pb)
773
 
            try:
774
 
                self.pp.next_phase()
775
 
                self._compute_transform()
776
 
                self.pp.next_phase()
777
 
                results = self.tt.apply(no_conflicts=True)
778
 
                self.write_modified(results)
779
 
                try:
780
 
                    self.this_tree.add_conflicts(self.cooked_conflicts)
781
 
                except errors.UnsupportedOperation:
782
 
                    pass
783
 
            finally:
784
 
                self.tt.finalize()
785
 
        finally:
786
 
            self.other_tree.unlock()
787
 
            self.base_tree.unlock()
788
 
            self.this_tree.unlock()
789
 
            self.pb.clear()
 
777
            self.this_tree.add_conflicts(self.cooked_conflicts)
 
778
        except errors.UnsupportedOperation:
 
779
            pass
790
780
 
791
781
    def make_preview_transform(self):
 
782
        operation = OperationWithCleanups(self._make_preview_transform)
 
783
        operation.add_cleanup(self.pb.clear)
792
784
        self.base_tree.lock_read()
 
785
        operation.add_cleanup(self.base_tree.unlock)
793
786
        self.other_tree.lock_read()
 
787
        operation.add_cleanup(self.other_tree.unlock)
 
788
        return operation.run_simple()
 
789
 
 
790
    def _make_preview_transform(self):
794
791
        self.tt = transform.TransformPreview(self.this_tree)
795
 
        try:
796
 
            self.pp.next_phase()
797
 
            self._compute_transform()
798
 
            self.pp.next_phase()
799
 
        finally:
800
 
            self.other_tree.unlock()
801
 
            self.base_tree.unlock()
802
 
            self.pb.clear()
 
792
        self.pp.next_phase()
 
793
        self._compute_transform()
 
794
        self.pp.next_phase()
803
795
        return self.tt
804
796
 
805
797
    def _compute_transform(self):