~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree.py

  • Committer: Robert Collins
  • Date: 2005-08-24 06:53:07 UTC
  • mto: (974.1.50) (1185.1.10) (1092.3.1)
  • mto: This revision was merged to the branch mainline in revision 1139.
  • Revision ID: robertc@robertcollins.net-20050824065307-bca8ae89734a53f8
merge from mpool

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
 
import stat
47
 
import fnmatch
48
 
 
49
 
from bzrlib.branch import Branch, needs_read_lock, needs_write_lock, quotefn
 
24
    
50
25
import bzrlib.tree
51
 
from bzrlib.osutils import appendpath, file_kind, isdir, splitpath, relpath
52
 
from bzrlib.errors import BzrCheckError, NotVersionedError
53
 
from bzrlib.trace import mutter
54
 
 
55
 
class TreeEntry(object):
56
 
    """An entry that implements the minium interface used by commands.
57
 
 
58
 
    This needs further inspection, it may be better to have 
59
 
    InventoryEntries without ids - though that seems wrong. For now,
60
 
    this is a parallel hierarchy to InventoryEntry, and needs to become
61
 
    one of several things: decorates to that hierarchy, children of, or
62
 
    parents of it.
63
 
    Another note is that these objects are currently only used when there is
64
 
    no InventoryEntry available - i.e. for unversioned objects.
65
 
    Perhaps they should be UnversionedEntry et al. ? - RBC 20051003
66
 
    """
67
 
 
68
 
    def __eq__(self, other):
69
 
        # yes, this us ugly, TODO: best practice __eq__ style.
70
 
        return (isinstance(other, TreeEntry)
71
 
                and other.__class__ == self.__class__)
72
 
 
73
 
    def kind_character(self):
74
 
        return "???"
75
 
 
76
 
 
77
 
class TreeDirectory(TreeEntry):
78
 
    """See TreeEntry. This is a directory in a working tree."""
79
 
 
80
 
    def __eq__(self, other):
81
 
        return (isinstance(other, TreeDirectory)
82
 
                and other.__class__ == self.__class__)
83
 
 
84
 
    def kind_character(self):
85
 
        return "/"
86
 
 
87
 
 
88
 
class TreeFile(TreeEntry):
89
 
    """See TreeEntry. This is a regular file in a working tree."""
90
 
 
91
 
    def __eq__(self, other):
92
 
        return (isinstance(other, TreeFile)
93
 
                and other.__class__ == self.__class__)
94
 
 
95
 
    def kind_character(self):
96
 
        return ''
97
 
 
98
 
 
99
 
class TreeLink(TreeEntry):
100
 
    """See TreeEntry. This is a symlink in a working tree."""
101
 
 
102
 
    def __eq__(self, other):
103
 
        return (isinstance(other, TreeLink)
104
 
                and other.__class__ == self.__class__)
105
 
 
106
 
    def kind_character(self):
107
 
        return ''
108
 
 
 
26
from errors import BzrCheckError
 
27
from trace import mutter
109
28
 
110
29
class WorkingTree(bzrlib.tree.Tree):
111
30
    """Working copy tree.
116
35
    It is possible for a `WorkingTree` to have a filename which is
117
36
    not listed in the Inventory and vice versa.
118
37
    """
119
 
 
120
 
    def __init__(self, basedir, branch=None):
121
 
        """Construct a WorkingTree for basedir.
122
 
 
123
 
        If the branch is not supplied, it is opened automatically.
124
 
        If the branch is supplied, it must be the branch for this basedir.
125
 
        (branch.base is not cross checked, because for remote branches that
126
 
        would be meaningless).
127
 
        """
 
38
    def __init__(self, basedir, inv):
128
39
        from bzrlib.hashcache import HashCache
129
40
        from bzrlib.trace import note, mutter
130
 
        assert isinstance(basedir, basestring), \
131
 
            "base directory %r is not a string" % basedir
132
 
        if branch is None:
133
 
            branch = Branch.open(basedir)
134
 
        assert isinstance(branch, Branch), \
135
 
            "branch %r is not a Branch" % branch
136
 
        self._inventory = branch.inventory
137
 
        self.path2id = self._inventory.path2id
