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