~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
1398 by Robert Collins
integrate in Gustavos x-bit patch
24
import stat
1140 by Martin Pool
- lift out import statements within WorkingTree
25
import fnmatch
26
        
453 by Martin Pool
- Split WorkingTree into its own file
27
import bzrlib.tree
1140 by Martin Pool
- lift out import statements within WorkingTree
28
from bzrlib.osutils import appendpath, file_kind, isdir, splitpath
29
from bzrlib.errors import BzrCheckError
30
from bzrlib.trace import mutter
453 by Martin Pool
- Split WorkingTree into its own file
31
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
32
class TreeEntry(object):
33
    """An entry that implements the minium interface used by commands.
34
35
    This needs further inspection, it may be better to have 
36
    InventoryEntries without ids - though that seems wrong. For now,
37
    this is a parallel hierarchy to InventoryEntry, and needs to become
38
    one of several things: decorates to that hierarchy, children of, or
39
    parents of it.
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
40
    Another note is that these objects are currently only used when there is
41
    no InventoryEntry available - i.e. for unversioned objects.
42
    Perhaps they should be UnversionedEntry et al. ? - RBC 20051003
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
43
    """
44
 
45
    def __eq__(self, other):
46
        # yes, this us ugly, TODO: best practice __eq__ style.
47
        return (isinstance(other, TreeEntry)
48
                and other.__class__ == self.__class__)
49
 
50
    def kind_character(self):
51
        return "???"
52
53
54
class TreeDirectory(TreeEntry):
55
    """See TreeEntry. This is a directory in a working tree."""
56
57
    def __eq__(self, other):
58
        return (isinstance(other, TreeDirectory)
59
                and other.__class__ == self.__class__)
60
61
    def kind_character(self):
62
        return "/"
63
64
65
class TreeFile(TreeEntry):
66
    """See TreeEntry. This is a regular file in a working tree."""
67
68
    def __eq__(self, other):
69
        return (isinstance(other, TreeFile)
70
                and other.__class__ == self.__class__)
71
72
    def kind_character(self):
73
        return ''
74
75
76
class TreeLink(TreeEntry):
77
    """See TreeEntry. This is a symlink in a working tree."""
78
79
    def __eq__(self, other):
80
        return (isinstance(other, TreeLink)
81
                and other.__class__ == self.__class__)
82
83
    def kind_character(self):
84
        return ''
85
86
453 by Martin Pool
- Split WorkingTree into its own file
87
class WorkingTree(bzrlib.tree.Tree):
88
    """Working copy tree.
89
90
    The inventory is held in the `Branch` working-inventory, and the
91
    files are in a directory on disk.
92
93
    It is possible for a `WorkingTree` to have a filename which is
94
    not listed in the Inventory and vice versa.
95
    """
96
    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.
97
        from bzrlib.hashcache import HashCache
98
        from bzrlib.trace import note, mutter
99
453 by Martin Pool
- Split WorkingTree into its own file
100
        self._inventory = inv
101
        self.basedir = basedir
102
        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.
103
104
        # update the whole cache up front and write to disk if anything changed;
105
        # in the future we might want to do this more selectively
106
        hc = self._hashcache = HashCache(basedir)
107
        hc.read()
954 by Martin Pool
- separate out code that just scans the hash cache to find files that are possibly
108
        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.
109
110
        if hc.needs_write:
111
            mutter("write hc")
112
            hc.write()
954 by Martin Pool
- separate out code that just scans the hash cache to find files that are possibly
113
            
114
            
115
    def __del__(self):
116
        if self._hashcache.needs_write:
117
            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.
118
453 by Martin Pool
- Split WorkingTree into its own file
119
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
120
    def __iter__(self):
121
        """Iterate through file_ids for this tree.
122
123
        file_ids are in a WorkingTree if they are in the working inventory
124
        and the working file exists.
125
        """
126
        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.
127
        for path, ie in inv.iter_entries():
1092.2.6 by Robert Collins
symlink support updated to work
128
            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.
129
                yield ie.file_id
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
130
131
453 by Martin Pool
- Split WorkingTree into its own file
132
    def __repr__(self):
133
        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
134
                               getattr(self, 'basedir', None))
453 by Martin Pool
- Split WorkingTree into its own file
135
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.
136
137
453 by Martin Pool
- Split WorkingTree into its own file
138
    def abspath(self, filename):
139
        return os.path.join(self.basedir, filename)
140
141
    def has_filename(self, filename):
1092.2.6 by Robert Collins
symlink support updated to work
142
        return bzrlib.osutils.lexists(self.abspath(filename))
