1
# Copyright (C) 2005 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
# TODO: build_working_dir can be built on something simpler than merge()
23
from bzrlib.branch import Branch
24
from bzrlib.delta import compare_trees
25
from bzrlib.errors import (BzrCommandError,
33
WorkingTreeNotRevision,
35
from bzrlib.fetch import greedy_fetch, fetch
37
from bzrlib.osutils import rename, pathjoin
38
from bzrlib.revision import common_ancestor, is_ancestor, NULL_REVISION
39
from bzrlib.transform import (Merge3Merger as ApplyMerge3, WeaveMerger,
41
from bzrlib.trace import mutter, warning, note
42
from bzrlib.workingtree import WorkingTree
44
# TODO: Report back as changes are merged in
46
# comments from abentley on irc: merge happens in two stages, each
47
# of which generates a changeset object
49
# stage 1: generate OLD->OTHER,
50
# stage 2: use MINE and OLD->OTHER to generate MINE -> RESULT
52
def _get_tree(treespec, local_branch=None):
53
location, revno = treespec
54
branch = Branch.open_containing(location)[0]
58
revision = branch.last_revision()
60
revision = branch.get_rev_id(revno)
62
revision = NULL_REVISION
63
return branch, _get_revid_tree(branch, revision, local_branch)
66
def _get_revid_tree(branch, revision, local_branch):
68
base_tree = branch.working_tree()
70
if local_branch is not None:
71
if local_branch.base != branch.base:
72
greedy_fetch(local_branch, branch, revision)
73
base_tree = local_branch.repository.revision_tree(revision)
75
base_tree = branch.repository.revision_tree(revision)
79
def build_working_dir(to_dir):
80
"""Build a working directory in an empty directory.
82
to_dir is a directory containing branch metadata but no working files,
83
typically constructed by cloning an existing branch.
85
This is split out as a special idiomatic case of merge. It could
86
eventually be done by just building the tree directly calling into
87
lower-level code (e.g. constructing a changeset).
89
# RBC 20051019 is this not just 'export' ?
90
# AB Well, export doesn't take care of inventory...
91
from transform import build_tree
92
this_branch = Branch.open_containing(to_dir)[0]
93
build_tree(this_branch, this_branch.basis_tree())
96
def transform_tree(from_tree, to_tree, interesting_ids=None):
97
merge_inner(from_tree.branch, to_tree, from_tree, ignore_zero=True,
98
interesting_ids=interesting_ids)
101
def merge_inner(this_branch, other_tree, base_tree, ignore_zero=False,
103
merge_type=ApplyMerge3,
104
interesting_ids=None,
108
interesting_files=None,
110
"""Primary interface for merging.
112
typical use is probably
113
'merge_inner(branch, branch.get_revision_tree(other_revision),
114
branch.get_revision_tree(base_revision))'
116
if this_tree is None:
117
this_tree = this_branch.working_tree()
118
merger = Merger(this_branch, other_tree, base_tree, this_tree=this_tree)
119
merger.backup_files = backup_files
120
merger.merge_type = merge_type
121
merger.interesting_ids = interesting_ids
122
if interesting_files:
123
assert not interesting_ids, ('Only supply interesting_ids'
124
' or interesting_files')
125
merger._set_interesting_files(interesting_files)
126
merger.show_base = show_base
127
merger.reprocess = reprocess
128
merger.other_rev_id = other_rev_id
129
merger.other_basis = other_rev_id
130
return merger.do_merge()
133
class Merger(object):
134
def __init__(self, this_branch, other_tree=None, base_tree=None, this_tree=None):
135
object.__init__(self)
136
assert this_tree is not None, "this_tree is required"
137
self.this_branch = this_branch
138
self.this_basis = this_branch.last_revision()
139
self.this_rev_id = None
140
self.this_tree = this_tree
141
self.this_revision_tree = None
142
self.this_basis_tree = None
143
self.other_tree = other_tree
144
self.base_tree = base_tree
145
self.ignore_zero = False
146
self.backup_files = False
147
self.interesting_ids = None
148
self.show_base = False
149
self.reprocess = False
151
def revision_tree(self, revision_id):
152
return self.this_branch.repository.revision_tree(revision_id)
154
def ensure_revision_trees(self):
155
if self.this_revision_tree is None:
156
self.this_basis_tree = self.this_branch.repository.revision_tree(
158
if self.this_basis == self.this_rev_id:
159
self.this_revision_tree = self.this_basis_tree
161
if self.other_rev_id is None:
162
other_basis_tree = self.revision_tree(self.other_basis)
163
changes = compare_trees(self.other_tree, other_basis_tree)
164
if changes.has_changed():
165
raise WorkingTreeNotRevision(self.this_tree)
166
other_rev_id = other_basis
167
self.other_tree = other_basis_tree
169
def file_revisions(self, file_id):
170
self.ensure_revision_trees()
171
def get_id(tree, file_id):
172
revision_id = tree.inventory[file_id].revision
173
assert revision_id is not None
175
if self.this_rev_id is None:
176
if self.this_basis_tree.get_file_sha1(file_id) != \
177
self.this_tree.get_file_sha1(file_id):
178
raise WorkingTreeNotRevision(self.this_tree)
180
trees = (self.this_basis_tree, self.other_tree)
181
return [get_id(tree, file_id) for tree in trees]
183
def merge_factory(self, file_id, base, other):
184
if self.merge_type.history_based:
185
if self.show_base is True:
186
raise BzrError("Cannot show base for hisory-based merges")
187
if self.reprocess is True:
188
raise BzrError("Cannot reprocess history-based merges")
190
t_revid, o_revid = self.file_revisions(file_id)
191
weave = self.this_basis_tree.get_weave(file_id)
192
contents_change = self.merge_type(weave, t_revid, o_revid)
194
if self.show_base is True or self.reprocess is True:
195
contents_change = self.merge_type(file_id, base, other,
196
show_base=self.show_base,
197
reprocess=self.reprocess)
199
contents_change = self.merge_type(file_id, base, other)
200
if self.backup_files:
201
contents_change = BackupBeforeChange(contents_change)
202
return contents_change
204
def check_basis(self, check_clean):
205
if self.this_basis is None:
206
raise BzrCommandError("This branch has no commits")
209
if self.this_basis != self.this_rev_id:
210
raise BzrCommandError("Working tree has uncommitted changes.")
212
def compare_basis(self):
213
changes = compare_trees(self.this_tree,
214
self.this_branch.basis_tree(), False)
215
if not changes.has_changed():
216
self.this_rev_id = self.this_basis
218
def set_interesting_files(self, file_list):
220
self._set_interesting_files(file_list)
221
except NotVersionedError, e:
222
raise BzrCommandError("%s is not a source file in any"
225
def _set_interesting_files(self, file_list):
226
"""Set the list of interesting ids from a list of files."""
227
if file_list is None:
228
self.interesting_ids = None
231
interesting_ids = set()
232
for path in file_list:
234
for tree in (self.this_tree, self.base_tree, self.other_tree):
235
file_id = tree.inventory.path2id(path)
236
if file_id is not None:
237
interesting_ids.add(file_id)
240
raise NotVersionedError(path=path)
241
self.interesting_ids = interesting_ids
243
def set_pending(self):
244
if not self.base_is_ancestor:
246
if self.other_rev_id is None:
248
ancestry = self.this_branch.repository.get_ancestry(self.this_basis)
249
if self.other_rev_id in ancestry:
251
self.this_tree.add_pending_merge(self.other_rev_id)
253
def set_other(self, other_revision):
254
other_branch, self.other_tree = _get_tree(other_revision,
256
if other_revision[1] == -1:
257
self.other_rev_id = other_branch.last_revision()
258
if self.other_rev_id is None:
259
raise NoCommits(other_branch)
260
self.other_basis = self.other_rev_id
261
elif other_revision[1] is not None:
262
self.other_rev_id = other_branch.get_rev_id(other_revision[1])
263
self.other_basis = self.other_rev_id
265
self.other_rev_id = None
266
self.other_basis = other_branch.last_revision()
267
if self.other_basis is None:
268
raise NoCommits(other_branch)
269
if other_branch.base != self.this_branch.base:
270
fetch(from_branch=other_branch, to_branch=self.this_branch,
271
last_revision=self.other_basis)
273
def set_base(self, base_revision):
274
mutter("doing merge() with no base_revision specified")
275
if base_revision == [None, None]:
277
self.base_rev_id = common_ancestor(self.this_basis,
279
self.this_branch.repository)
280
except NoCommonAncestor:
281
raise UnrelatedBranches()
282
self.base_tree = _get_revid_tree(self.this_branch, self.base_rev_id,
284
self.base_is_ancestor = True
286
base_branch, self.base_tree = _get_tree(base_revision)
287
if base_revision[1] == -1:
288
self.base_rev_id = base_branch.last_revision()
289
elif base_revision[1] is None:
290
self.base_rev_id = None
292
self.base_rev_id = base_branch.get_rev_id(base_revision[1])
293
fetch(from_branch=base_branch, to_branch=self.this_branch)
294
self.base_is_ancestor = is_ancestor(self.this_basis,
299
kwargs = {'working_tree':self.this_tree, 'this_tree': self.this_tree,
300
'other_tree': self.other_tree}
301
if self.merge_type.requires_base:
302
kwargs['base_tree'] = self.base_tree
303
if self.reprocess and not self.merge_type.supports_reprocess:
304
raise BzrError("Reprocess is not supported for this merge"
305
" type. %s" % merge_type)
307
kwargs['reprocess'] = self.reprocess
308
if self.show_base and not self.merge_type.supports_show_base:
309
raise BzrError("Showing base is not supported for this"
310
" merge type. %s" % self.merge_type)
312
kwargs['show_base'] = self.show_base
313
merge = self.merge_type(**kwargs)
314
return len(merge.cooked_conflicts)
316
def regen_inventory(self, new_entries):
317
old_entries = self.this_tree.read_working_inventory()
321
for path, file_id in new_entries:
324
new_entries_map[file_id] = path
326
def id2path(file_id):
327
path = new_entries_map.get(file_id)
330
entry = old_entries[file_id]
331
if entry.parent_id is None:
333
return pathjoin(id2path(entry.parent_id), entry.name)
335
for file_id in old_entries:
336
entry = old_entries[file_id]
337
path = id2path(file_id)
338
new_inventory[file_id] = (path, file_id, entry.parent_id,
340
by_path[path] = file_id
345
for path, file_id in new_entries:
347
del new_inventory[file_id]
350
new_path_list.append((path, file_id))
351
if file_id not in old_entries:
353
# Ensure no file is added before its parent
355
for path, file_id in new_path_list:
359
parent = by_path[os.path.dirname(path)]
360
abspath = pathjoin(self.this_tree.basedir, path)
361
kind = bzrlib.osutils.file_kind(abspath)
362
new_inventory[file_id] = (path, file_id, parent, kind)
363
by_path[path] = file_id
365
# Get a list in insertion order
366
new_inventory_list = new_inventory.values()
367
mutter ("""Inventory regeneration:
368
old length: %i insertions: %i deletions: %i new_length: %i"""\
369
% (len(old_entries), insertions, deletions,
370
len(new_inventory_list)))
371
assert len(new_inventory_list) == len(old_entries) + insertions\
373
new_inventory_list.sort()
374
return new_inventory_list
377
merge_types = { "merge3": (ApplyMerge3, "Native diff3-style merge"),
378
"diff3": (Diff3Merger, "Merge using external diff3"),
379
'weave': (WeaveMerger, "Weave-based merge")