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