~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-10-11 09:26:57 UTC
  • mfrom: (1996.3.37 use_lazy_importer)
  • Revision ID: pqm@pqm.ubuntu.com-20061011092657-e42bec6ef14c036c
(John Arbash Meinel) use lazy importing to improve startup time

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
WorkingTree.open(dir).
30
30
"""
31
31
 
32
 
MERGE_MODIFIED_HEADER_1 = "BZR merge-modified list format 1"
33
 
CONFLICT_HEADER_1 = "BZR conflict list format 1"
34
 
 
35
32
# TODO: Give the workingtree sole responsibility for the working inventory;
36
33
# remove the variable and references to it from the branch.  This may require
37
34
# updating the commit code so as to update the inventory within the working
39
36
# At the moment they may alias the inventory and have old copies of it in
40
37
# memory.  (Now done? -- mbp 20060309)
41
38
 
42
 
from binascii import hexlify
 
39
from cStringIO import StringIO
 
40
import os
 
41
import re
 
42
 
 
43
from bzrlib.lazy_import import lazy_import
 
44
lazy_import(globals(), """
43
45
import collections
44
46
from copy import deepcopy
45
 
from cStringIO import StringIO
46
47
import errno
47
48
import fnmatch
48
 
import os
49
 
import re
50
49
import stat
51
50
from time import time
52
51
import warnings
53
52
 
54
53
import bzrlib
55
 
from bzrlib import bzrdir, errors, ignores, osutils, urlutils
56
 
from bzrlib.atomicfile import AtomicFile
 
54
from bzrlib import (
 
55
    bzrdir,
 
56
    conflicts as _mod_conflicts,
 
57
    errors,
 
58
    ignores,
 
59
    merge,
 
60
    osutils,
 
61
    urlutils,
 
62
    textui,
 
63
    transform,
 
64
    xml5,
 
65
    xml6,
 
66
    )
57
67
import bzrlib.branch
58
 
from bzrlib.conflicts import Conflict, ConflictList, CONFLICT_SUFFIXES
 
68
from bzrlib.transport import get_transport
 
69
import bzrlib.ui
 
70
""")
 
71
 
59
72
from bzrlib.decorators import needs_read_lock, needs_write_lock
60
73
from bzrlib.errors import (BzrCheckError,
61
74
                           BzrError,
70
83
from bzrlib.inventory import InventoryEntry, Inventory
71
84
from bzrlib.lockable_files import LockableFiles, TransportLock
72
85
from bzrlib.lockdir import LockDir
73
 
from bzrlib.merge import merge_inner, transform_tree
74
86
import bzrlib.mutabletree
75
87
from bzrlib.mutabletree import needs_tree_write_lock
76
88
from bzrlib.osutils import (
77
 
                            abspath,
78
 
                            compact_date,
79
 
                            file_kind,
80
 
                            isdir,
81
 
                            getcwd,
82
 
                            pathjoin,
83
 
                            pumpfile,
84
 
                            safe_unicode,
85
 
                            splitpath,
86
 
                            rand_chars,
87
 
                            normpath,
88
 
                            realpath,
89
 
                            relpath,
90
 
                            rename,
91
 
                            supports_executable,
92
 
                            )
 
89
    compact_date,
 
90
    file_kind,
 
91
    isdir,
 
92
    pathjoin,
 
93
    safe_unicode,
 
94
    splitpath,
 
95
    rand_chars,
 
96
    normpath,
 
97
    realpath,
 
98
    supports_executable,
 
99
    )
 
100
from bzrlib.trace import mutter, note
 
101
from bzrlib.transport.local import LocalTransport
 
102
import bzrlib.tree
93
103
from bzrlib.progress import DummyProgress, ProgressPhase
94
104
from bzrlib.revision import NULL_REVISION
95
105
import bzrlib.revisiontree
101
111
        zero_eight,
102
112
        zero_eleven,
103
113
        )
104
 
from bzrlib.trace import mutter, note
105
 
from bzrlib.transform import build_tree
106
 
from bzrlib.transport import get_transport
107
 
from bzrlib.transport.local import LocalTransport
108
 
from bzrlib.textui import show_status
109
 
import bzrlib.ui
110
 
import bzrlib.xml5
111
 
 
 
114
 
 
115
 
 
116
MERGE_MODIFIED_HEADER_1 = "BZR merge-modified list format 1"
 
117
CONFLICT_HEADER_1 = "BZR conflict list format 1"
112
118
 
113
119
# the regex removes any weird characters; we don't escape them 
114
120
# but rather just pull them out
399
405
        else:
400
406
            try:
401
407
                xml = self.read_basis_inventory()
402
 
                inv = bzrlib.xml6.serializer_v6.read_inventory_from_string(xml)
 
408
                inv = xml6.serializer_v6.read_inventory_from_string(xml)
403
409
                if inv is not None and inv.revision_id == revision_id:
404
 
                    return bzrlib.tree.RevisionTree(self.branch.repository, 
 
410
                    return bzrlib.tree.RevisionTree(self.branch.repository,
405
411
                                                    inv, revision_id)
406
412
            except (NoSuchFile, errors.BadInventoryFormat):
407
413
                pass
463
469
        The path may be absolute or relative. If its a relative path it is 
464
470
        interpreted relative to the python current working directory.
465
471
        """
