/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

 * The internal storage of history, and logical branch identity have now
   been split into Branch, and Repository. The common locking and file 
   management routines are now in bzrlib.lockablefiles. 
   (Aaron Bentley, Robert Collins, Martin Pool)

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
        orig_path = path[:]
 
239
        tail = u''
 
240
        while True:
 
241
            try:
 
242
                return WorkingTree(path), tail
 
243
            except NotBranchError:
 
244
                pass
 
245
            if tail:
 
246
                tail = pathjoin(os.path.basename(path), tail)
 
247
            else:
 
248
                tail = os.path.basename(path)
 
249
            lastpath = path
 
250
            path = os.path.dirname(path)
 
251
            if lastpath == path:
 
252
                # reached the root, whatever that may be
 
253
                raise NotBranchError(path=orig_path)
 
254
 
 
255
    def __iter__(self):
 
256
        """Iterate through file_ids for this tree.
 
257
 
 
258
        file_ids are in a WorkingTree if they are in the working inventory
 
259
        and the working file exists.
 
260
        """
 
261
        inv = self._inventory
 
262
        for path, ie in inv.iter_entries():
 
263
            if bzrlib.osutils.lexists(self.abspath(path)):
 
264
                yield ie.file_id
 
265
 
 
266
    def __repr__(self):
 
267
        return "<%s of %s>" % (self.__class__.__name__,
 
268
                               getattr(self, 'basedir', None))
 
269
 
 
270
    def abspath(self, filename):
 
271
        return pathjoin(self.basedir, filename)
 
272
 
 
273
    def relpath(self, abs):
 
274
        """Return the local path portion from a given absolute path."""
 
275
        return relpath(self.basedir, abs)
 
276
 
 
277
    def has_filename(self, filename):
 
278
        return bzrlib.osutils.lexists(self.abspath(filename))
 
279
 
 
280
    def get_file(self, file_id):
 
281
        return self.get_file_byname(self.id2path(file_id))
 
282
 
 
283
    def get_file_byname(self, filename):
 
284
        return file(self.abspath(filename), 'rb')
 
285
 
 
286
    def get_root_id(self):
 
287
        """Return the id of this trees root"""
 
288
        inv = self.read_working_inventory()
 
289
        return inv.root.file_id
 
290
        
 
291
    def _get_store_filename(self, file_id):
 
292
        ## XXX: badly named; this is not in the store at all
 
293
        return self.abspath(self.id2path(file_id))
 
294
 
 
295
    @needs_write_lock
 
296
    def commit(self, *args, **kw):
 
297
        from bzrlib.commit import Commit
 
298
        Commit().commit(self.branch, *args, **kw)
 
299
        self._set_inventory(self.read_working_inventory())
 
300
 
 
301
    def id2abspath(self, file_id):
 
302
        return self.abspath(self.id2path(file_id))
 
303
 
 
304
    def has_id(self, file_id):
 
305
        # files that have been deleted are excluded
 
306
        inv = self._inventory
 
307
        if not inv.has_id(file_id):
 
308
            return False
 
309
        path = inv.id2path(file_id)
 
310
        return bzrlib.osutils.lexists(self.abspath(path))
 
311
 
 
312
    def has_or_had_id(self, file_id):
 
313
        if file_id == self.inventory.root.file_id:
 
314
            return True
 
315
        return self.inventory.has_id(file_id)
 
316
 
 
317
    __contains__ = has_id
 
318
 
 
319
    def get_file_size(self, file_id):
 
320
        return os.path.getsize(self.id2abspath(file_id))
 
321
 
 
322
    @needs_read_lock
 
323
    def get_file_sha1(self, file_id):
 
324
        path = self._inventory.id2path(file_id)
 
325
        return self._hashcache.get_sha1(path)
 
326
 
 
327
    def is_executable(self, file_id):
 
328
        if os.name == "nt":
 
329
            return self._inventory[file_id].executable
 
330
        else:
 
331
            path = self._inventory.id2path(file_id)
 
332
            mode = os.lstat(self.abspath(path)).st_mode
 
333
            return bool(stat.S_ISREG(mode) and stat.S_IEXEC&mode)
 
334
 
 
335
    @needs_write_lock
 
336
    def add(self, files, ids=None):
 
337
        """Make files versioned.
 
338
 
 
339
        Note that the command line normally calls smart_add instead,
 
340
        which can automatically recurse.
 
341
 
 
342
        This adds the files to the inventory, so that they will be
 
343
        recorded by the next commit.
 
344
 
 
345
        files
 
346
            List of paths to add, relative to the base of the tree.
 
347
 
 
348
        ids
 
349
            If set, use these instead of automatically generated ids.
 
350
            Must be the same length as the list of files, but may
 
351
            contain None for ids that are to be autogenerated.
 
352
 
 
353
        TODO: Perhaps have an option to add the ids even if the files do
 
354
              not (yet) exist.
 
355
 
 
356
        TODO: Perhaps callback with the ids and paths as they're added.
 
357
        """
 
358
        # TODO: Re-adding a file that is removed in the working copy
 
359
        # should probably put it back with the previous ID.
 
360
        if isinstance(files, basestring):
 
361
            assert(ids is None or isinstance(ids, basestring))
 
362
            files = [files]
 
363
            if ids is not None:
 
364
                ids = [ids]
 
365
 
 
366
        if ids is None:
 
367
            ids = [None] * len(files)
 
368
        else:
 
369
            assert(len(ids) == len(files))
 
370
 
 
371
        inv = self.read_working_inventory()
 
372
        for f,file_id in zip(files, ids):
 
373
            if is_control_file(f):
 
374
                raise BzrError("cannot add control file %s" % quotefn(f))
 
375
 
 
376
            fp = splitpath(f)
 
377
 
 
378
            if len(fp) == 0:
 
379
                raise BzrError("cannot add top-level %r" % f)
 
380
 
 
381
            fullpath = normpath(self.abspath(f))
 
382
 
 
383
            try:
 
384
                kind = file_kind(fullpath)
 
385
            except OSError:
 
386
                # maybe something better?
 
387
                raise BzrError('cannot add: not a regular file, symlink or directory: %s' % quotefn(f))
 
388
 
 
389
            if not InventoryEntry.versionable_kind(kind):
 
390
                raise BzrError('cannot add: not a versionable file ('
 
391
                               'i.e. regular file, symlink or directory): %s' % quotefn(f))
 
392
 
 
393
            if file_id is None:
 
394
                file_id = gen_file_id(f)
 
395
            inv.add_path(f, kind=kind, file_id=file_id)
 
396
 
 
397
            mutter("add file %s file_id:{%s} kind=%r" % (f, file_id, kind))
 
398
        self._write_inventory(inv)
 
399
 
 
400
    @needs_write_lock
 
401
    def add_pending_merge(self, *revision_ids):
 
402
        # TODO: Perhaps should check at this point that the
 
403
        # history of the revision is actually present?
 
404
        p = self.pending_merges()
 
405
        updated = False
 
406
        for rev_id in revision_ids:
 
407
            if rev_id in p:
 
408
                continue
 
409
            p.append(rev_id)
 
410
            updated = True
 
411
        if updated:
 
412
            self.set_pending_merges(p)
 
413
 
 
414
    @needs_read_lock
 
415
    def pending_merges(self):
 
416
        """Return a list of pending merges.
 
417
 
 
418
        These are revisions that have been merged into the working
 
419
        directory but not yet committed.
 
420
        """
 
421
        try:
 
422
            f = self.branch.control_files.get_utf8('pending-merges')
 
423
        except NoSuchFile:
 
424
            return []
 
425
        p = []
 
426
        for l in f.readlines():
 
427
            p.append(l.rstrip('\n'))
 
428
        return p
 
429
 
 
430
    @needs_write_lock
 
431
    def set_pending_merges(self, rev_list):
 
432
        self.branch.control_files.put_utf8('pending-merges', '\n'.join(rev_list))
 
433
 
 
434
    def get_symlink_target(self, file_id):
 
435
        return os.readlink(self.id2abspath(file_id))
 
436
 
 
437
    def file_class(self, filename):
 
438
        if self.path2id(filename):
 
439
            return 'V'
 
440
        elif self.is_ignored(filename):
 
441
            return 'I'
 
442
        else:
 
443
            return '?'
 
444
 
 
445
 
 
446
    def list_files(self):
 
447
        """Recursively list all files as (path, class, kind, id).
 
448
 
 
449
        Lists, but does not descend into unversioned directories.
 
450
 
 
451
        This does not include files that have been deleted in this
 
452
        tree.
 
453
 
 
454
        Skips the control directory.
 
455
        """
 
456
        inv = self._inventory
 
457
 
 
458
        def descend(from_dir_relpath, from_dir_id, dp):
 
459
            ls = os.listdir(dp)
 
460
            ls.sort()
 
461
            for f in ls:
 
462
                ## TODO: If we find a subdirectory with its own .bzr
 
463
                ## directory, then that is a separate tree and we
 
464
                ## should exclude it.
 
465
                if bzrlib.BZRDIR == f:
 
466
                    continue
 
467
 
 
468
                # path within tree
 
469
                fp = appendpath(from_dir_relpath, f)
 
470
 
 
471
                # absolute path
 
472
                fap = appendpath(dp, f)
 
473
                
 
474
                f_ie = inv.get_child(from_dir_id, f)
 
475
                if f_ie:
 
476
                    c = 'V'
 
477
                elif self.is_ignored(fp):
 
478
                    c = 'I'
 
479
                else:
 
480
                    c = '?'
 
481
 
 
482
                fk = file_kind(fap)
 
483
 
 
484
                if f_ie:
 
485
                    if f_ie.kind != fk:
 
486
                        raise BzrCheckError("file %r entered as kind %r id %r, "
 
487
                                            "now of kind %r"
 
488
                                            % (fap, f_ie.kind, f_ie.file_id, fk))
 
489
 
 
490
                # make a last minute entry
 
491
                if f_ie:
 
492
                    entry = f_ie
 
493
                else:
 
494
                    if fk == 'directory':
 
495
                        entry = TreeDirectory()
 
496
                    elif fk == 'file':
 
497
                        entry = TreeFile()
 
498
                    elif fk == 'symlink':
 
499
                        entry = TreeLink()
 
500
                    else:
 
501
                        entry = TreeEntry()
 
502
                
 
503
                yield fp, c, fk, (f_ie and f_ie.file_id), entry
 
504
 
 
505
                if fk != 'directory':
 
506
                    continue
 
507
 
 
508
                if c != 'V':
 
509
                    # don't descend unversioned directories
 
510
                    continue
 
511
                
 
512
                for ff in descend(fp, f_ie.file_id, fap):
 
513
                    yield ff
 
514
 
 
515
        for f in descend(u'', inv.root.file_id, self.basedir):
 
516
            yield f
 
517
 
 
518
    @needs_write_lock
 
519
    def move(self, from_paths, to_name):
 
520
        """Rename files.
 
521
 
 
522
        to_name must exist in the inventory.
 
523
 
 
524
        If to_name exists and is a directory, the files are moved into
 
525
        it, keeping their old names.  
 
526
 
 
527
        Note that to_name is only the last component of the new name;
 
528
        this doesn't change the directory.
 
529
 
 
530
        This returns a list of (from_path, to_path) pairs for each
 
531
        entry that is moved.
 
532
        """
 
533
        result = []
 
534
        ## TODO: Option to move IDs only
 
535
        assert not isinstance(from_paths, basestring)
 
536
        inv = self.inventory
 
537
        to_abs = self.abspath(to_name)
 
538
        if not isdir(to_abs):
 
539
            raise BzrError("destination %r is not a directory" % to_abs)
 
540
        if not self.has_filename(to_name):
 
541
            raise BzrError("destination %r not in working directory" % to_abs)
 
542
        to_dir_id = inv.path2id(to_name)
 
543
        if to_dir_id == None and to_name != '':
 
544
            raise BzrError("destination %r is not a versioned directory" % to_name)
 
545
        to_dir_ie = inv[to_dir_id]
 
546
        if to_dir_ie.kind not in ('directory', 'root_directory'):
 
547
            raise BzrError("destination %r is not a directory" % to_abs)
 
548
 
 
549
        to_idpath = inv.get_idpath(to_dir_id)
 
550
 
 
551
        for f in from_paths:
 
552
            if not self.has_filename(f):
 
553
                raise BzrError("%r does not exist in working tree" % f)
 
554
            f_id = inv.path2id(f)
 
555
            if f_id == None:
 
