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