~bzr-pqm/bzr/bzr.dev

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