41
40
from bzrlib.merge3 import Merge3
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,
48
FinalPaths, create_by_entry, unique_add)
50
49
from bzrlib.versionedfile import WeaveMerge
51
50
from bzrlib import ui
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,
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
111
self.recurse = recurse
112
self.change_reporter = change_reporter
114
110
def revision_tree(self, revision_id):
115
111
return self.this_branch.repository.revision_tree(revision_id)
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")
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:
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
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)
204
195
:param other_revision: The [path, revision] list to merge from.
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
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)
225
def set_other_revision(self, revision_id, other_branch):
226
"""Set 'other' based on a branch and revision id
228
:param revision_id: The revision to use for a tree
229
:param other_branch: The branch containing this tree
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)
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]:
248
pb = ui.ui_factory.nested_progress_bar()
226
pb = bzrlib.ui.ui_factory.nested_progress_bar()
250
228
this_repo = self.this_branch.repository
251
229
self.base_rev_id = common_ancestor(self.this_basis,
273
251
self.this_branch)
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,
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()
298
merge = self.merge_type(pb=self._pb,
299
change_reporter=self.change_reporter,
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(
306
if other_revision == sub_tree.last_revision():
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)
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.")
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)
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
426
self.change_reporter = change_reporter
427
370
if self.pp is None:
428
371
self.pp = ProgressPhase("Merge phase", 3, self.pb)
444
388
self.merge_executable(file_id, file_status)
446
390
child_pb.finished()
448
392
self.pp.next_phase()
449
393
child_pb = ui.ui_factory.nested_progress_bar()
451
395
fs_conflicts = resolve_conflicts(self.tt, child_pb)
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)
468
409
self.tt.finalize()
469
self.other_tree.unlock()
470
self.base_tree.unlock()
471
self.this_tree.unlock()
410
working_tree.unlock()
476
self.tt.final_kind(self.tt.root)
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),
482
if self.other_tree.inventory.root is None:
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:
489
self.tt.final_kind(other_root)
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)
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)
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,
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))
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):
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)
891
800
def _get_revision_tree(self, tree):
892
801
"""Return a revision tree related to this tree.
1007
915
DeprecationWarning,
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,
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()
1026
def get_merge_type_registry():
1027
"""Merge type registry is in bzrlib.option to avoid circular imports.
1029
This method provides a sanctioned way to retrieve it.
1031
from bzrlib import option
1032
return option._merge_type_registry
935
merge_types = { "merge3": (Merge3Merger, "Native diff3-style merge"),
936
"diff3": (Diff3Merger, "Merge using external diff3"),
937
'weave': (WeaveMerger, "Weave-based merge")
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)