~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree.py

  • Committer: Robert Collins
  • Date: 2005-10-16 23:53:02 UTC
  • mto: This revision was merged to the branch mainline in revision 1459.
  • Revision ID: robertc@lifelesslap.robertcollins.net-20051016235302-818de607403e1c6e
test that the presence of a signature does not make a missing base file magically appear present

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
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.
 
17
# TODO: Don't allow WorkingTrees to be constructed for remote branches.
34
18
 
35
19
# 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.
 
20
# good idea, because destructors are considered poor taste in Python, and
 
21
# it's not predictable when it will be written out.
44
22
 
45
23
import os
46
24
import stat
47
25
import fnmatch
48
 
 
49
 
from bzrlib.branch import Branch, needs_read_lock, needs_write_lock, quotefn
 
26
        
50
27
import bzrlib.tree
51
 
from bzrlib.osutils import appendpath, file_kind, isdir, splitpath, relpath
52
 
from bzrlib.errors import BzrCheckError, DivergedBranches, NotVersionedError
 
28
from bzrlib.osutils import appendpath, file_kind, isdir, splitpath
 
29
from bzrlib.errors import BzrCheckError
53
30
from bzrlib.trace import mutter
54
31
 
55
 
 
56
32
class TreeEntry(object):
57
33
    """An entry that implements the minium interface used by commands.
58
34
 
117
93
    It is possible for a `WorkingTree` to have a filename which is
118
94
    not listed in the Inventory and vice versa.
119
95
    """
120
 
 
121
 
    def __init__(self, basedir, branch=None):
122
 
        """Construct a WorkingTree for basedir.
123
 
 
124
 
        If the branch is not supplied, it is opened automatically.
125
 
        If the branch is supplied, it must be the branch for this basedir.
126
 
        (branch.base is not cross checked, because for remote branches that
127
 
        would be meaningless).
128
 
        """
 
96
    def __init__(self, basedir, inv):
129
97
        from bzrlib.hashcache import HashCache
130
98
        from bzrlib.trace import note, mutter
131
 
        assert isinstance(basedir, basestring), \
132
 
            "base directory %r is not a string" % basedir
133
 
        if branch is None:
134
 
            branch = Branch.open(basedir)
135
 
        assert isinstance(branch, Branch), \
136
 
            "branch %r is not a Branch" % branch
137
 
        self._inventory = branch.inventory
138
 
        self.path2id = self._inventory.path2id
139
 
        self.branch = branch
 
99
 
 
100
        self._inventory = inv
140
101
        self.basedir = basedir
 
102
        self.path2id = inv.path2id
141
103
 
142
104
        # update the whole cache up front and write to disk if anything changed;
143
105
        # in the future we might want to do this more selectively
144
 
        # two possible ways offer themselves : in self._unlock, write the cache
145
 
        # if needed, or, when the cache sees a change, append it to the hash
146
 
        # cache file, and have the parser take the most recent entry for a
147
 
        # given path only.
148
106
        hc = self._hashcache = HashCache(basedir)
149
107
        hc.read()
150
108
        hc.scan()
152
110
        if hc.needs_write:
153
111
            mutter("write hc")
154
112
            hc.write()
 
113
            
 
114
            
 
115
    def __del__(self):
 
116
        if self._hashcache.needs_write:
 
117
            self._hashcache.write()
 
118
 
155
119
 
156
120
    def __iter__(self):
157
121
        """Iterate through file_ids for this tree.
174
138
    def abspath(self, filename):
175
139
        return os.path.join(self.basedir, filename)
176
140
 
177
 
    def relpath(self, abspath):
178
 
        """Return the local path portion from a given absolute path."""
179
 
        return relpath(self.basedir, abspath)
180
 
 
181
141
    def has_filename(self, filename):
182
142
        return bzrlib.osutils.lexists(self.abspath(filename))
183
143
 
204
164
        path = inv.id2path(file_id)
205
165
        return bzrlib.osutils.lexists(self.abspath(path))
206
166
 
207
 
    def has_or_had_id(self, file_id):
208
 
        if file_id == self.inventory.root.file_id:
209
 
            return True
210
 
        return self.inventory.has_id(file_id)
211
167
 
212
168
    __contains__ = has_id
213
169
    
329
285
                conflicted.add(stem)
330
286
                yield stem
331
287
 
332
 
    @needs_write_lock
333
 
    def pull(self, source, remember=False, clobber=False):
334
 
        from bzrlib.merge import merge_inner
335
 
        source.lock_read()
336
 
        try:
337
 
            old_revision_history = self.branch.revision_history()
338
 
            try:
339
 
                self.branch.update_revisions(source)
340
 
            except DivergedBranches:
341
 
                if not clobber:
342
 
                    raise
343
 
                self.branch.set_revision_history(source.revision_history())
344
 
            new_revision_history = self.branch.revision_history()
345
 
            if new_revision_history != old_revision_history:
346
 
                if len(old_revision_history):
347
 
                    other_revision = old_revision_history[-1]
348
 
                else:
349
 
                    other_revision = None
350
 
                merge_inner(self.branch,
351
 
                            self.branch.basis_tree(), 
352
 
                            self.branch.revision_tree(other_revision))
353
 
            if self.branch.get_parent() is None or remember:
354
 
                self.branch.set_parent(source.base)
355
 
        finally:
356
 
            source.unlock()
357
 
 
358
288
    def extras(self):
359
289
        """Yield all unknown files in this WorkingTree.
360
290
 
446
376
        else:
447
377
            return None
448
378
 
449
 
    def kind(self, file_id):
450
 
        return file_kind(self.id2abspath(file_id))
451
 
 
452
 
    def lock_read(self):
453
 
        """See Branch.lock_read, and WorkingTree.unlock."""
454
 
        return self.branch.lock_read()
455
 
 
456
 
    def lock_write(self):
457
 
        """See Branch.lock_write, and WorkingTree.unlock."""
458
 
        return self.branch.lock_write()
459
 
 
460
 
    @needs_write_lock
461
 
    def remove(self, files, verbose=False):
462
 
        """Remove nominated files from the working inventory..
463
 
 
464
 
        This does not remove their text.  This does not run on XXX on what? RBC
465
 
 
466
 
        TODO: Refuse to remove modified files unless --force is given?
467
 
 
468
 
        TODO: Do something useful with directories.
469
 
 
470
 
        TODO: Should this remove the text or not?  Tough call; not
471
 
        removing may be useful and the user can just use use rm, and
472
 
        is the opposite of add.  Removing it is consistent with most
473
 
        other tools.  Maybe an option.
474
 
        """
475
 
        ## TODO: Normalize names
476
 
        ## TODO: Remove nested loops; better scalability
477
 
        if isinstance(files, basestring):
478
 
            files = [files]
479
 
 
480
 
        inv = self.inventory
481
 
 
482
 
        # do this before any modifications
483
 
        for f in files:
484
 
            fid = inv.path2id(f)
485
 
            if not fid:
486
 
                # TODO: Perhaps make this just a warning, and continue?
487
 
                # This tends to happen when 
488
 
                raise NotVersionedError(path=f)
489
 
            mutter("remove inventory entry %s {%s}" % (quotefn(f), fid))
490
 
            if verbose:
491
 
                # having remove it, it must be either ignored or unknown
492
 
                if self.is_ignored(f):
493
 
                    new_status = 'I'
494
 
                else:
495
 
                    new_status = '?'
496
 
                show_status(new_status, inv[fid].kind, quotefn(f))
497
 
            del inv[fid]
498
 
 
499
 
        self.branch._write_inventory(inv)
500
 
 
501
 
    def unlock(self):
502
 
        """See Branch.unlock.
503
 
        
504
 
        WorkingTree locking just uses the Branch locking facilities.
505
 
        This is current because all working trees have an embedded branch
506
 
        within them. IF in the future, we were to make branch data shareable
507
 
        between multiple working trees, i.e. via shared storage, then we 
508
 
        would probably want to lock both the local tree, and the branch.
509
 
        """
510
 
        return self.branch.unlock()
511
 
 
512
 
 
513
379
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
514
380
def get_conflicted_stem(path):
515
381
    for suffix in CONFLICT_SUFFIXES: