~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree.py

  • Committer: Martin Pool
  • Date: 2005-04-28 07:24:55 UTC
  • Revision ID: mbp@sourcefrog.net-20050428072453-7b99afa993a1e549
todo

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
 
import os
46
 
import stat
47
 
import fnmatch
48
 
 
49
 
from bzrlib.branch import Branch, needs_read_lock, needs_write_lock, quotefn
50
 
import bzrlib.tree
51
 
from bzrlib.osutils import appendpath, file_kind, isdir, splitpath, relpath
52
 
from bzrlib.errors import BzrCheckError, DivergedBranches, NotVersionedError
53
 
from bzrlib.trace import mutter
54
 
import bzrlib.xml5
55
 
 
56
 
 
57
 
class TreeEntry(object):
58
 
    """An entry that implements the minium interface used by commands.
59
 
 
60
 
    This needs further inspection, it may be better to have 
61
 
    InventoryEntries without ids - though that seems wrong. For now,
62
 
    this is a parallel hierarchy to InventoryEntry, and needs to become
63
 
    one of several things: decorates to that hierarchy, children of, or
64
 
    parents of it.
65
 
    Another note is that these objects are currently only used when there is
66
 
    no InventoryEntry available - i.e. for unversioned objects.
67
 
    Perhaps they should be UnversionedEntry et al. ? - RBC 20051003
68
 
    """
69
 
 
70
 
    def __eq__(self, other):
71
 
        # yes, this us ugly, TODO: best practice __eq__ style.
72
 
        return (isinstance(other, TreeEntry)
73
 
                and other.__class__ == self.__class__)
74
 
 
75
 
    def kind_character(self):
76
 
        return "???"
77
 
 
78
 
 
79
 
class TreeDirectory(TreeEntry):
80
 
    """See TreeEntry. This is a directory in a working tree."""
81
 
 
82
 
    def __eq__(self, other):
83
 
        return (isinstance(other, TreeDirectory)
84
 
                and other.__class__ == self.__class__)
85
 
 
86
 
    def kind_character(self):
87
 
        return "/"
88
 
 
89
 
 
90
 
class TreeFile(TreeEntry):
91
 
    """See TreeEntry. This is a regular file in a working tree."""
92
 
 
93
 
    def __eq__(self, other):
94
 
        return (isinstance(other, TreeFile)
95
 
                and other.__class__ == self.__class__)
96
 
 
97
 
    def kind_character(self):
98
 
        return ''
99
 
 
100
 
 
101
 
class TreeLink(TreeEntry):
102
 
    """See TreeEntry. This is a symlink in a working tree."""
103
 
 
104
 
    def __eq__(self, other):
105
 
        return (isinstance(other, TreeLink)
106
 
                and other.__class__ == self.__class__)
107
 
 
108
 
    def kind_character(self):
109
 
        return ''
110
 
 
111
 
 
112
 
class WorkingTree(bzrlib.tree.Tree):
113
 
    """Working copy tree.
114
 
 
115
 
    The inventory is held in the `Branch` working-inventory, and the
116
 
    files are in a directory on disk.
117
 
 
118
 
    It is possible for a `WorkingTree` to have a filename which is
119
 
    not listed in the Inventory and vice versa.
120
 
    """
121
 
 
122
 
    def __init__(self, basedir, branch=None):
123
 
        """Construct a WorkingTree for basedir.
124
 
 
125
 
        If the branch is not supplied, it is opened automatically.
126
 
        If the branch is supplied, it must be the branch for this basedir.
127
 
        (branch.base is not cross checked, because for remote branches that
128
 
        would be meaningless).
129
 
        """
130
 
        from bzrlib.hashcache import HashCache
131
 
        from bzrlib.trace import note, mutter
132
 
        assert isinstance(basedir, basestring), \
133
 
            "base directory %r is not a string" % basedir
134
 
        if branch is None:
135
 
            branch = Branch.open(basedir)
136
 
        assert isinstance(branch, Branch), \
137
 
            "branch %r is not a Branch" % branch
138
 
        self.branch = branch
139
 
        self.basedir = basedir
140
 
        self._inventory = self.read_working_inventory()
141
 
        self.path2id = self._inventory.path2id
142
 
 
143
 
        # update the whole cache up front and write to disk if anything changed;
144
 
        # in the future we might want to do this more selectively
145
 
        # two possible ways offer themselves : in self._unlock, write the cache
146
 
        # if needed, or, when the cache sees a change, append it to the hash
147
 
        # cache file, and have the parser take the most recent entry for a
148
 
        # given path only.
149
 
        hc = self._hashcache = HashCache(basedir)
150
 
        hc.read()
151
 
        hc.scan()
152
 
 
153
 
        if hc.needs_write:
154
 
            mutter("write hc")
155
 
            hc.write()
156
 
 
157
 
    def __iter__(self):
158
 
        """Iterate through file_ids for this tree.
159
 
 
160
 
        file_ids are in a WorkingTree if they are in the working inventory
161
 
        and the working file exists.
162
 
        """
163
 
        inv = self._inventory
164
 
        for path, ie in inv.iter_entries():
165
 
            if bzrlib.osutils.lexists(self.abspath(path)):
166
 
                yield ie.file_id
167
 
 
168
 
 
169
 
    def __repr__(self):
170
 
        return "<%s of %s>" % (self.__class__.__name__,
171
 
                               getattr(self, 'basedir', None))
172
 
 
173
 
 
174
 
 
175
 
    def abspath(self, filename):
176
 
        return os.path.join(self.basedir, filename)
177
 
 
178
 
    def relpath(self, abspath):
179
 
        """Return the local path portion from a given absolute path."""
180
 
        return relpath(self.basedir, abspath)
181
 
 
182
 
    def has_filename(self, filename):
183
 
        return bzrlib.osutils.lexists(self.abspath(filename))
184
 
 
185
 
    def get_file(self, file_id):
186
 
        return self.get_file_byname(self.id2path(file_id))
187
 
 
188
 
    def get_file_byname(self, filename):
189
 
        return file(self.abspath(filename), 'rb')
190
 
 
191
 
    def get_root_id(self):
192
 
        """Return the id of this trees root"""
193
 
        inv = self.read_working_inventory()
194
 
        return inv.root.file_id
195
 
        
196
 
    def _get_store_filename(self, file_id):
197
 
        ## XXX: badly named; this isn't in the store at all
198
 
        return self.abspath(self.id2path(file_id))
199
 
 
200
 
 
201
 
    def id2abspath(self, file_id):
202
 
        return self.abspath(self.id2path(file_id))
203
 
 
204
 
                
205
 
    def has_id(self, file_id):
206
 
        # files that have been deleted are excluded
207
 
        inv = self._inventory
208
 
        if not inv.has_id(file_id):
209
 
            return False
210
 
        path = inv.id2path(file_id)
211
 
        return bzrlib.osutils.lexists(self.abspath(path))
212
 
 
213
 
    def has_or_had_id(self, file_id):
214
 
        if file_id == self.inventory.root.file_id:
215
 
            return True
216
 
        return self.inventory.has_id(file_id)
217
 
 
218
 
    __contains__ = has_id
219
 
    
220
 
 
221
 
    def get_file_size(self, file_id):
222
 
        return os.path.getsize(self.id2abspath(file_id))
223
 
 
224
 
    def get_file_sha1(self, file_id):
225
 
        path = self._inventory.id2path(file_id)
226
 
        return self._hashcache.get_sha1(path)
227
 
 
228
 
 
229
 
    def is_executable(self, file_id):
230
 
        if os.name == "nt":
231
 
            return self._inventory[file_id].executable
232
 
        else:
233
 
            path = self._inventory.id2path(file_id)
234
 
            mode = os.lstat(self.abspath(path)).st_mode
235
 
            return bool(stat.S_ISREG(mode) and stat.S_IEXEC&mode)
236
 
 
237
 
    def get_symlink_target(self, file_id):
238
 
        return os.readlink(self.id2abspath(file_id))
239
 
 
240
 
    def file_class(self, filename):
241
 
        if self.path2id(filename):
242
 
            return 'V'
243
 
        elif self.is_ignored(filename):
244
 
            return 'I'
245
 
        else:
246
 
            return '?'
247
 
 
248
 
 
249
 
    def list_files(self):
250
 
        """Recursively list all files as (path, class, kind, id).
251
 
 
252
 
        Lists, but does not descend into unversioned directories.
253
 
 
254
 
        This does not include files that have been deleted in this
255
 
        tree.
256
 
 
257
 
        Skips the control directory.
258
 
        """
259
 
        inv = self._inventory
260
 
 
261
 
        def descend(from_dir_relpath, from_dir_id, dp):
262
 
            ls = os.listdir(dp)
263
 
            ls.sort()
264
 
            for f in ls:
265
 
                ## TODO: If we find a subdirectory with its own .bzr
266
 
                ## directory, then that is a separate tree and we
267
 
                ## should exclude it.
268
 
                if bzrlib.BZRDIR == f:
269
 
                    continue
270
 
 
271
 
                # path within tree
272
 
                fp = appendpath(from_dir_relpath, f)
273
 
 
274
 
                # absolute path
275
 
                fap = appendpath(dp, f)
276
 
                
277
 
                f_ie = inv.get_child(from_dir_id, f)
278
 
                if f_ie:
279
 
                    c = 'V'
280
 
                elif self.is_ignored(fp):
281
 
                    c = 'I'
282
 
                else:
283
 
                    c = '?'
284
 
 
285
 
                fk = file_kind(fap)
286
 
 
287
 
                if f_ie:
288
 
                    if f_ie.kind != fk:
289
 
                        raise BzrCheckError("file %r entered as kind %r id %r, "
290
 
                                            "now of kind %r"
291
 
                                            % (fap, f_ie.kind, f_ie.file_id, fk))
292
 
 
293
 
                # make a last minute entry
294
 
                if f_ie:
295
 
                    entry = f_ie
296
 
                else:
297
 
                    if fk == 'directory':
298
 
                        entry = TreeDirectory()
299
 
                    elif fk == 'file':
300
 
                        entry = TreeFile()
301
 
                    elif fk == 'symlink':
302
 
                        entry = TreeLink()
303
 
                    else:
304
 
                        entry = TreeEntry()
305
 
                
306
 
                yield fp, c, fk, (f_ie and f_ie.file_id), entry
307
 
 
308
 
                if fk != 'directory':
309
 
                    continue
310
 
 
311
 
                if c != 'V':
312
 
                    # don't descend unversioned directories
313
 
                    continue
314
 
                
315
 
                for ff in descend(fp, f_ie.file_id, fap):
316
 
                    yield ff
317
 
 
318
 
        for f in descend('', inv.root.file_id, self.basedir):
319
 
            yield f
320
 
            
321
 
 
322
 
 
323
 
    def unknowns(self):
324
 
        for subp in self.extras():
325
 
            if not self.is_ignored(subp):
326
 
                yield subp
327
 
 
328
 
    def iter_conflicts(self):
329
 
        conflicted = set()
330
 
        for path in (s[0] for s in self.list_files()):
331
 
            stem = get_conflicted_stem(path)
332
 
            if stem is None:
333
 
                continue
334
 
            if stem not in conflicted:
335
 
                conflicted.add(stem)
336
 
                yield stem
337
 
 
338
 
    @needs_write_lock
339
 
    def pull(self, source, overwrite=False):
340
 
        from bzrlib.merge import merge_inner
341
 
        source.lock_read()
342
 
        try:
343
 
            old_revision_history = self.branch.revision_history()
344
 
            self.branch.pull(source, overwrite)
345
 
            new_revision_history = self.branch.revision_history()
346
 
            if new_revision_history != old_revision_history:
347
 
                if len(old_revision_history):
348
 
                    other_revision = old_revision_history[-1]
349
 
                else:
350
 
                    other_revision = None
351
 
                merge_inner(self.branch,
352
 
                            self.branch.basis_tree(), 
353
 
                            self.branch.revision_tree(other_revision))
354
 
        finally:
355
 
            source.unlock()
356
 
 
357
 
    def extras(self):
358
 
        """Yield all unknown files in this WorkingTree.
359
 
 
360
 
        If there are any unknown directories then only the directory is
361
 
        returned, not all its children.  But if there are unknown files
362
 
        under a versioned subdirectory, they are returned.
363
 
 
364
 
        Currently returned depth-first, sorted by name within directories.
365
 
        """
366
 
        ## TODO: Work from given directory downwards
367
 
        for path, dir_entry in self.inventory.directories():
368
 
            mutter("search for unknowns in %r" % path)
369
 
            dirabs = self.abspath(path)
370
 
            if not isdir(dirabs):
371
 
                # e.g. directory deleted
372
 
                continue
373
 
 
374
 
            fl = []
375
 
            for subf in os.listdir(dirabs):
376
 
                if (subf != '.bzr'
377
 
                    and (subf not in dir_entry.children)):
378
 
                    fl.append(subf)
379
 
            
380
 
            fl.sort()
381
 
            for subf in fl:
382
 
                subp = appendpath(path, subf)
383
 
                yield subp
384
 
 
385
 
 
386
 
    def ignored_files(self):
387
 
        """Yield list of PATH, IGNORE_PATTERN"""
388
 
        for subp in self.extras():
389
 
            pat = self.is_ignored(subp)
390
 
            if pat != None:
391
 
                yield subp, pat
392
 
 
393
 
 
394
 
    def get_ignore_list(self):
395
 
        """Return list of ignore patterns.
396
 
 
397
 
        Cached in the Tree object after the first call.
398
 
        """
399
 
        if hasattr(self, '_ignorelist'):
400
 
            return self._ignorelist
401
 
 
402
 
        l = bzrlib.DEFAULT_IGNORE[:]
403
 
        if self.has_filename(bzrlib.IGNORE_FILENAME):
404
 
            f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
405
 
            l.extend([line.rstrip("\n\r") for line in f.readlines()])
406
 
        self._ignorelist = l
407
 
        return l
408
 
 
409
 
 
410
 
    def is_ignored(self, filename):
411
 
        r"""Check whether the filename matches an ignore pattern.
412
 
 
413
 
        Patterns containing '/' or '\' need to match the whole path;
414
 
        others match against only the last component.
415
 
 
416
 
        If the file is ignored, returns the pattern which caused it to
417
 
        be ignored, otherwise None.  So this can simply be used as a
418
 
        boolean if desired."""
419
 
 
420
 
        # TODO: Use '**' to match directories, and other extended
421
 
        # globbing stuff from cvs/rsync.
422
 
 
423
 
        # XXX: fnmatch is actually not quite what we want: it's only
424
 
        # approximately the same as real Unix fnmatch, and doesn't
425
 
        # treat dotfiles correctly and allows * to match /.
426
 
        # Eventually it should be replaced with something more
427
 
        # accurate.
428
 
        
429
 
        for pat in self.get_ignore_list():
430
 
            if '/' in pat or '\\' in pat:
431
 
                
432
 
                # as a special case, you can put ./ at the start of a
433
 
                # pattern; this is good to match in the top-level
434
 
                # only;
435
 
                
436
 
                if (pat[:2] == './') or (pat[:2] == '.\\'):
437
 
                    newpat = pat[2:]
438
 
                else:
439
 
                    newpat = pat
440
 
                if fnmatch.fnmatchcase(filename, newpat):
441
 
                    return pat
442
 
            else:
443
 
                if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):
444
 
                    return pat
445
 
        else:
446
 
            return None
447
 
 
448
 
    def kind(self, file_id):
449
 
        return file_kind(self.id2abspath(file_id))
450
 
 
451
 
    def lock_read(self):
452
 
        """See Branch.lock_read, and WorkingTree.unlock."""
453
 
        return self.branch.lock_read()
454
 
 
455
 
    def lock_write(self):
456
 
        """See Branch.lock_write, and WorkingTree.unlock."""
457
 
        return self.branch.lock_write()
458
 
 
459
 
    @needs_read_lock
460
 
    def read_working_inventory(self):
461
 
        """Read the working inventory."""
462
 
        # ElementTree does its own conversion from UTF-8, so open in
463
 
        # binary.
464
 
        f = self.branch.controlfile('inventory', 'rb')
465
 
        return bzrlib.xml5.serializer_v5.read_inventory(f)
466
 
 
467
 
    @needs_write_lock
468
 
    def remove(self, files, verbose=False):
469
 
        """Remove nominated files from the working inventory..
470
 
 
471
 
        This does not remove their text.  This does not run on XXX on what? RBC
472
 
 
473
 
        TODO: Refuse to remove modified files unless --force is given?
474
 
 
475
 
        TODO: Do something useful with directories.
476
 
 
477
 
        TODO: Should this remove the text or not?  Tough call; not
478
 
        removing may be useful and the user can just use use rm, and
479
 
        is the opposite of add.  Removing it is consistent with most
480
 
        other tools.  Maybe an option.
481
 
        """
482
 
        ## TODO: Normalize names
483
 
        ## TODO: Remove nested loops; better scalability
484
 
        if isinstance(files, basestring):
485
 
            files = [files]
486
 
 
487
 
        inv = self.inventory
488
 
 
489
 
        # do this before any modifications
490
 
        for f in files:
491
 
            fid = inv.path2id(f)
492
 
            if not fid:
493
 
                # TODO: Perhaps make this just a warning, and continue?
494
 
                # This tends to happen when 
495
 
                raise NotVersionedError(path=f)
496
 
            mutter("remove inventory entry %s {%s}" % (quotefn(f), fid))
497
 
            if verbose:
498
 
                # having remove it, it must be either ignored or unknown
499
 
                if self.is_ignored(f):
500
 
                    new_status = 'I'
501
 
                else:
502
 
                    new_status = '?'
503
 
                show_status(new_status, inv[fid].kind, quotefn(f))
504
 
            del inv[fid]
505
 
 
506
 
        self.branch._write_inventory(inv)
507
 
 
508
 
    @needs_write_lock
509
 
    def set_inventory(self, new_inventory_list):
510
 
        from bzrlib.inventory import (Inventory,
511
 
                                      InventoryDirectory,
512
 
                                      InventoryEntry,
513
 
                                      InventoryFile,
514
 
                                      InventoryLink)
515
 
        inv = Inventory(self.get_root_id())
516
 
        for path, file_id, parent, kind in new_inventory_list:
517
 
            name = os.path.basename(path)
518
 
            if name == "":
519
 
                continue
520
 
            # fixme, there should be a factory function inv,add_?? 
521
 
            if kind == 'directory':
522
 
                inv.add(InventoryDirectory(file_id, name, parent))
523
 
            elif kind == 'file':
524
 
                inv.add(InventoryFile(file_id, name, parent))
525
 
            elif kind == 'symlink':
526
 
                inv.add(InventoryLink(file_id, name, parent))
527
 
            else:
528
 
                raise BzrError("unknown kind %r" % kind)
529
 
        self.branch._write_inventory(inv)
530
 
 
531
 
    def unlock(self):
532
 
        """See Branch.unlock.
533
 
        
534
 
        WorkingTree locking just uses the Branch locking facilities.
535
 
        This is current because all working trees have an embedded branch
536
 
        within them. IF in the future, we were to make branch data shareable
537
 
        between multiple working trees, i.e. via shared storage, then we 
538
 
        would probably want to lock both the local tree, and the branch.
539
 
        """
540
 
        return self.branch.unlock()
541
 
 
542
 
 
543
 
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
544
 
def get_conflicted_stem(path):
545
 
    for suffix in CONFLICT_SUFFIXES:
546
 
        if path.endswith(suffix):
547
 
            return path[:-len(suffix)]