~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

  • Committer: Martin Pool
  • Date: 2005-08-30 06:10:39 UTC
  • Revision ID: mbp@sourcefrog.net-20050830061039-1d0347fb236c39ad
- clean up some code in revision.py

- move all exceptions to bzrlib.errors

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
 
# TODO: build_working_dir can be built on something simpler than merge()
40
 
 
41
 
# FIXME: merge() parameters seem oriented towards the command line
42
35
 
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.
55
48
    """
56
 
    def __init__(self, ignore_zero=False):
57
 
        ExceptionConflictHandler.__init__(self)
 
49
    def __init__(self, dir, ignore_zero=False):
 
50
        ExceptionConflictHandler.__init__(self, dir)
58
51
        self.conflicts = 0
59
52
        self.ignore_zero = ignore_zero
60
53
 
90
83
            last_new_name = name
91
84
        new_name = last_new_name+suffix
92
85
        try:
93
 
            rename(name, new_name)
 
86
            os.rename(name, new_name)
94
87
            return new_name
95
88
        except OSError, e:
96
89
            if e.errno != errno.EEXIST and e.errno != errno.ENOTEMPTY:
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)
119
112
 
120
113
    def new_contents_conflict(self, filename, other_contents):
140
133
            
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:
145
138
        revision = None
146
139
    elif revno == -1:
147
 
        revision = branch.last_revision()
 
140
        revision = branch.last_patch()
148
141
    else:
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,
151
144
                                  local_branch)
152
145
 
203
196
            return True
204
197
        return self.tree.inventory.has_id(file_id)
205
198
 
206
 
    def has_or_had_id(self, file_id):
207
 
        if file_id == self.tree.inventory.root.file_id:
208
 
            return True
209
 
        return self.tree.inventory.has_id(file_id)
210
 
 
211
199
    def readonly_path(self, id):
212
200
        if id not in self.tree:
213
201
            return None
225
213
            return self.cached[id]
226
214
 
227
215
 
228
 
def build_working_dir(to_dir):
229
 
    """Build a working directory in an empty directory.
230
 
 
231
 
    to_dir is a directory containing branch metadata but no working files,
232
 
    typically constructed by cloning an existing branch. 
233
 
 
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).
237
 
    """
238
 
    merge((to_dir, -1), (to_dir, 0), this_dir=to_dir,
239
 
          check_clean=False, ignore_zero=True)
240
 
 
241
216
 
242
217
def merge(other_revision, base_revision,
243
218
          check_clean=True, ignore_zero=False,
254
229
    check_clean
255
230
        If true, this_dir must have no uncommitted changes before the
256
231
        merge begins.
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.
260
 
 
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.
263
234
    """
 
235
    from bzrlib.revision import common_ancestor, MultipleRevisionSources
 
236
    from bzrlib.errors import NoSuchRevision
264
237
    tempdir = tempfile.mkdtemp(prefix="bzr-")
265
238
    try:
266
239
        if this_dir is None:
267
240
            this_dir = '.'
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")
272
245
        if check_clean:
277
250
        other_branch, other_tree = get_tree(other_revision, tempdir, "other",
278
251
                                            this_branch)
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
287
258
        else:
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]:
293
 
            try:
294
 
                base_rev_id = common_ancestor(this_rev_id, other_basis, 
295
 
                                              this_branch)
296
 
            except NoCommonAncestor:
 
262
            base_rev_id = common_ancestor(this_rev_id, other_basis, 
 
263
                                          this_branch)
 
264
            if base_rev_id is None:
297
265
                raise UnrelatedBranches()
298
266
            base_tree = get_revid_tree(this_branch, base_rev_id, tempdir, 
299
267
                                       "base", None)
301
269
        else:
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
307
275
            else:
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,
311
 
                                           this_branch)
 
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, 
 
280
                                                                       base_branch))
 
281
            else:
 
282
                base_is_ancestor = False
312
283
        if file_list is None:
313
284
            interesting_ids = None
314
285
        else:
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)
334
304
    finally:
335
305
        shutil.rmtree(tempdir)
370
340
 
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)
376
347
 
380
351
            if path == '.':
381
352
                path = ''
382
353
            else:
383
 
                assert path.startswith('.' + os.sep), "path is %s" % path
 
354
                assert path.startswith('./'), "path is %s" % path
384
355
            path = path[2:]
385
356
        adjust_ids.append((path, id))
386
357
    if len(adjust_ids) > 0: