~bzr-pqm/bzr/bzr.dev

453 by Martin Pool
- Split WorkingTree into its own file
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
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
17
# TODO: Don't allow WorkingTrees to be constructed for remote branches.
453 by Martin Pool
- Split WorkingTree into its own file
18
956 by Martin Pool
doc
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
453 by Martin Pool
- Split WorkingTree into its own file
23
import os
1140 by Martin Pool
- lift out import statements within WorkingTree
24
import fnmatch
25
        
453 by Martin Pool
- Split WorkingTree into its own file
26
import bzrlib.tree
1140 by Martin Pool
- lift out import statements within WorkingTree
27
from bzrlib.osutils import appendpath, file_kind, isdir, splitpath
28
from bzrlib.errors import BzrCheckError
29
from bzrlib.trace import mutter
453 by Martin Pool
- Split WorkingTree into its own file
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):
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
41
        from bzrlib.hashcache import HashCache
42
        from bzrlib.trace import note, mutter
43
453 by Martin Pool
- Split WorkingTree into its own file
44
        self._inventory = inv
45
        self.basedir = basedir
46
        self.path2id = inv.path2id
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
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()
954 by Martin Pool
- separate out code that just scans the hash cache to find files that are possibly
52
        hc.scan()
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
53
54
        if hc.needs_write:
55
            mutter("write hc")
56
            hc.write()
954 by Martin Pool
- separate out code that just scans the hash cache to find files that are possibly
57
            
58
            
59
    def __del__(self):
60
        if self._hashcache.needs_write:
61
            self._hashcache.write()
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
62
453 by Martin Pool
- Split WorkingTree into its own file
63
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
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
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
71
        for path, ie in inv.iter_entries():
1092.2.6 by Robert Collins
symlink support updated to work
72
            if bzrlib.osutils.lexists(self.abspath(path)):
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
73
                yield ie.file_id
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
74
75
453 by Martin Pool
- Split WorkingTree into its own file
76
    def __repr__(self):
77
        return "<%s of %s>" % (self.__class__.__name__,
954 by Martin Pool
- separate out code that just scans the hash cache to find files that are possibly
78
                               getattr(self, 'basedir', None))
453 by Martin Pool
- Split WorkingTree into its own file
79
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
80
81
453 by Martin Pool
- Split WorkingTree into its own file
82
    def abspath(self, filename):
83
        return os.path.join(self.basedir, filename)
84
85
    def has_filename(self, filename):
1092.2.6 by Robert Collins
symlink support updated to work
86
        return bzrlib.osutils.lexists(self.abspath(filename))
453 by Martin Pool
- Split WorkingTree into its own file
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
1248 by Martin Pool
- new weave based cleanup [broken]
98
99
    def id2abspath(self, file_id):
100
        return self.abspath(self.id2path(file_id))
101
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
102
                
453 by Martin Pool
- Split WorkingTree into its own file
103
    def has_id(self, file_id):
104
        # files that have been deleted are excluded
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
105
        inv = self._inventory
106
        if not inv.has_id(file_id):
453 by Martin Pool
- Split WorkingTree into its own file
107
            return False
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
108
        path = inv.id2path(file_id)
1092.2.6 by Robert Collins
symlink support updated to work
109
        return bzrlib.osutils.lexists(self.abspath(path))
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
110
111
112
    __contains__ = has_id
113
    
114
453 by Martin Pool
- Split WorkingTree into its own file
115
    def get_file_size(self, file_id):
1248 by Martin Pool
- new weave based cleanup [broken]
116
        return os.path.getsize(self.id2abspath(file_id))
453 by Martin Pool
- Split WorkingTree into its own file
117
118
    def get_file_sha1(self, file_id):
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
119
        path = self._inventory.id2path(file_id)
120
        return self._hashcache.get_sha1(path)
453 by Martin Pool
- Split WorkingTree into its own file
121
1092.2.6 by Robert Collins
symlink support updated to work
122
    def get_symlink_target(self, file_id):
123
        return os.readlink(self.id2path(file_id))
453 by Martin Pool
- Split WorkingTree into its own file
124
125
    def file_class(self, filename):
126
        if self.path2id(filename):
127
            return 'V'
128
        elif self.is_ignored(filename):
129
            return 'I'
130
        else:
131
            return '?'
132
133
134
    def list_files(self):
135
        """Recursively list all files as (path, class, kind, id).
136
137
        Lists, but does not descend into unversioned directories.
138
139
        This does not include files that have been deleted in this
140
        tree.
141
142
        Skips the control directory.
143
        """
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
144
        inv = self._inventory
453 by Martin Pool
- Split WorkingTree into its own file
145
146
        def descend(from_dir_relpath, from_dir_id, dp):
147
            ls = os.listdir(dp)
148
            ls.sort()
149
            for f in ls:
150
                ## TODO: If we find a subdirectory with its own .bzr
151
                ## directory, then that is a separate tree and we
152
                ## should exclude it.
153
                if bzrlib.BZRDIR == f:
154
                    continue
155
156
                # path within tree
157
                fp = appendpath(from_dir_relpath, f)
158
159
                # absolute path
160
                fap = appendpath(dp, f)
161
                
162
                f_ie = inv.get_child(from_dir_id, f)
163
                if f_ie:
164
                    c = 'V'
165
                elif self.is_ignored(fp):
166
                    c = 'I'
167
                else:
168
                    c = '?'
169
170
                fk = file_kind(fap)
171
172
                if f_ie:
173
                    if f_ie.kind != fk:
174
                        raise BzrCheckError("file %r entered as kind %r id %r, "
175
                                            "now of kind %r"
176
                                            % (fap, f_ie.kind, f_ie.file_id, fk))
177
178
                yield fp, c, fk, (f_ie and f_ie.file_id)
179
180
                if fk != 'directory':
181
                    continue
182
183
                if c != 'V':
184
                    # don't descend unversioned directories
185
                    continue
186
                
187
                for ff in descend(fp, f_ie.file_id, fap):
188
                    yield ff
189
190
        for f in descend('', inv.root.file_id, self.basedir):
191
            yield f
192
            
193
194
195
    def unknowns(self):
196
        for subp in self.extras():
197
            if not self.is_ignored(subp):
198
                yield subp
199
200
201
    def extras(self):
202
        """Yield all unknown files in this WorkingTree.
203
204
        If there are any unknown directories then only the directory is
205
        returned, not all its children.  But if there are unknown files
206
        under a versioned subdirectory, they are returned.
207
208
        Currently returned depth-first, sorted by name within directories.
209
        """
210
        ## TODO: Work from given directory downwards
211
        for path, dir_entry in self.inventory.directories():
212
            mutter("search for unknowns in %r" % path)
213
            dirabs = self.abspath(path)
214
            if not isdir(dirabs):
215
                # e.g. directory deleted
216
                continue
217
218
            fl = []
219
            for subf in os.listdir(dirabs):
220
                if (subf != '.bzr'
221
                    and (subf not in dir_entry.children)):
222
                    fl.append(subf)
223
            
224
            fl.sort()
225
            for subf in fl:
226
                subp = appendpath(path, subf)
227
                yield subp
228
229
230
    def ignored_files(self):
231
        """Yield list of PATH, IGNORE_PATTERN"""
232
        for subp in self.extras():
233
            pat = self.is_ignored(subp)
234
            if pat != None:
235
                yield subp, pat
236
237
238
    def get_ignore_list(self):
239
        """Return list of ignore patterns.
240
241
        Cached in the Tree object after the first call.
242
        """
243
        if hasattr(self, '_ignorelist'):
244
            return self._ignorelist
245
246
        l = bzrlib.DEFAULT_IGNORE[:]
247
        if self.has_filename(bzrlib.IGNORE_FILENAME):
248
            f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
249
            l.extend([line.rstrip("\n\r") for line in f.readlines()])
250
        self._ignorelist = l
251
        return l
252
253
254
    def is_ignored(self, filename):
255
        r"""Check whether the filename matches an ignore pattern.
256
257
        Patterns containing '/' or '\' need to match the whole path;
258
        others match against only the last component.
259
260
        If the file is ignored, returns the pattern which caused it to
261
        be ignored, otherwise None.  So this can simply be used as a
262
        boolean if desired."""
263
264
        # TODO: Use '**' to match directories, and other extended
265
        # globbing stuff from cvs/rsync.
266
267
        # XXX: fnmatch is actually not quite what we want: it's only
268
        # approximately the same as real Unix fnmatch, and doesn't
269
        # treat dotfiles correctly and allows * to match /.
270
        # Eventually it should be replaced with something more
271
        # accurate.
272
        
273
        for pat in self.get_ignore_list():
274
            if '/' in pat or '\\' in pat:
275
                
276
                # as a special case, you can put ./ at the start of a
277
                # pattern; this is good to match in the top-level
278
                # only;
279
                
280
                if (pat[:2] == './') or (pat[:2] == '.\\'):
281
                    newpat = pat[2:]
282
                else:
283
                    newpat = pat
284
                if fnmatch.fnmatchcase(filename, newpat):
285
                    return pat
286
            else:
287
                if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):
288
                    return pat
289
        else:
290
            return None
1140 by Martin Pool
- lift out import statements within WorkingTree
291