~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree.py

  • Committer: Robert Collins
  • Date: 2005-11-13 20:14:22 UTC
  • mfrom: (1185.16.159)
  • Revision ID: robertc@robertcollins.net-20051113201422-8a34ef413bfc8222
Stores with some compressed texts and some uncompressed texts are now able to
be used. (John A Meinel)

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.
 
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.
17
44
 
18
45
import os
19
 
    
 
46
import stat
 
47
import fnmatch
 
48
 
 
49
from bzrlib.branch import Branch, needs_read_lock, needs_write_lock, quotefn
20
50
import bzrlib.tree
21
 
from errors import BzrCheckError
22
 
from trace import mutter
23
 
import statcache
 
51
from bzrlib.osutils import (appendpath,
 
52
                            file_kind,
 
53
                            isdir,
 
54
                            pumpfile,
 
55
                            splitpath,
 
56
                            relpath)
 
57
from bzrlib.errors import BzrCheckError, DivergedBranches, NotVersionedError
 
58
from bzrlib.trace import mutter
 
59
import bzrlib.xml5
 
60
 
 
61
 
 
62
class TreeEntry(object):
 
63
    """An entry that implements the minium interface used by commands.
 
64
 
 
65
    This needs further inspection, it may be better to have 
 
66
    InventoryEntries without ids - though that seems wrong. For now,
 
67
    this is a parallel hierarchy to InventoryEntry, and needs to become
 
68
    one of several things: decorates to that hierarchy, children of, or
 
69
    parents of it.
 
70
    Another note is that these objects are currently only used when there is
 
71
    no InventoryEntry available - i.e. for unversioned objects.
 
72
    Perhaps they should be UnversionedEntry et al. ? - RBC 20051003
 
73
    """
 
74
 
 
75
    def __eq__(self, other):
 
76
        # yes, this us ugly, TODO: best practice __eq__ style.
 
77
        return (isinstance(other, TreeEntry)
 
78
                and other.__class__ == self.__class__)
 
79
 
 
80
    def kind_character(self):
 
81
        return "???"
 
82
 
 
83
 
 
84
class TreeDirectory(TreeEntry):
 
85
    """See TreeEntry. This is a directory in a working tree."""
 
86
 
 
87
    def __eq__(self, other):
 
88
        return (isinstance(other, TreeDirectory)
 
89
                and other.__class__ == self.__class__)
 
90
 
 
91
    def kind_character(self):
 
92
        return "/"
 
93
 
 
94
 
 
95
class TreeFile(TreeEntry):
 
96
    """See TreeEntry. This is a regular file in a working tree."""
 
97
 
 
98
    def __eq__(self, other):
 
99
        return (isinstance(other, TreeFile)
 
100
                and other.__class__ == self.__class__)
 
101
 
 
102
    def kind_character(self):
 
103
        return ''
 
104
 
 
105
 
 
106
class TreeLink(TreeEntry):
 
107
    """See TreeEntry. This is a symlink in a working tree."""
 
108
 
 
109
    def __eq__(self, other):
 
110
        return (isinstance(other, TreeLink)
 
111
                and other.__class__ == self.__class__)
 
112
 
 
113
    def kind_character(self):
 
114
        return ''
 
115
 
24
116
 
25
117
class WorkingTree(bzrlib.tree.Tree):
26
118
    """Working copy tree.
31
123
    It is possible for a `WorkingTree` to have a filename which is
32
124
    not listed in the Inventory and vice versa.
33
125
    """
34
 
    _statcache = None
35
 
    
36
 
    def __init__(self, basedir, inv):
37
 
        self._inventory = inv
 
126
 
 
127
    def __init__(self, basedir, branch=None):
 
128
        """Construct a WorkingTree for basedir.
 
129
 
 
130
        If the branch is not supplied, it is opened automatically.
 
131
        If the branch is supplied, it must be the branch for this basedir.
 
132
        (branch.base is not cross checked, because for remote branches that
 
133
        would be meaningless).
 
134
        """
 
135
        from bzrlib.hashcache import HashCache
 
136
        from bzrlib.trace import note, mutter
 
137
        assert isinstance(basedir, basestring), \
 
