~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree.py

  • Committer: Martin Pool
  • Date: 2005-05-03 07:48:35 UTC
  • Revision ID: mbp@sourcefrog.net-20050503074835-0133bd0c16440fcd
doc

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