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 |
|
453
by Martin Pool
- Split WorkingTree into its own file |
159 |
def has_id(self, file_id): |
160 |
# 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. |
161 |
inv = self._inventory |
162 |
if not inv.has_id(file_id): |
|
453
by Martin Pool
- Split WorkingTree into its own file |
163 |
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. |
164 |
path = inv.id2path(file_id) |
1092.2.6
by Robert Collins
symlink support updated to work |
165 |
return bzrlib.osutils.lexists(self.abspath(path)) |
462
by Martin Pool
- New form 'file_id in tree' to check if the file is present |
166 |
|
167 |
||
168 |
__contains__ = has_id |
|
169 |
||
170 |
||
453
by Martin Pool
- Split WorkingTree into its own file |
171 |
def get_file_size(self, file_id): |
1248
by Martin Pool
- new weave based cleanup [broken] |
172 |
return os.path.getsize(self.id2abspath(file_id)) |
453
by Martin Pool
- Split WorkingTree into its own file |
173 |
|
174 |
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. |
175 |
path = self._inventory.id2path(file_id) |
176 |
return self._hashcache.get_sha1(path) |
|
453
by Martin Pool
- Split WorkingTree into its own file |
177 |
|
1398
by Robert Collins
integrate in Gustavos x-bit patch |
178 |
|
179 |
def is_executable(self, file_id): |
|
180 |
if os.name == "nt": |
|
181 |
return self._inventory[file_id].executable |
|
182 |
else: |
|
183 |
path = self._inventory.id2path(file_id) |
|
184 |
mode = os.lstat(self.abspath(path)).st_mode |
|
185 |
return bool(stat.S_ISREG(mode) and stat.S_IEXEC&mode) |
|
186 |
||
1092.2.6
by Robert Collins
symlink support updated to work |
187 |
def get_symlink_target(self, file_id): |
188 |
return os.readlink(self.id2path(file_id)) |
|
453
by Martin Pool
- Split WorkingTree into its own file |
189 |
|
190 |
def file_class(self, filename): |
|
191 |
if self.path2id(filename): |
|
192 |
return 'V' |
|
193 |
elif self.is_ignored(filename): |
|
194 |
return 'I' |
|
195 |
else: |
|
196 |
return '?' |
|
197 |
||
198 |
||
199 |
def list_files(self): |
|
200 |
"""Recursively list all files as (path, class, kind, id).
|
|
201 |
||
202 |
Lists, but does not descend into unversioned directories.
|
|
203 |
||
204 |
This does not include files that have been deleted in this
|
|
205 |
tree.
|
|
206 |
||
207 |
Skips the control directory.
|
|
208 |
"""
|
|
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. |
209 |
inv = self._inventory |
453
by Martin Pool
- Split WorkingTree into its own file |
210 |
|
211 |
def descend(from_dir_relpath, from_dir_id, dp): |
|
212 |
ls = os.listdir(dp) |
|
213 |
ls.sort() |
|
214 |
for f in ls: |
|
215 |
## TODO: If we find a subdirectory with its own .bzr
|
|
216 |
## directory, then that is a separate tree and we
|
|
217 |
## should exclude it.
|
|
218 |
if bzrlib.BZRDIR == f: |
|
219 |
continue
|
|
220 |
||
221 |
# path within tree
|
|
222 |
fp = appendpath(from_dir_relpath, f) |
|
223 |
||
224 |
# absolute path
|
|
225 |
fap = appendpath(dp, f) |
|
226 |
||
227 |
f_ie = inv.get_child(from_dir_id, f) |
|
228 |
if f_ie: |
|
229 |
c = 'V' |
|
230 |
elif self.is_ignored(fp): |
|
231 |
c = 'I' |
|
232 |
else: |
|
233 |
c = '?' |
|
234 |
||
235 |
fk = file_kind(fap) |
|
236 |
||
237 |
if f_ie: |
|
238 |
if f_ie.kind != fk: |
|
239 |
raise BzrCheckError("file %r entered as kind %r id %r, " |
|
240 |
"now of kind %r" |
|
241 |
% (fap, f_ie.kind, f_ie.file_id, fk)) |
|
242 |
||
1399.1.2
by Robert Collins
push kind character creation into InventoryEntry and TreeEntry |
243 |
# make a last minute entry
|
244 |
if f_ie: |
|
245 |
entry = f_ie |
|
246 |
else: |
|
247 |
if fk == 'directory': |
|
248 |
entry = TreeDirectory() |
|
249 |
elif fk == 'file': |
|
250 |
entry = TreeFile() |
|
251 |
elif fk == 'symlink': |
|
252 |
entry = TreeLink() |
|
253 |
else: |
|
254 |
entry = TreeEntry() |
|
255 |
||
256 |
yield fp, c, fk, (f_ie and f_ie.file_id), entry |
|
453
by Martin Pool
- Split WorkingTree into its own file |
257 |
|
258 |
if fk != 'directory': |
|
259 |
continue
|
|
260 |
||
261 |
if c != 'V': |
|
262 |
# don't descend unversioned directories
|
|
263 |
continue
|
|
264 |
||
265 |
for ff in descend(fp, f_ie.file_id, fap): |
|
266 |
yield ff |
|
267 |
||
268 |
for f in descend('', inv.root.file_id, self.basedir): |
|
269 |
yield f |
|
270 |
||
271 |
||
272 |
||
273 |
def unknowns(self): |
|
274 |
for subp in self.extras(): |
|
275 |
if not self.is_ignored(subp): |
|
276 |
yield subp |
|
277 |
||
278 |
||
279 |
def extras(self): |
|
280 |
"""Yield all unknown files in this WorkingTree.
|
|
281 |
||
282 |
If there are any unknown directories then only the directory is
|
|
283 |
returned, not all its children. But if there are unknown files
|
|
284 |
under a versioned subdirectory, they are returned.
|
|
285 |
||
286 |
Currently returned depth-first, sorted by name within directories.
|
|
287 |
"""
|
|
288 |
## TODO: Work from given directory downwards
|
|
289 |
for path, dir_entry in self.inventory.directories(): |
|
290 |
mutter("search for unknowns in %r" % path) |
|
291 |
dirabs = self.abspath(path) |
|
292 |
if not isdir(dirabs): |
|
293 |
# e.g. directory deleted
|
|
294 |
continue
|
|
295 |
||
296 |
fl = [] |
|
297 |
for subf in os.listdir(dirabs): |
|
298 |
if (subf != '.bzr' |
|
299 |
and (subf not in dir_entry.children)): |
|
300 |
fl.append(subf) |
|
301 |
||
302 |
fl.sort() |
|
303 |
for subf in fl: |
|
304 |
subp = appendpath(path, subf) |
|
305 |
yield subp |
|
306 |
||
307 |
||
308 |
def ignored_files(self): |
|
309 |
"""Yield list of PATH, IGNORE_PATTERN"""
|
|
310 |
for subp in self.extras(): |
|
311 |
pat = self.is_ignored(subp) |
|
312 |
if pat != None: |
|
313 |
yield subp, pat |
|
314 |
||
315 |
||
316 |
def get_ignore_list(self): |
|
317 |
"""Return list of ignore patterns.
|
|
318 |
||
319 |
Cached in the Tree object after the first call.
|
|
320 |
"""
|
|
321 |
if hasattr(self, '_ignorelist'): |
|
322 |
return self._ignorelist |
|
323 |
||
324 |
l = bzrlib.DEFAULT_IGNORE[:] |
|
325 |
if self.has_filename(bzrlib.IGNORE_FILENAME): |
|
326 |
f = self.get_file_byname(bzrlib.IGNORE_FILENAME) |
|
327 |
l.extend([line.rstrip("\n\r") for line in f.readlines()]) |
|
328 |
self._ignorelist = l |
|
329 |
return l |
|
330 |
||
331 |
||
332 |
def is_ignored(self, filename): |
|
333 |
r"""Check whether the filename matches an ignore pattern. |
|
334 |
||
335 |
Patterns containing '/' or '\' need to match the whole path;
|
|
336 |
others match against only the last component.
|
|
337 |
||
338 |
If the file is ignored, returns the pattern which caused it to
|
|
339 |
be ignored, otherwise None. So this can simply be used as a
|
|
340 |
boolean if desired."""
|
|
341 |
||
342 |
# TODO: Use '**' to match directories, and other extended
|
|
343 |
# globbing stuff from cvs/rsync.
|
|
344 |
||
345 |
# XXX: fnmatch is actually not quite what we want: it's only
|
|
346 |
# approximately the same as real Unix fnmatch, and doesn't
|
|
347 |
# treat dotfiles correctly and allows * to match /.
|
|
348 |
# Eventually it should be replaced with something more
|
|
349 |
# accurate.
|
|
350 |
||
351 |
for pat in self.get_ignore_list(): |
|
352 |
if '/' in pat or '\\' in pat: |
|
353 |
||
354 |
# as a special case, you can put ./ at the start of a
|
|
355 |
# pattern; this is good to match in the top-level
|
|
356 |
# only;
|
|
357 |
||
358 |
if (pat[:2] == './') or (pat[:2] == '.\\'): |
|
359 |
newpat = pat[2:] |
|
360 |
else: |
|
361 |
newpat = pat |
|
362 |
if fnmatch.fnmatchcase(filename, newpat): |
|
363 |
return pat |
|
364 |
else: |
|
365 |
if fnmatch.fnmatchcase(splitpath(filename)[-1], pat): |
|
366 |
return pat |
|
367 |
else: |
|
368 |
return None |
|
1140
by Martin Pool
- lift out import statements within WorkingTree |
369 |