138
            "base directory %r is not a string" % basedir
 
139
        if branch is None:
 
140
            branch = Branch.open(basedir)
 
141
        assert isinstance(branch, Branch), \
 
142
            "branch %r is not a Branch" % branch
 
143
        self.branch = branch
38
144
        self.basedir = basedir
39
 
        self.path2id = inv.path2id
40
 
        self._update_statcache()
 
145
        self._inventory = self.read_working_inventory()
 
146
        self.path2id = self._inventory.path2id
 
147
 
 
148
        # update the whole cache up front and write to disk if anything changed;
 
149
        # in the future we might want to do this more selectively
 
150
        # two possible ways offer themselves : in self._unlock, write the cache
 
151
        # if needed, or, when the cache sees a change, append it to the hash
 
152
        # cache file, and have the parser take the most recent entry for a
 
153
        # given path only.
 
154
        hc = self._hashcache = HashCache(basedir)
 
155
        hc.read()
 
156
        hc.scan()
 
157
 
 
158
        if hc.needs_write:
 
159
            mutter("write hc")
 
160
            hc.write()
41
161
 
42
162
    def __iter__(self):
43
163
        """Iterate through file_ids for this tree.
46
166
        and the working file exists.
47
167
        """
48
168
        inv = self._inventory
49
 
        for file_id in self._inventory:
50
 
            # TODO: This is slightly redundant; we should be able to just
51
 
            # check the statcache but it only includes regular files.
52
 
            # only include files which still exist on disk
53
 
            ie = inv[file_id]
54
 
            if ie.kind == 'file':
55
 
                if ((file_id in self._statcache)
56
 
                    or (os.path.exists(self.abspath(inv.id2path(file_id))))):
57
 
                    yield file_id
58
 
 
 
169
        for path, ie in inv.iter_entries():
 
170
            if bzrlib.osutils.lexists(self.abspath(path)):
 
171
                yield ie.file_id
59
172
 
60
173
 
61
174
    def __repr__(self):
62
175
        return "<%s of %s>" % (self.__class__.__name__,
63
 
                               self.basedir)
 
176
                               getattr(self, 'basedir', None))
 
177
 
 
178
 
64
179
 
65
180
    def abspath(self, filename):
66
181
        return os.path.join(self.basedir, filename)
67
182
 
 
183
    def relpath(self, abspath):
 
184
        """Return the local path portion from a given absolute path."""
 
185
        return relpath(self.basedir, abspath)
 
186
 
68
187
    def has_filename(self, filename):
69
 
        return os.path.exists(self.abspath(filename))
 
188
        return bzrlib.osutils.lexists(self.abspath(filename))
70
189
 
71
190
    def get_file(self, file_id):
72
191
        return self.get_file_byname(self.id2path(file_id))
74
193
    def get_file_byname(self, filename):
75
194
        return file(self.abspath(filename), 'rb')
76
195
 
 
196
    def get_root_id(self):
 
197
        """Return the id of this trees root"""
 
198
        inv = self.read_working_inventory()
 
199
        return inv.root.file_id
 
200
        
77
201
    def _get_store_filename(self, file_id):
78
202
        ## XXX: badly named; this isn't in the store at all
79
203
        return self.abspath(self.id2path(file_id))
80
204
 
 
205
    @needs_write_lock
 
206
    def commit(self, *args, **kw):
 
207
        from bzrlib.commit import Commit
 
208
        Commit().commit(self.branch, *args, **kw)
 
209
        self._inventory = self.read_working_inventory()
 
210
 
 
211
    def id2abspath(self, file_id):
 
212
        return self.abspath(self.id2path(file_id))
 
213
 
81
214
                
82
215
    def has_id(self, file_id):
83
216
        # files that have been deleted are excluded
84
 
        if not self.inventory.has_id(file_id):
 
217
        inv = self._inventory
 
218
        if not inv.has_id(file_id):
85
219
            return False
86
 
        if file_id in self._statcache:
 
220
        path = inv.id2path(file_id)
 
221
        return bzrlib.osutils.lexists(self.abspath(path))
 
222
 
 
223
    def has_or_had_id(self, file_id):
 
