~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

  • Committer: Robert Collins
  • Date: 2005-08-25 10:04:51 UTC
  • mto: (974.1.50) (1185.1.10) (1092.3.1)
  • mto: This revision was merged to the branch mainline in revision 1139.
  • Revision ID: robertc@robertcollins.net-20050825100451-b01297285491ba46
make a default merge choose a sane base with branch.common_ancestor

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
18
 
import os
 
18
from bzrlib.merge_core import merge_flex, ApplyMerge3, BackupBeforeChange
 
19
from bzrlib.changeset import generate_changeset, ExceptionConflictHandler
 
20
from bzrlib.changeset import Inventory, Diff3Merge
 
21
from bzrlib.branch import find_branch
 
22
from bzrlib.revision import is_ancestor
 
23
import bzrlib.osutils
 
24
from bzrlib.errors import BzrCommandError, UnrelatedBranches
 
25
from bzrlib.delta import compare_trees
 
26
from trace import mutter, warning
 
27
import os.path
19
28
import tempfile
20
29
import shutil
21
30
import errno
22
31
from fetch import greedy_fetch
23
32
 
24
 
import bzrlib.osutils
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 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
36
33
 
37
34
# comments from abentley on irc: merge happens in two stages, each
38
35
# of which generates a changeset object
47
44
    conflict that are not explicitly handled cause an exception and
48
45
    terminate the merge.
49
46
    """
50
 
    def __init__(self, ignore_zero=False):
51
 
        ExceptionConflictHandler.__init__(self)
 
47
    def __init__(self, dir, ignore_zero=False):
 
48
        ExceptionConflictHandler.__init__(self, dir)
52
49
        self.conflicts = 0
53
50
        self.ignore_zero = ignore_zero
54
51
 
134
131
            
135
132
def get_tree(treespec, temp_root, label, local_branch=None):
136
133
    location, revno = treespec
137
 
    branch = Branch.open(location)
 
134
    branch = find_branch(location)
138
135
    if revno is None:
139
136
        revision = None
140
137
    elif revno == -1:
141
138
        revision = branch.last_patch()
142
139
    else:
143
 
        revision = branch.get_rev_id(revno)
 
140
        revision = branch.lookup_revision(revno)
144
141
    return branch, get_revid_tree(branch, revision, temp_root, label,
145
142
                                  local_branch)
146
143
 
197
194
            return True
198
195
        return self.tree.inventory.has_id(file_id)
199
196
 
200
 
    def has_or_had_id(self, file_id):
201
 
        if file_id == self.tree.inventory.root.file_id:
202
 
            return True
203
 
        return self.tree.inventory.has_id(file_id)
204
 
 
205
197
    def readonly_path(self, id):
206
198
        if id not in self.tree:
207
199
            return None
244
236
    try:
245
237
        if this_dir is None:
246
238
            this_dir = '.'
247
 
        this_branch = Branch.open_containing(this_dir)
 
239
        this_branch = find_branch(this_dir)
248
240
        this_rev_id = this_branch.last_patch()
249
241
        if this_rev_id is None:
250
242
            raise BzrCommandError("This branch has no commits")
257
249
                                            this_branch)
258
250
        if other_revision[1] == -1:
259
251
            other_rev_id = other_branch.last_patch()
260
 
            if other_rev_id is None:
261
 
                raise NoCommits(other_branch)
262
252
            other_basis = other_rev_id
263
253
        elif other_revision[1] is not None:
264
 
            other_rev_id = other_branch.get_rev_id(other_revision[1])
 
254
            other_rev_id = other_branch.lookup_revision(other_revision[1])
265
255
            other_basis = other_rev_id
266
256
        else:
267
257
            other_rev_id = None
268
258
            other_basis = other_branch.last_patch()
269
 
            if other_basis is None:
270
 
                raise NoCommits(other_branch)
271
259
        if base_revision == [None, None]:
 
260
            if other_revision[1] == -1:
 
261
                o_revno = None
 
262
            else:
 
263
                o_revno = other_revision[1]
 
264
                raise UnrelatedBranches()
272
265
            try:
273
 
                base_rev_id = common_ancestor(this_rev_id, other_basis, 
274
 
                                              this_branch)
275
 
            except NoCommonAncestor:
276
 
                raise UnrelatedBranches()
277
 
            base_tree = get_revid_tree(this_branch, base_rev_id, tempdir, 
278
 
                                       "base", None)
 
266
                base_revision = this_branch.get_revision(base_rev_id)
 
267
                base_branch = this_branch
 
268
            except NoSuchRevision:
 
269
                base_branch = other_branch
 
270
            base_tree = get_revid_tree(base_branch, base_rev_id, tempdir, 
 
271
                                       "base")
279
272
            base_is_ancestor = True
280
273
        else:
281
 
            base_branch, base_tree = get_tree(base_revision, tempdir, "base")
282
274
            if base_revision[1] == -1:
 
275
                base_branch, base_tree = get_tree(base_revision, tempdir, "base")
283
276
                base_rev_id = base_branch.last_patch()
284
277
            elif base_revision[1] is None:
285
 
                base_rev_id = None
286
 
            else:
287
 
                base_rev_id = base_branch.get_rev_id(base_revision[1])
288
 
            multi_source = MultipleRevisionSources(this_branch, base_branch)
289
 
            base_is_ancestor = is_ancestor(this_rev_id, base_rev_id,
290
 
                                           multi_source)
 
278
                base_revno, base_rev_id = this_branch.common_ancestor(other_branch)
 
279
                base_branch, base_tree = get_tree((base_revision[0], "revid:%s" % base_rev_id), tempdir, "base")
 
280
            else:
 
281
                base_branch, base_tree = get_tree(base_revision, tempdir, "base")
 
282
                base_rev_id = base_branch.lookup_revision(base_revision[1])
 
283
            if base_rev_id is not None:
 
284
                base_is_ancestor = is_ancestor(this_rev_id, base_rev_id, 
 
285
                                               MultipleRevisionSources(
 
286
                                               this_branch, 
 
287
                                               base_branch))
 
288
            else:
 
289
                base_is_ancestor = False
291
290
        if file_list is None:
292
291
            interesting_ids = None
293
292
        else:
307
306
        merge_inner(this_branch, other_tree, base_tree, tempdir, 
308
307
                    ignore_zero=ignore_zero, backup_files=backup_files, 
309
308
                    merge_type=merge_type, interesting_ids=interesting_ids)
310
 
        if base_is_ancestor and other_rev_id is not None\
311
 
            and other_rev_id not in this_branch.revision_history():
 
309
        if base_is_ancestor and other_rev_id is not None:
312
310
            this_branch.add_pending_merge(other_rev_id)
313
311
    finally:
314
312
        shutil.rmtree(tempdir)
349
347
 
350
348
    inv_changes = merge_flex(this_tree, base_tree, other_tree,
351
349
                             generate_cset_optimized, get_inventory,
352
 
                             MergeConflictHandler(ignore_zero=ignore_zero),
 
350
                             MergeConflictHandler(base_tree.root,
 
351
                                                  ignore_zero=ignore_zero),
353
352
                             merge_factory=merge_factory, 
354
353
                             interesting_ids=interesting_ids)
355
354
 
359
358
            if path == '.':
360
359
                path = ''
361
360
            else:
362
 
                assert path.startswith('.' + os.sep), "path is %s" % path
 
361
                assert path.startswith('./'), "path is %s" % path
363
362
            path = path[2:]
364
363
        adjust_ids.append((path, id))
365
364
    if len(adjust_ids) > 0: