/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

Merge from integration, mode-changes are broken.

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