453 by Martin Pool
- Split WorkingTree into its own file
143
144
    def get_file(self, file_id):
145
        return self.get_file_byname(self.id2path(file_id))
146
147
    def get_file_byname(self, filename):
148
        return file(self.abspath(filename), 'rb')
149
150
    def _get_store_filename(self, file_id):
151
        ## XXX: badly named; this isn't in the store at all
152
        return self.abspath(self.id2path(file_id))
153
1248 by Martin Pool
- new weave based cleanup [broken]
154
155
    def id2abspath(self, file_id):
156
        return self.abspath(self.id2path(file_id))
157
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
158
                
1185.12.38 by abentley
semi-broke merge
159
    def has_id(self, file_id, allow_root=False):
453 by Martin Pool
- Split WorkingTree into its own file
160
        # files that have been deleted are excluded
1185.12.38 by abentley
semi-broke merge
161
        inv = self.inventory
162
        if allow_root and file_id == inv.root.file_id:
163
            return True
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.
164
        if not inv.has_id(file_id):
453 by Martin Pool
- Split WorkingTree into its own file
165
            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.
166
        path = inv.id2path(file_id)
1092.2.6 by Robert Collins
symlink support updated to work
167
        return bzrlib.osutils.lexists(self.abspath(path))
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
168
169
170
    __contains__ = has_id
171
    
172
453 by Martin Pool
- Split WorkingTree into its own file
173
    def get_file_size(self, file_id):
1248 by Martin Pool
- new weave based cleanup [broken]
174
        return os.path.getsize(self.id2abspath(file_id))
453 by Martin Pool
- Split WorkingTree into its own file
175
176
    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.
177
        path = self._inventory.id2path(file_id)
178
        return self._hashcache.get_sha1(path)
453 by Martin Pool
- Split WorkingTree into its own file
179
1398 by Robert Collins
integrate in Gustavos x-bit patch
180
181
    def is_executable(self, file_id):
182
        if os.name == "nt":
183
            return self._inventory[file_id].executable
184
        else:
185
            path = self._inventory.id2path(file_id)
186
            mode = os.lstat(self.abspath(path)).st_mode
187
            return bool(stat.S_ISREG(mode) and stat.S_IEXEC&mode)
188
1092.2.6 by Robert Collins
symlink support updated to work
189
    def get_symlink_target(self, file_id):
1185.15.10 by Scott James Remnant
Fix WorkingTree.get_symlink_target() to read the absolute path of the
190
        return os.readlink(self.id2abspath(file_id))
453 by Martin Pool
- Split WorkingTree into its own file
191
192
    def file_class(self, filename):
193
        if self.path2id(filename):
194
            return 'V'
195
        elif self.is_ignored(filename):
196
            return 'I'
197
        else:
198
            return '?'
199
200
201
    def list_files(self):
202
        """Recursively list all files as (path, class, kind, id).
203
204
        Lists, but does not descend into unversioned directories.
205
206
        This does not include files that have been deleted in this
207
        tree.
208
209
        Skips the control directory.
210
        """
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.
211
        inv = self._inventory
453 by Martin Pool
- Split WorkingTree into its own file
212
213
        def descend(from_dir_relpath, from_dir_id, dp):
214
            ls = os.listdir(dp)
215
            ls.sort()
216
            for f in ls:
217
                ## TODO: If we find a subdirectory with its own .bzr
218
                ## directory, then that is a separate tree and we
219
                ## should exclude it.
220
                if bzrlib.BZRDIR == f:
221
                    continue
222
223
                # path within tree
224
                fp = appendpath(from_dir_relpath, f)
225
226
                # absolute path
227
                fap = appendpath(dp, f)
228
                
229
                f_ie = inv.get_child(from_dir_id, f)
230
                if f_ie:
231
                    c = 'V'
232
                elif self.is_ignored(fp):
233
                    c = 'I'
234
                else:
235
                    c = '?'
236
237
                fk = file_kind(fap)
238
239
                if f_ie:
240
                    if f_ie.kind != fk:
241
                        raise BzrCheckError("file %r entered as kind %r id %r, "
242
                                            "now of kind %r"
243
                                            % (fap, f_ie.kind, f_ie.file_id, fk))
244
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
245
                # make a last minute entry
246
                if f_ie:
247
                    entry = f_ie
248
                else:
249
                    if fk == 'directory':
250
                        entry = TreeDirectory()
251
                    elif fk == 'file':
252
                        entry = TreeFile()
253
                    elif fk == 'symlink':
254
                        entry = TreeLink()
255
                    else:
256
                        entry = TreeEntry()
257
                