466
 
        return relpath(self.basedir, path)
 
472
        return osutils.relpath(self.basedir, path)
467
473
 
468
474
    def has_filename(self, filename):
469
475
        return osutils.lexists(self.abspath(filename))
533
539
    def copy_content_into(self, tree, revision_id=None):
534
540
        """Copy the current content and user files of this tree into tree."""
535
541
        if revision_id is None:
536
 
            transform_tree(tree, self)
 
542
            merge.transform_tree(tree, self)
537
543
        else:
538
544
            # TODO now merge from tree.last_revision to revision (to preserve
539
545
            # user local changes)
540
 
            transform_tree(tree, self)
 
546
            merge.transform_tree(tree, self)
541
547
            tree.set_parent_ids([revision_id])
542
548
 
543
549
    def id2abspath(self, file_id):
1011
1017
                result.append((f, dest_path))
1012
1018
                inv.rename(inv.path2id(f), to_dir_id, name_tail)
1013
1019
                try:
1014
 
                    rename(self.abspath(f), self.abspath(dest_path))
 
1020
                    osutils.rename(self.abspath(f), self.abspath(dest_path))
1015
1021
                except OSError, e:
1016
1022
                    raise BzrError("failed to rename %r to %r: %s" %
1017
1023
                                   (f, dest_path, e[1]),
1063
1069
        from_abs = self.abspath(from_rel)
1064
1070
        to_abs = self.abspath(to_rel)
1065
1071
        try:
1066
 
            rename(from_abs, to_abs)
 
1072
            osutils.rename(from_abs, to_abs)
1067
1073
        except OSError, e:
1068
1074
            inv.rename(file_id, from_parent, from_name)
1069
1075
            raise BzrError("failed to rename %r to %r: %s"
1146
1152
                pb = bzrlib.ui.ui_factory.nested_progress_bar()
1147
1153
                try:
1148
1154
                    new_basis_tree = self.branch.basis_tree()
1149
 
                    merge_inner(self.branch,
 
1155
                    merge.merge_inner(
 
1156
                                self.branch,
1150
1157
                                new_basis_tree,
1151
1158
                                basis_tree,
1152
1159
                                this_tree=self,
1428
1435
    def _create_basis_xml_from_inventory(self, revision_id, inventory):
1429
1436
        """Create the text that will be saved in basis-inventory"""
1430
1437
        inventory.revision_id = revision_id
1431
 
        return bzrlib.xml6.serializer_v6.write_inventory_to_string(inventory)
 
1438
        return xml6.serializer_v6.write_inventory_to_string(inventory)
1432
1439
 
1433
1440
    def _cache_basis_inventory(self, new_revision):
1434
1441
        """Cache new_revision as the basis inventory."""
1467
1474
        """Read the working inventory."""
1468
1475
        # ElementTree does its own conversion from UTF-8, so open in
1469
1476
        # binary.
1470
 
        result = bzrlib.xml5.serializer_v5.read_inventory(
 
1477
        result = xml5.serializer_v5.read_inventory(
1471
1478
            self._control_files.get('inventory'))
1472
1479
        self._set_inventory(result)
1473
1480
        return result
1507
1514
                    new_status = 'I'
1508
1515
                else:
1509
1516
                    new_status = '?'
1510
 
                show_status(new_status, inv[fid].kind, f, to_file=to_file)
 
1517
                textui.show_status(new_status, inv[fid].kind, f,
 
1518
                                   to_file=to_file)
1511
1519
            del inv[fid]
1512
1520
 
1513
1521
        self._write_inventory(inv)
1515
1523
    @needs_tree_write_lock
1516
1524
    def revert(self, filenames, old_tree=None, backups=True, 
1517
1525
               pb=DummyProgress()):
1518
 
        from transform import revert
1519
 
        from conflicts import resolve
 
1526
        from bzrlib.conflicts import resolve
1520
1527
        if old_tree is None:
1521
1528
            old_tree = self.basis_tree()
1522
 
        conflicts = revert(self, old_tree, filenames, backups, pb)
 
1529
        conflicts = transform.revert(self, old_tree, filenames, backups, pb)
1523
1530
        if not len(filenames):
1524
1531
            self.set_parent_ids(self.get_parent_ids()[:1])
1525
1532
            resolve(self)
1617
1624
            # merge tree state up to new branch tip.
1618
1625
            basis = self.basis_tree()
1619
1626
            to_tree = self.branch.basis_tree()
1620
 
            result += merge_inner(self.branch,
 
1627
            result += merge.merge_inner(
 
1628
                                  self.branch,
1621
1629
                                  to_tree,
1622
1630
                                  basis,
1623
1631
                                  this_tree=self)
1658
1666
                base_rev_id = None
1659
1667
            base_tree = self.branch.repository.revision_tree(base_rev_id)
1660
1668
            other_tree = self.branch.repository.revision_tree(old_tip)
1661
 
            result += merge_inner(self.branch,
 
1669
            result += merge.merge_inner(
 
1670
                                  self.branch,
1662
1671
                                  other_tree,
1663
1672
                                  base_tree,
1664
1673
                                  this_tree=self)
1668
1677
    def _write_inventory(self, inv):
1669
1678
        """Write inventory as the current inventory."""
1670
1679
        sio = StringIO()
1671
 
        bzrlib.xml5.serializer_v5.write_inventory(inv, sio)
 
1680
        xml5.serializer_v5.write_inventory(inv, sio)
1672
1681
        sio.seek(0)
1673
1682
        self._control_files.put('inventory', sio)
1674
1683
        self._set_inventory(inv)
1682
1691
 
1683
1692
    @needs_read_lock
1684
1693
    def conflicts(self):
1685
 
        conflicts = ConflictList()
 
1694
        conflicts = _mod_conflicts.ConflictList()
1686
1695
        for conflicted in self._iter_conflicts():
1687
1696
            text = True
1688
1697
            try:
1701
1710
                    if text == False:
1702
1711
                        break
1703
1712
            ctype = {True: 'text conflict', False: 'contents conflict'}[text]
1704
 
            conflicts.append(Conflict.factory(ctype, path=conflicted,
 
1713
            conflicts.append(_mod_conflicts.Conflict.factory(ctype,
 
1714
                             path=conflicted,
1705
1715
                             file_id=self.path2id(conflicted)))
1706
1716
        return conflicts
1707
1717
 
1777
1787
    def add_conflicts(self, new_conflicts):
1778
1788
        conflict_set = set(self.conflicts())
1779
1789
        conflict_set.update(set(list(new_conflicts)))
1780
 
        self.set_conflicts(ConflictList(sorted(conflict_set,
1781
 
                                               key=Conflict.sort_key)))
 
1790
        self.set_conflicts(_mod_conflicts.ConflictList(sorted(conflict_set,
 
1791
                                       key=_mod_conflicts.Conflict.sort_key)))
1782
1792
 
1783
1793
    @needs_read_lock
1784
1794
    def conflicts(self):
1785
1795
        try:
1786
1796
            confile = self._control_files.get('conflicts')
1787
1797
        except NoSuchFile:
1788
 
            return ConflictList()
 
1798
            return _mod_conflicts.ConflictList()
1789
1799
        try:
1790
1800
            if confile.next() != CONFLICT_HEADER_1 + '\n':
1791
1801
                raise ConflictFormatError()
1792
1802
        except StopIteration:
1793
1803
            raise ConflictFormatError()
1794
 
        return ConflictList.from_stanzas(RioReader(confile))
 
1804
        return _mod_conflicts.ConflictList.from_stanzas(RioReader(confile))
1795
1805
 
1796
1806
    def unlock(self):
1797
1807
        if self._hashcache.needs_write and self._control_files._lock_count==1:
1804
1814
 
1805
1815
 
1806
1816
def get_conflicted_stem(path):
1807
 
    for suffix in CONFLICT_SUFFIXES:
 
1817
    for suffix in _mod_conflicts.CONFLICT_SUFFIXES:
1808
1818
        if path.endswith(suffix):
1809
1819
            return path[:-len(suffix)]
1810
1820
 
1916
1926
        """
1917
1927
        sio = StringIO()
1918
1928
        inv = Inventory()
1919
 
        bzrlib.xml5.serializer_v5.write_inventory(inv, sio)
 
1929
        xml5.serializer_v5.write_inventory(inv, sio)
1920
1930
        sio.seek(0)
1921
1931
        control_files.put('inventory', sio)
1922
1932
 
1951
1961
        wt.set_root_id(inv.root.file_id)
1952
1962
        basis_tree = branch.repository.revision_tree(revision)
1953
1963
        wt.set_parent_trees([(revision, basis_tree)])
1954
 
        build_tree(basis_tree, wt)
 
1964
        transform.build_tree(basis_tree, wt)
1955
1965
        return wt
1956
1966
 
1957
1967
    def __init__(self):
2032
2042
            wt._write_inventory(inv)
2033
2043
            wt.set_root_id(inv.root.file_id)
2034
2044
            basis_tree = branch.repository.revision_tree(revision_id)
2035
 
            if revision_id == bzrlib.revision.NULL_REVISION:
 
2045
            if revision_id == NULL_REVISION:
2036
2046
                wt.set_parent_trees([])
2037
2047
            else:
2038
2048
                wt.set_parent_trees([(revision_id, basis_tree)])
2039
 
            build_tree(basis_tree, wt)
 
2049
            transform.build_tree(basis_tree, wt)
2040
2050
        finally:
2041
2051
            wt.unlock()
2042
2052
            control_files.unlock()