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