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
"""WorkingTree object and friends.
19
A WorkingTree represents the editable working copy of a branch.
20
Operations which represent the WorkingTree are also done here,
21
such as renaming or adding files. The WorkingTree has an inventory
22
which is updated by these operations. A commit produces a
23
new revision based on the workingtree and its inventory.
25
At the moment every WorkingTree has its own branch. Remote
26
WorkingTrees aren't supported.
28
To get a WorkingTree, call Branch.working_tree():
32
# TODO: Don't allow WorkingTrees to be constructed for remote branches if
35
# FIXME: I don't know if writing out the cache from the destructor is really a
36
# good idea, because destructors are considered poor taste in Python, and it's
37
# not predictable when it will be written out.
39
# TODO: Give the workingtree sole responsibility for the working inventory;
40
# remove the variable and references to it from the branch. This may require
41
# updating the commit code so as to update the inventory within the working
42
# copy, and making sure there's only one WorkingTree for any directory on disk.
43
# At the momenthey may alias the inventory and have old copies of it in memory.
49
from bzrlib.branch import Branch, needs_read_lock, needs_write_lock, quotefn
51
from bzrlib.osutils import appendpath, file_kind, isdir, splitpath, relpath
52
from bzrlib.errors import BzrCheckError, NotVersionedError
53
from bzrlib.trace import mutter
55
class TreeEntry(object):
56
"""An entry that implements the minium interface used by commands.
58
This needs further inspection, it may be better to have
59
InventoryEntries without ids - though that seems wrong. For now,
60
this is a parallel hierarchy to InventoryEntry, and needs to become
61
one of several things: decorates to that hierarchy, children of, or
63
Another note is that these objects are currently only used when there is
64
no InventoryEntry available - i.e. for unversioned objects.
65
Perhaps they should be UnversionedEntry et al. ? - RBC 20051003
68
def __eq__(self, other):
69
# yes, this us ugly, TODO: best practice __eq__ style.
70
return (isinstance(other, TreeEntry)
71
and other.__class__ == self.__class__)
73
def kind_character(self):
77
class TreeDirectory(TreeEntry):
78
"""See TreeEntry. This is a directory in a working tree."""
80
def __eq__(self, other):
81
return (isinstance(other, TreeDirectory)
82
and other.__class__ == self.__class__)
84
def kind_character(self):
88
class TreeFile(TreeEntry):
89
"""See TreeEntry. This is a regular file in a working tree."""
91
def __eq__(self, other):
92
return (isinstance(other, TreeFile)
93
and other.__class__ == self.__class__)
95
def kind_character(self):
99
class TreeLink(TreeEntry):
100
"""See TreeEntry. This is a symlink in a working tree."""
102
def __eq__(self, other):
103
return (isinstance(other, TreeLink)
104
and other.__class__ == self.__class__)
106
def kind_character(self):
110
class WorkingTree(bzrlib.tree.Tree):
111
"""Working copy tree.
113
The inventory is held in the `Branch` working-inventory, and the
114
files are in a directory on disk.
116
It is possible for a `WorkingTree` to have a filename which is
117
not listed in the Inventory and vice versa.
120
def __init__(self, basedir, branch=None):
121
"""Construct a WorkingTree for basedir.
123
If the branch is not supplied, it is opened automatically.
124
If the branch is supplied, it must be the branch for this basedir.
125
(branch.base is not cross checked, because for remote branches that
126
would be meaningless).
128
from bzrlib.hashcache import HashCache
129
from bzrlib.trace import note, mutter
130
assert isinstance(basedir, basestring), \
131
"base directory %r is not a string" % basedir
133
branch = Branch.open(basedir)
134
assert isinstance(branch, Branch), \
135
"branch %r is not a Branch" % branch
136
self._inventory = branch.inventory
137
self.path2id = self._inventory.path2id
139
self.basedir = basedir
141
# update the whole cache up front and write to disk if anything changed;
142
# in the future we might want to do this more selectively
143
hc = self._hashcache = HashCache(basedir)
153
if self._hashcache.needs_write:
154
self._hashcache.write()
158
"""Iterate through file_ids for this tree.
160
file_ids are in a WorkingTree if they are in the working inventory
161
and the working file exists.
163
inv = self._inventory
164
for path, ie in inv.iter_entries():
165
if bzrlib.osutils.lexists(self.abspath(path)):
170
return "<%s of %s>" % (self.__class__.__name__,
171
getattr(self, 'basedir', None))
175
def abspath(self, filename):
176
return os.path.join(self.basedir, filename)
178
def relpath(self, abspath):
179
"""Return the local path portion from a given absolute path."""
180
return relpath(self.basedir, abspath)
182
def has_filename(self, filename):
183
return bzrlib.osutils.lexists(self.abspath(filename))
185
def get_file(self, file_id):
186
return self.get_file_byname(self.id2path(file_id))
188
def get_file_byname(self, filename):
189
return file(self.abspath(filename), 'rb')
191
def _get_store_filename(self, file_id):
192
## XXX: badly named; this isn't in the store at all
193
return self.abspath(self.id2path(file_id))
196
def id2abspath(self, file_id):
197
return self.abspath(self.id2path(file_id))
200
def has_id(self, file_id):
201
# files that have been deleted are excluded
202
inv = self._inventory
203
if not inv.has_id(file_id):
205
path = inv.id2path(file_id)
206
return bzrlib.osutils.lexists(self.abspath(path))
208
def has_or_had_id(self, file_id):
209
if file_id == self.inventory.root.file_id:
211
return self.inventory.has_id(file_id)
213
__contains__ = has_id
216
def get_file_size(self, file_id):
217
return os.path.getsize(self.id2abspath(file_id))
219
def get_file_sha1(self, file_id):
220
path = self._inventory.id2path(file_id)
221
return self._hashcache.get_sha1(path)
224
def is_executable(self, file_id):
226
return self._inventory[file_id].executable
228
path = self._inventory.id2path(file_id)
229
mode = os.lstat(self.abspath(path)).st_mode
230
return bool(stat.S_ISREG(mode) and stat.S_IEXEC&mode)
232
def get_symlink_target(self, file_id):
233
return os.readlink(self.id2abspath(file_id))
235
def file_class(self, filename):
236
if self.path2id(filename):
238
elif self.is_ignored(filename):
244
def list_files(self):
245
"""Recursively list all files as (path, class, kind, id).
247
Lists, but does not descend into unversioned directories.
249
This does not include files that have been deleted in this
252
Skips the control directory.
254
inv = self._inventory
256
def descend(from_dir_relpath, from_dir_id, dp):
260
## TODO: If we find a subdirectory with its own .bzr
261
## directory, then that is a separate tree and we
262
## should exclude it.
263
if bzrlib.BZRDIR == f:
267
fp = appendpath(from_dir_relpath, f)
270
fap = appendpath(dp, f)
272
f_ie = inv.get_child(from_dir_id, f)
275
elif self.is_ignored(fp):
284
raise BzrCheckError("file %r entered as kind %r id %r, "
286
% (fap, f_ie.kind, f_ie.file_id, fk))
288
# make a last minute entry
292
if fk == 'directory':
293
entry = TreeDirectory()
296
elif fk == 'symlink':
301
yield fp, c, fk, (f_ie and f_ie.file_id), entry
303
if fk != 'directory':
307
# don't descend unversioned directories
310
for ff in descend(fp, f_ie.file_id, fap):
313
for f in descend('', inv.root.file_id, self.basedir):
319
for subp in self.extras():
320
if not self.is_ignored(subp):
323
def iter_conflicts(self):
325
for path in (s[0] for s in self.list_files()):
326
stem = get_conflicted_stem(path)
329
if stem not in conflicted:
334
"""Yield all unknown files in this WorkingTree.
336
If there are any unknown directories then only the directory is
337
returned, not all its children. But if there are unknown files
338
under a versioned subdirectory, they are returned.
340
Currently returned depth-first, sorted by name within directories.
342
## TODO: Work from given directory downwards
343
for path, dir_entry in self.inventory.directories():
344
mutter("search for unknowns in %r" % path)
345
dirabs = self.abspath(path)
346
if not isdir(dirabs):
347
# e.g. directory deleted
351
for subf in os.listdir(dirabs):
353
and (subf not in dir_entry.children)):
358
subp = appendpath(path, subf)
362
def ignored_files(self):
363
"""Yield list of PATH, IGNORE_PATTERN"""
364
for subp in self.extras():
365
pat = self.is_ignored(subp)
370
def get_ignore_list(self):
371
"""Return list of ignore patterns.
373
Cached in the Tree object after the first call.
375
if hasattr(self, '_ignorelist'):
376
return self._ignorelist
378
l = bzrlib.DEFAULT_IGNORE[:]
379
if self.has_filename(bzrlib.IGNORE_FILENAME):
380
f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
381
l.extend([line.rstrip("\n\r") for line in f.readlines()])
386
def is_ignored(self, filename):
387
r"""Check whether the filename matches an ignore pattern.
389
Patterns containing '/' or '\' need to match the whole path;
390
others match against only the last component.
392
If the file is ignored, returns the pattern which caused it to
393
be ignored, otherwise None. So this can simply be used as a
394
boolean if desired."""
396
# TODO: Use '**' to match directories, and other extended
397
# globbing stuff from cvs/rsync.
399
# XXX: fnmatch is actually not quite what we want: it's only
400
# approximately the same as real Unix fnmatch, and doesn't
401
# treat dotfiles correctly and allows * to match /.
402
# Eventually it should be replaced with something more
405
for pat in self.get_ignore_list():
406
if '/' in pat or '\\' in pat:
408
# as a special case, you can put ./ at the start of a
409
# pattern; this is good to match in the top-level
412
if (pat[:2] == './') or (pat[:2] == '.\\'):
416
if fnmatch.fnmatchcase(filename, newpat):
419
if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):
424
def kind(self, file_id):
425
return file_kind(self.id2abspath(file_id))
428
"""See Branch.lock_read, and WorkingTree.unlock."""
429
return self.branch.lock_read()
431
def lock_write(self):
432
"""See Branch.lock_write, and WorkingTree.unlock."""
433
return self.branch.lock_write()
436
def remove(self, files, verbose=False):
437
"""Remove nominated files from the working inventory..
439
This does not remove their text. This does not run on XXX on what? RBC
441
TODO: Refuse to remove modified files unless --force is given?
443
TODO: Do something useful with directories.
445
TODO: Should this remove the text or not? Tough call; not
446
removing may be useful and the user can just use use rm, and
447
is the opposite of add. Removing it is consistent with most
448
other tools. Maybe an option.
450
## TODO: Normalize names
451
## TODO: Remove nested loops; better scalability
452
if isinstance(files, basestring):
457
# do this before any modifications
461
# TODO: Perhaps make this just a warning, and continue?
462
# This tends to happen when
463
raise NotVersionedError(path=f)
464
mutter("remove inventory entry %s {%s}" % (quotefn(f), fid))
466
# having remove it, it must be either ignored or unknown
467
if self.is_ignored(f):
471
show_status(new_status, inv[fid].kind, quotefn(f))
474
self.branch._write_inventory(inv)
477
"""See Branch.unlock.
479
WorkingTree locking just uses the Branch locking facilities.
480
This is current because all working trees have an embedded branch
481
within them. IF in the future, we were to make branch data shareable
482
between multiple working trees, i.e. via shared storage, then we
483
would probably want to lock both the local tree, and the branch.
485
return self.branch.unlock()
488
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
489
def get_conflicted_stem(path):
490
for suffix in CONFLICT_SUFFIXES:
491
if path.endswith(suffix):
492
return path[:-len(suffix)]