15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
from fetch import greedy_fetch
23
24
import bzrlib.osutils
24
25
import bzrlib.revision
25
26
from bzrlib.merge_core import merge_flex, ApplyMerge3, BackupBeforeChange
26
27
from bzrlib.changeset import generate_changeset, ExceptionConflictHandler
27
28
from bzrlib.changeset import Inventory, Diff3Merge
28
from bzrlib.branch import Branch
29
from bzrlib.errors import BzrCommandError, UnrelatedBranches, NoCommonAncestor
30
from bzrlib.errors import NoCommits
29
from bzrlib.branch import find_branch
30
from bzrlib.errors import BzrCommandError, UnrelatedBranches
31
31
from bzrlib.delta import compare_trees
32
32
from bzrlib.trace import mutter, warning
33
from bzrlib.fetch import greedy_fetch, fetch
33
from bzrlib.fetch import greedy_fetch
34
34
from bzrlib.revision import is_ancestor
35
from bzrlib.osutils import rename
36
from bzrlib.revision import common_ancestor, MultipleRevisionSources
37
from bzrlib.errors import NoSuchRevision
39
# TODO: build_working_dir can be built on something simpler than merge()
41
# FIXME: merge() parameters seem oriented towards the command line
43
36
# comments from abentley on irc: merge happens in two stages, each
44
37
# of which generates a changeset object
53
46
conflict that are not explicitly handled cause an exception and
54
47
terminate the merge.
56
def __init__(self, ignore_zero=False):
57
ExceptionConflictHandler.__init__(self)
49
def __init__(self, dir, ignore_zero=False):
50
ExceptionConflictHandler.__init__(self, dir)
59
52
self.ignore_zero = ignore_zero
114
107
self.add_suffix(this_path, ".THIS")
115
108
self.dump(base_lines, this_path+".BASE")
116
109
self.dump(other_lines, this_path+".OTHER")
117
rename(new_file, this_path)
110
os.rename(new_file, this_path)
118
111
self.conflict("Diff3 conflict encountered in %s" % this_path)
120
113
def new_contents_conflict(self, filename, other_contents):
141
134
def get_tree(treespec, temp_root, label, local_branch=None):
142
135
location, revno = treespec
143
branch = Branch.open_containing(location)
136
branch = find_branch(location)
144
137
if revno is None:
146
139
elif revno == -1:
147
revision = branch.last_revision()
140
revision = branch.last_patch()
149
revision = branch.get_rev_id(revno)
142
revision = branch.lookup_revision(revno)
150
143
return branch, get_revid_tree(branch, revision, temp_root, label,
204
197
return self.tree.inventory.has_id(file_id)
206
def has_or_had_id(self, file_id):
207
if file_id == self.tree.inventory.root.file_id:
209
return self.tree.inventory.has_id(file_id)
211
199
def readonly_path(self, id):
212
200
if id not in self.tree:
225
213
return self.cached[id]
228
def build_working_dir(to_dir):
229
"""Build a working directory in an empty directory.
231
to_dir is a directory containing branch metadata but no working files,
232
typically constructed by cloning an existing branch.
234
This is split out as a special idiomatic case of merge. It could
235
eventually be done by just building the tree directly calling into
236
lower-level code (e.g. constructing a changeset).
238
merge((to_dir, -1), (to_dir, 0), this_dir=to_dir,
239
check_clean=False, ignore_zero=True)
242
217
def merge(other_revision, base_revision,
243
218
check_clean=True, ignore_zero=False,
255
230
If true, this_dir must have no uncommitted changes before the
257
ignore_zero - If true, suppress the "zero conflicts" message when
258
there are no conflicts; should be set when doing something we expect
259
to complete perfectly.
261
All available ancestors of other_revision and base_revision are
232
all available ancestors of other_revision and base_revision are
262
233
automatically pulled into the branch.
235
from bzrlib.revision import common_ancestor, MultipleRevisionSources
236
from bzrlib.errors import NoSuchRevision
264
237
tempdir = tempfile.mkdtemp(prefix="bzr-")
266
239
if this_dir is None:
268
this_branch = Branch.open_containing(this_dir)
269
this_rev_id = this_branch.last_revision()
241
this_branch = find_branch(this_dir)
242
this_rev_id = this_branch.last_patch()
270
243
if this_rev_id is None:
271
244
raise BzrCommandError("This branch has no commits")
277
250
other_branch, other_tree = get_tree(other_revision, tempdir, "other",
279
252
if other_revision[1] == -1:
280
other_rev_id = other_branch.last_revision()
281
if other_rev_id is None:
282
raise NoCommits(other_branch)
253
other_rev_id = other_branch.last_patch()
283
254
other_basis = other_rev_id
284
255
elif other_revision[1] is not None:
285
other_rev_id = other_branch.get_rev_id(other_revision[1])
256
other_rev_id = other_branch.lookup_revision(other_revision[1])
286
257
other_basis = other_rev_id
288
259
other_rev_id = None
289
other_basis = other_branch.last_revision()
290
if other_basis is None:
291
raise NoCommits(other_branch)
260
other_basis = other_branch.last_patch()
292
261
if base_revision == [None, None]:
294
base_rev_id = common_ancestor(this_rev_id, other_basis,
296
except NoCommonAncestor:
262
base_rev_id = common_ancestor(this_rev_id, other_basis,
264
if base_rev_id is None:
297
265
raise UnrelatedBranches()
298
266
base_tree = get_revid_tree(this_branch, base_rev_id, tempdir,
302
270
base_branch, base_tree = get_tree(base_revision, tempdir, "base")
303
271
if base_revision[1] == -1:
304
base_rev_id = base_branch.last_revision()
272
base_rev_id = base_branch.last_patch()
305
273
elif base_revision[1] is None:
306
274
base_rev_id = None
308
base_rev_id = base_branch.get_rev_id(base_revision[1])
309
fetch(from_branch=base_branch, to_branch=this_branch)
310
base_is_ancestor = is_ancestor(this_rev_id, base_rev_id,
276
base_rev_id = base_branch.lookup_revision(base_revision[1])
277
if base_rev_id is not None:
278
base_is_ancestor = is_ancestor(this_rev_id, base_rev_id,
279
MultipleRevisionSources(this_branch,
282
base_is_ancestor = False
312
283
if file_list is None:
313
284
interesting_ids = None
328
299
merge_inner(this_branch, other_tree, base_tree, tempdir,
329
300
ignore_zero=ignore_zero, backup_files=backup_files,
330
301
merge_type=merge_type, interesting_ids=interesting_ids)
331
if base_is_ancestor and other_rev_id is not None\
332
and other_rev_id not in this_branch.revision_history():
302
if base_is_ancestor and other_rev_id is not None:
333
303
this_branch.add_pending_merge(other_rev_id)
335
305
shutil.rmtree(tempdir)
371
341
inv_changes = merge_flex(this_tree, base_tree, other_tree,
372
342
generate_cset_optimized, get_inventory,
373
MergeConflictHandler(ignore_zero=ignore_zero),
343
MergeConflictHandler(base_tree.root,
344
ignore_zero=ignore_zero),
374
345
merge_factory=merge_factory,
375
346
interesting_ids=interesting_ids)
383
assert path.startswith('.' + os.sep), "path is %s" % path
354
assert path.startswith('./'), "path is %s" % path
385
356
adjust_ids.append((path, id))
386
357
if len(adjust_ids) > 0: