14
14
# along with this program; if not, write to the Free Software
15
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
21
from errors import BzrCheckError
22
from trace import mutter
51
from bzrlib.osutils import appendpath, file_kind, isdir, splitpath, relpath
52
from bzrlib.errors import BzrCheckError, DivergedBranches, NotVersionedError
53
from bzrlib.trace import mutter
56
class TreeEntry(object):
57
"""An entry that implements the minium interface used by commands.
59
This needs further inspection, it may be better to have
60
InventoryEntries without ids - though that seems wrong. For now,
61
this is a parallel hierarchy to InventoryEntry, and needs to become
62
one of several things: decorates to that hierarchy, children of, or
64
Another note is that these objects are currently only used when there is
65
no InventoryEntry available - i.e. for unversioned objects.
66
Perhaps they should be UnversionedEntry et al. ? - RBC 20051003
69
def __eq__(self, other):
70
# yes, this us ugly, TODO: best practice __eq__ style.
71
return (isinstance(other, TreeEntry)
72
and other.__class__ == self.__class__)
74
def kind_character(self):
78
class TreeDirectory(TreeEntry):
79
"""See TreeEntry. This is a directory in a working tree."""
81
def __eq__(self, other):
82
return (isinstance(other, TreeDirectory)
83
and other.__class__ == self.__class__)
85
def kind_character(self):
89
class TreeFile(TreeEntry):
90
"""See TreeEntry. This is a regular file in a working tree."""
92
def __eq__(self, other):
93
return (isinstance(other, TreeFile)
94
and other.__class__ == self.__class__)
96
def kind_character(self):
100
class TreeLink(TreeEntry):
101
"""See TreeEntry. This is a symlink in a working tree."""
103
def __eq__(self, other):
104
return (isinstance(other, TreeLink)
105
and other.__class__ == self.__class__)
107
def kind_character(self):
25
111
class WorkingTree(bzrlib.tree.Tree):
26
112
"""Working copy tree.
31
117
It is possible for a `WorkingTree` to have a filename which is
32
118
not listed in the Inventory and vice versa.
36
def __init__(self, basedir, inv):
121
def __init__(self, basedir, branch=None):
122
"""Construct a WorkingTree for basedir.
124
If the branch is not supplied, it is opened automatically.
125
If the branch is supplied, it must be the branch for this basedir.
126
(branch.base is not cross checked, because for remote branches that
127
would be meaningless).
129
from bzrlib.hashcache import HashCache
130
from bzrlib.trace import note, mutter
131
assert isinstance(basedir, basestring), \
132
"base directory %r is not a string" % basedir
134
branch = Branch.open(basedir)
135
assert isinstance(branch, Branch), \
136
"branch %r is not a Branch" % branch
137
self._inventory = branch.inventory
138
self.path2id = self._inventory.path2id
38
140
self.basedir = basedir
39
self.path2id = inv.path2id
40
self._update_statcache()
142
# update the whole cache up front and write to disk if anything changed;
143
# in the future we might want to do this more selectively
144
# two possible ways offer themselves : in self._unlock, write the cache
145
# if needed, or, when the cache sees a change, append it to the hash
146
# cache file, and have the parser take the most recent entry for a
148
hc = self._hashcache = HashCache(basedir)
42
156
def __iter__(self):
43
157
"""Iterate through file_ids for this tree.
46
160
and the working file exists.
48
162
inv = self._inventory
49
for file_id in self._inventory:
50
# TODO: This is slightly redundant; we should be able to just
51
# check the statcache but it only includes regular files.
52
# only include files which still exist on disk
55
if ((file_id in self._statcache)
56
or (os.path.exists(self.abspath(inv.id2path(file_id))))):
163
for path, ie in inv.iter_entries():
164
if bzrlib.osutils.lexists(self.abspath(path)):
61
168
def __repr__(self):
62
169
return "<%s of %s>" % (self.__class__.__name__,
170
getattr(self, 'basedir', None))
65
174
def abspath(self, filename):
66
175
return os.path.join(self.basedir, filename)
177
def relpath(self, abspath):
178
"""Return the local path portion from a given absolute path."""
179
return relpath(self.basedir, abspath)
68
181
def has_filename(self, filename):
69
return os.path.exists(self.abspath(filename))
182
return bzrlib.osutils.lexists(self.abspath(filename))
71
184
def get_file(self, file_id):
72
185
return self.get_file_byname(self.id2path(file_id))
78
191
## XXX: badly named; this isn't in the store at all
79
192
return self.abspath(self.id2path(file_id))
195
def id2abspath(self, file_id):
196
return self.abspath(self.id2path(file_id))
82
199
def has_id(self, file_id):
83
200
# files that have been deleted are excluded
84
if not self.inventory.has_id(file_id):
201
inv = self._inventory
202
if not inv.has_id(file_id):
86
if file_id in self._statcache:
204
path = inv.id2path(file_id)
205
return bzrlib.osutils.lexists(self.abspath(path))
207
def has_or_had_id(self, file_id):
208
if file_id == self.inventory.root.file_id:
88
return os.path.exists(self.abspath(self.id2path(file_id)))
210
return self.inventory.has_id(file_id)
91
212
__contains__ = has_id
94
def _update_statcache(self):
96
if not self._statcache:
97
self._statcache = statcache.update_cache(self.basedir, self.inventory)
99
215
def get_file_size(self, file_id):
101
return os.stat(self._get_store_filename(file_id))[stat.ST_SIZE]
216
return os.path.getsize(self.id2abspath(file_id))
104
218
def get_file_sha1(self, file_id):
105
return self._statcache[file_id][statcache.SC_SHA1]
219
path = self._inventory.id2path(file_id)
220
return self._hashcache.get_sha1(path)
223
def is_executable(self, file_id):
225
return self._inventory[file_id].executable
227
path = self._inventory.id2path(file_id)
228
mode = os.lstat(self.abspath(path)).st_mode
229
return bool(stat.S_ISREG(mode) and stat.S_IEXEC&mode)
231
def get_symlink_target(self, file_id):
232
return os.readlink(self.id2abspath(file_id))
108
234
def file_class(self, filename):
109
235
if self.path2id(filename):
183
319
if not self.is_ignored(subp):
322
def iter_conflicts(self):
324
for path in (s[0] for s in self.list_files()):
325
stem = get_conflicted_stem(path)
328
if stem not in conflicted:
333
def pull(self, source, overwrite=False):
334
from bzrlib.merge import merge_inner
337
old_revision_history = self.branch.revision_history()
338
self.branch.pull(source, overwrite)
339
new_revision_history = self.branch.revision_history()
340
if new_revision_history != old_revision_history:
341
if len(old_revision_history):
342
other_revision = old_revision_history[-1]
344
other_revision = None
345
merge_inner(self.branch,
346
self.branch.basis_tree(),
347
self.branch.revision_tree(other_revision))
187
351
def extras(self):
188
352
"""Yield all unknown files in this WorkingTree.
442
def kind(self, file_id):
443
return file_kind(self.id2abspath(file_id))
446
"""See Branch.lock_read, and WorkingTree.unlock."""
447
return self.branch.lock_read()
449
def lock_write(self):
450
"""See Branch.lock_write, and WorkingTree.unlock."""
451
return self.branch.lock_write()
454
def remove(self, files, verbose=False):
455
"""Remove nominated files from the working inventory..
457
This does not remove their text. This does not run on XXX on what? RBC
459
TODO: Refuse to remove modified files unless --force is given?
461
TODO: Do something useful with directories.
463
TODO: Should this remove the text or not? Tough call; not
464
removing may be useful and the user can just use use rm, and
465
is the opposite of add. Removing it is consistent with most
466
other tools. Maybe an option.
468
## TODO: Normalize names
469
## TODO: Remove nested loops; better scalability
470
if isinstance(files, basestring):
475
# do this before any modifications
479
# TODO: Perhaps make this just a warning, and continue?
480
# This tends to happen when
481
raise NotVersionedError(path=f)
482
mutter("remove inventory entry %s {%s}" % (quotefn(f), fid))
484
# having remove it, it must be either ignored or unknown
485
if self.is_ignored(f):
489
show_status(new_status, inv[fid].kind, quotefn(f))
492
self.branch._write_inventory(inv)
495
"""See Branch.unlock.
497
WorkingTree locking just uses the Branch locking facilities.
498
This is current because all working trees have an embedded branch
499
within them. IF in the future, we were to make branch data shareable
500
between multiple working trees, i.e. via shared storage, then we
501
would probably want to lock both the local tree, and the branch.
503
return self.branch.unlock()
506
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
507
def get_conflicted_stem(path):
508
for suffix in CONFLICT_SUFFIXES:
509
if path.endswith(suffix):
510
return path[:-len(suffix)]