~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree.py

  • Committer: John Arbash Meinel
  • Date: 2006-09-12 21:19:54 UTC
  • mto: This revision was merged to the branch mainline in revision 2071.
  • Revision ID: john@arbash-meinel.com-20060912211954-3d8074cfb66e1139
Find a few places that weren't importing their dependencies.
Demandload a few more places.

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
    )
57
66
import bzrlib.branch
58
 
from bzrlib.conflicts import Conflict, ConflictList, CONFLICT_SUFFIXES
 
67
from bzrlib.transport import get_transport
 
68
import bzrlib.ui
 
69
""")
 
70
 
59
71
from bzrlib.decorators import needs_read_lock, needs_write_lock
60
72
from bzrlib.errors import (BzrCheckError,
61
73
                           BzrError,
70
82
from bzrlib.inventory import InventoryEntry, Inventory
71
83
from bzrlib.lockable_files import LockableFiles, TransportLock
72
84
from bzrlib.lockdir import LockDir
73
 
from bzrlib.merge import merge_inner, transform_tree
74
85
from bzrlib.osutils import (
75
 
                            abspath,
76
 
                            compact_date,
77
 
                            file_kind,
78
 
                            isdir,
79
 
                            getcwd,
80
 
                            pathjoin,
81
 
                            pumpfile,
82
 
                            safe_unicode,
83
 
                            splitpath,
84
 
                            rand_chars,
85
 
                            normpath,
86
 
                            realpath,
87
 
                            relpath,
88
 
                            rename,
89
 
                            supports_executable,
90
 
                            )
 
86
    compact_date,
 
87
    file_kind,
 
88
    isdir,
 
89
    pathjoin,
 
90
    safe_unicode,
 
91
    splitpath,
 
92
    rand_chars,
 
93
    normpath,
 
94
    realpath,
 
95
    supports_executable,
 
96
    )
 
97
from bzrlib.trace import mutter, note
 
98
from bzrlib.transport.local import LocalTransport
 
99
import bzrlib.tree
91
100
from bzrlib.progress import DummyProgress, ProgressPhase
92
101
from bzrlib.revision import NULL_REVISION
93
102
from bzrlib.rio import RioReader, rio_file, Stanza
98
107
        zero_eight,
99
108
        zero_eleven,
100
109
        )
101
 
from bzrlib.trace import mutter, note
102
 
from bzrlib.transform import build_tree
103
 
from bzrlib.transport import get_transport
104
 
from bzrlib.transport.local import LocalTransport
105
 
from bzrlib.textui import show_status
106
 
import bzrlib.tree
107
 
import bzrlib.ui
108
 
import bzrlib.xml5
109
 
 
 
110
 
 
111
 
 
112
MERGE_MODIFIED_HEADER_1 = "BZR merge-modified list format 1"
 
113
CONFLICT_HEADER_1 = "BZR conflict list format 1"
110
114
 
111
115
# the regex removes any weird characters; we don't escape them 
112
116
# but rather just pull them out
410
414
        else:
411
415
            try:
412
416
                xml = self.read_basis_inventory()
413
 
                inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(xml)
 
417
                inv = xml5.serializer_v5.read_inventory_from_string(xml)
414
418
                inv.root.revision = revision_id
415
419
            except NoSuchFile:
416
420
                inv = None
475
479
        The path may be absolute or relative. If its a relative path it is 
476
480
        interpreted relative to the python current working directory.
477
481
        """
478
 
        return relpath(self.basedir, path)
 
482
        return osutils.relpath(self.basedir, path)
479
483
 
480
484
    def has_filename(self, filename):
481
485
        return osutils.lexists(self.abspath(filename))
545
549
    def copy_content_into(self, tree, revision_id=None):
546
550
        """Copy the current content and user files of this tree into tree."""
547
551
        if revision_id is None:
548
 
            transform_tree(tree, self)
 
552
            merge.transform_tree(tree, self)
549
553
        else:
550
554
            # TODO now merge from tree.last_revision to revision (to preserve
551
555
            # user local changes)
552
 
            transform_tree(tree, self)
 
556
            merge.transform_tree(tree, self)
553
557
            tree.set_parent_ids([revision_id])
554
558
 
555
559
    @needs_write_lock
1034
1038
                result.append((f, dest_path))
1035
1039
                inv.rename(inv.path2id(f), to_dir_id, name_tail)