224
        if file_id == self.inventory.root.file_id:
87
225
            return True
88
 
        return os.path.exists(self.abspath(self.id2path(file_id)))
89
 
 
 
226
        return self.inventory.has_id(file_id)
90
227
 
91
228
    __contains__ = has_id
92
229
    
93
230
 
94
 
    def _update_statcache(self):
95
 
        import statcache
96
 
        if not self._statcache:
97
 
            self._statcache = statcache.update_cache(self.basedir, self.inventory)
98
 
 
99
231
    def get_file_size(self, file_id):
100
 
        import os, stat
101
 
        return os.stat(self._get_store_filename(file_id))[stat.ST_SIZE]
102
 
 
 
232
        return os.path.getsize(self.id2abspath(file_id))
103
233
 
104
234
    def get_file_sha1(self, file_id):
105
 
        return self._statcache[file_id][statcache.SC_SHA1]
106
 
 
 
235
        path = self._inventory.id2path(file_id)
 
236
        return self._hashcache.get_sha1(path)
 
237
 
 
238
 
 
239
    def is_executable(self, file_id):
 
240
        if os.name == "nt":
 
241
            return self._inventory[file_id].executable
 
242
        else:
 
243
            path = self._inventory.id2path(file_id)
 
244
            mode = os.lstat(self.abspath(path)).st_mode
 
245
            return bool(stat.S_ISREG(mode) and stat.S_IEXEC&mode)
 
246
 
 
247
    @needs_write_lock
 
248
    def add_pending_merge(self, *revision_ids):
 
249
        # TODO: Perhaps should check at this point that the
 
250
        # history of the revision is actually present?
 
251
        p = self.pending_merges()
 
252
        updated = False
 
253
        for rev_id in revision_ids:
 
254
            if rev_id in p:
 
255
                continue
 
256
            p.append(rev_id)
 
257
            updated = True
 
258
        if updated:
 
259
            self.set_pending_merges(p)
 
260
 
 
261
    def pending_merges(self):
 
262
        """Return a list of pending merges.
 
263
 
 
264
        These are revisions that have been merged into the working
 
265
        directory but not yet committed.
 
266
        """
 
267
        cfn = self.branch._rel_controlfilename('pending-merges')
 
268
        if not self.branch._transport.has(cfn):
 
269
            return []
 
270
        p = []
 
271
        for l in self.branch.controlfile('pending-merges', 'r').readlines():
 
272
            p.append(l.rstrip('\n'))
 
273
        return p
 
274
 
 
275
    @needs_write_lock
 
276
    def set_pending_merges(self, rev_list):
 
277
        self.branch.put_controlfile('pending-merges', '\n'.join(rev_list))
 
278
 
 
279
    def get_symlink_target(self, file_id):
 
280
        return os.readlink(self.id2abspath(file_id))
107
281
 
108
282
    def file_class(self, filename):
109
283
        if self.path2id(filename):
124
298
 
125
299
        Skips the control directory.
