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
17
# TODO: Don't allow WorkingTrees to be constructed for remote branches.
35
19
# 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.
20
# good idea, because destructors are considered poor taste in Python, and
21
# it's not predictable when it will be written out.
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, DivergedBranches, NotVersionedError
28
from bzrlib.osutils import appendpath, file_kind, isdir, splitpath
29
from bzrlib.errors import BzrCheckError
53
30
from bzrlib.trace import mutter
57
32
class TreeEntry(object):
58
33
"""An entry that implements the minium interface used by commands.
118
93
It is possible for a `WorkingTree` to have a filename which is
119
94
not listed in the Inventory and vice versa.
122
def __init__(self, basedir, branch=None):
123
"""Construct a WorkingTree for basedir.
125
If the branch is not supplied, it is opened automatically.
126
If the branch is supplied, it must be the branch for this basedir.
127
(branch.base is not cross checked, because for remote branches that
128
would be meaningless).
96
def __init__(self, basedir, inv):
130
97
from bzrlib.hashcache import HashCache
131
98
from bzrlib.trace import note, mutter
132
assert isinstance(basedir, basestring), \
133
"base directory %r is not a string" % basedir
135
branch = Branch.open(basedir)
136
assert isinstance(branch, Branch), \
137
"branch %r is not a Branch" % branch
100
self._inventory = inv
139
101
self.basedir = basedir
140
self._inventory = self.read_working_inventory()
141
self.path2id = self._inventory.path2id
102
self.path2id = inv.path2id
143
104
# update the whole cache up front and write to disk if anything changed;
144
105
# in the future we might want to do this more selectively
145
# two possible ways offer themselves : in self._unlock, write the cache
146
# if needed, or, when the cache sees a change, append it to the hash
147
# cache file, and have the parser take the most recent entry for a
149
106
hc = self._hashcache = HashCache(basedir)
335
285
conflicted.add(stem)
339
def pull(self, source, overwrite=False):
340
from bzrlib.merge import merge_inner
343
old_revision_history = self.branch.revision_history()
344
self.branch.pull(source, overwrite)
345
new_revision_history = self.branch.revision_history()
346
if new_revision_history != old_revision_history:
347
if len(old_revision_history):
348
other_revision = old_revision_history[-1]
350
other_revision = None
351
merge_inner(self.branch,
352
self.branch.basis_tree(),
353
self.branch.revision_tree(other_revision))
357
288
def extras(self):
358
289
"""Yield all unknown files in this WorkingTree.
448
def kind(self, file_id):
449
return file_kind(self.id2abspath(file_id))
452
"""See Branch.lock_read, and WorkingTree.unlock."""
453
return self.branch.lock_read()
455
def lock_write(self):
456
"""See Branch.lock_write, and WorkingTree.unlock."""
457
return self.branch.lock_write()
460
def read_working_inventory(self):
461
"""Read the working inventory."""
462
# ElementTree does its own conversion from UTF-8, so open in
464
f = self.branch.controlfile('inventory', 'rb')
465
return bzrlib.xml5.serializer_v5.read_inventory(f)
468
def remove(self, files, verbose=False):
469
"""Remove nominated files from the working inventory..
471
This does not remove their text. This does not run on XXX on what? RBC
473
TODO: Refuse to remove modified files unless --force is given?
475
TODO: Do something useful with directories.
477
TODO: Should this remove the text or not? Tough call; not
478
removing may be useful and the user can just use use rm, and
479
is the opposite of add. Removing it is consistent with most
480
other tools. Maybe an option.
482
## TODO: Normalize names
483
## TODO: Remove nested loops; better scalability
484
if isinstance(files, basestring):
489
# do this before any modifications
493
# TODO: Perhaps make this just a warning, and continue?
494
# This tends to happen when
495
raise NotVersionedError(path=f)
496
mutter("remove inventory entry %s {%s}" % (quotefn(f), fid))
498
# having remove it, it must be either ignored or unknown
499
if self.is_ignored(f):
503
show_status(new_status, inv[fid].kind, quotefn(f))
506
self.branch._write_inventory(inv)
509
def set_inventory(self, new_inventory_list):
510
from bzrlib.inventory import (Inventory,
515
inv = Inventory(self.get_root_id())
516
for path, file_id, parent, kind in new_inventory_list:
517
name = os.path.basename(path)
520
# fixme, there should be a factory function inv,add_??
521
if kind == 'directory':
522
inv.add(InventoryDirectory(file_id, name, parent))
524
inv.add(InventoryFile(file_id, name, parent))
525
elif kind == 'symlink':
526
inv.add(InventoryLink(file_id, name, parent))
528
raise BzrError("unknown kind %r" % kind)
529
self.branch._write_inventory(inv)
532
"""See Branch.unlock.
534
WorkingTree locking just uses the Branch locking facilities.
535
This is current because all working trees have an embedded branch
536
within them. IF in the future, we were to make branch data shareable
537
between multiple working trees, i.e. via shared storage, then we
538
would probably want to lock both the local tree, and the branch.
540
return self.branch.unlock()
543
379
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
544
380
def get_conflicted_stem(path):
545
381
for suffix in CONFLICT_SUFFIXES: