~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

[merge] bzr.dev 2255

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
import os
19
19
import errno
20
 
from tempfile import mkdtemp
21
20
import warnings
22
21
 
 
22
from bzrlib import (
 
23
    osutils,
 
24
    )
23
25
from bzrlib.branch import Branch
24
26
from bzrlib.conflicts import ConflictList, Conflict
25
27
from bzrlib.errors import (BzrCommandError,
36
38
                           BinaryFile,
37
39
                           )
38
40
from bzrlib.merge3 import Merge3
39
 
import bzrlib.osutils
40
 
from bzrlib.osutils import rename, pathjoin, rmtree
 
41
from bzrlib.osutils import rename, pathjoin
41
42
from progress import DummyProgress, ProgressPhase
42
43
from bzrlib.revision import common_ancestor, is_ancestor, NULL_REVISION
43
44
from bzrlib.textfile import check_text_lines
44
45
from bzrlib.trace import mutter, warning, note
45
46
from bzrlib.transform import (TreeTransform, resolve_conflicts, cook_conflicts,
46
 
                              FinalPaths, create_by_entry, unique_add)
 
47
                              FinalPaths, create_by_entry, unique_add,
 
48
                              ROOT_PARENT)
47
49
from bzrlib.versionedfile import WeaveMerge
48
50
from bzrlib import ui
49
51
 
139
141
 
140
142
    def check_basis(self, check_clean, require_commits=True):
141
143
        if self.this_basis is None and require_commits is True:
142
 
            raise BzrCommandError("This branch has no commits")
 
144
            raise BzrCommandError("This branch has no commits."
 
145
                                  " (perhaps you would prefer 'bzr pull')")
143
146
        if check_clean:
144
147
            self.compare_basis()
145
148
            if self.this_basis != self.this_rev_id:
221
224
        mutter("doing merge() with no base_revision specified")
222
225
        if base_revision == [None, None]:
223
226
            try:
224
 
                pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
227
                pb = ui.ui_factory.nested_progress_bar()
225
228
                try:
226
229
                    this_repo = self.this_branch.repository
227
230
                    self.base_rev_id = common_ancestor(self.this_basis, 
324
327
            else:
325
328
                parent = by_path[os.path.dirname(path)]
326
329
            abspath = pathjoin(self.this_tree.basedir, path)
327
 
            kind = bzrlib.osutils.file_kind(abspath)
 
330
            kind = osutils.file_kind(abspath)
328
331
            if file_id in self.base_tree.inventory:
329
332
                executable = getattr(self.base_tree.inventory[file_id], 'executable', False)
330
333
            else:
373
376
        else:
374
377
            all_ids = set(base_tree)
375
378
            all_ids.update(other_tree)
376
 
        working_tree.lock_write()
 
379
        working_tree.lock_tree_write()
377
380
        self.tt = TreeTransform(working_tree, self.pb)
378
381
        try:
379
382
            self.pp.next_phase()
386
389
                    self.merge_executable(file_id, file_status)
387
390
            finally:
388
391
                child_pb.finished()
389
 
                
 
392
            self.fix_root()
390
393
            self.pp.next_phase()
391
394
            child_pb = ui.ui_factory.nested_progress_bar()
392
395
            try:
408
411
            working_tree.unlock()
409
412
            self.pb.clear()
410
413
 
 
414
    def fix_root(self):
 
415
        try:
 
416
            self.tt.final_kind(self.tt.root)
 
417
        except NoSuchFile:
 
418
            self.tt.cancel_deletion(self.tt.root)
 
419
        if self.tt.final_file_id(self.tt.root) is None:
 
420
            self.tt.version_file(self.tt.tree_file_id(self.tt.root), 
 
421
                                 self.tt.root)
 
422
        if self.other_tree.inventory.root is None:
 
423
            return
 
424
        other_root_file_id = self.other_tree.inventory.root.file_id
 
425
        other_root = self.tt.trans_id_file_id(other_root_file_id)
 
426
        if other_root == self.tt.root:
 
427
            return
 
428
        try:
 
429
            self.tt.final_kind(other_root)
 
430
        except NoSuchFile:
 
431
            return
 
432
        self.reparent_children(self.other_tree.inventory.root, self.tt.root)
 
433
        self.tt.cancel_creation(other_root)
 
434
        self.tt.cancel_versioning(other_root)
 
435
 
 
436
    def reparent_children(self, ie, target):
 
437
        for thing, child in ie.children.iteritems():
 
438
            trans_id = self.tt.trans_id_file_id(child.file_id)
 
439
            self.tt.adjust_path(self.tt.final_name(trans_id), target, trans_id)
 
440
 
411
441
    def write_modified(self, results):
412
442
        modified_hashes = {}
413
443
        for path in results.modified_paths:
518
548
                        "conflict": other_entry}
519
549
        trans_id = self.tt.trans_id_file_id(file_id)
520
550
        parent_id = winner_entry[parent_id_winner].parent_id
521
 
        parent_trans_id = self.tt.trans_id_file_id(parent_id)
522
 
        self.tt.adjust_path(winner_entry[name_winner].name, parent_trans_id,
523
 
                            trans_id)
 
551
        if parent_id is not None:
 
552
            parent_trans_id = self.tt.trans_id_file_id(parent_id)
 
553
            self.tt.adjust_path(winner_entry[name_winner].name, 
 
554
                                parent_trans_id, trans_id)
524
555
 
525
556
    def merge_contents(self, file_id):
526
557
        """Performa a merge on file_id contents."""
542
573
            parent_id = self.tt.final_parent(trans_id)
543
574
            if file_id in self.this_tree.inventory:
544
575
                self.tt.unversion_file(trans_id)
545
 
                self.tt.delete_contents(trans_id)
 
576
                if file_id in self.this_tree:
 
577
                    self.tt.delete_contents(trans_id)
546
578
            file_group = self._dump_conflicts(name, parent_id, file_id, 
547
579
                                              set_version=True)
548
580
            self._raw_conflicts.append(('contents conflict', file_group))
868
900
        will be dumped, and a will be conflict noted.
869
901
        """
870
902
        import bzrlib.patch
871
 
        temp_dir = mkdtemp(prefix="bzr-")
 
903
        temp_dir = osutils.mkdtemp(prefix="bzr-")
872
904
        try:
873
905
            new_file = pathjoin(temp_dir, "new")
874
906
            this = self.dump_file(temp_dir, "this", self.this_tree, file_id)
888
920
                self._dump_conflicts(name, parent_id, file_id)
889
921
                self._raw_conflicts.append(('text conflict', trans_id))
890
922
        finally:
891
 
            rmtree(temp_dir)
 
923
            osutils.rmtree(temp_dir)
892
924
 
893
925
 
894
926
def merge_inner(this_branch, other_tree, base_tree, ignore_zero=False,