126
300
        """
127
 
        from osutils import appendpath, file_kind
128
 
        import os
129
 
 
130
 
        inv = self.inventory
 
301
        inv = self._inventory
131
302
 
132
303
        def descend(from_dir_relpath, from_dir_id, dp):
133
304
            ls = os.listdir(dp)
161
332
                                            "now of kind %r"
162
333
                                            % (fap, f_ie.kind, f_ie.file_id, fk))
163
334
 
164
 
                yield fp, c, fk, (f_ie and f_ie.file_id)
 
335
                # make a last minute entry
 
336
                if f_ie:
 
337
                    entry = f_ie
 
338
                else:
 
339
                    if fk == 'directory':
 
340
                        entry = TreeDirectory()
 
341
                    elif fk == 'file':
 
342
                        entry = TreeFile()
 
343
                    elif fk == 'symlink':
 
344
                        entry = TreeLink()
 
345
                    else:
 
346
                        entry = TreeEntry()
 
347
                
 
348
                yield fp, c, fk, (f_ie and f_ie.file_id), entry
165
349
 
166
350
                if fk != 'directory':
167
351
                    continue
183
367
            if not self.is_ignored(subp):
184
368
                yield subp
185
369
 
 
370
    def iter_conflicts(self):
 
371
        conflicted = set()
 
372
        for path in (s[0] for s in self.list_files()):
 
373
            stem = get_conflicted_stem(path)
 
374
            if stem is None:
 
375
                continue
 
376
            if stem not in conflicted:
 
377
                conflicted.add(stem)
 
378
                yield stem
 
379
 
 
380
    @needs_write_lock
 
381
    def pull(self, source, overwrite=False):
 
382
        from bzrlib.merge import merge_inner
 
383
        source.lock_read()
 
384
        try:
 
385
            old_revision_history = self.branch.revision_history()
 
386
            self.branch.pull(source, overwrite)
 
387
            new_revision_history = self.branch.revision_history()
 
388
            if new_revision_history != old_revision_history:
 
389
                if len(old_revision_history):
 
390
                    other_revision = old_revision_history[-1]
 
391
                else:
 
392
                    other_revision = None
 
393
                merge_inner(self.branch,
 
394
                            self.branch.basis_tree(), 
 
395
                            self.branch.revision_tree(other_revision))
 
396
        finally:
 
397
            source.unlock()
186
398
 
187
399
    def extras(self):
188
400
        """Yield all unknown files in this WorkingTree.
194
406
        Currently returned depth-first, sorted by name within directories.
195
407
        """
196
408
        ## TODO: Work from given directory downwards
197
 
        from osutils import isdir, appendpath
198
 
        
199
409
        for path, dir_entry in self.inventory.directories():
200
 
            mutter("search for unknowns in %r" % path)
 
410
            mutter("search for unknowns in %r", path)
201
411
            dirabs = self.abspath(path)
202
412
            if not isdir(dirabs):
203
413
                # e.g. directory deleted
258
468
        # Eventually it should be replaced with something more
259
469
        # accurate.
260
470
        
261
 
        import fnmatch
262
 
        from osutils import splitpath
263
 
        
264
471
        for pat in self.get_ignore_list():
265
472
            if '/' in pat or '\\' in pat:
266
473
                
279
486
                    return pat
280
487
        else:
281
488
            return None
282
 
        
283
 
 
284
 
        
285
 
        
286
 
 
 
489
 
 
490
    def kind(self, file_id):
 
491
        return file_kind(self.id2abspath(file_id))
 
492
 
 
493
    def lock_read(self):
 
494
        """See Branch.lock_read, and WorkingTree.unlock."""
 
495
        return self.branch.lock_read()
 
496
 
 
497
    def lock_write(self):
 
498
        """See Branch.lock_write, and WorkingTree.unlock."""
 
499
        return self.branch.lock_write()
 
500
 
 
501
    @needs_read_lock
 
502
    def read_working_inventory(self):
 
503
        """Read the working inventory."""
 
504
        # ElementTree does its own conversion from UTF-8, so open in
 
505
        # binary.
 
506
        f = self.branch.controlfile('inventory', 'rb')
 
507
        return bzrlib.xml5.serializer_v5.read_inventory(f)
 
508
 
 
509
    @needs_write_lock
 
510
    def remove(self, files, verbose=False):
 
511
        """Remove nominated files from the working inventory..
 
512
 
 
513
        This does not remove their text.  This does not run on XXX on what? RBC
 
514
 
 
515
        TODO: Refuse to remove modified files unless --force is given?
 
516
 
 
517
        TODO: Do something useful with directories.
 
518
 
 
519
        TODO: Should this remove the text or not?  Tough call; not
 
520
        removing may be useful and the user can just use use rm, and
 
521
        is the opposite of add.  Removing it is consistent with most
 
522
        other tools.  Maybe an option.
 
523
        """
 
524
        ## TODO: Normalize names
 
525
        ## TODO: Remove nested loops; better scalability
 
526
        if isinstance(files, basestring):
 
527
            files = [files]
 
528
 
 
529
        inv = self.inventory
 
530
 
 
531
        # do this before any modifications
 
532
        for f in files:
 
533
            fid = inv.path2id(f)
 
534
            if not fid:
 
535
                # TODO: Perhaps make this just a warning, and continue?
 
536
                # This tends to happen when 
 
537
                raise NotVersionedError(path=f)
 
538
            mutter("remove inventory entry %s {%s}", quotefn(f), fid)
 
539
            if verbose:
 
540
                # having remove it, it must be either ignored or unknown
 
541
                if self.is_ignored(f):
 
542
                    new_status = 'I'
 
543
                else:
 
544
                    new_status = '?'
 
545
                show_status(new_status, inv[fid].kind, quotefn(f))
 
546
            del inv[fid]
 
547
 
 
548
        self._write_inventory(inv)
 
549
 
 
550
    @needs_write_lock
 
551
    def revert(self, filenames, old_tree=None, backups=True):
 
552
        from bzrlib.merge import merge_inner
 
553
        if old_tree is None:
 
554
            old_tree = self.branch.basis_tree()
 
555
        merge_inner(self.branch, old_tree,
 
556
                    self, ignore_zero=True,
 
557
                    backup_files=backups, 
 
558
                    interesting_files=filenames)
 
559
        if not len(filenames):
 
560
            self.set_pending_merges([])
 
561
 
 
562
    @needs_write_lock
 
563
    def set_inventory(self, new_inventory_list):
 
564
        from bzrlib.inventory import (Inventory,
 
565
                                      InventoryDirectory,
 
566
                                      InventoryEntry,
 
567
                                      InventoryFile,
 
568
                                      InventoryLink)
 
569
        inv = Inventory(self.get_root_id())
 
570
        for path, file_id, parent, kind in new_inventory_list:
 
571
            name = os.path.basename(path)
 
572
            if name == "":
 
573
                continue
 
574
            # fixme, there should be a factory function inv,add_?? 
 
575
            if kind == 'directory':
 
576
                inv.add(InventoryDirectory(file_id, name, parent))
 
577
            elif kind == 'file':
 
578
                inv.add(InventoryFile(file_id, name, parent))
 
579
            elif kind == 'symlink':
 
580
                inv.add(InventoryLink(file_id, name, parent))
 
581
            else:
 
582
                raise BzrError("unknown kind %r" % kind)
 
583
        self._write_inventory(inv)
 
584
 
 
585
    @needs_write_lock
 
586
    def set_root_id(self, file_id):
 
587
        """Set the root id for this tree."""
 
588
        inv = self.read_working_inventory()
 
589
        orig_root_id = inv.root.file_id
 
590
        del inv._byid[inv.root.file_id]
 
591
        inv.root.file_id = file_id
 
592
        inv._byid[inv.root.file_id] = inv.root
 
593
        for fid in inv:
 
594
            entry = inv[fid]
 
595
            if entry.parent_id in (None, orig_root_id):
 
596
                entry.parent_id = inv.root.file_id
 
597
        self._write_inventory(inv)
 
598
 
 
599
    def unlock(self):
 
600
        """See Branch.unlock.
 
601
        
 
602
        WorkingTree locking just uses the Branch locking facilities.
 
603
        This is current because all working trees have an embedded branch
 
604
        within them. IF in the future, we were to make branch data shareable
 
605
        between multiple working trees, i.e. via shared storage, then we 
 
606
        would probably want to lock both the local tree, and the branch.
 
607
        """
 
608
        return self.branch.unlock()
 
609
 
 
610
    @needs_write_lock
 
611
    def _write_inventory(self, inv):
 
612
        """Write inventory as the current inventory."""
 
613
        from cStringIO import StringIO
 
614
        from bzrlib.atomicfile import AtomicFile
 
615
        sio = StringIO()
 
616
        bzrlib.xml5.serializer_v5.write_inventory(inv, sio)
 
617
        sio.seek(0)
 
618
        f = AtomicFile(self.branch.controlfilename('inventory'))
 
619
        try:
 
620
            pumpfile(sio, f)
 
621
            f.commit()
 
622
        finally:
 
623
            f.close()
 
624
        mutter('wrote working inventory')
 
625
            
 
626
 
 
627
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
 
628
def get_conflicted_stem(path):
 
629
    for suffix in CONFLICT_SUFFIXES:
 
630
        if path.endswith(suffix):
 
631
            return path[:-len(suffix)]