258
                yield fp, c, fk, (f_ie and f_ie.file_id), entry
453 by Martin Pool
- Split WorkingTree into its own file
259
260
                if fk != 'directory':
261
                    continue
262
263
                if c != 'V':
264
                    # don't descend unversioned directories
265
                    continue
266
                
267
                for ff in descend(fp, f_ie.file_id, fap):
268
                    yield ff
269
270
        for f in descend('', inv.root.file_id, self.basedir):
271
            yield f
272
            
273
274
275
    def unknowns(self):
276
        for subp in self.extras():
277
            if not self.is_ignored(subp):
278
                yield subp
279
1185.14.6 by Aaron Bentley
Made iter_conflicts a WorkingTree method
280
    def iter_conflicts(self):
281
        conflicted = set()
282
        for path in (s[0] for s in self.list_files()):
283
            stem = get_conflicted_stem(path)
284
            if stem is None:
285
                continue
286
            if stem not in conflicted:
287
                conflicted.add(stem)
288
                yield stem
453 by Martin Pool
- Split WorkingTree into its own file
289
290
    def extras(self):
291
        """Yield all unknown files in this WorkingTree.
292
293
        If there are any unknown directories then only the directory is
294
        returned, not all its children.  But if there are unknown files
295
        under a versioned subdirectory, they are returned.
296
297
        Currently returned depth-first, sorted by name within directories.
298
        """
299
        ## TODO: Work from given directory downwards
300
        for path, dir_entry in self.inventory.directories():
301
            mutter("search for unknowns in %r" % path)
302
            dirabs = self.abspath(path)
303
            if not isdir(dirabs):
304
                # e.g. directory deleted
305
                continue
306
307
            fl = []
308
            for subf in os.listdir(dirabs):
309
                if (subf != '.bzr'
310
                    and (subf not in dir_entry.children)):
311
                    fl.append(subf)
312
            
313
            fl.sort()
314
            for subf in fl:
315
                subp = appendpath(path, subf)
316
                yield subp
317
318
319
    def ignored_files(self):
320
        """Yield list of PATH, IGNORE_PATTERN"""
321
        for subp in self.extras():
322
            pat = self.is_ignored(subp)
323
            if pat != None:
324
                yield subp, pat
325
326
327
    def get_ignore_list(self):
328
        """Return list of ignore patterns.
329
330
        Cached in the Tree object after the first call.
331
        """
332
        if hasattr(self, '_ignorelist'):
333
            return self._ignorelist
334
335
        l = bzrlib.DEFAULT_IGNORE[:]
336
        if self.has_filename(bzrlib.IGNORE_FILENAME):
337
            f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
338
            l.extend([line.rstrip("\n\r") for line in f.readlines()])
339
        self._ignorelist = l
340
        return l
341
342
343
    def is_ignored(self, filename):
344
        r"""Check whether the filename matches an ignore pattern.
345
346
        Patterns containing '/' or '\' need to match the whole path;
347
        others match against only the last component.
348
349
        If the file is ignored, returns the pattern which caused it to
350
        be ignored, otherwise None.  So this can simply be used as a
351
        boolean if desired."""
352
353
        # TODO: Use '**' to match directories, and other extended
354
        # globbing stuff from cvs/rsync.
355
356
        # XXX: fnmatch is actually not quite what we want: it's only
357
        # approximately the same as real Unix fnmatch, and doesn't
358
        # treat dotfiles correctly and allows * to match /.
359
        # Eventually it should be replaced with something more
360
        # accurate.
361
        
362
        for pat in self.get_ignore_list():
363
            if '/' in pat or '\\' in pat:
364
                
365
                # as a special case, you can put ./ at the start of a
366
                # pattern; this is good to match in the top-level
367
                # only;
368
                
369
                if (pat[:2] == './') or (pat[:2] == '.\\'):
370
                    newpat = pat[2:]
371
                else:
372
                    newpat = pat
373
                if fnmatch.fnmatchcase(filename, newpat):
374
                    return pat
375
            else:
376
                if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):
377
                    return pat
378
        else:
379
            return None
1185.14.6 by Aaron Bentley
Made iter_conflicts a WorkingTree method
380
1185.12.28 by Aaron Bentley
Removed use of readonly path for executability test
381
    def kind(self, file_id):
382
        return file_kind(self.id2abspath(file_id))
383
1185.14.6 by Aaron Bentley
Made iter_conflicts a WorkingTree method
384
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
385
def get_conflicted_stem(path):
386
    for suffix in CONFLICT_SUFFIXES:
387
        if path.endswith(suffix):
388
            return path[:-len(suffix)]