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 WorkingTree(dir[, branch])
 
 
32
# FIXME: I don't know if writing out the cache from the destructor is really a
 
 
33
# good idea, because destructors are considered poor taste in Python, and it's
 
 
34
# not predictable when it will be written out.
 
 
36
# TODO: Give the workingtree sole responsibility for the working inventory;
 
 
37
# remove the variable and references to it from the branch.  This may require
 
 
38
# updating the commit code so as to update the inventory within the working
 
 
39
# copy, and making sure there's only one WorkingTree for any directory on disk.
 
 
40
# At the momenthey may alias the inventory and have old copies of it in memory.
 
 
42
from copy import deepcopy
 
 
43
from cStringIO import StringIO
 
 
50
from bzrlib.atomicfile import AtomicFile
 
 
51
from bzrlib.branch import (Branch,
 
 
56
from bzrlib.errors import (BzrCheckError,
 
 
59
                           WeaveRevisionNotPresent,
 
 
62
from bzrlib.inventory import InventoryEntry
 
 
63
from bzrlib.osutils import (appendpath,
 
 
78
from bzrlib.symbol_versioning import *
 
 
79
from bzrlib.textui import show_status
 
 
81
from bzrlib.trace import mutter
 
 
85
def gen_file_id(name):
 
 
86
    """Return new file id.
 
 
88
    This should probably generate proper UUIDs, but for the moment we
 
 
89
    cope with just randomness because running uuidgen every time is
 
 
92
    from binascii import hexlify
 
 
99
    idx = name.rfind('\\')
 
 
101
        name = name[idx+1 : ]
 
 
103
    # make it not a hidden file
 
 
104
    name = name.lstrip('.')
 
 
106
    # remove any wierd characters; we don't escape them but rather
 
 
108
    name = re.sub(r'[^\w.]', '', name)
 
 
110
    s = hexlify(rand_bytes(8))
 
 
111
    return '-'.join((name, compact_date(time()), s))
 
 
115
    """Return a new tree-root file id."""
 
 
116
    return gen_file_id('TREE_ROOT')
 
 
119
class TreeEntry(object):
 
 
120
    """An entry that implements the minium interface used by commands.
 
 
122
    This needs further inspection, it may be better to have 
 
 
123
    InventoryEntries without ids - though that seems wrong. For now,
 
 
124
    this is a parallel hierarchy to InventoryEntry, and needs to become
 
 
125
    one of several things: decorates to that hierarchy, children of, or
 
 
127
    Another note is that these objects are currently only used when there is
 
 
128
    no InventoryEntry available - i.e. for unversioned objects.
 
 
129
    Perhaps they should be UnversionedEntry et al. ? - RBC 20051003
 
 
132
    def __eq__(self, other):
 
 
133
        # yes, this us ugly, TODO: best practice __eq__ style.
 
 
134
        return (isinstance(other, TreeEntry)
 
 
135
                and other.__class__ == self.__class__)
 
 
137
    def kind_character(self):
 
 
141
class TreeDirectory(TreeEntry):
 
 
142
    """See TreeEntry. This is a directory in a working tree."""
 
 
144
    def __eq__(self, other):
 
 
145
        return (isinstance(other, TreeDirectory)
 
 
146
                and other.__class__ == self.__class__)
 
 
148
    def kind_character(self):
 
 
152
class TreeFile(TreeEntry):
 
 
153
    """See TreeEntry. This is a regular file in a working tree."""
 
 
155
    def __eq__(self, other):
 
 
156
        return (isinstance(other, TreeFile)
 
 
157
                and other.__class__ == self.__class__)
 
 
159
    def kind_character(self):
 
 
163
class TreeLink(TreeEntry):
 
 
164
    """See TreeEntry. This is a symlink in a working tree."""
 
 
166
    def __eq__(self, other):
 
 
167
        return (isinstance(other, TreeLink)
 
 
168
                and other.__class__ == self.__class__)
 
 
170
    def kind_character(self):
 
 
174
class WorkingTree(bzrlib.tree.Tree):
 
 
175
    """Working copy tree.
 
 
177
    The inventory is held in the `Branch` working-inventory, and the
 
 
178
    files are in a directory on disk.
 
 
180
    It is possible for a `WorkingTree` to have a filename which is
 
 
181
    not listed in the Inventory and vice versa.
 
 
184
    def __init__(self, basedir='.', branch=None, _inventory=None):
 
 
185
        """Construct a WorkingTree for basedir.
 
 
187
        If the branch is not supplied, it is opened automatically.
 
 
188
        If the branch is supplied, it must be the branch for this basedir.
 
 
189
        (branch.base is not cross checked, because for remote branches that
 
 
190
        would be meaningless).
 
 
192
        from bzrlib.hashcache import HashCache
 
 
193
        from bzrlib.trace import note, mutter
 
 
194
        assert isinstance(basedir, basestring), \
 
 
195
            "base directory %r is not a string" % basedir
 
 
196
        basedir = safe_unicode(basedir)
 
 
198
            branch = Branch.open(basedir)
 
 
199
        assert isinstance(branch, Branch), \
 
 
200
            "branch %r is not a Branch" % branch
 
 
202
        self.basedir = realpath(basedir)
 
 
204
        # update the whole cache up front and write to disk if anything changed;
 
 
205
        # in the future we might want to do this more selectively
 
 
206
        # two possible ways offer themselves : in self._unlock, write the cache
 
 
207
        # if needed, or, when the cache sees a change, append it to the hash
 
 
208
        # cache file, and have the parser take the most recent entry for a
 
 
210
        hc = self._hashcache = HashCache(basedir)
 
 
218
        if _inventory is None:
 
 
219
            self._set_inventory(self.read_working_inventory())
 
 
221
            self._set_inventory(_inventory)
 
 
223
    def _set_inventory(self, inv):
 
 
224
        self._inventory = inv
 
 
225
        self.path2id = self._inventory.path2id
 
 
228
    def open_containing(path=None):
 
 
229
        """Open an existing working tree which has its root about path.
 
 
231
        This probes for a working tree at path and searches upwards from there.
 
 
233
        Basically we keep looking up until we find the control directory or
 
 
234
        run into /.  If there isn't one, raises NotBranchError.
 
 
235
        TODO: give this a new exception.
 
 
236
        If there is one, it is returned, along with the unused portion of path.
 
 
242
            if path.find('://') != -1:
 
 
243
                raise NotBranchError(path=path)
 
 
249
                return WorkingTree(path), tail
 
 
250
            except NotBranchError:
 
 
253
                tail = pathjoin(os.path.basename(path), tail)
 
 
255
                tail = os.path.basename(path)
 
 
257
            path = os.path.dirname(path)
 
 
259
                # reached the root, whatever that may be
 
 
260
                raise NotBranchError(path=orig_path)
 
 
263
        """Iterate through file_ids for this tree.
 
 
265
        file_ids are in a WorkingTree if they are in the working inventory
 
 
266
        and the working file exists.
 
 
268
        inv = self._inventory
 
 
269
        for path, ie in inv.iter_entries():
 
 
270
            if bzrlib.osutils.lexists(self.abspath(path)):
 
 
274
        return "<%s of %s>" % (self.__class__.__name__,
 
 
275
                               getattr(self, 'basedir', None))
 
 
277
    def abspath(self, filename):
 
 
278
        return pathjoin(self.basedir, filename)
 
 
281
    def create(branch, directory):
 
 
282
        """Create a workingtree for branch at directory.
 
 
284
        If existing_directory already exists it must have a .bzr directory.
 
 
285
        If it does not exist, it will be created.
 
 
287
        This returns a new WorkingTree object for the new checkout.
 
 
289
        TODO FIXME RBC 20060124 when we have checkout formats in place this
 
 
290
        should accept an optional revisionid to checkout [and reject this if
 
 
291
        checking out into the same dir as a pre-checkout-aware branch format.]
 
 
296
            if e.errno != errno.EEXIST:
 
 
299
            os.mkdir(pathjoin(directory, '.bzr'))
 
 
301
            if e.errno != errno.EEXIST:
 
 
303
        inv = branch.revision_tree(branch.last_revision()).inventory
 
 
304
        wt = WorkingTree(directory, branch, inv)
 
 
305
        wt._write_inventory(inv)
 
 
306
        if branch.last_revision() is not None:
 
 
307
            wt.set_last_revision(branch.last_revision())
 
 
308
        wt.set_pending_merges([])
 
 
313
    def create_standalone(directory):
 
 
314
        """Create a checkout and a branch at directory.
 
 
316
        Directory must exist and be empty.
 
 
318
        directory = safe_unicode(directory)
 
 
319
        b = Branch.create(directory)
 
 
320
        return WorkingTree.create(b, directory)
 
 
322
    def relpath(self, abs):
 
 
323
        """Return the local path portion from a given absolute path."""
 
 
324
        return relpath(self.basedir, abs)
 
 
326
    def has_filename(self, filename):
 
 
327
        return bzrlib.osutils.lexists(self.abspath(filename))
 
 
329
    def get_file(self, file_id):
 
 
330
        return self.get_file_byname(self.id2path(file_id))
 
 
332
    def get_file_byname(self, filename):
 
 
333
        return file(self.abspath(filename), 'rb')
 
 
335
    def get_root_id(self):
 
 
336
        """Return the id of this trees root"""
 
 
337
        inv = self.read_working_inventory()
 
 
338
        return inv.root.file_id
 
 
340
    def _get_store_filename(self, file_id):
 
 
341
        ## XXX: badly named; this is not in the store at all
 
 
342
        return self.abspath(self.id2path(file_id))
 
 
345
    def commit(self, *args, **kwargs):
 
 
346
        from bzrlib.commit import Commit
 
 
347
        # args for wt.commit start at message from the Commit.commit method,
 
 
348
        # but with branch a kwarg now, passing in args as is results in the
 
 
349
        #message being used for the branch
 
 
350
        args = (deprecated_nonce, ) + args
 
 
351
        Commit().commit(working_tree=self, *args, **kwargs)
 
 
352
        self._set_inventory(self.read_working_inventory())
 
 
354
    def id2abspath(self, file_id):
 
 
355
        return self.abspath(self.id2path(file_id))
 
 
357
    def has_id(self, file_id):
 
 
358
        # files that have been deleted are excluded
 
 
359
        inv = self._inventory
 
 
360
        if not inv.has_id(file_id):
 
 
362
        path = inv.id2path(file_id)
 
 
363
        return bzrlib.osutils.lexists(self.abspath(path))
 
 
365
    def has_or_had_id(self, file_id):
 
 
366
        if file_id == self.inventory.root.file_id:
 
 
368
        return self.inventory.has_id(file_id)
 
 
370
    __contains__ = has_id
 
 
372
    def get_file_size(self, file_id):
 
 
373
        return os.path.getsize(self.id2abspath(file_id))
 
 
376
    def get_file_sha1(self, file_id):
 
 
377
        path = self._inventory.id2path(file_id)
 
 
378
        return self._hashcache.get_sha1(path)
 
 
380
    def is_executable(self, file_id):
 
 
382
            return self._inventory[file_id].executable
 
 
384
            path = self._inventory.id2path(file_id)
 
 
385
            mode = os.lstat(self.abspath(path)).st_mode
 
 
386
            return bool(stat.S_ISREG(mode) and stat.S_IEXEC&mode)
 
 
389
    def add(self, files, ids=None):
 
 
390
        """Make files versioned.
 
 
392
        Note that the command line normally calls smart_add instead,
 
 
393
        which can automatically recurse.
 
 
395
        This adds the files to the inventory, so that they will be
 
 
396
        recorded by the next commit.
 
 
399
            List of paths to add, relative to the base of the tree.
 
 
402
            If set, use these instead of automatically generated ids.
 
 
403
            Must be the same length as the list of files, but may
 
 
404
            contain None for ids that are to be autogenerated.
 
 
406
        TODO: Perhaps have an option to add the ids even if the files do
 
 
409
        TODO: Perhaps callback with the ids and paths as they're added.
 
 
411
        # TODO: Re-adding a file that is removed in the working copy
 
 
412
        # should probably put it back with the previous ID.
 
 
413
        if isinstance(files, basestring):
 
 
414
            assert(ids is None or isinstance(ids, basestring))
 
 
420
            ids = [None] * len(files)
 
 
422
            assert(len(ids) == len(files))
 
 
424
        inv = self.read_working_inventory()
 
 
425
        for f,file_id in zip(files, ids):
 
 
426
            if is_control_file(f):
 
 
427
                raise BzrError("cannot add control file %s" % quotefn(f))
 
 
432
                raise BzrError("cannot add top-level %r" % f)
 
 
434
            fullpath = normpath(self.abspath(f))
 
 
437
                kind = file_kind(fullpath)
 
 
439
                # maybe something better?
 
 
440
                raise BzrError('cannot add: not a regular file, symlink or directory: %s' % quotefn(f))
 
 
442
            if not InventoryEntry.versionable_kind(kind):
 
 
443
                raise BzrError('cannot add: not a versionable file ('
 
 
444
                               'i.e. regular file, symlink or directory): %s' % quotefn(f))
 
 
447
                file_id = gen_file_id(f)
 
 
448
            inv.add_path(f, kind=kind, file_id=file_id)
 
 
450
            mutter("add file %s file_id:{%s} kind=%r" % (f, file_id, kind))
 
 
451
        self._write_inventory(inv)
 
 
454
    def add_pending_merge(self, *revision_ids):
 
 
455
        # TODO: Perhaps should check at this point that the
 
 
456
        # history of the revision is actually present?
 
 
457
        p = self.pending_merges()
 
 
459
        for rev_id in revision_ids:
 
 
465
            self.set_pending_merges(p)
 
 
467
    def pending_merges(self):
 
 
468
        """Return a list of pending merges.
 
 
470
        These are revisions that have been merged into the working
 
 
471
        directory but not yet committed.
 
 
474
            merges_file = self._controlfile('pending-merges')
 
 
476
            if e.errno != errno.ENOENT:
 
 
480
        for l in merges_file.readlines():
 
 
481
            p.append(l.rstrip('\n'))
 
 
484
    def _abs_controlfilename(self, name):
 
 
485
        """return the path for the controlfile name in the workingtree."""
 
 
486
        return pathjoin(self.basedir, '.bzr', name)
 
 
488
    def _controlfile(self, name, encoding='utf-8'):
 
 
489
        """Get a control file for the checkout.
 
 
491
        FIXME RBC 20060123 when storage comes in this should be a lockable
 
 
495
        return codecs.open(self._abs_controlfilename(name), encoding=encoding)
 
 
498
    def set_pending_merges(self, rev_list):
 
 
500
        sio.write('\n'.join(rev_list).encode('utf-8'))
 
 
502
        f = AtomicFile(self._abs_controlfilename('pending-merges'))
 
 
509
    def get_symlink_target(self, file_id):
 
 
510
        return os.readlink(self.id2abspath(file_id))
 
 
512
    def file_class(self, filename):
 
 
513
        if self.path2id(filename):
 
 
515
        elif self.is_ignored(filename):
 
 
521
    def list_files(self):
 
 
522
        """Recursively list all files as (path, class, kind, id).
 
 
524
        Lists, but does not descend into unversioned directories.
 
 
526
        This does not include files that have been deleted in this
 
 
529
        Skips the control directory.
 
 
531
        inv = self._inventory
 
 
533
        def descend(from_dir_relpath, from_dir_id, dp):
 
 
537
                ## TODO: If we find a subdirectory with its own .bzr
 
 
538
                ## directory, then that is a separate tree and we
 
 
539
                ## should exclude it.
 
 
540
                if bzrlib.BZRDIR == f:
 
 
544
                fp = appendpath(from_dir_relpath, f)
 
 
547
                fap = appendpath(dp, f)
 
 
549
                f_ie = inv.get_child(from_dir_id, f)
 
 
552
                elif self.is_ignored(fp):
 
 
561
                        raise BzrCheckError("file %r entered as kind %r id %r, "
 
 
563
                                            % (fap, f_ie.kind, f_ie.file_id, fk))
 
 
565
                # make a last minute entry
 
 
569
                    if fk == 'directory':
 
 
570
                        entry = TreeDirectory()
 
 
573
                    elif fk == 'symlink':
 
 
578
                yield fp, c, fk, (f_ie and f_ie.file_id), entry
 
 
580
                if fk != 'directory':
 
 
584
                    # don't descend unversioned directories
 
 
587
                for ff in descend(fp, f_ie.file_id, fap):
 
 
590
        for f in descend(u'', inv.root.file_id, self.basedir):
 
 
594
    def move(self, from_paths, to_name):
 
 
597
        to_name must exist in the inventory.
 
 
599
        If to_name exists and is a directory, the files are moved into
 
 
600
        it, keeping their old names.  
 
 
602
        Note that to_name is only the last component of the new name;
 
 
603
        this doesn't change the directory.
 
 
605
        This returns a list of (from_path, to_path) pairs for each
 
 
609
        ## TODO: Option to move IDs only
 
 
610
        assert not isinstance(from_paths, basestring)
 
 
612
        to_abs = self.abspath(to_name)
 
 
613
        if not isdir(to_abs):
 
 
614
            raise BzrError("destination %r is not a directory" % to_abs)
 
 
615
        if not self.has_filename(to_name):
 
 
616
            raise BzrError("destination %r not in working directory" % to_abs)
 
 
617
        to_dir_id = inv.path2id(to_name)
 
 
618
        if to_dir_id == None and to_name != '':
 
 
619
            raise BzrError("destination %r is not a versioned directory" % to_name)
 
 
620
        to_dir_ie = inv[to_dir_id]
 
 
621
        if to_dir_ie.kind not in ('directory', 'root_directory'):
 
 
622
            raise BzrError("destination %r is not a directory" % to_abs)
 
 
624
        to_idpath = inv.get_idpath(to_dir_id)
 
 
627
            if not self.has_filename(f):
 
 
628
                raise BzrError("%r does not exist in working tree" % f)
 
 
629
            f_id = inv.path2id(f)
 
 
631
                raise BzrError("%r is not versioned" % f)
 
 
632
            name_tail = splitpath(f)[-1]
 
 
633
            dest_path = appendpath(to_name, name_tail)
 
 
634
            if self.has_filename(dest_path):
 
 
635
                raise BzrError("destination %r already exists" % dest_path)
 
 
636
            if f_id in to_idpath:
 
 
637
                raise BzrError("can't move %r to a subdirectory of itself" % f)
 
 
639
        # OK, so there's a race here, it's possible that someone will
 
 
640
        # create a file in this interval and then the rename might be
 
 
641
        # left half-done.  But we should have caught most problems.
 
 
642
        orig_inv = deepcopy(self.inventory)
 
 
645
                name_tail = splitpath(f)[-1]
 
 
646
                dest_path = appendpath(to_name, name_tail)
 
 
647
                result.append((f, dest_path))
 
 
648
                inv.rename(inv.path2id(f), to_dir_id, name_tail)
 
 
650
                    rename(self.abspath(f), self.abspath(dest_path))
 
 
652
                    raise BzrError("failed to rename %r to %r: %s" %
 
 
653
                                   (f, dest_path, e[1]),
 
 
654
                            ["rename rolled back"])
 
 
656
            # restore the inventory on error
 
 
657
            self._set_inventory(orig_inv)
 
 
659
        self._write_inventory(inv)
 
 
663
    def rename_one(self, from_rel, to_rel):
 
 
666
        This can change the directory or the filename or both.
 
 
669
        if not self.has_filename(from_rel):
 
 
670
            raise BzrError("can't rename: old working file %r does not exist" % from_rel)
 
 
671
        if self.has_filename(to_rel):
 
 
672
            raise BzrError("can't rename: new working file %r already exists" % to_rel)
 
 
674
        file_id = inv.path2id(from_rel)
 
 
676
            raise BzrError("can't rename: old name %r is not versioned" % from_rel)
 
 
679
        from_parent = entry.parent_id
 
 
680
        from_name = entry.name
 
 
682
        if inv.path2id(to_rel):
 
 
683
            raise BzrError("can't rename: new name %r is already versioned" % to_rel)
 
 
685
        to_dir, to_tail = os.path.split(to_rel)
 
 
686
        to_dir_id = inv.path2id(to_dir)
 
 
687
        if to_dir_id == None and to_dir != '':
 
 
688
            raise BzrError("can't determine destination directory id for %r" % to_dir)
 
 
690
        mutter("rename_one:")
 
 
691
        mutter("  file_id    {%s}" % file_id)
 
 
692
        mutter("  from_rel   %r" % from_rel)
 
 
693
        mutter("  to_rel     %r" % to_rel)
 
 
694
        mutter("  to_dir     %r" % to_dir)
 
 
695
        mutter("  to_dir_id  {%s}" % to_dir_id)
 
 
697
        inv.rename(file_id, to_dir_id, to_tail)
 
 
699
        from_abs = self.abspath(from_rel)
 
 
700
        to_abs = self.abspath(to_rel)
 
 
702
            rename(from_abs, to_abs)
 
 
704
            inv.rename(file_id, from_parent, from_name)
 
 
705
            raise BzrError("failed to rename %r to %r: %s"
 
 
706
                    % (from_abs, to_abs, e[1]),
 
 
707
                    ["rename rolled back"])
 
 
708
        self._write_inventory(inv)
 
 
712
        """Return all unknown files.
 
 
714
        These are files in the working directory that are not versioned or
 
 
715
        control files or ignored.
 
 
717
        >>> from bzrlib.branch import ScratchBranch
 
 
718
        >>> b = ScratchBranch(files=['foo', 'foo~'])
 
 
719
        >>> tree = WorkingTree(b.base, b)
 
 
720
        >>> map(str, tree.unknowns())
 
 
723
        >>> list(b.unknowns())
 
 
725
        >>> tree.remove('foo')
 
 
726
        >>> list(b.unknowns())
 
 
729
        for subp in self.extras():
 
 
730
            if not self.is_ignored(subp):
 
 
733
    def iter_conflicts(self):
 
 
735
        for path in (s[0] for s in self.list_files()):
 
 
736
            stem = get_conflicted_stem(path)
 
 
739
            if stem not in conflicted:
 
 
744
    def pull(self, source, overwrite=False):
 
 
745
        from bzrlib.merge import merge_inner
 
 
748
            old_revision_history = self.branch.revision_history()
 
 
749
            count = self.branch.pull(source, overwrite)
 
 
750
            new_revision_history = self.branch.revision_history()
 
 
751
            if new_revision_history != old_revision_history:
 
 
752
                if len(old_revision_history):
 
 
753
                    other_revision = old_revision_history[-1]
 
 
755
                    other_revision = None
 
 
756
                merge_inner(self.branch,
 
 
757
                            self.branch.basis_tree(), 
 
 
758
                            self.branch.revision_tree(other_revision),
 
 
760
                self.set_last_revision(self.branch.last_revision())
 
 
766
        """Yield all unknown files in this WorkingTree.
 
 
768
        If there are any unknown directories then only the directory is
 
 
769
        returned, not all its children.  But if there are unknown files
 
 
770
        under a versioned subdirectory, they are returned.
 
 
772
        Currently returned depth-first, sorted by name within directories.
 
 
774
        ## TODO: Work from given directory downwards
 
 
775
        for path, dir_entry in self.inventory.directories():
 
 
776
            mutter("search for unknowns in %r", path)
 
 
777
            dirabs = self.abspath(path)
 
 
778
            if not isdir(dirabs):
 
 
779
                # e.g. directory deleted
 
 
783
            for subf in os.listdir(dirabs):
 
 
785
                    and (subf not in dir_entry.children)):
 
 
790
                subp = appendpath(path, subf)
 
 
794
    def ignored_files(self):
 
 
795
        """Yield list of PATH, IGNORE_PATTERN"""
 
 
796
        for subp in self.extras():
 
 
797
            pat = self.is_ignored(subp)
 
 
802
    def get_ignore_list(self):
 
 
803
        """Return list of ignore patterns.
 
 
805
        Cached in the Tree object after the first call.
 
 
807
        if hasattr(self, '_ignorelist'):
 
 
808
            return self._ignorelist
 
 
810
        l = bzrlib.DEFAULT_IGNORE[:]
 
 
811
        if self.has_filename(bzrlib.IGNORE_FILENAME):
 
 
812
            f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
 
 
813
            l.extend([line.rstrip("\n\r") for line in f.readlines()])
 
 
818
    def is_ignored(self, filename):
 
 
819
        r"""Check whether the filename matches an ignore pattern.
 
 
821
        Patterns containing '/' or '\' need to match the whole path;
 
 
822
        others match against only the last component.
 
 
824
        If the file is ignored, returns the pattern which caused it to
 
 
825
        be ignored, otherwise None.  So this can simply be used as a
 
 
826
        boolean if desired."""
 
 
828
        # TODO: Use '**' to match directories, and other extended
 
 
829
        # globbing stuff from cvs/rsync.
 
 
831
        # XXX: fnmatch is actually not quite what we want: it's only
 
 
832
        # approximately the same as real Unix fnmatch, and doesn't
 
 
833
        # treat dotfiles correctly and allows * to match /.
 
 
834
        # Eventually it should be replaced with something more
 
 
837
        for pat in self.get_ignore_list():
 
 
838
            if '/' in pat or '\\' in pat:
 
 
840
                # as a special case, you can put ./ at the start of a
 
 
841
                # pattern; this is good to match in the top-level
 
 
844
                if (pat[:2] == './') or (pat[:2] == '.\\'):
 
 
848
                if fnmatch.fnmatchcase(filename, newpat):
 
 
851
                if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):
 
 
856
    def kind(self, file_id):
 
 
857
        return file_kind(self.id2abspath(file_id))
 
 
860
        """See Branch.lock_read, and WorkingTree.unlock."""
 
 
861
        return self.branch.lock_read()
 
 
863
    def lock_write(self):
 
 
864
        """See Branch.lock_write, and WorkingTree.unlock."""
 
 
865
        return self.branch.lock_write()
 
 
867
    def _basis_inventory_name(self, revision_id):
 
 
868
        return 'basis-inventory.%s' % revision_id
 
 
870
    def set_last_revision(self, new_revision, old_revision=None):
 
 
873
                path = self._basis_inventory_name(old_revision)
 
 
874
                path = self.branch._rel_controlfilename(path)
 
 
875
                self.branch._transport.delete(path)
 
 
879
            xml = self.branch.get_inventory_xml(new_revision)
 
 
880
            path = self._basis_inventory_name(new_revision)
 
 
881
            self.branch.put_controlfile(path, xml)
 
 
882
        except WeaveRevisionNotPresent:
 
 
885
    def read_basis_inventory(self, revision_id):
 
 
886
        """Read the cached basis inventory."""
 
 
887
        path = self._basis_inventory_name(revision_id)
 
 
888
        return self.branch.controlfile(path, 'r').read()
 
 
891
    def read_working_inventory(self):
 
 
892
        """Read the working inventory."""
 
 
893
        # ElementTree does its own conversion from UTF-8, so open in
 
 
895
        return bzrlib.xml5.serializer_v5.read_inventory(
 
 
896
            self._controlfile('inventory', encoding=None))
 
 
899
    def remove(self, files, verbose=False):
 
 
900
        """Remove nominated files from the working inventory..
 
 
902
        This does not remove their text.  This does not run on XXX on what? RBC
 
 
904
        TODO: Refuse to remove modified files unless --force is given?
 
 
906
        TODO: Do something useful with directories.
 
 
908
        TODO: Should this remove the text or not?  Tough call; not
 
 
909
        removing may be useful and the user can just use use rm, and
 
 
910
        is the opposite of add.  Removing it is consistent with most
 
 
911
        other tools.  Maybe an option.
 
 
913
        ## TODO: Normalize names
 
 
914
        ## TODO: Remove nested loops; better scalability
 
 
915
        if isinstance(files, basestring):
 
 
920
        # do this before any modifications
 
 
924
                # TODO: Perhaps make this just a warning, and continue?
 
 
925
                # This tends to happen when 
 
 
926
                raise NotVersionedError(path=f)
 
 
927
            mutter("remove inventory entry %s {%s}", quotefn(f), fid)
 
 
929
                # having remove it, it must be either ignored or unknown
 
 
930
                if self.is_ignored(f):
 
 
934
                show_status(new_status, inv[fid].kind, quotefn(f))
 
 
937
        self._write_inventory(inv)
 
 
940
    def revert(self, filenames, old_tree=None, backups=True):
 
 
941
        from bzrlib.merge import merge_inner
 
 
943
            old_tree = self.branch.basis_tree()
 
 
944
        merge_inner(self.branch, old_tree,
 
 
945
                    self, ignore_zero=True,
 
 
946
                    backup_files=backups, 
 
 
947
                    interesting_files=filenames,
 
 
949
        if not len(filenames):
 
 
950
            self.set_pending_merges([])
 
 
953
    def set_inventory(self, new_inventory_list):
 
 
954
        from bzrlib.inventory import (Inventory,
 
 
959
        inv = Inventory(self.get_root_id())
 
 
960
        for path, file_id, parent, kind in new_inventory_list:
 
 
961
            name = os.path.basename(path)
 
 
964
            # fixme, there should be a factory function inv,add_?? 
 
 
965
            if kind == 'directory':
 
 
966
                inv.add(InventoryDirectory(file_id, name, parent))
 
 
968
                inv.add(InventoryFile(file_id, name, parent))
 
 
969
            elif kind == 'symlink':
 
 
970
                inv.add(InventoryLink(file_id, name, parent))
 
 
972
                raise BzrError("unknown kind %r" % kind)
 
 
973
        self._write_inventory(inv)
 
 
976
    def set_root_id(self, file_id):
 
 
977
        """Set the root id for this tree."""
 
 
978
        inv = self.read_working_inventory()
 
 
979
        orig_root_id = inv.root.file_id
 
 
980
        del inv._byid[inv.root.file_id]
 
 
981
        inv.root.file_id = file_id
 
 
982
        inv._byid[inv.root.file_id] = inv.root
 
 
985
            if entry.parent_id in (None, orig_root_id):
 
 
986
                entry.parent_id = inv.root.file_id
 
 
987
        self._write_inventory(inv)
 
 
990
        """See Branch.unlock.
 
 
992
        WorkingTree locking just uses the Branch locking facilities.
 
 
993
        This is current because all working trees have an embedded branch
 
 
994
        within them. IF in the future, we were to make branch data shareable
 
 
995
        between multiple working trees, i.e. via shared storage, then we 
 
 
996
        would probably want to lock both the local tree, and the branch.
 
 
998
        if self._hashcache.needs_write and self.branch._lock_count==1:
 
 
999
            self._hashcache.write()
 
 
1000
        return self.branch.unlock()
 
 
1003
    def _write_inventory(self, inv):
 
 
1004
        """Write inventory as the current inventory."""
 
 
1006
        bzrlib.xml5.serializer_v5.write_inventory(inv, sio)
 
 
1008
        f = AtomicFile(self._abs_controlfilename('inventory'))
 
 
1014
        self._set_inventory(inv)
 
 
1015
        mutter('wrote working inventory')
 
 
1018
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
 
 
1019
def get_conflicted_stem(path):
 
 
1020
    for suffix in CONFLICT_SUFFIXES:
 
 
1021
        if path.endswith(suffix):
 
 
1022
            return path[:-len(suffix)]