1
# Copyright (C) 2005 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
# TODO: Don't allow WorkingTrees to be constructed for remote branches.
19
# FIXME: I don't know if writing out the cache from the destructor is really a
20
# good idea, because destructors are considered poor taste in Python, and
21
# it's not predictable when it will be written out.
27
from bzrlib.branch import Branch
29
from bzrlib.osutils import appendpath, file_kind, isdir, splitpath
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):
88
class WorkingTree(bzrlib.tree.Tree):
91
The inventory is held in the `Branch` working-inventory, and the
92
files are in a directory on disk.
94
It is possible for a `WorkingTree` to have a filename which is
95
not listed in the Inventory and vice versa.
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).
105
from bzrlib.hashcache import HashCache
106
from bzrlib.trace import note, mutter
109
branch = Branch.open(basedir)
110
self._inventory = branch.inventory
111
self.path2id = self._inventory.path2id
113
self.basedir = basedir
115
# update the whole cache up front and write to disk if anything changed;
116
# in the future we might want to do this more selectively
117
hc = self._hashcache = HashCache(basedir)
127
if self._hashcache.needs_write:
128
self._hashcache.write()
132
"""Iterate through file_ids for this tree.
134
file_ids are in a WorkingTree if they are in the working inventory
135
and the working file exists.
137
inv = self._inventory
138
for path, ie in inv.iter_entries():
139
if bzrlib.osutils.lexists(self.abspath(path)):
144
return "<%s of %s>" % (self.__class__.__name__,
145
getattr(self, 'basedir', None))
149
def abspath(self, filename):
150
return os.path.join(self.basedir, filename)
152
def has_filename(self, filename):
153
return bzrlib.osutils.lexists(self.abspath(filename))
155
def get_file(self, file_id):
156
return self.get_file_byname(self.id2path(file_id))
158
def get_file_byname(self, filename):
159
return file(self.abspath(filename), 'rb')
161
def _get_store_filename(self, file_id):
162
## XXX: badly named; this isn't in the store at all
163
return self.abspath(self.id2path(file_id))
166
def id2abspath(self, file_id):
167
return self.abspath(self.id2path(file_id))
170
def has_id(self, file_id):
171
# files that have been deleted are excluded
172
inv = self._inventory
173
if not inv.has_id(file_id):
175
path = inv.id2path(file_id)
176
return bzrlib.osutils.lexists(self.abspath(path))
179
__contains__ = has_id
182
def get_file_size(self, file_id):
183
return os.path.getsize(self.id2abspath(file_id))
185
def get_file_sha1(self, file_id):
186
path = self._inventory.id2path(file_id)
187
return self._hashcache.get_sha1(path)
190
def is_executable(self, file_id):
192
return self._inventory[file_id].executable
194
path = self._inventory.id2path(file_id)
195
mode = os.lstat(self.abspath(path)).st_mode
196
return bool(stat.S_ISREG(mode) and stat.S_IEXEC&mode)
198
def get_symlink_target(self, file_id):
199
return os.readlink(self.id2abspath(file_id))
201
def file_class(self, filename):
202
if self.path2id(filename):
204
elif self.is_ignored(filename):
210
def list_files(self):
211
"""Recursively list all files as (path, class, kind, id).
213
Lists, but does not descend into unversioned directories.
215
This does not include files that have been deleted in this
218
Skips the control directory.
220
inv = self._inventory
222
def descend(from_dir_relpath, from_dir_id, dp):
226
## TODO: If we find a subdirectory with its own .bzr
227
## directory, then that is a separate tree and we
228
## should exclude it.
229
if bzrlib.BZRDIR == f:
233
fp = appendpath(from_dir_relpath, f)
236
fap = appendpath(dp, f)
238
f_ie = inv.get_child(from_dir_id, f)
241
elif self.is_ignored(fp):
250
raise BzrCheckError("file %r entered as kind %r id %r, "
252
% (fap, f_ie.kind, f_ie.file_id, fk))
254
# make a last minute entry
258
if fk == 'directory':
259
entry = TreeDirectory()
262
elif fk == 'symlink':
267
yield fp, c, fk, (f_ie and f_ie.file_id), entry
269
if fk != 'directory':
273
# don't descend unversioned directories
276
for ff in descend(fp, f_ie.file_id, fap):
279
for f in descend('', inv.root.file_id, self.basedir):
285
for subp in self.extras():
286
if not self.is_ignored(subp):
289
def iter_conflicts(self):
291
for path in (s[0] for s in self.list_files()):
292
stem = get_conflicted_stem(path)
295
if stem not in conflicted:
300
"""Yield all unknown files in this WorkingTree.
302
If there are any unknown directories then only the directory is
303
returned, not all its children. But if there are unknown files
304
under a versioned subdirectory, they are returned.
306
Currently returned depth-first, sorted by name within directories.
308
## TODO: Work from given directory downwards
309
for path, dir_entry in self.inventory.directories():
310
mutter("search for unknowns in %r" % path)
311
dirabs = self.abspath(path)
312
if not isdir(dirabs):
313
# e.g. directory deleted
317
for subf in os.listdir(dirabs):
319
and (subf not in dir_entry.children)):
324
subp = appendpath(path, subf)
328
def ignored_files(self):
329
"""Yield list of PATH, IGNORE_PATTERN"""
330
for subp in self.extras():
331
pat = self.is_ignored(subp)
336
def get_ignore_list(self):
337
"""Return list of ignore patterns.
339
Cached in the Tree object after the first call.
341
if hasattr(self, '_ignorelist'):
342
return self._ignorelist
344
l = bzrlib.DEFAULT_IGNORE[:]
345
if self.has_filename(bzrlib.IGNORE_FILENAME):
346
f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
347
l.extend([line.rstrip("\n\r") for line in f.readlines()])
352
def is_ignored(self, filename):
353
r"""Check whether the filename matches an ignore pattern.
355
Patterns containing '/' or '\' need to match the whole path;
356
others match against only the last component.
358
If the file is ignored, returns the pattern which caused it to
359
be ignored, otherwise None. So this can simply be used as a
360
boolean if desired."""
362
# TODO: Use '**' to match directories, and other extended
363
# globbing stuff from cvs/rsync.
365
# XXX: fnmatch is actually not quite what we want: it's only
366
# approximately the same as real Unix fnmatch, and doesn't
367
# treat dotfiles correctly and allows * to match /.
368
# Eventually it should be replaced with something more
371
for pat in self.get_ignore_list():
372
if '/' in pat or '\\' in pat:
374
# as a special case, you can put ./ at the start of a
375
# pattern; this is good to match in the top-level
378
if (pat[:2] == './') or (pat[:2] == '.\\'):
382
if fnmatch.fnmatchcase(filename, newpat):
385
if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):
390
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
391
def get_conflicted_stem(path):
392
for suffix in CONFLICT_SUFFIXES:
393
if path.endswith(suffix):
394
return path[:-len(suffix)]