~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree.py

  • Committer: Martin Pool
  • Date: 2005-08-29 06:56:22 UTC
  • Revision ID: mbp@sourcefrog.net-20050829065622-5aa7add87c38f188
- additional trace messages for plugins

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
# TODO: Don't allow WorkingTrees to be constructed for remote branches.
 
18
 
 
19
# FIXME: I don't know if writing out the cache from the destructor is really a
 
20
# good idea, because destructors are considered poor taste in Python, and
 
21
# it's not predictable when it will be written out.
 
22
 
 
23
import os
 
24
    
 
25
import bzrlib.tree
 
26
from errors import BzrCheckError
 
27
from trace import mutter
 
28
 
 
29
class WorkingTree(bzrlib.tree.Tree):
 
30
    """Working copy tree.
 
31
 
 
32
    The inventory is held in the `Branch` working-inventory, and the
 
33
    files are in a directory on disk.
 
34
 
 
35
    It is possible for a `WorkingTree` to have a filename which is
 
36
    not listed in the Inventory and vice versa.
 
37
    """
 
38
    def __init__(self, basedir, inv):
 
39
        from bzrlib.hashcache import HashCache
 
40
        from bzrlib.trace import note, mutter
 
41
 
 
42
        self._inventory = inv
 
43
        self.basedir = basedir
 
44
        self.path2id = inv.path2id
 
45
 
 
46
        # update the whole cache up front and write to disk if anything changed;
 
47
        # in the future we might want to do this more selectively
 
48
        hc = self._hashcache = HashCache(basedir)
 
49
        hc.read()
 
50
        hc.scan()
 
51
 
 
52
        if hc.needs_write:
 
53
            mutter("write hc")
 
54
            hc.write()
 
55
            
 
56
            
 
57
    def __del__(self):
 
58
        if self._hashcache.needs_write:
 
59
            self._hashcache.write()
 
60
 
 
61
 
 
62
    def __iter__(self):
 
63
        """Iterate through file_ids for this tree.
 
64
 
 
65
        file_ids are in a WorkingTree if they are in the working inventory
 
66
        and the working file exists.
 
67
        """
 
68
        inv = self._inventory
 
69
        for path, ie in inv.iter_entries():
 
70
            if os.path.exists(self.abspath(path)):
 
71
                yield ie.file_id
 
72
 
 
73
 
 
74
    def __repr__(self):
 
75
        return "<%s of %s>" % (self.__class__.__name__,
 
76
                               getattr(self, 'basedir', None))
 
77
 
 
78
 
 
79
 
 
80
    def abspath(self, filename):
 
81
        return os.path.join(self.basedir, filename)
 
82
 
 
83
    def has_filename(self, filename):
 
84
        return os.path.exists(self.abspath(filename))
 
85
 
 
86
    def get_file(self, file_id):
 
87
        return self.get_file_byname(self.id2path(file_id))
 
88
 
 
89
    def get_file_byname(self, filename):
 
90
        return file(self.abspath(filename), 'rb')
 
91
 
 
92
    def _get_store_filename(self, file_id):
 
93
        ## XXX: badly named; this isn't in the store at all
 
94
        return self.abspath(self.id2path(file_id))
 
95
 
 
96
                
 
97
    def has_id(self, file_id):
 
98
        # files that have been deleted are excluded
 
99
        inv = self._inventory
 
100
        if not inv.has_id(file_id):
 
101
            return False
 
102
        path = inv.id2path(file_id)
 
103
        return os.path.exists(self.abspath(path))
 
104
 
 
105
 
 
106
    __contains__ = has_id
 
107
    
 
108
 
 
109
    def get_file_size(self, file_id):
 
110
        # is this still called?
 
111
        raise NotImplementedError()
 
112
 
 
113
 
 
114
    def get_file_sha1(self, file_id):
 
115
        path = self._inventory.id2path(file_id)
 
116
        return self._hashcache.get_sha1(path)
 
117
 
 
118
 
 
119
    def file_class(self, filename):
 
120
        if self.path2id(filename):
 
121
            return 'V'
 
122
        elif self.is_ignored(filename):
 
123
            return 'I'
 
124
        else:
 
125
            return '?'
 
126
 
 
127
 
 
128
    def list_files(self):
 
129
        """Recursively list all files as (path, class, kind, id).
 
130
 
 
131
        Lists, but does not descend into unversioned directories.
 
132
 
 
133
        This does not include files that have been deleted in this
 
134
        tree.
 
135
 
 
136
        Skips the control directory.
 
137
        """
 
138
        from osutils import appendpath, file_kind
 
139
        import os
 
140
 
 
141
        inv = self._inventory
 
142
 
 
143
        def descend(from_dir_relpath, from_dir_id, dp):
 
144
            ls = os.listdir(dp)
 
145
            ls.sort()
 
146
            for f in ls:
 
147
                ## TODO: If we find a subdirectory with its own .bzr
 
148
                ## directory, then that is a separate tree and we
 
149
                ## should exclude it.
 
150
                if bzrlib.BZRDIR == f:
 
151
                    continue
 
152
 
 
153
                # path within tree
 
154
                fp = appendpath(from_dir_relpath, f)
 
155
 
 
156
                # absolute path
 
157
                fap = appendpath(dp, f)
 
158
                
 
159
                f_ie = inv.get_child(from_dir_id, f)
 
160
                if f_ie:
 
161
                    c = 'V'
 
162
                elif self.is_ignored(fp):
 
163
                    c = 'I'
 
164
                else:
 
165
                    c = '?'
 
166
 
 
167
                fk = file_kind(fap)
 
168
 
 
169
                if f_ie:
 
170
                    if f_ie.kind != fk:
 
171
                        raise BzrCheckError("file %r entered as kind %r id %r, "
 
172
                                            "now of kind %r"
 
173
                                            % (fap, f_ie.kind, f_ie.file_id, fk))
 
174
 
 
175
                yield fp, c, fk, (f_ie and f_ie.file_id)
 
176
 
 
177
                if fk != 'directory':
 
178
                    continue
 
179
 
 
180
                if c != 'V':
 
181
                    # don't descend unversioned directories
 
182
                    continue
 
183
                
 
184
                for ff in descend(fp, f_ie.file_id, fap):
 
185
                    yield ff
 
186
 
 
187
        for f in descend('', inv.root.file_id, self.basedir):
 
188
            yield f
 
189
            
 
190
 
 
191
 
 
192
    def unknowns(self):
 
193
        for subp in self.extras():
 
194
            if not self.is_ignored(subp):
 
195
                yield subp
 
196
 
 
197
 
 
198
    def extras(self):
 
199
        """Yield all unknown files in this WorkingTree.
 
200
 
 
201
        If there are any unknown directories then only the directory is
 
202
        returned, not all its children.  But if there are unknown files
 
203
        under a versioned subdirectory, they are returned.
 
204
 
 
205
        Currently returned depth-first, sorted by name within directories.
 
206
        """
 
207
        ## TODO: Work from given directory downwards
 
208
        from osutils import isdir, appendpath
 
209
        
 
210
        for path, dir_entry in self.inventory.directories():
 
211
            mutter("search for unknowns in %r" % path)
 
212
            dirabs = self.abspath(path)
 
213
            if not isdir(dirabs):
 
214
                # e.g. directory deleted
 
215
                continue
 
216
 
 
217
            fl = []
 
218
            for subf in os.listdir(dirabs):
 
219
                if (subf != '.bzr'
 
220
                    and (subf not in dir_entry.children)):
 
221
                    fl.append(subf)
 
222
            
 
223
            fl.sort()
 
224
            for subf in fl:
 
225
                subp = appendpath(path, subf)
 
226
                yield subp
 
227
 
 
228
 
 
229
    def ignored_files(self):
 
230
        """Yield list of PATH, IGNORE_PATTERN"""
 
231
        for subp in self.extras():
 
232
            pat = self.is_ignored(subp)
 
233
            if pat != None:
 
234
                yield subp, pat
 
235
 
 
236
 
 
237
    def get_ignore_list(self):
 
238
        """Return list of ignore patterns.
 
239
 
 
240
        Cached in the Tree object after the first call.
 
241
        """
 
242
        if hasattr(self, '_ignorelist'):
 
243
            return self._ignorelist
 
244
 
 
245
        l = bzrlib.DEFAULT_IGNORE[:]
 
246
        if self.has_filename(bzrlib.IGNORE_FILENAME):
 
247
            f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
 
248
            l.extend([line.rstrip("\n\r") for line in f.readlines()])
 
249
        self._ignorelist = l
 
250
        return l
 
251
 
 
252
 
 
253
    def is_ignored(self, filename):
 
254
        r"""Check whether the filename matches an ignore pattern.
 
255
 
 
256
        Patterns containing '/' or '\' need to match the whole path;
 
257
        others match against only the last component.
 
258
 
 
259
        If the file is ignored, returns the pattern which caused it to
 
260
        be ignored, otherwise None.  So this can simply be used as a
 
261
        boolean if desired."""
 
262
 
 
263
        # TODO: Use '**' to match directories, and other extended
 
264
        # globbing stuff from cvs/rsync.
 
265
 
 
266
        # XXX: fnmatch is actually not quite what we want: it's only
 
267
        # approximately the same as real Unix fnmatch, and doesn't
 
268
        # treat dotfiles correctly and allows * to match /.
 
269
        # Eventually it should be replaced with something more
 
270
        # accurate.
 
271
        
 
272
        import fnmatch
 
273
        from osutils import splitpath
 
274
        
 
275
        for pat in self.get_ignore_list():
 
276
            if '/' in pat or '\\' in pat:
 
277
                
 
278
                # as a special case, you can put ./ at the start of a
 
279
                # pattern; this is good to match in the top-level
 
280
                # only;
 
281
                
 
282
                if (pat[:2] == './') or (pat[:2] == '.\\'):
 
283
                    newpat = pat[2:]
 
284
                else:
 
285
                    newpat = pat
 
286
                if fnmatch.fnmatchcase(filename, newpat):
 
287
                    return pat
 
288
            else:
 
289
                if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):
 
290
                    return pat
 
291
        else:
 
292
            return None
 
293
        
 
 
b'\\ No newline at end of file'