21
21
# it's not predictable when it will be written out.
27
from bzrlib.branch import Branch
26
from errors import BzrCheckError
27
from trace import mutter
29
from bzrlib.osutils import appendpath, file_kind, isdir, splitpath, relpath
30
from bzrlib.errors import BzrCheckError
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):
29
88
class WorkingTree(bzrlib.tree.Tree):
30
89
"""Working copy tree.
35
94
It is possible for a `WorkingTree` to have a filename which is
36
95
not listed in the Inventory and vice versa.
38
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).
39
105
from bzrlib.hashcache import HashCache
40
106
from bzrlib.trace import note, mutter
109
branch = Branch.open(basedir)
110
self._inventory = branch.inventory
111
self.path2id = self._inventory.path2id
43
113
self.basedir = basedir
44
self.path2id = inv.path2id
46
115
# update the whole cache up front and write to disk if anything changed;
47
116
# in the future we might want to do this more selectively
80
149
def abspath(self, filename):
81
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)
83
156
def has_filename(self, filename):
84
return os.path.exists(self.abspath(filename))
157
return bzrlib.osutils.lexists(self.abspath(filename))
86
159
def get_file(self, file_id):
87
160
return self.get_file_byname(self.id2path(file_id))
93
166
## XXX: badly named; this isn't in the store at all
94
167
return self.abspath(self.id2path(file_id))
170
def id2abspath(self, file_id):
171
return self.abspath(self.id2path(file_id))
97
174
def has_id(self, file_id):
98
175
# files that have been deleted are excluded
100
177
if not inv.has_id(file_id):
102
179
path = inv.id2path(file_id)
103
return os.path.exists(self.abspath(path))
180
return bzrlib.osutils.lexists(self.abspath(path))
182
def has_or_had_id(self, file_id):
183
if file_id == self.inventory.root.file_id:
185
return self.inventory.has_id(file_id)
106
187
__contains__ = has_id
109
190
def get_file_size(self, file_id):
110
# is this still called?
111
raise NotImplementedError()
191
return os.path.getsize(self.id2abspath(file_id))
114
193
def get_file_sha1(self, file_id):
115
194
path = self._inventory.id2path(file_id)
116
195
return self._hashcache.get_sha1(path)
198
def is_executable(self, file_id):
200
return self._inventory[file_id].executable
202
path = self._inventory.id2path(file_id)
203
mode = os.lstat(self.abspath(path)).st_mode
204
return bool(stat.S_ISREG(mode) and stat.S_IEXEC&mode)
206
def get_symlink_target(self, file_id):
207
return os.readlink(self.id2abspath(file_id))
119
209
def file_class(self, filename):
120
210
if self.path2id(filename):
173
260
% (fap, f_ie.kind, f_ie.file_id, fk))
175
yield fp, c, fk, (f_ie and f_ie.file_id)
262
# make a last minute entry
266
if fk == 'directory':
267
entry = TreeDirectory()
270
elif fk == 'symlink':
275
yield fp, c, fk, (f_ie and f_ie.file_id), entry
177
277
if fk != 'directory':
194
294
if not self.is_ignored(subp):
297
def iter_conflicts(self):
299
for path in (s[0] for s in self.list_files()):
300
stem = get_conflicted_stem(path)
303
if stem not in conflicted:
198
307
def extras(self):
199
308
"""Yield all unknown files in this WorkingTree.
205
314
Currently returned depth-first, sorted by name within directories.
207
316
## TODO: Work from given directory downwards
208
from osutils import isdir, appendpath
210
317
for path, dir_entry in self.inventory.directories():
211
318
mutter("search for unknowns in %r" % path)
212
319
dirabs = self.abspath(path)
b'\\ No newline at end of file'
398
def kind(self, file_id):
399
return file_kind(self.id2abspath(file_id))
401
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
402
def get_conflicted_stem(path):
403
for suffix in CONFLICT_SUFFIXES:
404
if path.endswith(suffix):
405
return path[:-len(suffix)]