556
                raise BzrError("%r is not versioned" % f)
 
557
            name_tail = splitpath(f)[-1]
 
558
            dest_path = appendpath(to_name, name_tail)
 
559
            if self.has_filename(dest_path):
 
560
                raise BzrError("destination %r already exists" % dest_path)
 
561
            if f_id in to_idpath:
 
562
                raise BzrError("can't move %r to a subdirectory of itself" % f)
 
563
 
 
564
        # OK, so there's a race here, it's possible that someone will
 
565
        # create a file in this interval and then the rename might be
 
566
        # left half-done.  But we should have caught most problems.
 
567
        orig_inv = deepcopy(self.inventory)
 
568
        try:
 
569
            for f in from_paths:
 
570
                name_tail = splitpath(f)[-1]
 
571
                dest_path = appendpath(to_name, name_tail)
 
572
                result.append((f, dest_path))
 
573
                inv.rename(inv.path2id(f), to_dir_id, name_tail)
 
574
                try:
 
575
                    rename(self.abspath(f), self.abspath(dest_path))
 
576
                except OSError, e:
 
577
                    raise BzrError("failed to rename %r to %r: %s" %
 
578
                                   (f, dest_path, e[1]),
 
579
                            ["rename rolled back"])
 
580
        except:
 
581
            # restore the inventory on error
 
582
            self._set_inventory(orig_inv)
 
583
            raise
 
584
        self._write_inventory(inv)
 
585
        return result
 
586
 
 
587
    @needs_write_lock
 
588
    def rename_one(self, from_rel, to_rel):
 
589
        """Rename one file.
 
590
 
 
591
        This can change the directory or the filename or both.
 
592
        """
 
593
        inv = self.inventory
 
594
        if not self.has_filename(from_rel):
 
595
            raise BzrError("can't rename: old working file %r does not exist" % from_rel)
 
596
        if self.has_filename(to_rel):
 
597
            raise BzrError("can't rename: new working file %r already exists" % to_rel)
 
598
 
 
599
        file_id = inv.path2id(from_rel)
 
600
        if file_id == None:
 
601
            raise BzrError("can't rename: old name %r is not versioned" % from_rel)
 
602
 
 
603
        entry = inv[file_id]
 
604
        from_parent = entry.parent_id
 
605
        from_name = entry.name
 
606
        
 
607
        if inv.path2id(to_rel):
 
608
            raise BzrError("can't rename: new name %r is already versioned" % to_rel)
 
609
 
 
610
        to_dir, to_tail = os.path.split(to_rel)
 
611
        to_dir_id = inv.path2id(to_dir)
 
612
        if to_dir_id == None and to_dir != '':
 
613
            raise BzrError("can't determine destination directory id for %r" % to_dir)
 
614
 
 
615
        mutter("rename_one:")
 
616
        mutter("  file_id    {%s}" % file_id)
 
617
        mutter("  from_rel   %r" % from_rel)
 
618
        mutter("  to_rel     %r" % to_rel)
 
619
        mutter("  to_dir     %r" % to_dir)
 
620
        mutter("  to_dir_id  {%s}" % to_dir_id)
 
621
 
 
622
        inv.rename(file_id, to_dir_id, to_tail)
 
623
 
 
624
        from_abs = self.abspath(from_rel)
 
625
        to_abs = self.abspath(to_rel)
 
626
        try:
 
627
            rename(from_abs, to_abs)
 
628
        except OSError, e:
 
629
            inv.rename(file_id, from_parent, from_name)
 
630
            raise BzrError("failed to rename %r to %r: %s"
 
631
                    % (from_abs, to_abs, e[1]),
 
632
                    ["rename rolled back"])
 
633
        self._write_inventory(inv)
 
634
 
 
635
    @needs_read_lock
 
636
    def unknowns(self):
 
637
        """Return all unknown files.
 
638
 
 
639
        These are files in the working directory that are not versioned or
 
640
        control files or ignored.
 
641
        
 
642
        >>> from bzrlib.branch import ScratchBranch
 
643
        >>> b = ScratchBranch(files=['foo', 'foo~'])
 
644
        >>> tree = WorkingTree(b.base, b)
 
645
        >>> map(str, tree.unknowns())
 
646
        ['foo']
 
647
        >>> tree.add('foo')
 
648
        >>> list(b.unknowns())
 
649
        []
 
650
        >>> tree.remove('foo')
 
651
        >>> list(b.unknowns())
 
652
        [u'foo']
 
653
        """
 
