~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree.py

  • Committer: Martin Pool
  • Date: 2005-05-11 06:07:43 UTC
  • Revision ID: mbp@sourcefrog.net-20050511060743-82c5a87967ad4537
- remove diff_trees from public api

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