29
29
from bzrlib.errors import BzrCommandError, UnrelatedBranches, NoCommonAncestor
30
30
from bzrlib.errors import NoCommits
31
31
from bzrlib.delta import compare_trees
32
from bzrlib.trace import mutter, warning
32
from bzrlib.trace import mutter, warning, note
33
33
from bzrlib.fetch import greedy_fetch, fetch
34
34
from bzrlib.revision import is_ancestor
35
35
from bzrlib.osutils import rename
140
142
def rem_contents_conflict(self, filename, this_contents, base_contents):
141
143
base_contents(filename+".BASE", self, False)
142
144
this_contents(filename+".THIS", self, False)
145
return ReplaceContents(this_contents, None)
147
def rem_contents_conflict(self, filename, this_contents, base_contents):
148
base_contents(filename+".BASE", self, False)
149
this_contents(filename+".THIS", self, False)
143
150
self.conflict("Other branch deleted locally modified file %s" %
145
152
return ReplaceContents(this_contents, None)
147
154
def abs_this_path(self, file_id):
148
155
"""Return the absolute path for a file_id in the this tree."""
149
relpath = self.this_tree.id2path(file_id)
150
return self.this_tree.tree.abspath(relpath)
156
return self.this_tree.id2abspath(file_id)
152
158
def add_missing_parents(self, file_id, tree):
153
159
"""If some of the parents for file_id are missing, add them."""
154
entry = tree.tree.inventory[file_id]
160
entry = tree.inventory[file_id]
155
161
if entry.parent_id not in self.this_tree:
156
162
return self.create_all_missing(entry.parent_id, tree)
160
166
def create_all_missing(self, file_id, tree):
161
167
"""Add contents for a file_id and all its parents to a tree."""
162
entry = tree.tree.inventory[file_id]
168
entry = tree.inventory[file_id]
163
169
if entry.parent_id is not None and entry.parent_id not in self.this_tree:
164
170
abspath = self.create_all_missing(entry.parent_id, tree)
172
178
def create(self, file_id, path, tree, reverse=False):
173
179
"""Uses tree data to create a filesystem object for the file_id"""
174
from merge_core import get_id_contents
175
get_id_contents(file_id, tree)(path, self, reverse)
180
from changeset import get_contents
181
get_contents(tree, file_id)(path, self, reverse)
177
183
def missing_for_merge(self, file_id, other_path):
178
184
"""The file_id doesn't exist in THIS, but does in OTHER and BASE"""
183
189
self.create(file_id, stem+".OTHER", self.other_tree)
184
190
self.create(file_id, stem+".BASE", self.base_tree)
192
def threeway_contents_conflict(filename, this_contents, base_contents,
194
self.conflict("Three-way conflict merging %s" % filename)
186
196
def finalize(self):
187
197
if not self.ignore_zero:
188
print "%d conflicts encountered.\n" % self.conflicts
198
note("%d conflicts encountered.\n" % self.conflicts)
190
def get_tree(treespec, temp_root, label, local_branch=None):
200
def get_tree(treespec, local_branch=None):
191
201
location, revno = treespec
192
branch = Branch.open_containing(location)
202
branch = Branch.open_containing(location)[0]
193
203
if revno is None:
195
205
elif revno == -1:
196
206
revision = branch.last_revision()
198
208
revision = branch.get_rev_id(revno)
199
return branch, get_revid_tree(branch, revision, temp_root, label,
209
return branch, get_revid_tree(branch, revision, local_branch)
202
def get_revid_tree(branch, revision, temp_root, label, local_branch):
211
def get_revid_tree(branch, revision, local_branch):
203
212
if revision is None:
204
213
base_tree = branch.working_tree()
208
217
base_tree = local_branch.revision_tree(revision)
210
219
base_tree = branch.revision_tree(revision)
211
temp_path = os.path.join(temp_root, label)
213
return MergeTree(base_tree, temp_path)
216
223
def file_exists(tree, file_id):
217
224
return tree.has_filename(tree.id2path(file_id))
220
class MergeTree(object):
221
def __init__(self, tree, tempdir):
222
object.__init__(self)
223
if hasattr(tree, "basedir"):
224
self.root = tree.basedir
228
self.tempdir = tempdir
229
os.mkdir(os.path.join(self.tempdir, "texts"))
230
os.mkdir(os.path.join(self.tempdir, "symlinks"))
234
return self.tree.__iter__()
236
def __contains__(self, file_id):
237
return file_id in self.tree
239
def get_file(self, file_id):
240
return self.tree.get_file(file_id)
242
def get_file_sha1(self, id):
243
return self.tree.get_file_sha1(id)
245
def is_executable(self, id):
246
return self.tree.is_executable(id)
248
def id2path(self, file_id):
249
return self.tree.id2path(file_id)
251
def has_id(self, file_id):
252
return self.tree.has_id(file_id)
254
def has_or_had_id(self, file_id):
255
if file_id == self.tree.inventory.root.file_id:
257
return self.tree.inventory.has_id(file_id)
259
def has_or_had_id(self, file_id):
260
if file_id == self.tree.inventory.root.file_id:
262
return self.tree.inventory.has_id(file_id)
264
def readonly_path(self, id):
265
if id not in self.tree:
267
if self.root is not None:
268
return self.tree.abspath(self.tree.id2path(id))
270
kind = self.tree.inventory[id].kind
271
if kind in ("directory", "root_directory"):
273
if not self.cached.has_key(id):
275
path = os.path.join(self.tempdir, "texts", id)
276
outfile = file(path, "wb")
277
outfile.write(self.tree.get_file(id).read())
278
assert(bzrlib.osutils.lexists(path))
279
if self.tree.is_executable(id):
282
assert kind == "symlink"
283
path = os.path.join(self.tempdir, "symlinks", id)
284
target = self.tree.get_symlink_target(id)
285
os.symlink(target, path)
286
self.cached[id] = path
287
return self.cached[id]
290
227
def build_working_dir(to_dir):
291
228
"""Build a working directory in an empty directory.
336
273
this_branch.basis_tree(), False)
337
274
if changes.has_changed():
338
275
raise BzrCommandError("Working tree has uncommitted changes.")
339
other_branch, other_tree = get_tree(other_revision, tempdir, "other",
276
other_branch, other_tree = get_tree(other_revision, this_branch)
341
277
if other_revision[1] == -1:
342
278
other_rev_id = other_branch.last_revision()
343
279
if other_rev_id is None:
358
294
except NoCommonAncestor:
359
295
raise UnrelatedBranches()
360
base_tree = get_revid_tree(this_branch, base_rev_id, tempdir,
296
base_tree = get_revid_tree(this_branch, base_rev_id, None)
362
297
base_is_ancestor = True
364
base_branch, base_tree = get_tree(base_revision, tempdir, "base")
299
base_branch, base_tree = get_tree(base_revision)
365
300
if base_revision[1] == -1:
366
301
base_rev_id = base_branch.last_revision()
367
302
elif base_revision[1] is None:
377
312
interesting_ids = set()
378
313
this_tree = this_branch.working_tree()
379
314
for fname in file_list:
380
path = this_branch.relpath(fname)
315
path = this_tree.relpath(fname)
382
for tree in (this_tree, base_tree.tree, other_tree.tree):
317
for tree in (this_tree, base_tree, other_tree):
383
318
file_id = tree.inventory.path2id(path)
384
319
if file_id is not None:
385
320
interesting_ids.add(file_id)
405
340
source_file.interesting = source_file.id in interesting_ids
408
def generate_cset_optimized(tree_a, tree_b, interesting_ids=None):
409
"""Generate a changeset. If interesting_ids is supplied, only changes
410
to those files will be shown. Metadata changes are stripped.
412
cset = generate_changeset(tree_a, tree_b, interesting_ids)
413
for entry in cset.entries.itervalues():
414
entry.metadata_change = None
418
343
def merge_inner(this_branch, other_tree, base_tree, tempdir,
419
344
ignore_zero=False, merge_type=ApplyMerge3, backup_files=False,
420
345
interesting_ids=None):
425
350
contents_change = BackupBeforeChange(contents_change)
426
351
return contents_change
428
this_tree = get_tree((this_branch.base, None), tempdir, "this")[1]
353
this_tree = get_tree((this_branch.base, None))[1]
430
355
def get_inventory(tree):
431
return tree.tree.inventory
356
return tree.inventory
433
358
inv_changes = merge_flex(this_tree, base_tree, other_tree,
434
generate_cset_optimized, get_inventory,
359
generate_changeset, get_inventory,
435
360
MergeConflictHandler(this_tree, base_tree,
436
361
other_tree, ignore_zero=ignore_zero),
437
362
merge_factory=merge_factory,