~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

  • Committer: Martin Pool
  • Date: 2005-11-11 23:22:03 UTC
  • mto: (1185.33.43 bzr.dev)
  • mto: This revision was merged to the branch mainline in revision 1512.
  • Revision ID: mbp@sourcefrog.net-20051111232203-e1c6e4437f6fe9a7
Don't enable default logging twice

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
import bzrlib.osutils
24
24
import bzrlib.revision
25
25
from bzrlib.merge_core import merge_flex, ApplyMerge3, BackupBeforeChange
 
26
from bzrlib.merge_core import WeaveMerge
26
27
from bzrlib.changeset import generate_changeset, ExceptionConflictHandler
27
28
from bzrlib.changeset import Inventory, Diff3Merge, ReplaceContents
28
29
from bzrlib.branch import Branch
29
30
from bzrlib.errors import BzrCommandError, UnrelatedBranches, NoCommonAncestor
30
 
from bzrlib.errors import NoCommits
 
31
from bzrlib.errors import NoCommits, WorkingTreeNotRevision, NotBranchError
31
32
from bzrlib.delta import compare_trees
32
33
from bzrlib.trace import mutter, warning, note
33
34
from bzrlib.fetch import greedy_fetch, fetch
34
 
from bzrlib.revision import is_ancestor
 
35
from bzrlib.revision import is_ancestor, NULL_REVISION
35
36
from bzrlib.osutils import rename
36
37
from bzrlib.revision import common_ancestor, MultipleRevisionSources
37
38
from bzrlib.errors import NoSuchRevision
38
39
 
 
40
# TODO: Report back as changes are merged in
 
41
 
39
42
# TODO: build_working_dir can be built on something simpler than merge()
40
43
 
41
44
# FIXME: merge() parameters seem oriented towards the command line
83
86
        for line in lines:
84
87
            d_file.write(line)
85
88
 
86
 
    def add_suffix(self, name, suffix, last_new_name=None):
 
89
    def add_suffix(self, name, suffix, last_new_name=None, fix_inventory=True):
87
90
        """Rename a file to append a suffix.  If the new name exists, the
88
91
        suffix is added repeatedly until a non-existant name is found
89
92
 
96
99
        new_name = last_new_name+suffix
97
100
        try:
98
101
            rename(name, new_name)
99
 
            return new_name
 
102
            if fix_inventory is True:
 
103
                try:
 
104
                    relpath = self.this_tree.relpath(name)
 
105
                except NotBranchError:
 
106
                    relpath = None
 
107
                if relpath is not None:
 
108
                    file_id = self.this_tree.path2id(relpath)
 
109
                    if file_id is not None:
 
110
                        new_path = self.this_tree.relpath(new_name)
 
111
                        rename(new_name, name)
 
112
                        self.this_tree.branch.rename_one(relpath, new_path)
 
113
                        assert self.this_tree.id2path(file_id) == relpath
 
114
                        self.this_tree._inventory = self.this_tree.read_working_inventory()
 
115
                        assert self.this_tree.id2path(file_id) == new_path
100
116
        except OSError, e:
101
117
            if e.errno != errno.EEXIST and e.errno != errno.ENOTEMPTY:
102
118
                raise
103
 
            return self.add_suffix(name, suffix, last_new_name=new_name)
 
119
            return self.add_suffix(name, suffix, last_new_name=new_name, 
 
120
                                   fix_inventory=fix_inventory)
 
121
        return new_name
104
122
 
105
123
    def conflict(self, text):
106
124
        warning(text)
116
134
        :param base_path: Path to the file text for the BASE tree
117
135
        :param other_path: Path to the file text for the OTHER tree
118
136
        """
119
 
        self.add_suffix(this_path, ".THIS")
 
137
        self.add_suffix(this_path, ".THIS", fix_inventory=False)
120
138
        self.dump(base_lines, this_path+".BASE")
121
139
        self.dump(other_lines, this_path+".OTHER")
122
140
        rename(new_file, this_path)
123
141
        self.conflict("Diff3 conflict encountered in %s" % this_path)
124
142
 
 
143
    def weave_merge_conflict(self, filename, weave, other_i, out_file):
 
144
        """
 
145
        Handle weave conflicts by producing a .THIS, and .OTHER.  The
 
146
        main file will be a version with diff3-style conflicts.
 
147
        """
 
148
        self.add_suffix(filename, ".THIS")
 
149
        out_file.commit()
 
150
        self.dump(weave.get_iter(other_i), filename+".OTHER")
 
151
        self.conflict("Text conflict encountered in %s" % filename)
 
152
 
125
153
    def new_contents_conflict(self, filename, other_contents):
126
154
        """Conflicting contents for newly added file."""
127
 
        other.contents.apply(filename + ".OTHER")
 
155
        other_contents(filename + ".OTHER", self, False)
128
156
        self.conflict("Conflict in newly added file %s" % filename)
129
157
    
130
158
 
206
234
        revision = branch.last_revision()
207
235
    else:
208
236
        revision = branch.get_rev_id(revno)
 
237
        if revision is None:
 
238
            revision = NULL_REVISION
209
239
    return branch, get_revid_tree(branch, revision, local_branch)
210
240
 
211
241
def get_revid_tree(branch, revision, local_branch):
235
265
    lower-level code (e.g. constructing a changeset).
236
266
    """
237
267
    # RBC 20051019 is this not just 'export' ?
238
 
    merge((to_dir, -1), (to_dir, 0), this_dir=to_dir,
239
 
          check_clean=False, ignore_zero=True)
 
268
    # Well, export doesn't take care of inventory...
 
269
    this_branch = Branch.open_containing(to_dir)[0]
 
270
    transform_tree(this_branch.working_tree(), this_branch.basis_tree())
240
271
 
 
272
def transform_tree(from_tree, to_tree):
 
273
    merge_inner(from_tree.branch, to_tree, from_tree, ignore_zero=True)
241
274
 
242
275
def merge(other_revision, base_revision,
243
276
          check_clean=True, ignore_zero=False,
244
277
          this_dir=None, backup_files=False, merge_type=ApplyMerge3,
245
 
          file_list=None):
 
278
          file_list=None, show_base=False, reprocess=False):
246
279
    """Merge changes into a tree.
247
280
 
248
281
    base_revision
249
 
        tuple(path, revision) Base for three-way merge.
 
282
        list(path, revno) Base for three-way merge.  
 
283
        If [None, None] then a base will be automatically determined.
250
284
    other_revision
251
 
        tuple(path, revision) Other revision for three-way merge.
 
285
        list(path, revno) Other revision for three-way merge.
252
286
    this_dir
253
287
        Directory to merge changes into; '.' by default.
254
288
    check_clean
257
291
    ignore_zero - If true, suppress the "zero conflicts" message when 
258
292
        there are no conflicts; should be set when doing something we expect
259
293
        to complete perfectly.
 
294
    file_list - If supplied, merge only changes to selected files.
260
295
 
261
296
    All available ancestors of other_revision and base_revision are
262
297
    automatically pulled into the branch.
 
298
 
 
299
    The revno may be -1 to indicate the last revision on the branch, which is
 
300
    the typical case.
 
301
 
 
302
    This function is intended for use from the command line; programmatic
 
303
    clients might prefer to call merge_inner(), which has less magic behavior.
263
304
    """
264
305
    if this_dir is None:
265
306
        this_dir = '.'
266
307
    this_branch = Branch.open_containing(this_dir)[0]
267
 
    this_rev_id = this_branch.last_revision()
268
 
    if this_rev_id is None:
269
 
        raise BzrCommandError("This branch has no commits")
270
 
    if check_clean:
271
 
        changes = compare_trees(this_branch.working_tree(), 
272
 
                                this_branch.basis_tree(), False)
273
 
        if changes.has_changed():
274
 
            raise BzrCommandError("Working tree has uncommitted changes.")
275
 
    other_branch, other_tree = get_tree(other_revision, this_branch)
276
 
    if other_revision[1] == -1:
277
 
        other_rev_id = other_branch.last_revision()
278
 
        if other_rev_id is None:
279
 
            raise NoCommits(other_branch)
280
 
        other_basis = other_rev_id
281
 
    elif other_revision[1] is not None:
282
 
        other_rev_id = other_branch.get_rev_id(other_revision[1])
283
 
        other_basis = other_rev_id
284
 
    else:
285
 
        other_rev_id = None
286
 
        other_basis = other_branch.last_revision()
287
 
        if other_basis is None:
288
 
            raise NoCommits(other_branch)
289
 
    if base_revision == [None, None]:
290
 
        try:
291
 
            base_rev_id = common_ancestor(this_rev_id, other_basis, 
292
 
                                          this_branch)
293
 
        except NoCommonAncestor:
294
 
            raise UnrelatedBranches()
295
 
        base_tree = get_revid_tree(this_branch, base_rev_id, None)
296
 
        base_is_ancestor = True
297
 
    else:
298
 
        base_branch, base_tree = get_tree(base_revision)
299
 
        if base_revision[1] == -1:
300
 
            base_rev_id = base_branch.last_revision()
301
 
        elif base_revision[1] is None:
302
 
            base_rev_id = None
 
308
    if show_base and not merge_type is ApplyMerge3:
 
309
        raise BzrCommandError("Show-base is not supported for this merge"
 
310
                              " type. %s" % merge_type)
 
311
    if reprocess and not merge_type is ApplyMerge3:
 
312
        raise BzrCommandError("Reprocess is not supported for this merge"
 
313
                              " type. %s" % merge_type)
 
314
    if reprocess and show_base:
 
315
        raise BzrCommandError("Cannot reprocess and show base.")
 
316
    merger = Merger(this_branch)
 
317
    merger.check_basis(check_clean)
 
318
    merger.set_other(other_revision)
 
319
    merger.set_base(base_revision)
 
320
    merger.backup_files = backup_files
 
321
    merger.merge_type = merge_type 
 
322
    merger.set_interesting_files(file_list)
 
323
    merger.show_base = show_base 
 
324
    merger.reprocess = reprocess
 
325
    merger.conflict_handler = MergeConflictHandler(merger.this_tree, 
 
326
                                                   merger.base_tree, 
 
327
                                                   merger.other_tree,
 
328
                                                   ignore_zero=ignore_zero)
 
329
    conflicts = merger.do_merge()
 
330
    merger.set_pending()
 
331
    return conflicts
 
332
 
 
333
def merge_inner(this_branch, other_tree, base_tree, ignore_zero=False,
 
334
                backup_files=False, merge_type=ApplyMerge3, 
 
335
                interesting_ids=None, show_base=False, reprocess=False):
 
336
    """Primary interface for merging. 
 
337
 
 
338
        typical use is probably 
 
339
        'merge_inner(branch, branch.get_revision_tree(other_revision),
 
340
                     branch.get_revision_tree(base_revision))'
 
341
        """
 
342
    merger = Merger(this_branch, other_tree, base_tree)
 
343
    merger.backup_files = False
 
344
    merger.merge_type = ApplyMerge3
 
345
    merger.interesting_ids = interesting_ids
 
346
    merger.show_base = show_base 
 
347
    merger.reprocess = reprocess
 
348
    merger.conflict_handler = MergeConflictHandler(merger.this_tree, base_tree, 
 
349
                                                   other_tree,
 
350
                                                   ignore_zero=ignore_zero)
 
351
    return merger.do_merge()
 
352
 
 
353
 
 
354
class Merger(object):
 
355
    def __init__(self, this_branch, other_tree=None, base_tree=None):
 
356
        object.__init__(self)
 
357
        self.this_branch = this_branch
 
358
        self.this_basis = this_branch.last_revision()
 
359
        self.this_rev_id = None
 
360
        self.this_tree = this_branch.working_tree()
 
361
        self.this_revision_tree = None
 
362
        self.other_tree = other_tree
 
363
        self.base_tree = base_tree
 
364
        self.ignore_zero = False
 
365
        self.backup_files = False
 
366
        self.interesting_ids = None
 
367
        self.show_base = False
 
368
        self.reprocess = False
 
369
        self.conflict_handler = MergeConflictHandler(self.this_tree, base_tree, 
 
370
                                                     other_tree)
 
371
 
 
372
    def revision_tree(self, revision_id):
 
373
        return self.this_branch.revision_tree(revision_id)
 
374
 
 
375
    def ensure_revision_trees(self):
 
376
        if self.this_revision_tree is None:
 
377
            if self.this_rev_id is None:
 
378
                self.compare_basis()
 
379
            if self.this_rev_id is None:
 
380
                raise WorkingTreeNotRevision(self.this_tree)
 
381
            self.this_revision_tree = self.this_branch.revision_tree(
 
382
                self.this_rev_id)
 
383
 
 
384
        if self.other_rev_id is None:
 
385
            other_basis_tree = self.revision_tree(self.other_basis)
 
386
            changes = compare_trees(self.other_tree, other_basis_tree)
 
387
            if changes.has_changed():
 
388
                raise WorkingTreeNotRevision(self.this_tree)
 
389
            other_rev_id = other_basis
 
390
            self.other_tree = other_basis_tree
 
391
 
 
392
 
 
393
    def file_revisions(self, file_id):
 
394
        self.ensure_revision_trees()
 
395
        def get_id(tree, file_id):
 
396
            revision_id = tree.inventory[file_id].revision
 
397
            assert revision_id is not None
 
398
            return revision_id
 
399
        trees = (self.this_revision_tree, self.other_tree)
 
400
        return [get_id(tree, file_id) for tree in trees]
 
401
            
 
402
 
 
403
    def merge_factory(self, file_id, base, other):
 
404
        if self.merge_type.history_based:
 
405
            t_revid, o_revid = self.file_revisions(file_id)
 
406
            weave = self.this_revision_tree.get_weave(file_id)
 
407
            contents_change = self.merge_type(weave, t_revid, o_revid)
303
408
        else:
304
 
            base_rev_id = base_branch.get_rev_id(base_revision[1])
305
 
        fetch(from_branch=base_branch, to_branch=this_branch)
306
 
        base_is_ancestor = is_ancestor(this_rev_id, base_rev_id,
307
 
                                       this_branch)
308
 
    if file_list is None:
309
 
        interesting_ids = None
310
 
    else:
 
409
            if self.show_base is True or self.reprocess is True:
 
410
                contents_change = self.merge_type(file_id, base, other, 
 
411
                                                  show_base=self.show_base, 
 
412
                                                  reprocess=self.reprocess)
 
413
            else:
 
414
                contents_change = self.merge_type(file_id, base, other)
 
415
        if self.backup_files:
 
416
            contents_change = BackupBeforeChange(contents_change)
 
417
        return contents_change
 
418
 
 
419
    def check_basis(self, check_clean):
 
420
        if self.this_basis is None:
 
421
            raise BzrCommandError("This branch has no commits")
 
422
        if check_clean:
 
423
            self.compare_basis()
 
424
            if self.this_basis != self.this_rev_id:
 
425
                raise BzrCommandError("Working tree has uncommitted changes.")
 
426
 
 
427
    def compare_basis(self):
 
428
        changes = compare_trees(self.this_branch.working_tree(), 
 
429
                                self.this_branch.basis_tree(), False)
 
430
        if not changes.has_changed():
 
431
            self.this_rev_id = self.this_basis
 
432
 
 
433
    def set_interesting_files(self, file_list):
 
434
        if file_list is None:
 
435
            self.interesting_ids = None
 
436
            return
 
437
 
311
438
        interesting_ids = set()
312
 
        this_tree = this_branch.working_tree()
313
439
        for fname in file_list:
314
 
            path = this_tree.relpath(fname)
 
440
            path = self.this_tree.relpath(fname)
315
441
            found_id = False
316
 
            for tree in (this_tree, base_tree, other_tree):
 
442
            for tree in (self.this_tree, self.base_tree, self.other_tree):
317
443
                file_id = tree.inventory.path2id(path)
318
444
                if file_id is not None:
319
445
                    interesting_ids.add(file_id)
321
447
            if not found_id:
322
448
                raise BzrCommandError("%s is not a source file in any"
323
449
                                      " tree." % fname)
324
 
    merge_inner(this_branch, other_tree, base_tree, tempdir=None, 
325
 
                ignore_zero=ignore_zero, backup_files=backup_files, 
326
 
                merge_type=merge_type, interesting_ids=interesting_ids)
327
 
    if base_is_ancestor and other_rev_id is not None\
328
 
        and other_rev_id not in this_branch.revision_history():
329
 
        this_branch.add_pending_merge(other_rev_id)
330
 
 
331
 
 
332
 
def set_interesting(inventory_a, inventory_b, interesting_ids):
333
 
    """Mark files whose ids are in interesting_ids as interesting
334
 
    """
335
 
    for inventory in (inventory_a, inventory_b):
336
 
        for path, source_file in inventory.iteritems():
337
 
             source_file.interesting = source_file.id in interesting_ids
338
 
 
339
 
 
340
 
def merge_inner(this_branch, other_tree, base_tree, tempdir=None, 
341
 
                ignore_zero=False, merge_type=ApplyMerge3, backup_files=False,
342
 
                interesting_ids=None):
343
 
    """Primary interface for merging. 
344
 
 
345
 
    typical use is probably 
346
 
    'merge_inner(branch, branch.get_revision_tree(other_revision),
347
 
                 branch.get_revision_tree(base_revision))'
348
 
    """
349
 
    if tempdir is None:
350
 
        _tempdir = tempfile.mkdtemp(prefix="bzr-")
351
 
    else:
352
 
        _tempdir = tempdir
353
 
    try:
354
 
        _merge_inner(this_branch, other_tree, base_tree, _tempdir,
355
 
                     ignore_zero, merge_type, backup_files, interesting_ids)
356
 
    finally:
357
 
        if tempdir is None:
358
 
            shutil.rmtree(_tempdir)
359
 
 
360
 
 
361
 
def _merge_inner(this_branch, other_tree, base_tree, user_tempdir, 
362
 
                ignore_zero=False, merge_type=ApplyMerge3, backup_files=False,
363
 
                interesting_ids=None):
364
 
    def merge_factory(file_id, base, other):
365
 
        contents_change = merge_type(file_id, base, other)
366
 
        if backup_files:
367
 
            contents_change = BackupBeforeChange(contents_change)
368
 
        return contents_change
369
 
 
370
 
    this_tree = get_tree((this_branch.base, None))[1]
371
 
 
372
 
    def get_inventory(tree):
373
 
        return tree.inventory
374
 
 
375
 
    inv_changes = merge_flex(this_tree, base_tree, other_tree,
376
 
                             generate_changeset, get_inventory,
377
 
                             MergeConflictHandler(this_tree, base_tree,
378
 
                             other_tree, ignore_zero=ignore_zero),
379
 
                             merge_factory=merge_factory, 
380
 
                             interesting_ids=interesting_ids)
381
 
 
382
 
    adjust_ids = []
383
 
    for id, path in inv_changes.iteritems():
384
 
        if path is not None:
385
 
            if path == '.':
386
 
                path = ''
 
450
        self.interesting_ids = interesting_ids
 
451
 
 
452
    def set_pending(self):
 
453
        if not self.base_is_ancestor:
 
454
            return
 
455
        if self.other_rev_id is None:
 
456
            return
 
457
        if self.other_rev_id in self.this_branch.get_ancestry(self.this_basis):
 
458
            return
 
459
        self.this_branch.add_pending_merge(self.other_rev_id)
 
460
 
 
461
    def set_other(self, other_revision):
 
462
        other_branch, self.other_tree = get_tree(other_revision, 
 
463
                                                 self.this_branch)
 
464
        if other_revision[1] == -1:
 
465
            self.other_rev_id = other_branch.last_revision()
 
466
            if self.other_rev_id is None:
 
467
                raise NoCommits(other_branch)
 
468
            self.other_basis = self.other_rev_id
 
469
        elif other_revision[1] is not None:
 
470
            self.other_rev_id = other_branch.get_rev_id(other_revision[1])
 
471
            self.other_basis = self.other_rev_id
 
472
        else:
 
473
            self.other_rev_id = None
 
474
            self.other_basis = other_branch.last_revision()
 
475
            if self.other_basis is None:
 
476
                raise NoCommits(other_branch)
 
477
        fetch(from_branch=other_branch, to_branch=self.this_branch, 
 
478
              last_revision=self.other_basis)
 
479
 
 
480
    def set_base(self, base_revision):
 
481
        mutter("doing merge() with no base_revision specified")
 
482
        if base_revision == [None, None]:
 
483
            try:
 
484
                self.base_rev_id = common_ancestor(self.this_basis, 
 
485
                                                   self.other_basis, 
 
486
                                                   self.this_branch)
 
487
            except NoCommonAncestor:
 
488
                raise UnrelatedBranches()
 
489
            self.base_tree = get_revid_tree(self.this_branch, self.base_rev_id,
 
490
                                            None)
 
491
            self.base_is_ancestor = True
 
492
        else:
 
493
            base_branch, self.base_tree = get_tree(base_revision)
 
494
            if base_revision[1] == -1:
 
495
                self.base_rev_id = base_branch.last_revision()
 
496
            elif base_revision[1] is None:
 
497
                self.base_rev_id = None
387
498
            else:
388
 
                assert path.startswith('.' + os.sep), "path is %s" % path
389
 
            path = path[2:]
390
 
        adjust_ids.append((path, id))
391
 
    if len(adjust_ids) > 0:
392
 
        this_branch.set_inventory(regen_inventory(this_branch, 
393
 
                                                  this_tree.basedir,
394
 
                                                  adjust_ids))
395
 
 
396
 
 
397
 
def regen_inventory(this_branch, root, new_entries):
398
 
    old_entries = this_branch.read_working_inventory()
399
 
    new_inventory = {}
400
 
    by_path = {}
401
 
    new_entries_map = {} 
402
 
    for path, file_id in new_entries:
403
 
        if path is None:
404
 
            continue
405
 
        new_entries_map[file_id] = path
406
 
 
407
 
    def id2path(file_id):
408
 
        path = new_entries_map.get(file_id)
409
 
        if path is not None:
410
 
            return path
411
 
        entry = old_entries[file_id]
412
 
        if entry.parent_id is None:
413
 
            return entry.name
414
 
        return os.path.join(id2path(entry.parent_id), entry.name)
 
499
                self.base_rev_id = base_branch.get_rev_id(base_revision[1])
 
500
            fetch(from_branch=base_branch, to_branch=self.this_branch)
 
501
            self.base_is_ancestor = is_ancestor(self.this_basis, 
 
502
                                                self.base_rev_id,
 
503
                                                self.this_branch)
 
504
 
 
505
    def do_merge(self):
 
506
        def get_inventory(tree):
 
507
            return tree.inventory
 
508
 
 
509
        inv_changes = merge_flex(self.this_tree, self.base_tree, 
 
510
                                 self.other_tree,
 
511
                                 generate_changeset, get_inventory,
 
512
                                 self.conflict_handler,
 
513
                                 merge_factory=self.merge_factory, 
 
514
                                 interesting_ids=self.interesting_ids)
 
515
 
 
516
        adjust_ids = []
 
517
        for id, path in inv_changes.iteritems():
 
518
            if path is not None:
 
519
                if path == '.':
 
520
                    path = ''
 
521
                else:
 
522
                    assert path.startswith('.' + os.sep), "path is %s" % path
 
523
                path = path[2:]
 
524
            adjust_ids.append((path, id))
 
525
        if len(adjust_ids) > 0:
 
526
            self.this_branch.working_tree().set_inventory(self.regen_inventory(adjust_ids))
 
527
        conflicts = self.conflict_handler.conflicts
 
528
        self.conflict_handler.finalize()
 
529
        return conflicts
 
530
 
 
531
    def regen_inventory(self, new_entries):
 
532
        old_entries = self.this_branch.working_tree().read_working_inventory()
 
533
        new_inventory = {}
 
534
        by_path = {}
 
535
        new_entries_map = {} 
 
536
        for path, file_id in new_entries:
 
537
            if path is None:
 
538
                continue
 
539
            new_entries_map[file_id] = path
 
540
 
 
541
        def id2path(file_id):
 
542
            path = new_entries_map.get(file_id)
 
543
            if path is not None:
 
544
                return path
 
545
            entry = old_entries[file_id]
 
546
            if entry.parent_id is None:
 
547
                return entry.name
 
548
            return os.path.join(id2path(entry.parent_id), entry.name)
 
549
            
 
550
        for file_id in old_entries:
 
551
            entry = old_entries[file_id]
 
552
            path = id2path(file_id)
 
553
            new_inventory[file_id] = (path, file_id, entry.parent_id, 
 
554
                                      entry.kind)
 
555
            by_path[path] = file_id
415
556
        
416
 
    for file_id in old_entries:
417
 
        entry = old_entries[file_id]
418
 
        path = id2path(file_id)
419
 
        new_inventory[file_id] = (path, file_id, entry.parent_id, entry.kind)
420
 
        by_path[path] = file_id
421
 
    
422
 
    deletions = 0
423
 
    insertions = 0
424
 
    new_path_list = []
425
 
    for path, file_id in new_entries:
426
 
        if path is None:
427
 
            del new_inventory[file_id]
428
 
            deletions += 1
429
 
        else:
430
 
            new_path_list.append((path, file_id))
431
 
            if file_id not in old_entries:
432
 
                insertions += 1
433
 
    # Ensure no file is added before its parent
434
 
    new_path_list.sort()
435
 
    for path, file_id in new_path_list:
436
 
        if path == '':
437
 
            parent = None
438
 
        else:
439
 
            parent = by_path[os.path.dirname(path)]
440
 
        kind = bzrlib.osutils.file_kind(os.path.join(root, path))
441
 
        new_inventory[file_id] = (path, file_id, parent, kind)
442
 
        by_path[path] = file_id 
 
557
        deletions = 0
 
558
        insertions = 0
 
559
        new_path_list = []
 
560
        for path, file_id in new_entries:
 
561
            if path is None:
 
562
                del new_inventory[file_id]
 
563
                deletions += 1
 
564
            else:
 
565
                new_path_list.append((path, file_id))
 
566
                if file_id not in old_entries:
 
567
                    insertions += 1
 
568
        # Ensure no file is added before its parent
 
569
        new_path_list.sort()
 
570
        for path, file_id in new_path_list:
 
571
            if path == '':
 
572
                parent = None
 
573
            else:
 
574
                parent = by_path[os.path.dirname(path)]
 
575
            abspath = os.path.join(self.this_tree.basedir, path)
 
576
            kind = bzrlib.osutils.file_kind(abspath)
 
577
            new_inventory[file_id] = (path, file_id, parent, kind)
 
578
            by_path[path] = file_id 
443
579
 
444
 
    # Get a list in insertion order
445
 
    new_inventory_list = new_inventory.values()
446
 
    mutter ("""Inventory regeneration:
447
 
old length: %i insertions: %i deletions: %i new_length: %i"""\
448
 
        % (len(old_entries), insertions, deletions, len(new_inventory_list)))
449
 
    assert len(new_inventory_list) == len(old_entries) + insertions - deletions
450
 
    new_inventory_list.sort()
451
 
    return new_inventory_list
 
580
        # Get a list in insertion order
 
581
        new_inventory_list = new_inventory.values()
 
582
        mutter ("""Inventory regeneration:
 
583
    old length: %i insertions: %i deletions: %i new_length: %i"""\
 
584
            % (len(old_entries), insertions, deletions, 
 
585
               len(new_inventory_list)))
 
586
        assert len(new_inventory_list) == len(old_entries) + insertions\
 
587
            - deletions
 
588
        new_inventory_list.sort()
 
589
        return new_inventory_list
452
590
 
453
591
merge_types = {     "merge3": (ApplyMerge3, "Native diff3-style merge"), 
454
 
                     "diff3": (Diff3Merge,  "Merge using external diff3")
 
592
                     "diff3": (Diff3Merge,  "Merge using external diff3"),
 
593
                     'weave': (WeaveMerge, "Weave-based merge")
455
594
              }
456
595