~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

  • Committer: Martin Pool
  • Date: 2005-09-22 13:32:02 UTC
  • Revision ID: mbp@sourcefrog.net-20050922133202-347cfd35d2941dd5
- simple weave-based annotate code (not complete)

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
import os.path
19
19
import tempfile
20
20
import shutil
21
21
import errno
 
22
from fetch import greedy_fetch
22
23
 
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
38
 
 
39
35
 
40
36
# comments from abentley on irc: merge happens in two stages, each
41
37
# of which generates a changeset object
50
46
    conflict that are not explicitly handled cause an exception and
51
47
    terminate the merge.
52
48
    """
53
 
    def __init__(self, ignore_zero=False):
54
 
        ExceptionConflictHandler.__init__(self)
 
49
    def __init__(self, dir, ignore_zero=False):
 
50
        ExceptionConflictHandler.__init__(self, dir)
55
51
        self.conflicts = 0
56
52
        self.ignore_zero = ignore_zero
57
53
 
87
83
            last_new_name = name
88
84
        new_name = last_new_name+suffix
89
85
        try:
90
 
            rename(name, new_name)
 
86
            os.rename(name, new_name)
91
87
            return new_name
92
88
        except OSError, e:
93
89
            if e.errno != errno.EEXIST and e.errno != errno.ENOTEMPTY:
111
107
        self.add_suffix(this_path, ".THIS")
112
108
        self.dump(base_lines, this_path+".BASE")
113
109
        self.dump(other_lines, this_path+".OTHER")
114
 
        rename(new_file, this_path)
 
110
        os.rename(new_file, this_path)
115
111
        self.conflict("Diff3 conflict encountered in %s" % this_path)
116
112
 
117
113
    def new_contents_conflict(self, filename, other_contents):
137
133
            
138
134
def get_tree(treespec, temp_root, label, local_branch=None):
139
135
    location, revno = treespec
140
 
    branch = Branch.open_containing(location)
 
136
    branch = find_branch(location)
141
137
    if revno is None:
142
138
        revision = None
143
139
    elif revno == -1:
144
140
        revision = branch.last_revision()
145
141
    else:
146
 
        revision = branch.get_rev_id(revno)
 
142
        revision = branch.lookup_revision(revno)
147
143
    return branch, get_revid_tree(branch, revision, temp_root, label,
148
144
                                  local_branch)
149
145
 
238
234
    check_clean
239
235
        If true, this_dir must have no uncommitted changes before the
240
236
        merge begins.
241
 
 
242
 
    All available ancestors of other_revision and base_revision are
 
237
    all available ancestors of other_revision and base_revision are
243
238
    automatically pulled into the branch.
244
239
    """
 
240
    from bzrlib.revision import common_ancestor, MultipleRevisionSources
 
241
    from bzrlib.errors import NoSuchRevision
245
242
    tempdir = tempfile.mkdtemp(prefix="bzr-")
246
243
    try:
247
244
        if this_dir is None:
248
245
            this_dir = '.'
249
 
        this_branch = Branch.open_containing(this_dir)
 
246
        this_branch = find_branch(this_dir)
250
247
        this_rev_id = this_branch.last_revision()
251
248
        if this_rev_id is None:
252
249
            raise BzrCommandError("This branch has no commits")
259
256
                                            this_branch)
260
257
        if other_revision[1] == -1:
261
258
            other_rev_id = other_branch.last_revision()
262
 
            if other_rev_id is None:
263
 
                raise NoCommits(other_branch)
264
259
            other_basis = other_rev_id
265
260
        elif other_revision[1] is not None:
266
 
            other_rev_id = other_branch.get_rev_id(other_revision[1])
 
261
            other_rev_id = other_branch.lookup_revision(other_revision[1])
267
262
            other_basis = other_rev_id
268
263
        else:
269
264
            other_rev_id = None
270
265
            other_basis = other_branch.last_revision()
271
 
            if other_basis is None:
272
 
                raise NoCommits(other_branch)
273
266
        if base_revision == [None, None]:
274
 
            try:
275
 
                base_rev_id = common_ancestor(this_rev_id, other_basis, 
276
 
                                              this_branch)
277
 
            except NoCommonAncestor:
 
267
            base_rev_id = common_ancestor(this_rev_id, other_basis, 
 
268
                                          this_branch)
 
269
            if base_rev_id is None:
278
270
                raise UnrelatedBranches()
279
271
            base_tree = get_revid_tree(this_branch, base_rev_id, tempdir, 
280
272
                                       "base", None)
286
278
            elif base_revision[1] is None:
287
279
                base_rev_id = None
288
280
            else:
289
 
                base_rev_id = base_branch.get_rev_id(base_revision[1])
290
 
            fetch(from_branch=base_branch, to_branch=this_branch)
291
 
            base_is_ancestor = is_ancestor(this_rev_id, base_rev_id,
292
 
                                           this_branch)
 
281
                base_rev_id = base_branch.lookup_revision(base_revision[1])
 
282
            if base_rev_id is not None:
 
283
                base_is_ancestor = is_ancestor(this_rev_id, base_rev_id, 
 
284
                                               MultipleRevisionSources(this_branch, 
 
285
                                                                       base_branch))
 
286
            else:
 
287
                base_is_ancestor = False
293
288
        if file_list is None:
294
289
            interesting_ids = None
295
290
        else:
351
346
 
352
347
    inv_changes = merge_flex(this_tree, base_tree, other_tree,
353
348
                             generate_cset_optimized, get_inventory,
354
 
                             MergeConflictHandler(ignore_zero=ignore_zero),
 
349
                             MergeConflictHandler(base_tree.root,
 
350
                                                  ignore_zero=ignore_zero),
355
351
                             merge_factory=merge_factory, 
356
352
                             interesting_ids=interesting_ids)
357
353
 
361
357
            if path == '.':
362
358
                path = ''
363
359
            else:
364
 
                assert path.startswith('.' + os.sep), "path is %s" % path
 
360
                assert path.startswith('./'), "path is %s" % path
365
361
            path = path[2:]
366
362
        adjust_ids.append((path, id))
367
363
    if len(adjust_ids) > 0: