/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/workingtree.py

  • Committer: Martin Pool
  • Date: 2006-01-13 06:38:56 UTC
  • mto: (1185.65.28 storage)
  • mto: This revision was merged to the branch mainline in revision 1550.
  • Revision ID: mbp@sourcefrog.net-20060113063856-484eed116191727b
Pass through wrapped function name and docstrign 
in needs_read_lock and needs_write_lock decorators.  Test this works.
(Suggestion from John)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 Canonical Ltd
 
2
 
 
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.
 
7
 
 
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.
 
12
 
 
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
 
16
 
 
17
"""WorkingTree object and friends.
 
18
 
 
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.
 
24
 
 
25
At the moment every WorkingTree has its own branch.  Remote
 
26
WorkingTrees aren't supported.
 
27
 
 
28
To get a WorkingTree, call Branch.working_tree():
 
29
"""
 
30
 
 
31
 
 
32
# TODO: Don't allow WorkingTrees to be constructed for remote branches if 
 
33
# they don't work.
 
34
 
 
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.
 
38
 
 
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.
 
44
 
 
45
from copy import deepcopy
 
46
import os
 
47
import stat
 
48
import fnmatch
 
49
 
 
50
from bzrlib.branch import (Branch,
 
51
                           is_control_file,
 
52
                           quotefn)
 
53
from bzrlib.errors import (BzrCheckError,
 
54
                           BzrError,
 
55
                           DivergedBranches,
 
56
                           WeaveRevisionNotPresent,
 
57
                           NotBranchError,
 
58
                           NoSuchFile,
 
59
                           NotVersionedError)
 
60
from bzrlib.inventory import InventoryEntry
 
61
from bzrlib.osutils import (appendpath,
 
62
                            compact_date,
 
63
                            file_kind,
 
64
                            isdir,
 
65
                            getcwd,
 
66
                            pathjoin,
 
67
                            pumpfile,
 
68
                            splitpath,
 
69
                            rand_bytes,
 
70
                            abspath,
 
71
                            normpath,
 
72
                            realpath,
 
73
                            relpath,
 
74
                            rename)
 
75
from bzrlib.textui import show_status
 
76
import bzrlib.tree
 
77
from bzrlib.trace import mutter
 
78
import bzrlib.xml5
 
79
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
80
 
 
81
 
 
82
def gen_file_id(name):
 
83
    """Return new file id.
 
84
 
 
85
    This should probably generate proper UUIDs, but for the moment we
 
86
    cope with just randomness because running uuidgen every time is
 
87
    slow."""
 
88
    import re
 
89
    from binascii import hexlify
 
90
    from time import time
 
91
 
 
92
    # get last component
 
93
    idx = name.rfind('/')
 
94
    if idx != -1:
 
95
        name = name[idx+1 : ]
 
96
    idx = name.rfind('\\')
 
97
    if idx != -1:
 
98
        name = name[idx+1 : ]
 
99
 
 
100
    # make it not a hidden file
 
101
    name = name.lstrip('.')
 
102
 
 
103
    # remove any wierd characters; we don't escape them but rather
 
104
    # just pull them out
 
105
    name = re.sub(r'[^\w.]', '', name)
 
106
 
 
107
    s = hexlify(rand_bytes(8))
 
108
    return '-'.join((name, compact_date(time()), s))
 
109
 
 
110
 
 
111
def gen_root_id():
 
112
    """Return a new tree-root file id."""
 
113
    return gen_file_id('TREE_ROOT')
 
114
 
 
115
 
 
116
class TreeEntry(object):
 
117
    """An entry that implements the minium interface used by commands.
 
118
 
 
119
    This needs further inspection, it may be better to have 
 
120
    InventoryEntries without ids - though that seems wrong. For now,
 
121
    this is a parallel hierarchy to InventoryEntry, and needs to become
 
122
    one of several things: decorates to that hierarchy, children of, or
 
123
    parents of it.
 
124
    Another note is that these objects are currently only used when there is
 
125
    no InventoryEntry available - i.e. for unversioned objects.
 
126
    Perhaps they should be UnversionedEntry et al. ? - RBC 20051003
 
127
    """
 
128
 
 
129
    def __eq__(self, other):
 
130
        # yes, this us ugly, TODO: best practice __eq__ style.
 
131
        return (isinstance(other, TreeEntry)
 
132
                and other.__class__ == self.__class__)
 
133
 
 
134
    def kind_character(self):
 
135
        return "???"
 
136
 
 
137
 
 
138
class TreeDirectory(TreeEntry):
 
139
    """See TreeEntry. This is a directory in a working tree."""
 
140
 
 
141
    def __eq__(self, other):
 
142
        return (isinstance(other, TreeDirectory)
 
143
                and other.__class__ == self.__class__)
 
144
 
 
145
    def kind_character(self):
 
146
        return "/"
 
147
 
 
148
 
 
149
class TreeFile(TreeEntry):
 
150
    """See TreeEntry. This is a regular file in a working tree."""
 
151
 
 
152
    def __eq__(self, other):
 
153
        return (isinstance(other, TreeFile)
 
154
                and other.__class__ == self.__class__)
 
155
 
 
156
    def kind_character(self):
 
157
        return ''
 
158
 
 
159
 
 
160
class TreeLink(TreeEntry):
 
161
    """See TreeEntry. This is a symlink in a working tree."""
 
162
 
 
163
    def __eq__(self, other):
 
164
        return (isinstance(other, TreeLink)
 
165
                and other.__class__ == self.__class__)
 
166
 
 
167
    def kind_character(self):
 
168
        return ''
 
169
 
 
170
 
 
171
class WorkingTree(bzrlib.tree.Tree):
 
172
    """Working copy tree.
 
173
 
 
174
    The inventory is held in the `Branch` working-inventory, and the
 
175
    files are in a directory on disk.
 
176
 
 
177
    It is possible for a `WorkingTree` to have a filename which is
 
178
    not listed in the Inventory and vice versa.
 
179
    """
 
180
 
 
181
    def __init__(self, basedir=u'.', branch=None):
 
182
        """Construct a WorkingTree for basedir.
 
183
 
 
184
        If the branch is not supplied, it is opened automatically.
 
185
        If the branch is supplied, it must be the branch for this basedir.
 
186
        (branch.base is not cross checked, because for remote branches that
 
187
        would be meaningless).
 
188
        """
 
189
        from bzrlib.hashcache import HashCache
 
190
        from bzrlib.trace import note, mutter
 
191
        assert isinstance(basedir, basestring), \
 
192
            "base directory %r is not a string" % basedir
 
193
        if branch is None:
 
194
            branch = Branch.open(basedir)
 
195
        assert isinstance(branch, Branch), \
 
196
            "branch %r is not a Branch" % branch
 
197
        self.branch = branch
 
198
        self.basedir = realpath(basedir)
 
199
 
 
200
        # update the whole cache up front and write to disk if anything changed;
 
201
        # in the future we might want to do this more selectively
 
202
        # two possible ways offer themselves : in self._unlock, write the cache
 
203
        # if needed, or, when the cache sees a change, append it to the hash
 
204
        # cache file, and have the parser take the most recent entry for a
 
205
        # given path only.
 
206
        hc = self._hashcache = HashCache(basedir)
 
207
        hc.read()
 
208
        hc.scan()
 
209
 
 
210
        if hc.needs_write:
 
211
            mutter("write hc")
 
212
            hc.write()
 
213
 
 
214
        self._set_inventory(self.read_working_inventory())
 
215
 
 
216
    def _set_inventory(self, inv):
 
217
        self._inventory = inv
 
218
        self.path2id = self._inventory.path2id
 
219
 
 
220
    @staticmethod
 
221
    def open_containing(path=None):
 
222
        """Open an existing working tree which has its root about path.
 
223
        
 
224
        This probes for a working tree at path and searches upwards from there.
 
225
 
 
226
        Basically we keep looking up until we find the control directory or
 
227
        run into /.  If there isn't one, raises NotBranchError.
 
228
        TODO: give this a new exception.
 
229
        If there is one, it is returned, along with the unused portion of path.
 
230
        """
 
231
        if path is None:
 
232
            path = getcwd()
 
233
        else:
 
234
            # sanity check.
 
235
            if path.find('://') != -1:
 
236
                raise NotBranchError(path=path)
 
237
        path = abspath(path)
 
238
        tail = u''
 
239
        while True:
 
240
            try:
 
241
                return WorkingTree(path), tail
 
242
            except NotBranchError:
 
243
                pass
 
244
            if tail:
 
245
                tail = pathjoin(os.path.basename(path), tail)
 
246
            else:
 
247
                tail = os.path.basename(path)
 
248
            lastpath = path
 
249
            path = os.path.dirname(path)
 
250
            if lastpath == path:
 
251
                # reached the root, whatever that may be
 
252
                raise NotBranchError(path=path)
 
253
 
 
254
    def __iter__(self):
 
255
        """Iterate through file_ids for this tree.
 
256
 
 
257
        file_ids are in a WorkingTree if they are in the working inventory
 
258
        and the working file exists.
 
259
        """
 
260
        inv = self._inventory
 
261
        for path, ie in inv.iter_entries():
 
262
            if bzrlib.osutils.lexists(self.abspath(path)):
 
263
                yield ie.file_id
 
264
 
 
265
    def __repr__(self):
 