1036
1040
                try:
1037
 
                    rename(self.abspath(f), self.abspath(dest_path))
 
1041
                    osutils.rename(self.abspath(f), self.abspath(dest_path))
1038
1042
                except OSError, e:
1039
1043
                    raise BzrError("failed to rename %r to %r: %s" %
1040
1044
                                   (f, dest_path, e[1]),
1086
1090
        from_abs = self.abspath(from_rel)
1087
1091
        to_abs = self.abspath(to_rel)
1088
1092
        try:
1089
 
            rename(from_abs, to_abs)
 
1093
            osutils.rename(from_abs, to_abs)
1090
1094
        except OSError, e:
1091
1095
            inv.rename(file_id, from_parent, from_name)
1092
1096
            raise BzrError("failed to rename %r to %r: %s"
1169
1173
                pb = bzrlib.ui.ui_factory.nested_progress_bar()
1170
1174
                try:
1171
1175
                    new_basis_tree = self.branch.basis_tree()
1172
 
                    merge_inner(self.branch,
 
1176
                    merge.merge_inner(
 
1177
                                self.branch,
1173
1178
                                new_basis_tree,
1174
1179
                                basis_tree,
1175
1180
                                this_tree=self,
1445
1450
                inv = self.branch.repository.deserialise_inventory(
1446
1451
                    new_revision, xml)
1447
1452
                inv.revision_id = new_revision
1448
 
                xml = bzrlib.xml5.serializer_v5.write_inventory_to_string(inv)
 
1453
                xml = xml5.serializer_v5.write_inventory_to_string(inv)
1449
1454
            assert isinstance(xml, str), 'serialised xml must be bytestring.'
1450
1455
            path = self._basis_inventory_name()
1451
1456
            sio = StringIO(xml)
1463
1468
        """Read the working inventory."""
1464
1469
        # ElementTree does its own conversion from UTF-8, so open in
1465
1470
        # binary.
1466
 
        result = bzrlib.xml5.serializer_v5.read_inventory(
 
1471
        result = xml5.serializer_v5.read_inventory(
1467
1472
            self._control_files.get('inventory'))
1468
1473
        self._set_inventory(result)
1469
1474
        return result
1503
1508
                    new_status = 'I'
1504
1509
                else:
1505
1510
                    new_status = '?'
1506
 
                show_status(new_status, inv[fid].kind, f, to_file=to_file)
 
1511
                textui.show_status(new_status, inv[fid].kind, f,
 
1512
                                   to_file=to_file)
1507
1513
            del inv[fid]
1508
1514
 
1509
1515
        self._write_inventory(inv)
1511
1517
    @needs_write_lock
1512
1518
    def revert(self, filenames, old_tree=None, backups=True, 
1513
1519
               pb=DummyProgress()):
1514
 
        from transform import revert
1515
 
        from conflicts import resolve
 
1520
        from bzrlib.conflicts import resolve
1516
1521
        if old_tree is None:
1517
1522
            old_tree = self.basis_tree()
1518
 
        conflicts = revert(self, old_tree, filenames, backups, pb)
 
1523
        conflicts = transform.revert(self, old_tree, filenames, backups, pb)
1519
1524
        if not len(filenames):
1520
1525
            self.set_parent_ids(self.get_parent_ids()[:1])
1521
1526
            resolve(self)
1613
1618
            # merge tree state up to new branch tip.
1614
1619
            basis = self.basis_tree()
1615
1620
            to_tree = self.branch.basis_tree()
1616
 
            result += merge_inner(self.branch,
 
1621
            result += merge.merge_inner(
 
1622
                                  self.branch,
1617
1623
                                  to_tree,
1618
1624
                                  basis,
1619
1625
                                  this_tree=self)
1654
1660
                base_rev_id = None
1655
1661
            base_tree = self.branch.repository.revision_tree(base_rev_id)
1656
1662
            other_tree = self.branch.repository.revision_tree(old_tip)
1657
 
            result += merge_inner(self.branch,
 
1663
            result += merge.merge_inner(
 
1664
                                  self.branch,
1658
1665
                                  other_tree,
1659
1666
                                  base_tree,
1660
1667
                                  this_tree=self)
1664
1671
    def _write_inventory(self, inv):
1665
1672
        """Write inventory as the current inventory."""
1666
1673
        sio = StringIO()
1667
 
        bzrlib.xml5.serializer_v5.write_inventory(inv, sio)
 
1674
        xml5.serializer_v5.write_inventory(inv, sio)
1668
1675
        sio.seek(0)
1669
1676
        self._control_files.put('inventory', sio)
1670
1677
        self._set_inventory(inv)
1678
1685
 
1679
1686
    @needs_read_lock
1680
1687
    def conflicts(self):
1681
 
        conflicts = ConflictList()
 
1688
        conflicts = _mod_conflicts.ConflictList()
1682
1689
        for conflicted in self._iter_conflicts():
1683
1690
            text = True
1684
1691
            try:
1697
1704
                    if text == False:
1698
1705
                        break
1699
1706
            ctype = {True: 'text conflict', False: 'contents conflict'}[text]
1700
 
            conflicts.append(Conflict.factory(ctype, path=conflicted,
 
1707
            conflicts.append(_mod_conflicts.Conflict.factory(ctype,
 
1708
                             path=conflicted,
1701
1709
                             file_id=self.path2id(conflicted)))
1702
1710
        return conflicts
1703
1711
 
1760
1768
    def add_conflicts(self, new_conflicts):
1761
1769
        conflict_set = set(self.conflicts())
1762
1770
        conflict_set.update(set(list(new_conflicts)))
1763
 
        self.set_conflicts(ConflictList(sorted(conflict_set,
1764
 
                                               key=Conflict.sort_key)))
 
1771
        self.set_conflicts(_mod_conflicts.ConflictList(sorted(conflict_set,
 
1772
                                       key=_mod_conflicts.Conflict.sort_key)))
1765
1773
 
1766
1774
    @needs_read_lock
1767
1775
    def conflicts(self):
1768
1776
        try:
1769
1777
            confile = self._control_files.get('conflicts')
1770
1778
        except NoSuchFile:
1771
 
            return ConflictList()
 
1779
            return _mod_conflicts.ConflictList()
1772
1780
        try:
1773
1781
            if confile.next() != CONFLICT_HEADER_1 + '\n':
1774
1782
                raise ConflictFormatError()
1775
1783
        except StopIteration:
1776
1784
            raise ConflictFormatError()
1777
 
        return ConflictList.from_stanzas(RioReader(confile))
 
1785
        return _mod_conflicts.ConflictList.from_stanzas(RioReader(confile))
1778
1786
 
1779
1787
    def unlock(self):
1780
1788
        if self._hashcache.needs_write and self._control_files._lock_count==1:
1787
1795
 
1788
1796
 
1789
1797
def get_conflicted_stem(path):
1790
 
    for suffix in CONFLICT_SUFFIXES:
 
1798
    for suffix in _mod_conflicts.CONFLICT_SUFFIXES:
1791
1799
        if path.endswith(suffix):
1792
1800
            return path[:-len(suffix)]
1793
1801
 
1899
1907
        """
1900
1908
        sio = StringIO()
1901
1909
        inv = Inventory()
1902
 
        bzrlib.xml5.serializer_v5.write_inventory(inv, sio)
 
1910
        xml5.serializer_v5.write_inventory(inv, sio)
1903
1911
        sio.seek(0)
1904
1912
        control_files.put('inventory', sio)
1905
1913
 
1934
1942
        wt.set_root_id(inv.root.file_id)
1935
1943
        basis_tree = branch.repository.revision_tree(revision)
1936
1944
        wt.set_parent_trees([(revision, basis_tree)])
1937
 
        build_tree(basis_tree, wt)
 
1945
        transform.build_tree(basis_tree, wt)
1938
1946
        return wt
1939
1947
 
1940
1948
    def __init__(self):
2015
2023
            wt._write_inventory(inv)
2016
2024
            wt.set_root_id(inv.root.file_id)
2017
2025
            basis_tree = branch.repository.revision_tree(revision_id)
2018
 
            if revision_id == bzrlib.revision.NULL_REVISION:
 
2026
            if revision_id == NULL_REVISION:
2019
2027
                wt.set_parent_trees([])
2020
2028
            else:
2021
2029
                wt.set_parent_trees([(revision_id, basis_tree)])
2022
 
            build_tree(basis_tree, wt)
 
2030
            transform.build_tree(basis_tree, wt)
2023
2031
        finally:
2024
2032
            wt.unlock()
2025
2033
            control_files.unlock()