21
21
# it's not predictable when it will be written out.
27
from bzrlib.branch import Branch
27
from bzrlib.osutils import appendpath, file_kind, isdir, splitpath
29
from bzrlib.osutils import appendpath, file_kind, isdir, splitpath, relpath
28
30
from bzrlib.errors import BzrCheckError
29
31
from bzrlib.trace import mutter
33
class TreeEntry(object):
34
"""An entry that implements the minium interface used by commands.
36
This needs further inspection, it may be better to have
37
InventoryEntries without ids - though that seems wrong. For now,
38
this is a parallel hierarchy to InventoryEntry, and needs to become
39
one of several things: decorates to that hierarchy, children of, or
41
Another note is that these objects are currently only used when there is
42
no InventoryEntry available - i.e. for unversioned objects.
43
Perhaps they should be UnversionedEntry et al. ? - RBC 20051003
46
def __eq__(self, other):
47
# yes, this us ugly, TODO: best practice __eq__ style.
48
return (isinstance(other, TreeEntry)
49
and other.__class__ == self.__class__)
51
def kind_character(self):
55
class TreeDirectory(TreeEntry):
56
"""See TreeEntry. This is a directory in a working tree."""
58
def __eq__(self, other):
59
return (isinstance(other, TreeDirectory)
60
and other.__class__ == self.__class__)
62
def kind_character(self):
66
class TreeFile(TreeEntry):
67
"""See TreeEntry. This is a regular file in a working tree."""
69
def __eq__(self, other):
70
return (isinstance(other, TreeFile)
71
and other.__class__ == self.__class__)
73
def kind_character(self):
77
class TreeLink(TreeEntry):
78
"""See TreeEntry. This is a symlink in a working tree."""
80
def __eq__(self, other):
81
return (isinstance(other, TreeLink)
82
and other.__class__ == self.__class__)
84
def kind_character(self):
31
88
class WorkingTree(bzrlib.tree.Tree):
32
89
"""Working copy tree.
37
94
It is possible for a `WorkingTree` to have a filename which is
38
95
not listed in the Inventory and vice versa.
40
def __init__(self, basedir, inv):
97
def __init__(self, basedir, branch=None):
98
"""Construct a WorkingTree for basedir.
100
If the branch is not supplied, it is opened automatically.
101
If the branch is supplied, it must be the branch for this basedir.
102
(branch.base is not cross checked, because for remote branches that
103
would be meaningless).
41
105
from bzrlib.hashcache import HashCache
42
106
from bzrlib.trace import note, mutter
109
branch = Branch.open(basedir)
110
self._inventory = branch.inventory
111
self.path2id = self._inventory.path2id
45
113
self.basedir = basedir
46
self.path2id = inv.path2id
48
115
# update the whole cache up front and write to disk if anything changed;
49
116
# in the future we might want to do this more selectively
82
149
def abspath(self, filename):
83
150
return os.path.join(self.basedir, filename)
152
def relpath(self, abspath):
153
"""Return the local path portion from a given absolute path."""
154
return relpath(self.basedir, abspath)
85
156
def has_filename(self, filename):
86
return os.path.exists(self.abspath(filename))
157
return bzrlib.osutils.lexists(self.abspath(filename))
88
159
def get_file(self, file_id):
89
160
return self.get_file_byname(self.id2path(file_id))
115
186
def get_file_size(self, file_id):
116
187
return os.path.getsize(self.id2abspath(file_id))
119
189
def get_file_sha1(self, file_id):
120
190
path = self._inventory.id2path(file_id)
121
191
return self._hashcache.get_sha1(path)
194
def is_executable(self, file_id):
196
return self._inventory[file_id].executable
198
path = self._inventory.id2path(file_id)
199
mode = os.lstat(self.abspath(path)).st_mode
200
return bool(stat.S_ISREG(mode) and stat.S_IEXEC&mode)
202
def get_symlink_target(self, file_id):
203
return os.readlink(self.id2abspath(file_id))
124
205
def file_class(self, filename):
125
206
if self.path2id(filename):
175
256
% (fap, f_ie.kind, f_ie.file_id, fk))
177
yield fp, c, fk, (f_ie and f_ie.file_id)
258
# make a last minute entry
262
if fk == 'directory':
263
entry = TreeDirectory()
266
elif fk == 'symlink':
271
yield fp, c, fk, (f_ie and f_ie.file_id), entry
179
273
if fk != 'directory':
196
290
if not self.is_ignored(subp):
293
def iter_conflicts(self):
295
for path in (s[0] for s in self.list_files()):
296
stem = get_conflicted_stem(path)
299
if stem not in conflicted:
200
303
def extras(self):
201
304
"""Yield all unknown files in this WorkingTree.
394
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
395
def get_conflicted_stem(path):
396
for suffix in CONFLICT_SUFFIXES:
397
if path.endswith(suffix):
398
return path[:-len(suffix)]