138
 
        self.branch = branch
 
41
 
 
42
        self._inventory = inv
139
43
        self.basedir = basedir
 
44
        self.path2id = inv.path2id
140
45
 
141
46
        # update the whole cache up front and write to disk if anything changed;
142
47
        # in the future we might want to do this more selectively
162
67
        """
163
68
        inv = self._inventory
164
69
        for path, ie in inv.iter_entries():
165
 
            if bzrlib.osutils.lexists(self.abspath(path)):
 
70
            if os.path.exists(self.abspath(path)):
166
71
                yield ie.file_id
167
72
 
168
73
 
175
80
    def abspath(self, filename):
176
81
        return os.path.join(self.basedir, filename)
177
82
 
178
 
    def relpath(self, abspath):
179
 
        """Return the local path portion from a given absolute path."""
180
 
        return relpath(self.basedir, abspath)
181
 
 
182
83
    def has_filename(self, filename):
183
 
        return bzrlib.osutils.lexists(self.abspath(filename))
 
84
        return os.path.exists(self.abspath(filename))
184
85
 
185
86
    def get_file(self, file_id):
186
87
        return self.get_file_byname(self.id2path(file_id))
192
93
        ## XXX: badly named; this isn't in the store at all
193
94
        return self.abspath(self.id2path(file_id))
194
95
 
195
 
 
196
 
    def id2abspath(self, file_id):
197
 
        return self.abspath(self.id2path(file_id))
198
 
 
199
96
                
200
97
    def has_id(self, file_id):
201
98
        # files that have been deleted are excluded
203
100
        if not inv.has_id(file_id):
204
101
            return False
205
102
        path = inv.id2path(file_id)
206
 
        return bzrlib.osutils.lexists(self.abspath(path))
 
103
        return os.path.exists(self.abspath(path))
207
104
 
208
 
    def has_or_had_id(self, file_id):
209
 
        if file_id == self.inventory.root.file_id:
210
 
            return True
211
 
        return self.inventory.has_id(file_id)
212
105
 
213
106
    __contains__ = has_id
214
107
    
215
108
 
216
109
    def get_file_size(self, file_id):
217
 
        return os.path.getsize(self.id2abspath(file_id))
 
110
        # is this still called?
 
111
        raise NotImplementedError()
 
112
 
218
113
 
219
114
    def get_file_sha1(self, file_id):
220
115
        path = self._inventory.id2path(file_id)
221
116
        return self._hashcache.get_sha1(path)
222
117
 
223
118
 
224
 
    def is_executable(self, file_id):
225
 
        if os.name == "nt":
226
 
            return self._inventory[file_id].executable
227
 
        else:
228
 
            path = self._inventory.id2path(file_id)
229
 
            mode = os.lstat(self.abspath(path)).st_mode
230
 
            return bool(stat.S_ISREG(mode) and stat.S_IEXEC&mode)
231
 
 
232
 
    def get_symlink_target(self, file_id):
233
 
        return os.readlink(self.id2abspath(file_id))
234
 
 
235
119
    def file_class(self, filename):
236
120
        if self.path2id(filename):
237
121
            return 'V'
251
135
 
252
136
        Skips the control directory.
253
137
        """
 
138
        from osutils import appendpath, file_kind
 
139
        import os
 
140
 
254
141
        inv = self._inventory
255
142
 
256
143
        def descend(from_dir_relpath, from_dir_id, dp):
285
172
                                            "now of kind %r"
286
173
                                            % (fap, f_ie.kind, f_ie.file_id, fk))
287
174
 
288
 
                # make a last minute entry
289
 
                if f_ie:
290
 
                    entry = f_ie
291
 
                else:
292
 
                    if fk == 'directory':
293
 
                        entry = TreeDirectory()
294
 
                    elif fk == 'file':
295
 
                        entry = TreeFile()
296
 
                    elif fk == 'symlink':
297
 
                        entry = TreeLink()
298
 
                    else:
299
 
                        entry = TreeEntry()
300
 
                
301
 
                yield fp, c, fk, (f_ie and f_ie.file_id), entry
 
175
                yield fp, c, fk, (f_ie and f_ie.file_id)
302
176
 
303
177
                if fk != 'directory':
304
178
                    continue
320
194
            if not self.is_ignored(subp):
321
195
                yield subp
322
196
 
323
 
    def iter_conflicts(self):
324
 
        conflicted = set()
325
 
        for path in (s[0] for s in self.list_files()):
326
 
            stem = get_conflicted_stem(path)
327
 
            if stem is None:
328
 
                continue
329
 
            if stem not in conflicted:
330
 
                conflicted.add(stem)
331
 
                yield stem
332
197
 
333
198
    def extras(self):
334
199
        """Yield all unknown files in this WorkingTree.
340
205
        Currently returned depth-first, sorted by name within directories.
341
206
        """
342
207
        ## TODO: Work from given directory downwards
 
208
        from osutils import isdir, appendpath
 
209
        
343
210
        for path, dir_entry in self.inventory.directories():
344
211
            mutter("search for unknowns in %r" % path)
345
212
            dirabs = self.abspath(path)
402
269
        # Eventually it should be replaced with something more
403
270
        # accurate.
404
271
        
 
272
        import fnmatch
 
273
        from osutils import splitpath
 
274
        
405
275
        for pat in self.get_ignore_list():
406
276
            if '/' in pat or '\\' in pat:
407
277
                
420
290
                    return pat
421
291
        else:
422
292
            return None
423
 
 
424
 
    def kind(self, file_id):
425
 
        return file_kind(self.id2abspath(file_id))
426
 
 
427
 
    def lock_read(self):
428
 
        """See Branch.lock_read, and WorkingTree.unlock."""
429
 
        return self.branch.lock_read()
430
 
 
431
 
    def lock_write(self):
432
 
        """See Branch.lock_write, and WorkingTree.unlock."""
433
 
        return self.branch.lock_write()
434
 
 
435
 
    @needs_write_lock
436
 
    def remove(self, files, verbose=False):
437
 
        """Remove nominated files from the working inventory..
438
 
 
439
 
        This does not remove their text.  This does not run on XXX on what? RBC
440
 
 
441
 
        TODO: Refuse to remove modified files unless --force is given?
442
 
 
443
 
        TODO: Do something useful with directories.
444
 
 
445
 
        TODO: Should this remove the text or not?  Tough call; not
446
 
        removing may be useful and the user can just use use rm, and
447
 
        is the opposite of add.  Removing it is consistent with most
448
 
        other tools.  Maybe an option.
449
 
        """
450
 
        ## TODO: Normalize names
451
 
        ## TODO: Remove nested loops; better scalability
452
 
        if isinstance(files, basestring):
453
 
            files = [files]
454
 
 
455
 
        inv = self.inventory
456
 
 
457
 
        # do this before any modifications
458
 
        for f in files:
459
 
            fid = inv.path2id(f)
460
 
            if not fid:
461
 
                # TODO: Perhaps make this just a warning, and continue?
462
 
                # This tends to happen when 
463
 
                raise NotVersionedError(path=f)
464
 
            mutter("remove inventory entry %s {%s}" % (quotefn(f), fid))
465
 
            if verbose:
466
 
                # having remove it, it must be either ignored or unknown
467
 
                if self.is_ignored(f):
468
 
                    new_status = 'I'
469
 
                else:
470
 
                    new_status = '?'
471
 
                show_status(new_status, inv[fid].kind, quotefn(f))
472
 
            del inv[fid]
473
 
 
474
 
        self.branch._write_inventory(inv)
475
 
 
476
 
    def unlock(self):
477
 
        """See Branch.unlock.
478
 
        
479
 
        WorkingTree locking just uses the Branch locking facilities.
480
 
        This is current because all working trees have an embedded branch
481
 
        within them. IF in the future, we were to make branch data shareable
482
 
        between multiple working trees, i.e. via shared storage, then we 
483
 
        would probably want to lock both the local tree, and the branch.
484
 
        """
485
 
        return self.branch.unlock()
486
 
 
487
 
 
488
 
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
489
 
def get_conflicted_stem(path):
490
 
    for suffix in CONFLICT_SUFFIXES:
491
 
        if path.endswith(suffix):
492
 
            return path[:-len(suffix)]
 
293
        
 
 
b'\\ No newline at end of file'