~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

  • Committer: John Arbash Meinel
  • Date: 2006-10-11 00:23:23 UTC
  • mfrom: (2070 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2071.
  • Revision ID: john@arbash-meinel.com-20061011002323-82ba88c293d7caff
[merge] bzr.dev 2070

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
from bzrlib import (
23
23
    osutils,
24
 
    registry,
25
24
    )
26
25
from bzrlib.branch import Branch
27
26
from bzrlib.conflicts import ConflictList, Conflict
39
38
                           BinaryFile,
40
39
                           )
41
40
from bzrlib.merge3 import Merge3
 
41
import bzrlib.osutils
42
42
from bzrlib.osutils import rename, pathjoin
43
43
from progress import DummyProgress, ProgressPhase
44
44
from bzrlib.revision import common_ancestor, is_ancestor, NULL_REVISION
45
45
from bzrlib.textfile import check_text_lines
46
46
from bzrlib.trace import mutter, warning, note
47
47
from bzrlib.transform import (TreeTransform, resolve_conflicts, cook_conflicts,
48
 
                              FinalPaths, create_by_entry, unique_add,
49
 
                              ROOT_PARENT)
 
48
                              FinalPaths, create_by_entry, unique_add)
50
49
from bzrlib.versionedfile import WeaveMerge
51
50
from bzrlib import ui
52
51
 
87
86
 
88
87
 
89
88
class Merger(object):
90
 
    def __init__(self, this_branch, other_tree=None, base_tree=None,
91
 
                 this_tree=None, pb=DummyProgress(), change_reporter=None,
92
 
                 recurse='down'):
 
89
    def __init__(self, this_branch, other_tree=None, base_tree=None, 
 
90
                 this_tree=None, pb=DummyProgress()):
93
91
        object.__init__(self)
94
92
        assert this_tree is not None, "this_tree is required"
95
93
        self.this_branch = this_branch
99
97
        self.this_revision_tree = None
100
98
        self.this_basis_tree = None
101
99
        self.other_tree = other_tree
102
 
        self.other_branch = None
103
100
        self.base_tree = base_tree
104
101
        self.ignore_zero = False
105
102
        self.backup_files = False
106
103
        self.interesting_ids = None
107
104
        self.show_base = False
108
105
        self.reprocess = False
109
 
        self._pb = pb
 
106
        self._pb = pb 
110
107
        self.pp = None
111
 
        self.recurse = recurse
112
 
        self.change_reporter = change_reporter
 
108
 
113
109
 
114
110
    def revision_tree(self, revision_id):
115
111
        return self.this_branch.repository.revision_tree(revision_id)
145
141
 
146
142
    def check_basis(self, check_clean, require_commits=True):
147
143
        if self.this_basis is None and require_commits is True:
148
 
            raise BzrCommandError("This branch has no commits."
149
 
                                  " (perhaps you would prefer 'bzr pull')")
 
144
            raise BzrCommandError("This branch has no commits")
150
145
        if check_clean:
151
146
            self.compare_basis()
152
147
            if self.this_basis != self.this_rev_id:
173
168
        interesting_ids = set()
174
169
        for path in file_list:
175
170
            found_id = False
176
 
            # TODO: jam 20070226 The trees are not locked at this time,
177
 
            #       wouldn't it make merge faster if it locks everything in the
178
 
            #       beginning? It locks at do_merge time, but this happens
179
 
            #       before that.
180
171
            for tree in (self.this_tree, self.base_tree, self.other_tree):
181
 
                file_id = tree.path2id(path)
 
172
                file_id = tree.inventory.path2id(path)
182
173
                if file_id is not None:
183
174
                    interesting_ids.add(file_id)
184
175
                    found_id = True
203
194
 
204
195
        :param other_revision: The [path, revision] list to merge from.
205
196
        """
206
 
        self.other_branch, self.other_tree = _get_tree(other_revision,
 
197
        other_branch, self.other_tree = _get_tree(other_revision,
207
198
                                                  self.this_branch)
208
199
        if other_revision[1] == -1:
209
 
            self.other_rev_id = self.other_branch.last_revision()
 
200
            self.other_rev_id = other_branch.last_revision()
210
201
            if self.other_rev_id is None:
211
 
                raise NoCommits(self.other_branch)
 
202
                raise NoCommits(other_branch)
212
203
            self.other_basis = self.other_rev_id
213
204
        elif other_revision[1] is not None:
214
 
            self.other_rev_id = self.other_branch.get_rev_id(other_revision[1])
 
205
            self.other_rev_id = other_branch.get_rev_id(other_revision[1])
215
206
            self.other_basis = self.other_rev_id
216
207
        else:
217
208
            self.other_rev_id = None
218
 
            self.other_basis = self.other_branch.last_revision()
 
209
            self.other_basis = other_branch.last_revision()
219
210
            if self.other_basis is None:
220
 
                raise NoCommits(self.other_branch)
221
 
        if self.other_branch.base != self.this_branch.base:
222
 
            self.this_branch.fetch(self.other_branch,
223
 
                                   last_revision=self.other_basis)
224
 
 
225
 
    def set_other_revision(self, revision_id, other_branch):
226
 
        """Set 'other' based on a branch and revision id
227
 
 
228
 
        :param revision_id: The revision to use for a tree
229
 
        :param other_branch: The branch containing this tree
230
 
        """
231
 
        self.other_rev_id = revision_id
232
 
        self.other_branch = other_branch
233
 
        self.this_branch.fetch(other_branch, self.other_rev_id)
234
 
        self.other_tree = self.revision_tree(revision_id)
235
 
        self.other_basis = revision_id
 
211
                raise NoCommits(other_branch)
 
212
        if other_branch.base != self.this_branch.base:
 
213
            self.this_branch.fetch(other_branch, last_revision=self.other_basis)
236
214
 
237
215
    def find_base(self):
238
216
        self.set_base([None, None])
245
223
        mutter("doing merge() with no base_revision specified")
246
224
        if base_revision == [None, None]:
247
225
            try:
248
 
                pb = ui.ui_factory.nested_progress_bar()
 
226
                pb = bzrlib.ui.ui_factory.nested_progress_bar()
249
227
                try:
250
228
                    this_repo = self.this_branch.repository
251
229
                    self.base_rev_id = common_ancestor(self.this_basis, 
273
251
                                                self.this_branch)
274
252
 
275
253
    def do_merge(self):
276
 
        kwargs = {'working_tree':self.this_tree, 'this_tree': self.this_tree,
277
 
                  'other_tree': self.other_tree,
 
254
        kwargs = {'working_tree':self.this_tree, 'this_tree': self.this_tree, 
 
255
                  'other_tree': self.other_tree, 
278
256
                  'interesting_ids': self.interesting_ids,
279
257
                  'pp': self.pp}
280
258
        if self.merge_type.requires_base:
289
267
        elif self.show_base:
290
268
            raise BzrError("Showing base is not supported for this"
291
269
                                  " merge type. %s" % self.merge_type)
292
 
        self.this_tree.lock_tree_write()
293
 
        if self.base_tree is not None:
294
 
            self.base_tree.lock_read()
295
 
        if self.other_tree is not None:
296
 
            self.other_tree.lock_read()
297
 
        try:
298
 
            merge = self.merge_type(pb=self._pb,
299
 
                                    change_reporter=self.change_reporter,
300
 
                                    **kwargs)
301
 
            if self.recurse == 'down':
302
 
                for path, file_id in self.this_tree.iter_references():
303
 
                    sub_tree = self.this_tree.get_nested_tree(file_id, path)
304
 
                    other_revision = self.other_tree.get_reference_revision(
305
 
                        file_id, path)
306
 
                    if  other_revision == sub_tree.last_revision():
307
 
                        continue
308
 
                    sub_merge = Merger(sub_tree.branch, this_tree=sub_tree)
309
 
                    sub_merge.merge_type = self.merge_type
310
 
                    relpath = self.this_tree.relpath(path)
311
 
                    other_branch = self.other_branch.reference_parent(file_id, relpath)
312
 
                    sub_merge.set_other_revision(other_revision, other_branch)
313
 
                    base_revision = self.base_tree.get_reference_revision(file_id)
314
 
                    sub_merge.base_tree = \
315
 
                        sub_tree.branch.repository.revision_tree(base_revision)
316
 
                    sub_merge.do_merge()
317
 
 
318
 
        finally:
319
 
            if self.other_tree is not None:
320
 
                self.other_tree.unlock()
321
 
            if self.base_tree is not None:
322
 
                self.base_tree.unlock()
323
 
            self.this_tree.unlock()
 
270
        merge = self.merge_type(pb=self._pb, **kwargs)
324
271
        if len(merge.cooked_conflicts) == 0:
325
272
            if not self.ignore_zero:
326
273
                note("All changes applied successfully.")
379
326
            else:
380
327
                parent = by_path[os.path.dirname(path)]
381
328
            abspath = pathjoin(self.this_tree.basedir, path)
382
 
            kind = osutils.file_kind(abspath)
 
329
            kind = bzrlib.osutils.file_kind(abspath)
383
330
            if file_id in self.base_tree.inventory:
384
331
                executable = getattr(self.base_tree.inventory[file_id], 'executable', False)
385
332
            else:
408
355
 
409
356
    def __init__(self, working_tree, this_tree, base_tree, other_tree, 
410
357
                 interesting_ids=None, reprocess=False, show_base=False,
411
 
                 pb=DummyProgress(), pp=None, change_reporter=None):
 
358
                 pb=DummyProgress(), pp=None):
412
359
        """Initialize the merger object and perform the merge."""
413
360
        object.__init__(self)
414
361
        self.this_tree = working_tree
415
 
        self.this_tree.lock_tree_write()
416
362
        self.base_tree = base_tree
417
 
        self.base_tree.lock_read()
418
363
        self.other_tree = other_tree
419
 
        self.other_tree.lock_read()
420
364
        self._raw_conflicts = []
421
365
        self.cooked_conflicts = []
422
366
        self.reprocess = reprocess
423
367
        self.show_base = show_base
424
368
        self.pb = pb
425
369
        self.pp = pp
426
 
        self.change_reporter = change_reporter
427
370
        if self.pp is None:
428
371
            self.pp = ProgressPhase("Merge phase", 3, self.pb)
429
372
 
432
375
        else:
433
376
            all_ids = set(base_tree)
434
377
            all_ids.update(other_tree)
 
378
        working_tree.lock_write()
435
379
        self.tt = TreeTransform(working_tree, self.pb)
436
380
        try:
437
381
            self.pp.next_phase()
444
388
                    self.merge_executable(file_id, file_status)
445
389
            finally:
446
390
                child_pb.finished()
447
 
            self.fix_root()
 
391
                
448
392
            self.pp.next_phase()
449
393
            child_pb = ui.ui_factory.nested_progress_bar()
450
394
            try:
451
395
                fs_conflicts = resolve_conflicts(self.tt, child_pb)
452
396
            finally:
453
397
                child_pb.finished()
454
 
            if change_reporter is not None:
455
 
                from bzrlib import delta
456
 
                delta.report_changes(self.tt._iter_changes(), change_reporter)
457
398
            self.cook_conflicts(fs_conflicts)
458
399
            for conflict in self.cooked_conflicts:
459
400
                warning(conflict)
466
407
                pass
467
408
        finally:
468
409
            self.tt.finalize()
469
 
            self.other_tree.unlock()
470
 
            self.base_tree.unlock()
471
 
            self.this_tree.unlock()
 
410
            working_tree.unlock()
472
411
            self.pb.clear()
473
412
 
474
 
    def fix_root(self):
475
 
        try:
476
 
            self.tt.final_kind(self.tt.root)
477
 
        except NoSuchFile:
478
 
            self.tt.cancel_deletion(self.tt.root)
479
 
        if self.tt.final_file_id(self.tt.root) is None:
480
 
            self.tt.version_file(self.tt.tree_file_id(self.tt.root), 
481
 
                                 self.tt.root)
482
 
        if self.other_tree.inventory.root is None:
483
 
            return
484
 
        other_root_file_id = self.other_tree.inventory.root.file_id
485
 
        other_root = self.tt.trans_id_file_id(other_root_file_id)
486
 
        if other_root == self.tt.root:
487
 
            return
488
 
        try:
489
 
            self.tt.final_kind(other_root)
490
 
        except NoSuchFile:
491
 
            return
492
 
        self.reparent_children(self.other_tree.inventory.root, self.tt.root)
493
 
        self.tt.cancel_creation(other_root)
494
 
        self.tt.cancel_versioning(other_root)
495
 
 
496
 
    def reparent_children(self, ie, target):
497
 
        for thing, child in ie.children.iteritems():
498
 
            trans_id = self.tt.trans_id_file_id(child.file_id)
499
 
            self.tt.adjust_path(self.tt.final_name(trans_id), target, trans_id)
500
 
 
501
413
    def write_modified(self, results):
502
414
        modified_hashes = {}
503
415
        for path in results.modified_paths:
608
520
                        "conflict": other_entry}
609
521
        trans_id = self.tt.trans_id_file_id(file_id)
610
522
        parent_id = winner_entry[parent_id_winner].parent_id
611
 
        if parent_id is not None:
612
 
            parent_trans_id = self.tt.trans_id_file_id(parent_id)
613
 
            self.tt.adjust_path(winner_entry[name_winner].name, 
614
 
                                parent_trans_id, trans_id)
 
523
        parent_trans_id = self.tt.trans_id_file_id(parent_id)
 
524
        self.tt.adjust_path(winner_entry[name_winner].name, parent_trans_id,
 
525
                            trans_id)
615
526
 
616
527
    def merge_contents(self, file_id):
617
528
        """Performa a merge on file_id contents."""
633
544
            parent_id = self.tt.final_parent(trans_id)
634
545
            if file_id in self.this_tree.inventory:
635
546
                self.tt.unversion_file(trans_id)
636
 
                if file_id in self.this_tree:
637
 
                    self.tt.delete_contents(trans_id)
 
547
                self.tt.delete_contents(trans_id)
638
548
            file_group = self._dump_conflicts(name, parent_id, file_id, 
639
549
                                              set_version=True)
640
550
            self._raw_conflicts.append(('contents conflict', file_group))
879
789
 
880
790
    def __init__(self, working_tree, this_tree, base_tree, other_tree, 
881
791
                 interesting_ids=None, pb=DummyProgress(), pp=None,
882
 
                 reprocess=False, change_reporter=None):
 
792
                 reprocess=False):
883
793
        self.this_revision_tree = self._get_revision_tree(this_tree)
884
794
        self.other_revision_tree = self._get_revision_tree(other_tree)
885
795
        super(WeaveMerger, self).__init__(working_tree, this_tree, 
886
796
                                          base_tree, other_tree, 
887
797
                                          interesting_ids=interesting_ids, 
888
 
                                          pb=pb, pp=pp, reprocess=reprocess,
889
 
                                          change_reporter=change_reporter)
 
798
                                          pb=pb, pp=pp, reprocess=reprocess)
890
799
 
891
800
    def _get_revision_tree(self, tree):
892
801
        """Return a revision tree related to this tree.
985
894
 
986
895
 
987
896
def merge_inner(this_branch, other_tree, base_tree, ignore_zero=False,
988
 
                backup_files=False,
989
 
                merge_type=Merge3Merger,
990
 
                interesting_ids=None,
991
 
                show_base=False,
992
 
                reprocess=False,
 
897
                backup_files=False, 
 
898
                merge_type=Merge3Merger, 
 
899
                interesting_ids=None, 
 
900
                show_base=False, 
 
901
                reprocess=False, 
993
902
                other_rev_id=None,
994
903
                interesting_files=None,
995
904
                this_tree=None,
996
 
                pb=DummyProgress(),
997
 
                change_reporter=None):
 
905
                pb=DummyProgress()):
998
906
    """Primary interface for merging. 
999
907
 
1000
908
        typical use is probably 
1007
915
             DeprecationWarning,
1008
916
             stacklevel=2)
1009
917
        this_tree = this_branch.bzrdir.open_workingtree()
1010
 
    merger = Merger(this_branch, other_tree, base_tree, this_tree=this_tree,
1011
 
                    pb=pb, change_reporter=change_reporter)
 
918
    merger = Merger(this_branch, other_tree, base_tree, this_tree=this_tree, 
 
919
                    pb=pb)
1012
920
    merger.backup_files = backup_files
1013
921
    merger.merge_type = merge_type
1014
922
    merger.interesting_ids = interesting_ids
1023
931
    merger.other_basis = other_rev_id
1024
932
    return merger.do_merge()
1025
933
 
1026
 
def get_merge_type_registry():
1027
 
    """Merge type registry is in bzrlib.option to avoid circular imports.
1028
 
 
1029
 
    This method provides a sanctioned way to retrieve it.
1030
 
    """
1031
 
    from bzrlib import option
1032
 
    return option._merge_type_registry
 
934
 
 
935
merge_types = {     "merge3": (Merge3Merger, "Native diff3-style merge"), 
 
936
                     "diff3": (Diff3Merger,  "Merge using external diff3"),
 
937
                     'weave': (WeaveMerger, "Weave-based merge")
 
938
              }
 
939
 
 
940
 
 
941
def merge_type_help():
 
942
    templ = '%s%%7s: %%s' % (' '*12)
 
943
    lines = [templ % (f[0], f[1][1]) for f in merge_types.iteritems()]
 
944
    return '\n'.join(lines)