266
        return "<%s of %s>" % (self.__class__.__name__,
 
267
                               getattr(self, 'basedir', None))
 
268
 
 
269
    def abspath(self, filename):
 
270
        return pathjoin(self.basedir, filename)
 
271
 
 
272
    def relpath(self, abs):
 
273
        """Return the local path portion from a given absolute path."""
 
274
        return relpath(self.basedir, abs)
 
275
 
 
276
    def has_filename(self, filename):
 
277
        return bzrlib.osutils.lexists(self.abspath(filename))
 
278
 
 
279
    def get_file(self, file_id):
 
280
        return self.get_file_byname(self.id2path(file_id))
 
281
 
 
282
    def get_file_byname(self, filename):
 
283
        return file(self.abspath(filename), 'rb')
 
284
 
 
285
    def get_root_id(self):
 
286
        """Return the id of this trees root"""
 
287
        inv = self.read_working_inventory()
 
288
        return inv.root.file_id
 
289
        
 
290
    def _get_store_filename(self, file_id):
 
291
        ## XXX: badly named; this is not in the store at all
 
292
        return self.abspath(self.id2path(file_id))
 
293
 
 
294
    @needs_write_lock
 
295
    def commit(self, *args, **kw):
 
296
        from bzrlib.commit import Commit
 
297
        Commit().commit(self.branch, *args, **kw)
 
298
        self._set_inventory(self.read_working_inventory())
 
299
 
 
300
    def id2abspath(self, file_id):
 
301
        return self.abspath(self.id2path(file_id))
 
302
 
 
303
    def has_id(self, file_id):
 
304
        # files that have been deleted are excluded
 
305
        inv = self._inventory
 
306
        if not inv.has_id(file_id):
 
307
            return False
 
308
        path = inv.id2path(file_id)
 
309
        return bzrlib.osutils.lexists(self.abspath(path))
 
310
 
 
311
    def has_or_had_id(self, file_id):
 
312
        if file_id == self.inventory.root.file_id:
 
313
            return True
 
314
        return self.inventory.has_id(file_id)
 
315
 
 
316
    __contains__ = has_id
 
317
 
 
318
    def get_file_size(self, file_id):
 
319
        return os.path.getsize(self.id2abspath(file_id))
 
320
 
 
321
    @needs_read_lock
 
322
    def get_file_sha1(self, file_id):
 
323
        path = self._inventory.id2path(file_id)
 
324
        return self._hashcache.get_sha1(path)
 
325
 
 
326
    def is_executable(self, file_id):
 
327
        if os.name == "nt":
 
328
            return self._inventory[file_id].executable
 
329
        else:
 
330
            path = self._inventory.id2path(file_id)
 
331
            mode = os.lstat(self.abspath(path)).st_mode
 
332
            return bool(stat.S_ISREG(mode) and stat.S_IEXEC&mode)
 
333
 
 
334
    @needs_write_lock
 
335
    def add(self, files, ids=None):
 
336
        """Make files versioned.
 
337
 
 
338
        Note that the command line normally calls smart_add instead,
 
339
        which can automatically recurse.
 
340
 
 
341
        This adds the files to the inventory, so that they will be
 
342
        recorded by the next commit.
 
343
 
 
344
        files
 
345
            List of paths to add, relative to the base of the tree.
 
346
 
 
347
        ids
 
348
            If set, use these instead of automatically generated ids.
 
349
            Must be the same length as the list of files, but may
 
350
            contain None for ids that are to be autogenerated.
 
351
 
 
352
        TODO: Perhaps have an option to add the ids even if the files do
 
353
              not (yet) exist.
 
354
 
 
355
        TODO: Perhaps callback with the ids and paths as they're added.
 
356
        """
 
357
        # TODO: Re-adding a file that is removed in the working copy
 
358
        # should probably put it back with the previous ID.
 
359
        if isinstance(files, basestring):
 
360
            assert(ids is None or isinstance(ids, basestring))
 
361
            files = [files]
 
362
            if ids is not None:
 
363
                ids = [ids]
 
364
 
 
365
        if ids is None:
 
366
            ids = [None] * len(files)
 
367
        else:
 
368
            assert(len(ids) == len(files))
 
369
 
 
370
        inv = self.read_working_inventory()
 
371
        for f,file_id in zip(files, ids):
 
372
            if is_control_file(f):
 
373
                raise BzrError("cannot add control file %s" % quotefn(f))
 
374
 
 
375
            fp = splitpath(f)
 
376
 
 
377
            if len(fp) == 0:
 
378
                raise BzrError("cannot add top-level %r" % f)
 
379
 
 
380
            fullpath = normpath(self.abspath(f))
 
381
 
 
382
            try:
 
383
                kind = file_kind(fullpath)
 
384
            except OSError:
 
385
                # maybe something better?
 
386
                raise BzrError('cannot add: not a regular file, symlink or directory: %s' % quotefn(f))
 
387
 
 
388
            if not InventoryEntry.versionable_kind(kind):
 
389
                raise BzrError('cannot add: not a versionable file ('
 
390
                               'i.e. regular file, symlink or directory): %s' % quotefn(f))
 
391
 
 
392
            if file_id is None:
 
393
                file_id = gen_file_id(f)
 
394
            inv.add_path(f, kind=kind, file_id=file_id)
 
395
 
 
396
            mutter("add file %s file_id:{%s} kind=%r" % (f, file_id, kind))
 
397
        self._write_inventory(inv)
 
398
 
 
399
    @needs_write_lock
 
400
    def add_pending_merge(self, *revision_ids):
 
401
        # TODO: Perhaps should check at this point that the
 
402
        # history of the revision is actually present?
 
403
        p = self.pending_merges()
 
404
        updated = False
 
405
        for rev_id in revision_ids:
 
406
            if rev_id in p:
 
407
                continue
 
408
            p.append(rev_id)
 
409
            updated = True
 
410
        if updated:
 
411
            self.set_pending_merges(p)
 
412
 
 
413
    @needs_read_lock
 
414
    def pending_merges(self):
 
415
        """Return a list of pending merges.
 
416
 
 
417
        These are revisions that have been merged into the working
 
418
        directory but not yet committed.
 
419
        """
 
420
        try:
 
421
            f = self.branch.control_files.controlfile('pending-merges', 'r')
 
422
        except NoSuchFile:
 
423
            return []
 
424
        p = []
 
425
        for l in f.readlines():
 
426
            p.append(l.rstrip('\n'))
 
427
        return p
 
428
 
 
429
    @needs_write_lock
 
430
    def set_pending_merges(self, rev_list):
 
431
        self.branch.control_files.put_utf8('pending-merges', '\n'.join(rev_list))
 
432
 
 
433
    def get_symlink_target(self, file_id):
 
434
        return os.readlink(self.id2abspath(file_id))
 
435
 
 
436
    def file_class(self, filename):
 
437
        if self.path2id(filename):
 
438
            return 'V'
 
439
        elif self.is_ignored(filename):
 
440
            return 'I'
 
441
        else:
 
442
            return '?'
 
443
 
 
444
 
 
445
    def list_files(self):
 
446
        """Recursively list all files as (path, class, kind, id).
 
447
 
 
448
        Lists, but does not descend into unversioned directories.
 
449
 
 
450
        This does not include files that have been deleted in this
 
451
        tree.
 
452
 
 
453
        Skips the control directory.
 
454
        """
 
455
        inv = self._inventory
 
456
 
 
457
        def descend(from_dir_relpath, from_dir_id, dp):
 
458
            ls = os.listdir(dp)
 
459
            ls.sort()
 
460
            for f in ls:
 
461
                ## TODO: If we find a subdirectory with its own .bzr
 
462
                ## directory, then that is a separate tree and we
 
463
                ## should exclude it.
 
464
                if bzrlib.BZRDIR == f:
 
465
                    continue
 
466
 
 
467
                # path within tree
 
468
                fp = appendpath(from_dir_relpath, f)
 
469
 
 
470
                # absolute path
 
471
                fap = appendpath(dp, f)
 
472
                
 
473
                f_ie = inv.get_child(from_dir_id, f)
 
474
                if f_ie:
 
475
                    c = 'V'
 
476
                elif self.is_ignored(fp):
 
477
                    c = 'I'
 
478
                else:
 
479
                    c = '?'
 
480
 
 
481
                fk = file_kind(fap)
 
482
 
 
483
                if f_ie:
 
484
                    if f_ie.kind != fk:
 
485
                        raise BzrCheckError("file %r entered as kind %r id %r, "
 
486
                                            "now of kind %r"
 
487
                                            % (fap, f_ie.kind, f_ie.file_id, fk))
 
488
 
 
489
                # make a last minute entry
 
490
                if f_ie:
 
491
                    entry = f_ie
 
492
                else:
 
493
                    if fk == 'directory':
 
494
                        entry = TreeDirectory()
 
495
                    elif fk == 'file':
 
496
                        entry = TreeFile()
 
497
                    elif fk == 'symlink':
 
498
                        entry = TreeLink()
 
499
                    else:
 
500
                        entry = TreeEntry()
 
501
                
 
502
                yield fp, c, fk, (f_ie and f_ie.file_id), entry
 
503
 
 
504
                if fk != 'directory':
 
505
                    continue
 
506
 
 
507
                if c != 'V':
 
508
                    # don't descend unversioned directories
 
509
                    continue
 
510
                
 
511
                for ff in descend(fp, f_ie.file_id, fap):
 
512
                    yield ff
 
513
 
 
514
        for f in descend(u'', inv.root.file_id, self.basedir):
 
515
            yield f
 
516
 
 
517
    @needs_write_lock
 
518
    def move(self, from_paths, to_name):
 
519
        """Rename files.
 
520
 
 
521
        to_name must exist in the inventory.
 
522
 
 
523
        If to_name exists and is a directory, the files are moved into
 
524
        it, keeping their old names.  
 
525
 
 
526
        Note that to_name is only the last component of the new name;
 
527
        this doesn't change the directory.
 
528
 
 
529
        This returns a list of (from_path, to_path) pairs for each
 
530
        entry that is moved.
 
531
        """
 
532
        result = []
 
533
        ## TODO: Option to move IDs only
 
534
        assert not isinstance(from_paths, basestring)
 
535
        inv = self.inventory
 
536
        to_abs = self.abspath(to_name)
 
537
        if not isdir(to_abs):
 
538
            raise BzrError("destination %r is not a directory" % to_abs)
 
539
        if not self.has_filename(to_name):
 
540
            raise BzrError("destination %r not in working directory" % to_abs)
 
541
        to_dir_id = inv.path2id(to_name)
 
542
        if to_dir_id == None and to_name != '':
 
543
            raise BzrError("destination %r is not a versioned directory" % to_name)
 
544
        to_dir_ie = inv[to_dir_id]
 
545
        if to_dir_ie.kind not in ('directory', 'root_directory'):
 
546
            raise BzrError("destination %r is not a directory" % to_abs)
 
547
 
 
548
        to_idpath = inv.get_idpath(to_dir_id)
 
549
 
 
550
        for f in from_paths:
 
551
            if not self.has_filename(f):
 
552
                raise BzrError("%r does not exist in working tree" % f)
 
553
            f_id = inv.path2id(f)
 
554
            if f_id == None:
 
555
                raise BzrError("%r is not versioned" % f)
 
556
            name_tail = splitpath(f)[-1]
 
557
            dest_path = appendpath(to_name, name_tail)
 
558
            if self.has_filename(dest_path):
 
559
                raise BzrError("destination %r already exists" % dest_path)
 
560
            if f_id in to_idpath:
 
561
                raise BzrError("can't move %r to a subdirectory of itself" % f)
 
562
 
 
563
        # OK, so there's a race here, it's possible that someone will
 
564
        # create a file in this interval and then the rename might be
 
565
        # left half-done.  But we should have caught most problems.
 
566
        orig_inv = deepcopy(self.inventory)
 
567
        try:
 
568
            for f in from_paths:
 
569
                name_tail = splitpath(f)[-1]
 
570
                dest_path = appendpath(to_name, name_tail)
 
571
                result.append((f, dest_path))
 
572
                inv.rename(inv.path2id(f), to_dir_id, name_tail)
 
573
                try:
 
574
                    rename(self.abspath(f), self.abspath(dest_path))
 
575
                except OSError, e:
 
576
                    raise BzrError("failed to rename %r to %r: %s" %
 
577
                                   (f, dest_path, e[1]),
 
578
                            ["rename rolled back"])
 
579
        except:
 
580
            # restore the inventory on error
 
581
            self._set_inventory(orig_inv)
 
582
            raise
 
583
        self._write_inventory(inv)
 
584
        return result
 
585
 
 
586
    @needs_write_lock
 
587
    def rename_one(self, from_rel, to_rel):
 
588
        """Rename one file.
 
589
 
 
590
        This can change the directory or the filename or both.
 
591
        """
 
592
        inv = self.inventory
 
593
        if not self.has_filename(from_rel):
 
594
            raise BzrError("can't rename: old working file %r does not exist" % from_rel)
 
595
        if self.has_filename(to_rel):
 
596
            raise BzrError("can't rename: new working file %r already exists" % to_rel)
 
597
 
 
598
        file_id = inv.path2id(from_rel)
 
599
        if file_id == None:
 
600
            raise BzrError("can't rename: old name %r is not versioned" % from_rel)
 
601
 
 
602
        entry = inv[file_id]
 
603
        from_parent = entry.parent_id
 
604
        from_name = entry.name
 
605
        
 
606
        if inv.path2id(to_rel):
 
607
            raise BzrError("can't rename: new name %r is already versioned" % to_rel)
 
608
 
 
609
        to_dir, to_tail = os.path.split(to_rel)
 
610
        to_dir_id = inv.path2id(to_dir)
 
611
        if to_dir_id == None and to_dir != '':
 
612
            raise BzrError("can't determine destination directory id for %r" % to_dir)
 
613
 
 
614
        mutter("rename_one:")
 
615
        mutter("  file_id    {%s}" % file_id)
 
616
        mutter("  from_rel   %r" % from_rel)
 
617
        mutter("  to_rel     %r" % to_rel)
 
618
        mutter("  to_dir     %r" % to_dir)
 
619
        mutter("  to_dir_id  {%s}" % to_dir_id)
 
620
 
 
621
        inv.rename(file_id, to_dir_id, to_tail)
 
622
 
 
623
        from_abs = self.abspath(from_rel)
 
624
        to_abs = self.abspath(to_rel)
 
625
        try:
 
626
            rename(from_abs, to_abs)
 
627
        except OSError, e:
 
628
            inv.rename(file_id, from_parent, from_name)
 
629
            raise BzrError("failed to rename %r to %r: %s"
 
630
                    % (from_abs, to_abs, e[1]),
 
631
                    ["rename rolled back"])
 
632
        self._write_inventory(inv)
 
633
 
 
634
    @needs_read_lock
 
635
    def unknowns(self):
 
636
        """Return all unknown files.
 
637
 
 
638
        These are files in the working directory that are not versioned or
 
639
        control files or ignored.
 
640
        
 
641
        >>> from bzrlib.branch import ScratchBranch
 
642
        >>> b = ScratchBranch(files=['foo', 'foo~'])
 
643
        >>> tree = WorkingTree(b.base, b)
 
644
        >>> map(str, tree.unknowns())
 
645
        ['foo']
 
646
        >>> tree.add('foo')
 
647
        >>> list(b.unknowns())
 
648
        []
 
649
        >>> tree.remove('foo')
 
650
        >>> list(b.unknowns())
 
651
        [u'foo']
 
652
        """
 
653
        for subp in self.extras():
 
654
            if not self.is_ignored(subp):
 
655
                yield subp
 
656
 
 
657
    def iter_conflicts(self):
 
658
        conflicted = set()
 
659
        for path in (s[0] for s in self.list_files()):
 
660
            stem = get_conflicted_stem(path)
 
661
            if stem is None:
 
662
                continue
 
663
            if stem not in conflicted:
 
664
                conflicted.add(stem)
 
665
                yield stem
 
666
 
 
667
    @needs_write_lock
 
668
    def pull(self, source, overwrite=False):
 
669
        from bzrlib.merge import merge_inner
 
670
        source.lock_read()
 
671
        try:
 
672
            old_revision_history = self.branch.revision_history()
 
673
            count = self.branch.pull(source, overwrite)
 
674
            new_revision_history = self.branch.revision_history()
 
675
            if new_revision_history != old_revision_history:
 
676
                if len(old_revision_history):
 
677
                    other_revision = old_revision_history[-1]
 
678
                else:
 
679
                    other_revision = None
 
680
                repository = self.branch.repository
 
681
                merge_inner(self.branch,
 
682
                            self.branch.basis_tree(), 
 
683
                            repository.revision_tree(other_revision))
 
684
            return count
 
685
        finally:
 
686
            source.unlock()
 
687
 
 
688
    def extras(self):
 
689
        """Yield all unknown files in this WorkingTree.
 
690
 
 
691
        If there are any unknown directories then only the directory is
 
692
        returned, not all its children.  But if there are unknown files
 
693
        under a versioned subdirectory, they are returned.
 
694
 
 
695
        Currently returned depth-first, sorted by name within directories.
 
696
        """
 
697
        ## TODO: Work from given directory downwards
 
698
        for path, dir_entry in self.inventory.directories():
 
699
            mutter("search for unknowns in %r", path)
 
700
            dirabs = self.abspath(path)
 
701
            if not isdir(dirabs):
 
702
                # e.g. directory deleted
 
703
                continue
 
704
 
 
705
            fl = []
 
706
            for subf in os.listdir(dirabs):
 
707
                if (subf != '.bzr'
 
708
                    and (subf not in dir_entry.children)):
 
709
                    fl.append(subf)
 
710
            
 
711
            fl.sort()
 
712
            for subf in fl:
 
713
                subp = appendpath(path, subf)
 
714
                yield subp
 
715
 
 
716
 
 
717
    def ignored_files(self):
 
718
        """Yield list of PATH, IGNORE_PATTERN"""
 
719
        for subp in self.extras():
 
720
            pat = self.is_ignored(subp)
 
721
            if pat != None:
 
722
                yield subp, pat
 
723
 
 
724
 
 
725
    def get_ignore_list(self):
 
726
        """Return list of ignore patterns.
 
727
 
 
728
        Cached in the Tree object after the first call.
 
729
        """
 
730
        if hasattr(self, '_ignorelist'):
 
731
            return self._ignorelist
 
732
 
 
733
        l = bzrlib.DEFAULT_IGNORE[:]
 