654
        for subp in self.extras():
 
655
            if not self.is_ignored(subp):
 
656
                yield subp
 
657
 
 
658
    def iter_conflicts(self):
 
659
        conflicted = set()
 
660
        for path in (s[0] for s in self.list_files()):
 
661
            stem = get_conflicted_stem(path)
 
662
            if stem is None:
 
663
                continue
 
664
            if stem not in conflicted:
 
665
                conflicted.add(stem)
 
666
                yield stem
 
667
 
 
668
    @needs_write_lock
 
669
    def pull(self, source, overwrite=False):
 
670
        from bzrlib.merge import merge_inner
 
671
        source.lock_read()
 
672
        try:
 
673
            old_revision_history = self.branch.revision_history()
 
674
            count = self.branch.pull(source, overwrite)
 
675
            new_revision_history = self.branch.revision_history()
 
676
            if new_revision_history != old_revision_history:
 
677
                if len(old_revision_history):
 
678
                    other_revision = old_revision_history[-1]
 
679
                else:
 
680
                    other_revision = None
 
681
                repository = self.branch.repository
 
682
                merge_inner(self.branch,
 
683
                            self.branch.basis_tree(), 
 
684
                            repository.revision_tree(other_revision))
 
685
            return count
 
686
        finally:
 
687
            source.unlock()
 
688
 
 
689
    def extras(self):
 
690
        """Yield all unknown files in this WorkingTree.
 
691
 
 
692
        If there are any unknown directories then only the directory is
 
693
        returned, not all its children.  But if there are unknown files
 
694
        under a versioned subdirectory, they are returned.
 
695
 
 
696
        Currently returned depth-first, sorted by name within directories.
 
697
        """
 
698
        ## TODO: Work from given directory downwards
 
699
        for path, dir_entry in self.inventory.directories():
 
700
            mutter("search for unknowns in %r", path)
 
701
            dirabs = self.abspath(path)
 
702
            if not isdir(dirabs):
 
703
                # e.g. directory deleted
 
704
                continue
 
705
 
 
706
            fl = []
 
707
            for subf in os.listdir(dirabs):
 
708
                if (subf != '.bzr'
 
709
                    and (subf not in dir_entry.children)):
 
710
                    fl.append(subf)
 
711
            
 
712
            fl.sort()
 
713
            for subf in fl:
 
714
                subp = appendpath(path, subf)
 
715
                yield subp
 
716
 
 
717
 
 
718
    def ignored_files(self):
 
719
        """Yield list of PATH, IGNORE_PATTERN"""
 
720
        for subp in self.extras():
 
721
            pat = self.is_ignored(subp)
 
722
            if pat != None:
 
723
                yield subp, pat
 
724
 
 
725
 
 
726
    def get_ignore_list(self):
 
727
        """Return list of ignore patterns.
 
728
 
 
729
        Cached in the Tree object after the first call.
 
730
        """
 
731
        if hasattr(self, '_ignorelist'):
 
732
            return self._ignorelist
 
733
 
 
734
        l = bzrlib.DEFAULT_IGNORE[:]
 
735
        if self.has_filename(bzrlib.IGNORE_FILENAME):
 
736
            f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
 
737
            l.extend([line.rstrip("\n\r") for line in f.readlines()])
 
738
        self._ignorelist = l
 
739
        return l
 
740
 
 
741
 
 
742
    def is_ignored(self, filename):
 
743
        r"""Check whether the filename matches an ignore pattern.
 
744
 
 
745
        Patterns containing '/' or '\' need to match the whole path;
 
746
        others match against only the last component.
 
747
 
 
748
        If the file is ignored, returns the pattern which caused it to
 
749
        be ignored, otherwise None.  So this can simply be used as a
 
750
        boolean if desired."""
 
751
 
 
752
        # TODO: Use '**' to match directories, and other extended
 
753
        # globbing stuff from cvs/rsync.
 
754
 
 
755
        # XXX: fnmatch is actually not quite what we want: it's only
 
756
        # approximately the same as real Unix fnmatch, and doesn't
 
