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
1
from bzrlib.merge_core import merge_flex, ApplyMerge3, BackupBeforeChange
2
from bzrlib.changeset import generate_changeset, ExceptionConflictHandler
3
from bzrlib.changeset import Inventory, Diff3Merge
4
from bzrlib import find_branch
6
from bzrlib.errors import BzrCommandError
7
from bzrlib.delta import compare_trees
8
from trace import mutter, warning
22
13
from fetch import greedy_fetch
25
import bzrlib.revision
26
from bzrlib.merge_core import merge_flex, ApplyMerge3, BackupBeforeChange
27
from bzrlib.changeset import generate_changeset, ExceptionConflictHandler
28
from bzrlib.changeset import Inventory, Diff3Merge
29
from bzrlib.branch import find_branch
30
from bzrlib.errors import BzrCommandError, UnrelatedBranches, NoCommonAncestor
31
from bzrlib.errors import NoCommits
32
from bzrlib.delta import compare_trees
33
from bzrlib.trace import mutter, warning
34
from bzrlib.fetch import greedy_fetch
35
from bzrlib.revision import is_ancestor
37
16
# comments from abentley on irc: merge happens in two stages, each
38
17
# of which generates a changeset object
40
19
# stage 1: generate OLD->OTHER,
41
20
# stage 2: use MINE and OLD->OTHER to generate MINE -> RESULT
22
class UnrelatedBranches(BzrCommandError):
24
msg = "Branches have no common ancestor, and no base revision"\
26
BzrCommandError.__init__(self, msg)
43
29
class MergeConflictHandler(ExceptionConflictHandler):
44
30
"""Handle conflicts encountered while merging.
192
178
def has_id(self, file_id):
193
179
return self.tree.has_id(file_id)
195
def has_or_had_id(self, file_id):
196
if file_id == self.tree.inventory.root.file_id:
198
return self.tree.inventory.has_id(file_id)
200
def has_or_had_id(self, file_id):
201
if file_id == self.tree.inventory.root.file_id:
203
return self.tree.inventory.has_id(file_id)
205
181
def readonly_path(self, id):
206
182
if id not in self.tree:
227
203
"""Merge changes into a tree.
230
tuple(path, revision) Base for three-way merge.
206
Base for three-way merge.
232
tuple(path, revision) Other revision for three-way merge.
208
Other revision for three-way merge.
234
210
Directory to merge changes into; '.' by default.
238
214
all available ancestors of other_revision and base_revision are
239
215
automatically pulled into the branch.
241
from bzrlib.revision import common_ancestor, MultipleRevisionSources
217
from bzrlib.revision import common_ancestor, is_ancestor
218
from bzrlib.revision import MultipleRevisionSources
242
219
from bzrlib.errors import NoSuchRevision
243
220
tempdir = tempfile.mkdtemp(prefix="bzr-")
258
235
if other_revision[1] == -1:
259
236
other_rev_id = other_branch.last_patch()
260
if other_rev_id is None:
261
raise NoCommits(other_branch)
262
237
other_basis = other_rev_id
263
238
elif other_revision[1] is not None:
264
239
other_rev_id = other_branch.lookup_revision(other_revision[1])
267
242
other_rev_id = None
268
243
other_basis = other_branch.last_patch()
269
if other_basis is None:
270
raise NoCommits(other_branch)
271
244
if base_revision == [None, None]:
273
base_rev_id = common_ancestor(this_rev_id, other_basis,
275
except NoCommonAncestor:
245
base_rev_id = common_ancestor(this_rev_id, other_basis,
247
if base_rev_id is None:
276
248
raise UnrelatedBranches()
277
249
base_tree = get_revid_tree(this_branch, base_rev_id, tempdir,
287
259
base_rev_id = base_branch.lookup_revision(base_revision[1])
288
260
if base_rev_id is not None:
289
261
base_is_ancestor = is_ancestor(this_rev_id, base_rev_id,
290
MultipleRevisionSources(this_branch,
262
MultipleRevisionSources(
293
266
base_is_ancestor = False
294
267
if file_list is None:
310
283
merge_inner(this_branch, other_tree, base_tree, tempdir,
311
284
ignore_zero=ignore_zero, backup_files=backup_files,
312
285
merge_type=merge_type, interesting_ids=interesting_ids)
313
if base_is_ancestor and other_rev_id is not None\
314
and other_rev_id not in this_branch.revision_history():
286
if base_is_ancestor and other_rev_id is not None:
315
287
this_branch.add_pending_merge(other_rev_id)
317
289
shutil.rmtree(tempdir)