734
        if self.has_filename(bzrlib.IGNORE_FILENAME):
 
735
            f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
 
736
            l.extend([line.rstrip("\n\r") for line in f.readlines()])
 
737
        self._ignorelist = l
 
738
        return l
 
739
 
 
740
 
 
741
    def is_ignored(self, filename):
 
742
        r"""Check whether the filename matches an ignore pattern.
 
743
 
 
744
        Patterns containing '/' or '\' need to match the whole path;
 
745
        others match against only the last component.
 
746
 
 
747
        If the file is ignored, returns the pattern which caused it to
 
748
        be ignored, otherwise None.  So this can simply be used as a
 
749
        boolean if desired."""
 
750
 
 
751
        # TODO: Use '**' to match directories, and other extended
 
752
        # globbing stuff from cvs/rsync.
 
753
 
 
754
        # XXX: fnmatch is actually not quite what we want: it's only
 
755
        # approximately the same as real Unix fnmatch, and doesn't
 
756
        # treat dotfiles correctly and allows * to match /.
 
757
        # Eventually it should be replaced with something more
 
758
        # accurate.
 
759
        
 
760
        for pat in self.get_ignore_list():
 
761
            if '/' in pat or '\\' in pat:
 
762
                
 
763
                # as a special case, you can put ./ at the start of a
 
764
                # pattern; this is good to match in the top-level
 
765
                # only;
 
766
                
 
767
                if (pat[:2] == './') or (pat[:2] == '.\\'):
 
768
                    newpat = pat[2:]
 
769
                else:
 
770
                    newpat = pat
 
771
                if fnmatch.fnmatchcase(filename, newpat):
 
772
                    return pat
 
773
            else:
 
774
                if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):
 
775
                    return pat
 
776
        else:
 
777
            return None
 
778
 
 
779
    def kind(self, file_id):
 
780
        return file_kind(self.id2abspath(file_id))
 
781
 
 
782
    def lock_read(self):
 
783
        """See Branch.lock_read, and WorkingTree.unlock."""
 
784
        return self.branch.lock_read()
 
785
 
 
786
    def lock_write(self):
 
787
        """See Branch.lock_write, and WorkingTree.unlock."""
 
788
        return self.branch.lock_write()
 
789
 
 
790
    def _basis_inventory_name(self, revision_id):
 
791
        return 'basis-inventory.%s' % revision_id
 
792
 
 
793
    def set_last_revision(self, new_revision, old_revision=None):
 
794
        if old_revision is not None:
 
795
            try:
 
796
                path = self._basis_inventory_name(old_revision)
 
797
                path = self.branch.control_files._escape(path)
 
798
                self.branch.control_files._transport.delete(path)
 
799
            except NoSuchFile:
 
800
                pass
 
801
        try:
 
802
            xml = self.branch.repository.get_inventory_xml(new_revision)
 
803
            path = self._basis_inventory_name(new_revision)
 
804
            self.branch.control_files.put_utf8(path, xml)
 
805
        except WeaveRevisionNotPresent:
 
806
            pass
 
807
 
 
808
    def read_basis_inventory(self, revision_id):
 
809
        """Read the cached basis inventory."""
 
810
        path = self._basis_inventory_name(revision_id)
 
811
        return self.branch.control_files.controlfile(path, 'r').read()
 
812
        
 
813
    @needs_read_lock
 
814
    def read_working_inventory(self):
 
815
        """Read the working inventory."""
 
816
        # ElementTree does its own conversion from UTF-8, so open in
 
817
        # binary.
 
818
        f = self.branch.control_files.controlfile('inventory', 'rb')
 
819
        return bzrlib.xml5.serializer_v5.read_inventory(f)
 
820
 
 
821
    @needs_write_lock
 
822
    def remove(self, files, verbose=False):
 
823
        """Remove nominated files from the working inventory..
 
824
 
 
825
        This does not remove their text.  This does not run on XXX on what? RBC
 
826
 
 
827
        TODO: Refuse to remove modified files unless --force is given?
 
828
 
 
829
        TODO: Do something useful with directories.
 
830
 
 
831
        TODO: Should this remove the text or not?  Tough call; not
 
832
        removing may be useful and the user can just use use rm, and
 
833
        is the opposite of add.  Removing it is consistent with most
 
834
        other tools.  Maybe an option.
 
835
        """
 
836
        ## TODO: Normalize names
 
837
        ## TODO: Remove nested loops; better scalability
 
838
        if isinstance(files, basestring):
 
839
            files = [files]
 
840
 
 
841
        inv = self.inventory
 
842
 
 
843
        # do this before any modifications
 
844
        for f in files:
 
845
            fid = inv.path2id(f)
 
846
            if not fid:
 
847
                # TODO: Perhaps make this just a warning, and continue?
 
848
                # This tends to happen when 
 
849
                raise NotVersionedError(path=f)
 
850
            mutter("remove inventory entry %s {%s}", quotefn(f), fid)
 
851
            if verbose:
 
852
                # having remove it, it must be either ignored or unknown
 
853
                if self.is_ignored(f):
 
854
                    new_status = 'I'
 
855
                else:
 
856
                    new_status = '?'
 
857
                show_status(new_status, inv[fid].kind, quotefn(f))
 
858
            del inv[fid]
 
859
 
 
860
        self._write_inventory(inv)
 
861
 
 
862
    @needs_write_lock
 
863
    def revert(self, filenames, old_tree=None, backups=True):
 
864
        from bzrlib.merge import merge_inner
 
865
        if old_tree is None:
 
866
            old_tree = self.branch.basis_tree()
 
867
        merge_inner(self.branch, old_tree,
 
868
                    self, ignore_zero=True,
 
869
                    backup_files=backups, 
 
870
                    interesting_files=filenames)
 
871
        if not len(filenames):
 
872
            self.set_pending_merges([])
 
873
 
 
874
    @needs_write_lock
 
875
    def set_inventory(self, new_inventory_list):
 
876
        from bzrlib.inventory import (Inventory,
 
877
                                      InventoryDirectory,
 
878
                                      InventoryEntry,
 
879
                                      InventoryFile,
 
880
                                      InventoryLink)
 
881
        inv = Inventory(self.get_root_id())
 
882
        for path, file_id, parent, kind in new_inventory_list:
 
883
            name = os.path.basename(path)
 
884
            if name == "":
 
885
                continue
 
886
            # fixme, there should be a factory function inv,add_?? 
 
887
            if kind == 'directory':
 
888
                inv.add(InventoryDirectory(file_id, name, parent))
 
889
            elif kind == 'file':
 
890
                inv.add(InventoryFile(file_id, name, parent))
 
891
            elif kind == 'symlink':
 
892
                inv.add(InventoryLink(file_id, name, parent))
 
893
            else:
 
894
                raise BzrError("unknown kind %r" % kind)
 
895
        self._write_inventory(inv)
 
896
 
 
897
    @needs_write_lock
 
898
    def set_root_id(self, file_id):
 
899
        """Set the root id for this tree."""
 
900
        inv = self.read_working_inventory()
 
901
        orig_root_id = inv.root.file_id
 
902
        del inv._byid[inv.root.file_id]
 
903
        inv.root.file_id = file_id
 
904
        inv._byid[inv.root.file_id] = inv.root
 
905
        for fid in inv:
 
906
            entry = inv[fid]
 
907
            if entry.parent_id in (None, orig_root_id):
 
908
                entry.parent_id = inv.root.file_id
 
909
        self._write_inventory(inv)
 
910
 
 
911
    def unlock(self):
 
912
        """See Branch.unlock.
 
913
        
 
914
        WorkingTree locking just uses the Branch locking facilities.
 
915
        This is current because all working trees have an embedded branch
 
916
        within them. IF in the future, we were to make branch data shareable
 
917
        between multiple working trees, i.e. via shared storage, then we 
 
918
        would probably want to lock both the local tree, and the branch.
 
919
        """
 
920
        # FIXME: We want to write out the hashcache only when the last lock on
 
921
        # this working copy is released.  Peeking at the lock count is a bit
 
922
        # of a nasty hack; probably it's better to have a transaction object,
 
923
        # which can do some finalization when it's either successfully or
 
924
        # unsuccessfully completed.  (Denys's original patch did that.)
 
925
        if self._hashcache.needs_write and self.branch.control_files._lock_count==1:
 
926
            self._hashcache.write()
 
927
        return self.branch.unlock()
 
928
 
 
929
    @needs_write_lock
 
930
    def _write_inventory(self, inv):
 
931
        """Write inventory as the current inventory."""
 
932
        from cStringIO import StringIO
 
933
        from bzrlib.atomicfile import AtomicFile
 
934
        sio = StringIO()
 
935
        bzrlib.xml5.serializer_v5.write_inventory(inv, sio)
 
936
        sio.seek(0)
 
937
        f = AtomicFile(self.branch.control_files.controlfilename('inventory'))
 
938
        try:
 
939
            pumpfile(sio, f)
 
940
            f.commit()
 
941
        finally:
 
942
            f.close()
 
943
        self._set_inventory(inv)
 
944
        mutter('wrote working inventory')
 
945
            
 
946
 
 
947
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
 
948
def get_conflicted_stem(path):
 
949
    for suffix in CONFLICT_SUFFIXES:
 
950
        if path.endswith(suffix):
 
951
            return path[:-len(suffix)]