757
        # treat dotfiles correctly and allows * to match /.
 
758
        # Eventually it should be replaced with something more
 
759
        # accurate.
 
760
        
 
761
        for pat in self.get_ignore_list():
 
762
            if '/' in pat or '\\' in pat:
 
763
                
 
764
                # as a special case, you can put ./ at the start of a
 
765
                # pattern; this is good to match in the top-level
 
766
                # only;
 
767
                
 
768
                if (pat[:2] == './') or (pat[:2] == '.\\'):
 
769
                    newpat = pat[2:]
 
770
                else:
 
771
                    newpat = pat
 
772
                if fnmatch.fnmatchcase(filename, newpat):
 
773
                    return pat
 
774
            else:
 
775
                if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):
 
776
                    return pat
 
777
        else:
 
778
            return None
 
779
 
 
780
    def kind(self, file_id):
 
781
        return file_kind(self.id2abspath(file_id))
 
782
 
 
783
    def lock_read(self):
 
784
        """See Branch.lock_read, and WorkingTree.unlock."""
 
785
        return self.branch.lock_read()
 
786
 
 
787
    def lock_write(self):
 
788
        """See Branch.lock_write, and WorkingTree.unlock."""
 
789
        return self.branch.lock_write()
 
790
 
 
791
    def _basis_inventory_name(self, revision_id):
 
792
        return 'basis-inventory.%s' % revision_id
 
793
 
 
794
    def set_last_revision(self, new_revision, old_revision=None):
 
795
        if old_revision is not None:
 
796
            try:
 
797
                path = self._basis_inventory_name(old_revision)
 
798
                path = self.branch.control_files._escape(path)
 
799
                self.branch.control_files._transport.delete(path)
 
800
            except NoSuchFile:
 
801
                pass
 
802
        try:
 
803
            xml = self.branch.repository.get_inventory_xml(new_revision)
 
804
            path = self._basis_inventory_name(new_revision)
 
805
            self.branch.control_files.put_utf8(path, xml)
 
806
        except WeaveRevisionNotPresent:
 
807
            pass
 
808
 
 
809
    def read_basis_inventory(self, revision_id):
 
810
        """Read the cached basis inventory."""
 
811
        path = self._basis_inventory_name(revision_id)
 
812
        return self.branch.control_files.get_utf8(path).read()
 
813
        
 
814
    @needs_read_lock
 
815
    def read_working_inventory(self):
 
816
        """Read the working inventory."""
 
817
        # ElementTree does its own conversion from UTF-8, so open in
 
818
        # binary.
 
819
        f = self.branch.control_files.get('inventory')
 
820
        return bzrlib.xml5.serializer_v5.read_inventory(f)
 
821
 
 
822
    @needs_write_lock
 
823
    def remove(self, files, verbose=False):
 
824
        """Remove nominated files from the working inventory..
 
825
 
 
826
        This does not remove their text.  This does not run on XXX on what? RBC
 
827
 
 
828
        TODO: Refuse to remove modified files unless --force is given?
 
829
 
 
830
        TODO: Do something useful with directories.
 
831
 
 
832
        TODO: Should this remove the text or not?  Tough call; not
 
833
        removing may be useful and the user can just use use rm, and
 
834
        is the opposite of add.  Removing it is consistent with most
 
835
        other tools.  Maybe an option.
 
836
        """
 
837
        ## TODO: Normalize names
 
838
        ## TODO: Remove nested loops; better scalability
 
839
        if isinstance(files, basestring):
 
840
            files = [files]
 
841
 
 
842
        inv = self.inventory
 
843
 
 
844
        # do this before any modifications
 
845
        for f in files:
 
846
            fid = inv.path2id(f)
 
847
            if not fid:
 
848
                # TODO: Perhaps make this just a warning, and continue?
 
849
                # This tends to happen when 
 
850
                raise NotVersionedError(path=f)
 
851
            mutter("remove inventory entry %s {%s}", quotefn(f), fid)
 
852
            if verbose:
 
853
                # having remove it, it must be either ignored or unknown
 
854
                if self.is_ignored(f):
 
855
                    new_status = 'I'
 
856
                else:
 
857
                    new_status = '?'
 
858
                show_status(new_status, inv[fid].kind, quotefn(f))
 
859
            del inv[fid]
 
860
 
 
861
        self._write_inventory(inv)
 
862
 
 
863
    @needs_write_lock
 
864
    def revert(self, filenames, old_tree=None, backups=True):
 
865
        from bzrlib.merge import merge_inner
 
866
        if old_tree is None:
 
867
            old_tree = self.branch.basis_tree()
 
868
        merge_inner(self.branch, old_tree,
 
869
                    self, ignore_zero=True,
 
870
                    backup_files=backups, 
 
871
                    interesting_files=filenames)
 
872
        if not len(filenames):
 
873
            self.set_pending_merges([])
 
874
 
 
875
    @needs_write_lock
 
876
    def set_inventory(self, new_inventory_list):
 
877
        from bzrlib.inventory import (Inventory,
 
878
                                      InventoryDirectory,
 
879
                                      InventoryEntry,
 
880
                                      InventoryFile,
 
881
                                      InventoryLink)
 
882
        inv = Inventory(self.get_root_id())
 
883
        for path, file_id, parent, kind in new_inventory_list:
 
884
            name = os.path.basename(path)
 
885
            if name == "":
 
886
                continue
 
887
            # fixme, there should be a factory function inv,add_?? 
 
888
            if kind == 'directory':
 
889
                inv.add(InventoryDirectory(file_id, name, parent))
 
890
            elif kind == 'file':
 
891
                inv.add(InventoryFile(file_id, name, parent))
 
892
            elif kind == 'symlink':
 
893
                inv.add(InventoryLink(file_id, name, parent))
 
894
            else:
 
895
                raise BzrError("unknown kind %r" % kind)
 
896
        self._write_inventory(inv)
 
897
 
 
898
    @needs_write_lock
 
899
    def set_root_id(self, file_id):
 
900
        """Set the root id for this tree."""
 
901
        inv = self.read_working_inventory()
 
902
        orig_root_id = inv.root.file_id
 
903
        del inv._byid[inv.root.file_id]
 
904
        inv.root.file_id = file_id
 
905
        inv._byid[inv.root.file_id] = inv.root
 
906
        for fid in inv:
 
907
            entry = inv[fid]
 
908
            if entry.parent_id in (None, orig_root_id):
 
909
                entry.parent_id = inv.root.file_id
 
910
        self._write_inventory(inv)
 
911
 
 
912
    def unlock(self):
 
913
        """See Branch.unlock.
 
914
        
 
915
        WorkingTree locking just uses the Branch locking facilities.
 
916
        This is current because all working trees have an embedded branch
 
917
        within them. IF in the future, we were to make branch data shareable
 
918
        between multiple working trees, i.e. via shared storage, then we 
 
919
        would probably want to lock both the local tree, and the branch.
 
920
        """
 
921
        # FIXME: We want to write out the hashcache only when the last lock on
 
922
        # this working copy is released.  Peeking at the lock count is a bit
 
923
        # of a nasty hack; probably it's better to have a transaction object,
 
924
        # which can do some finalization when it's either successfully or
 
925
        # unsuccessfully completed.  (Denys's original patch did that.)
 
926
        if self._hashcache.needs_write and self.branch.control_files._lock_count==1:
 
927
            self._hashcache.write()
 
928
        return self.branch.unlock()
 
929
 
 
930
    @needs_write_lock
 
931
    def _write_inventory(self, inv):
 
932
        """Write inventory as the current inventory."""
 
933
        from cStringIO import StringIO
 
934
        from bzrlib.atomicfile import AtomicFile
 
935
        sio = StringIO()
 
936
        bzrlib.xml5.serializer_v5.write_inventory(inv, sio)
 
937
        sio.seek(0)
 
938
        f = AtomicFile(self.branch.control_files.controlfilename('inventory'))
 
939
        try:
 
940
            pumpfile(sio, f)
 
941
            f.commit()
 
942
        finally:
 
943
            f.close()
 
944
        self._set_inventory(inv)
 
945
        mutter('wrote working inventory')
 
946
            
 
947
 
 
948
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
 
949
def get_conflicted_stem(path):
 
950
    for suffix in CONFLICT_SUFFIXES:
 
951
        if path.endswith(suffix):
 
952
            return path[:-len(suffix)]