##// END OF EJS Templates
dirstate.walk: eliminate filter function...
Matt Mackall -
r6819:78624d74 default
parent child Browse files
Show More
@@ -1,658 +1,623 b''
1 """
1 """
2 dirstate.py - working directory tracking for mercurial
2 dirstate.py - working directory tracking for mercurial
3
3
4 Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
5
5
6 This software may be used and distributed according to the terms
6 This software may be used and distributed according to the terms
7 of the GNU General Public License, incorporated herein by reference.
7 of the GNU General Public License, incorporated herein by reference.
8 """
8 """
9
9
10 from node import nullid
10 from node import nullid
11 from i18n import _
11 from i18n import _
12 import struct, os, bisect, stat, util, errno, ignore
12 import struct, os, bisect, stat, util, errno, ignore
13 import cStringIO, osutil, sys
13 import cStringIO, osutil, sys
14
14
15 _unknown = ('?', 0, 0, 0)
15 _unknown = ('?', 0, 0, 0)
16 _format = ">cllll"
16 _format = ">cllll"
17
17
18 def _finddirs(path):
18 def _finddirs(path):
19 pos = len(path)
19 pos = len(path)
20 while 1:
20 while 1:
21 pos = path.rfind('/', 0, pos)
21 pos = path.rfind('/', 0, pos)
22 if pos == -1:
22 if pos == -1:
23 break
23 break
24 yield path[:pos]
24 yield path[:pos]
25
25
26 class dirstate(object):
26 class dirstate(object):
27
27
28 def __init__(self, opener, ui, root):
28 def __init__(self, opener, ui, root):
29 self._opener = opener
29 self._opener = opener
30 self._root = root
30 self._root = root
31 self._dirty = False
31 self._dirty = False
32 self._dirtypl = False
32 self._dirtypl = False
33 self._ui = ui
33 self._ui = ui
34
34
35 def __getattr__(self, name):
35 def __getattr__(self, name):
36 if name == '_map':
36 if name == '_map':
37 self._read()
37 self._read()
38 return self._map
38 return self._map
39 elif name == '_copymap':
39 elif name == '_copymap':
40 self._read()
40 self._read()
41 return self._copymap
41 return self._copymap
42 elif name == '_foldmap':
42 elif name == '_foldmap':
43 _foldmap = {}
43 _foldmap = {}
44 for name in self._map:
44 for name in self._map:
45 norm = os.path.normcase(os.path.normpath(name))
45 norm = os.path.normcase(os.path.normpath(name))
46 _foldmap[norm] = name
46 _foldmap[norm] = name
47 self._foldmap = _foldmap
47 self._foldmap = _foldmap
48 return self._foldmap
48 return self._foldmap
49 elif name == '_branch':
49 elif name == '_branch':
50 try:
50 try:
51 self._branch = (self._opener("branch").read().strip()
51 self._branch = (self._opener("branch").read().strip()
52 or "default")
52 or "default")
53 except IOError:
53 except IOError:
54 self._branch = "default"
54 self._branch = "default"
55 return self._branch
55 return self._branch
56 elif name == '_pl':
56 elif name == '_pl':
57 self._pl = [nullid, nullid]
57 self._pl = [nullid, nullid]
58 try:
58 try:
59 st = self._opener("dirstate").read(40)
59 st = self._opener("dirstate").read(40)
60 if len(st) == 40:
60 if len(st) == 40:
61 self._pl = st[:20], st[20:40]
61 self._pl = st[:20], st[20:40]
62 except IOError, err:
62 except IOError, err:
63 if err.errno != errno.ENOENT: raise
63 if err.errno != errno.ENOENT: raise
64 return self._pl
64 return self._pl
65 elif name == '_dirs':
65 elif name == '_dirs':
66 dirs = {}
66 dirs = {}
67 for f,s in self._map.items():
67 for f,s in self._map.items():
68 if s[0] != 'r':
68 if s[0] != 'r':
69 for base in _finddirs(f):
69 for base in _finddirs(f):
70 dirs[base] = dirs.get(base, 0) + 1
70 dirs[base] = dirs.get(base, 0) + 1
71 self._dirs = dirs
71 self._dirs = dirs
72 return self._dirs
72 return self._dirs
73 elif name == '_ignore':
73 elif name == '_ignore':
74 files = [self._join('.hgignore')]
74 files = [self._join('.hgignore')]
75 for name, path in self._ui.configitems("ui"):
75 for name, path in self._ui.configitems("ui"):
76 if name == 'ignore' or name.startswith('ignore.'):
76 if name == 'ignore' or name.startswith('ignore.'):
77 files.append(os.path.expanduser(path))
77 files.append(os.path.expanduser(path))
78 self._ignore = ignore.ignore(self._root, files, self._ui.warn)
78 self._ignore = ignore.ignore(self._root, files, self._ui.warn)
79 return self._ignore
79 return self._ignore
80 elif name == '_slash':
80 elif name == '_slash':
81 self._slash = self._ui.configbool('ui', 'slash') and os.sep != '/'
81 self._slash = self._ui.configbool('ui', 'slash') and os.sep != '/'
82 return self._slash
82 return self._slash
83 elif name == '_checklink':
83 elif name == '_checklink':
84 self._checklink = util.checklink(self._root)
84 self._checklink = util.checklink(self._root)
85 return self._checklink
85 return self._checklink
86 elif name == '_checkexec':
86 elif name == '_checkexec':
87 self._checkexec = util.checkexec(self._root)
87 self._checkexec = util.checkexec(self._root)
88 return self._checkexec
88 return self._checkexec
89 elif name == '_checkcase':
89 elif name == '_checkcase':
90 self._checkcase = not util.checkcase(self._join('.hg'))
90 self._checkcase = not util.checkcase(self._join('.hg'))
91 return self._checkcase
91 return self._checkcase
92 elif name == 'normalize':
92 elif name == 'normalize':
93 if self._checkcase:
93 if self._checkcase:
94 self.normalize = self._normalize
94 self.normalize = self._normalize
95 else:
95 else:
96 self.normalize = lambda x: x
96 self.normalize = lambda x: x
97 return self.normalize
97 return self.normalize
98 else:
98 else:
99 raise AttributeError, name
99 raise AttributeError, name
100
100
101 def _join(self, f):
101 def _join(self, f):
102 return os.path.join(self._root, f)
102 return os.path.join(self._root, f)
103
103
104 def flagfunc(self, fallback):
104 def flagfunc(self, fallback):
105 if self._checklink:
105 if self._checklink:
106 if self._checkexec:
106 if self._checkexec:
107 def f(x):
107 def f(x):
108 p = os.path.join(self._root, x)
108 p = os.path.join(self._root, x)
109 if os.path.islink(p):
109 if os.path.islink(p):
110 return 'l'
110 return 'l'
111 if util.is_exec(p):
111 if util.is_exec(p):
112 return 'x'
112 return 'x'
113 return ''
113 return ''
114 return f
114 return f
115 def f(x):
115 def f(x):
116 if os.path.islink(os.path.join(self._root, x)):
116 if os.path.islink(os.path.join(self._root, x)):
117 return 'l'
117 return 'l'
118 if 'x' in fallback(x):
118 if 'x' in fallback(x):
119 return 'x'
119 return 'x'
120 return ''
120 return ''
121 return f
121 return f
122 if self._checkexec:
122 if self._checkexec:
123 def f(x):
123 def f(x):
124 if 'l' in fallback(x):
124 if 'l' in fallback(x):
125 return 'l'
125 return 'l'
126 if util.is_exec(os.path.join(self._root, x)):
126 if util.is_exec(os.path.join(self._root, x)):
127 return 'x'
127 return 'x'
128 return ''
128 return ''
129 return f
129 return f
130 return fallback
130 return fallback
131
131
132 def getcwd(self):
132 def getcwd(self):
133 cwd = os.getcwd()
133 cwd = os.getcwd()
134 if cwd == self._root: return ''
134 if cwd == self._root: return ''
135 # self._root ends with a path separator if self._root is '/' or 'C:\'
135 # self._root ends with a path separator if self._root is '/' or 'C:\'
136 rootsep = self._root
136 rootsep = self._root
137 if not util.endswithsep(rootsep):
137 if not util.endswithsep(rootsep):
138 rootsep += os.sep
138 rootsep += os.sep
139 if cwd.startswith(rootsep):
139 if cwd.startswith(rootsep):
140 return cwd[len(rootsep):]
140 return cwd[len(rootsep):]
141 else:
141 else:
142 # we're outside the repo. return an absolute path.
142 # we're outside the repo. return an absolute path.
143 return cwd
143 return cwd
144
144
145 def pathto(self, f, cwd=None):
145 def pathto(self, f, cwd=None):
146 if cwd is None:
146 if cwd is None:
147 cwd = self.getcwd()
147 cwd = self.getcwd()
148 path = util.pathto(self._root, cwd, f)
148 path = util.pathto(self._root, cwd, f)
149 if self._slash:
149 if self._slash:
150 return util.normpath(path)
150 return util.normpath(path)
151 return path
151 return path
152
152
153 def __getitem__(self, key):
153 def __getitem__(self, key):
154 ''' current states:
154 ''' current states:
155 n normal
155 n normal
156 m needs merging
156 m needs merging
157 r marked for removal
157 r marked for removal
158 a marked for addition
158 a marked for addition
159 ? not tracked'''
159 ? not tracked'''
160 return self._map.get(key, ("?",))[0]
160 return self._map.get(key, ("?",))[0]
161
161
162 def __contains__(self, key):
162 def __contains__(self, key):
163 return key in self._map
163 return key in self._map
164
164
165 def __iter__(self):
165 def __iter__(self):
166 for x in util.sort(self._map):
166 for x in util.sort(self._map):
167 yield x
167 yield x
168
168
169 def parents(self):
169 def parents(self):
170 return self._pl
170 return self._pl
171
171
172 def branch(self):
172 def branch(self):
173 return self._branch
173 return self._branch
174
174
175 def setparents(self, p1, p2=nullid):
175 def setparents(self, p1, p2=nullid):
176 self._dirty = self._dirtypl = True
176 self._dirty = self._dirtypl = True
177 self._pl = p1, p2
177 self._pl = p1, p2
178
178
179 def setbranch(self, branch):
179 def setbranch(self, branch):
180 self._branch = branch
180 self._branch = branch
181 self._opener("branch", "w").write(branch + '\n')
181 self._opener("branch", "w").write(branch + '\n')
182
182
183 def _read(self):
183 def _read(self):
184 self._map = {}
184 self._map = {}
185 self._copymap = {}
185 self._copymap = {}
186 if not self._dirtypl:
186 if not self._dirtypl:
187 self._pl = [nullid, nullid]
187 self._pl = [nullid, nullid]
188 try:
188 try:
189 st = self._opener("dirstate").read()
189 st = self._opener("dirstate").read()
190 except IOError, err:
190 except IOError, err:
191 if err.errno != errno.ENOENT: raise
191 if err.errno != errno.ENOENT: raise
192 return
192 return
193 if not st:
193 if not st:
194 return
194 return
195
195
196 if not self._dirtypl:
196 if not self._dirtypl:
197 self._pl = [st[:20], st[20: 40]]
197 self._pl = [st[:20], st[20: 40]]
198
198
199 # deref fields so they will be local in loop
199 # deref fields so they will be local in loop
200 dmap = self._map
200 dmap = self._map
201 copymap = self._copymap
201 copymap = self._copymap
202 unpack = struct.unpack
202 unpack = struct.unpack
203 e_size = struct.calcsize(_format)
203 e_size = struct.calcsize(_format)
204 pos1 = 40
204 pos1 = 40
205 l = len(st)
205 l = len(st)
206
206
207 # the inner loop
207 # the inner loop
208 while pos1 < l:
208 while pos1 < l:
209 pos2 = pos1 + e_size
209 pos2 = pos1 + e_size
210 e = unpack(">cllll", st[pos1:pos2]) # a literal here is faster
210 e = unpack(">cllll", st[pos1:pos2]) # a literal here is faster
211 pos1 = pos2 + e[4]
211 pos1 = pos2 + e[4]
212 f = st[pos2:pos1]
212 f = st[pos2:pos1]
213 if '\0' in f:
213 if '\0' in f:
214 f, c = f.split('\0')
214 f, c = f.split('\0')
215 copymap[f] = c
215 copymap[f] = c
216 dmap[f] = e # we hold onto e[4] because making a subtuple is slow
216 dmap[f] = e # we hold onto e[4] because making a subtuple is slow
217
217
218 def invalidate(self):
218 def invalidate(self):
219 for a in "_map _copymap _foldmap _branch _pl _dirs _ignore".split():
219 for a in "_map _copymap _foldmap _branch _pl _dirs _ignore".split():
220 if a in self.__dict__:
220 if a in self.__dict__:
221 delattr(self, a)
221 delattr(self, a)
222 self._dirty = False
222 self._dirty = False
223
223
224 def copy(self, source, dest):
224 def copy(self, source, dest):
225 if source == dest:
225 if source == dest:
226 return
226 return
227 self._dirty = True
227 self._dirty = True
228 self._copymap[dest] = source
228 self._copymap[dest] = source
229
229
230 def copied(self, file):
230 def copied(self, file):
231 return self._copymap.get(file, None)
231 return self._copymap.get(file, None)
232
232
233 def copies(self):
233 def copies(self):
234 return self._copymap
234 return self._copymap
235
235
236 def _droppath(self, f):
236 def _droppath(self, f):
237 if self[f] not in "?r" and "_dirs" in self.__dict__:
237 if self[f] not in "?r" and "_dirs" in self.__dict__:
238 dirs = self._dirs
238 dirs = self._dirs
239 for base in _finddirs(f):
239 for base in _finddirs(f):
240 if dirs[base] == 1:
240 if dirs[base] == 1:
241 del dirs[base]
241 del dirs[base]
242 else:
242 else:
243 dirs[base] -= 1
243 dirs[base] -= 1
244
244
245 def _addpath(self, f, check=False):
245 def _addpath(self, f, check=False):
246 oldstate = self[f]
246 oldstate = self[f]
247 if check or oldstate == "r":
247 if check or oldstate == "r":
248 if '\r' in f or '\n' in f:
248 if '\r' in f or '\n' in f:
249 raise util.Abort(
249 raise util.Abort(
250 _("'\\n' and '\\r' disallowed in filenames: %r") % f)
250 _("'\\n' and '\\r' disallowed in filenames: %r") % f)
251 if f in self._dirs:
251 if f in self._dirs:
252 raise util.Abort(_('directory %r already in dirstate') % f)
252 raise util.Abort(_('directory %r already in dirstate') % f)
253 # shadows
253 # shadows
254 for d in _finddirs(f):
254 for d in _finddirs(f):
255 if d in self._dirs:
255 if d in self._dirs:
256 break
256 break
257 if d in self._map and self[d] != 'r':
257 if d in self._map and self[d] != 'r':
258 raise util.Abort(
258 raise util.Abort(
259 _('file %r in dirstate clashes with %r') % (d, f))
259 _('file %r in dirstate clashes with %r') % (d, f))
260 if oldstate in "?r" and "_dirs" in self.__dict__:
260 if oldstate in "?r" and "_dirs" in self.__dict__:
261 dirs = self._dirs
261 dirs = self._dirs
262 for base in _finddirs(f):
262 for base in _finddirs(f):
263 dirs[base] = dirs.get(base, 0) + 1
263 dirs[base] = dirs.get(base, 0) + 1
264
264
265 def normal(self, f):
265 def normal(self, f):
266 'mark a file normal and clean'
266 'mark a file normal and clean'
267 self._dirty = True
267 self._dirty = True
268 self._addpath(f)
268 self._addpath(f)
269 s = os.lstat(self._join(f))
269 s = os.lstat(self._join(f))
270 self._map[f] = ('n', s.st_mode, s.st_size, s.st_mtime, 0)
270 self._map[f] = ('n', s.st_mode, s.st_size, s.st_mtime, 0)
271 if f in self._copymap:
271 if f in self._copymap:
272 del self._copymap[f]
272 del self._copymap[f]
273
273
274 def normallookup(self, f):
274 def normallookup(self, f):
275 'mark a file normal, but possibly dirty'
275 'mark a file normal, but possibly dirty'
276 if self._pl[1] != nullid and f in self._map:
276 if self._pl[1] != nullid and f in self._map:
277 # if there is a merge going on and the file was either
277 # if there is a merge going on and the file was either
278 # in state 'm' or dirty before being removed, restore that state.
278 # in state 'm' or dirty before being removed, restore that state.
279 entry = self._map[f]
279 entry = self._map[f]
280 if entry[0] == 'r' and entry[2] in (-1, -2):
280 if entry[0] == 'r' and entry[2] in (-1, -2):
281 source = self._copymap.get(f)
281 source = self._copymap.get(f)
282 if entry[2] == -1:
282 if entry[2] == -1:
283 self.merge(f)
283 self.merge(f)
284 elif entry[2] == -2:
284 elif entry[2] == -2:
285 self.normaldirty(f)
285 self.normaldirty(f)
286 if source:
286 if source:
287 self.copy(source, f)
287 self.copy(source, f)
288 return
288 return
289 if entry[0] == 'm' or entry[0] == 'n' and entry[2] == -2:
289 if entry[0] == 'm' or entry[0] == 'n' and entry[2] == -2:
290 return
290 return
291 self._dirty = True
291 self._dirty = True
292 self._addpath(f)
292 self._addpath(f)
293 self._map[f] = ('n', 0, -1, -1, 0)
293 self._map[f] = ('n', 0, -1, -1, 0)
294 if f in self._copymap:
294 if f in self._copymap:
295 del self._copymap[f]
295 del self._copymap[f]
296
296
297 def normaldirty(self, f):
297 def normaldirty(self, f):
298 'mark a file normal, but dirty'
298 'mark a file normal, but dirty'
299 self._dirty = True
299 self._dirty = True
300 self._addpath(f)
300 self._addpath(f)
301 self._map[f] = ('n', 0, -2, -1, 0)
301 self._map[f] = ('n', 0, -2, -1, 0)
302 if f in self._copymap:
302 if f in self._copymap:
303 del self._copymap[f]
303 del self._copymap[f]
304
304
305 def add(self, f):
305 def add(self, f):
306 'mark a file added'
306 'mark a file added'
307 self._dirty = True
307 self._dirty = True
308 self._addpath(f, True)
308 self._addpath(f, True)
309 self._map[f] = ('a', 0, -1, -1, 0)
309 self._map[f] = ('a', 0, -1, -1, 0)
310 if f in self._copymap:
310 if f in self._copymap:
311 del self._copymap[f]
311 del self._copymap[f]
312
312
313 def remove(self, f):
313 def remove(self, f):
314 'mark a file removed'
314 'mark a file removed'
315 self._dirty = True
315 self._dirty = True
316 self._droppath(f)
316 self._droppath(f)
317 size = 0
317 size = 0
318 if self._pl[1] != nullid and f in self._map:
318 if self._pl[1] != nullid and f in self._map:
319 entry = self._map[f]
319 entry = self._map[f]
320 if entry[0] == 'm':
320 if entry[0] == 'm':
321 size = -1
321 size = -1
322 elif entry[0] == 'n' and entry[2] == -2:
322 elif entry[0] == 'n' and entry[2] == -2:
323 size = -2
323 size = -2
324 self._map[f] = ('r', 0, size, 0, 0)
324 self._map[f] = ('r', 0, size, 0, 0)
325 if size == 0 and f in self._copymap:
325 if size == 0 and f in self._copymap:
326 del self._copymap[f]
326 del self._copymap[f]
327
327
328 def merge(self, f):
328 def merge(self, f):
329 'mark a file merged'
329 'mark a file merged'
330 self._dirty = True
330 self._dirty = True
331 s = os.lstat(self._join(f))
331 s = os.lstat(self._join(f))
332 self._addpath(f)
332 self._addpath(f)
333 self._map[f] = ('m', s.st_mode, s.st_size, s.st_mtime, 0)
333 self._map[f] = ('m', s.st_mode, s.st_size, s.st_mtime, 0)
334 if f in self._copymap:
334 if f in self._copymap:
335 del self._copymap[f]
335 del self._copymap[f]
336
336
337 def forget(self, f):
337 def forget(self, f):
338 'forget a file'
338 'forget a file'
339 self._dirty = True
339 self._dirty = True
340 try:
340 try:
341 self._droppath(f)
341 self._droppath(f)
342 del self._map[f]
342 del self._map[f]
343 except KeyError:
343 except KeyError:
344 self._ui.warn(_("not in dirstate: %s\n") % f)
344 self._ui.warn(_("not in dirstate: %s\n") % f)
345
345
346 def _normalize(self, path):
346 def _normalize(self, path):
347 normpath = os.path.normcase(os.path.normpath(path))
347 normpath = os.path.normcase(os.path.normpath(path))
348 if normpath in self._foldmap:
348 if normpath in self._foldmap:
349 return self._foldmap[normpath]
349 return self._foldmap[normpath]
350 elif os.path.exists(path):
350 elif os.path.exists(path):
351 self._foldmap[normpath] = util.fspath(path, self._root)
351 self._foldmap[normpath] = util.fspath(path, self._root)
352 return self._foldmap[normpath]
352 return self._foldmap[normpath]
353 else:
353 else:
354 return path
354 return path
355
355
356 def clear(self):
356 def clear(self):
357 self._map = {}
357 self._map = {}
358 if "_dirs" in self.__dict__:
358 if "_dirs" in self.__dict__:
359 delattr(self, "_dirs");
359 delattr(self, "_dirs");
360 self._copymap = {}
360 self._copymap = {}
361 self._pl = [nullid, nullid]
361 self._pl = [nullid, nullid]
362 self._dirty = True
362 self._dirty = True
363
363
364 def rebuild(self, parent, files):
364 def rebuild(self, parent, files):
365 self.clear()
365 self.clear()
366 for f in files:
366 for f in files:
367 if 'x' in files.flags(f):
367 if 'x' in files.flags(f):
368 self._map[f] = ('n', 0777, -1, 0, 0)
368 self._map[f] = ('n', 0777, -1, 0, 0)
369 else:
369 else:
370 self._map[f] = ('n', 0666, -1, 0, 0)
370 self._map[f] = ('n', 0666, -1, 0, 0)
371 self._pl = (parent, nullid)
371 self._pl = (parent, nullid)
372 self._dirty = True
372 self._dirty = True
373
373
374 def write(self):
374 def write(self):
375 if not self._dirty:
375 if not self._dirty:
376 return
376 return
377 st = self._opener("dirstate", "w", atomictemp=True)
377 st = self._opener("dirstate", "w", atomictemp=True)
378
378
379 try:
379 try:
380 gran = int(self._ui.config('dirstate', 'granularity', 1))
380 gran = int(self._ui.config('dirstate', 'granularity', 1))
381 except ValueError:
381 except ValueError:
382 gran = 1
382 gran = 1
383 limit = sys.maxint
383 limit = sys.maxint
384 if gran > 0:
384 if gran > 0:
385 limit = util.fstat(st).st_mtime - gran
385 limit = util.fstat(st).st_mtime - gran
386
386
387 cs = cStringIO.StringIO()
387 cs = cStringIO.StringIO()
388 copymap = self._copymap
388 copymap = self._copymap
389 pack = struct.pack
389 pack = struct.pack
390 write = cs.write
390 write = cs.write
391 write("".join(self._pl))
391 write("".join(self._pl))
392 for f, e in self._map.iteritems():
392 for f, e in self._map.iteritems():
393 if f in copymap:
393 if f in copymap:
394 f = "%s\0%s" % (f, copymap[f])
394 f = "%s\0%s" % (f, copymap[f])
395 if e[3] > limit and e[0] == 'n':
395 if e[3] > limit and e[0] == 'n':
396 e = (e[0], 0, -1, -1, 0)
396 e = (e[0], 0, -1, -1, 0)
397 e = pack(_format, e[0], e[1], e[2], e[3], len(f))
397 e = pack(_format, e[0], e[1], e[2], e[3], len(f))
398 write(e)
398 write(e)
399 write(f)
399 write(f)
400 st.write(cs.getvalue())
400 st.write(cs.getvalue())
401 st.rename()
401 st.rename()
402 self._dirty = self._dirtypl = False
402 self._dirty = self._dirtypl = False
403
403
404 def _filter(self, files):
405 ret = {}
406 unknown = []
407
408 for x in files:
409 if x == '.':
410 return self._map.copy()
411 if x not in self._map:
412 unknown.append(x)
413 else:
414 ret[x] = self._map[x]
415
416 if not unknown:
417 return ret
418
419 b = util.sort(self._map)
420 blen = len(b)
421
422 for x in unknown:
423 bs = bisect.bisect(b, "%s%s" % (x, '/'))
424 while bs < blen:
425 s = b[bs]
426 if len(s) > len(x) and s.startswith(x):
427 ret[s] = self._map[s]
428 else:
429 break
430 bs += 1
431 return ret
432
433 def _supported(self, f, mode, verbose=False):
404 def _supported(self, f, mode, verbose=False):
434 if stat.S_ISREG(mode) or stat.S_ISLNK(mode):
405 if stat.S_ISREG(mode) or stat.S_ISLNK(mode):
435 return True
406 return True
436 if verbose:
407 if verbose:
437 kind = 'unknown'
408 kind = 'unknown'
438 if stat.S_ISCHR(mode): kind = _('character device')
409 if stat.S_ISCHR(mode): kind = _('character device')
439 elif stat.S_ISBLK(mode): kind = _('block device')
410 elif stat.S_ISBLK(mode): kind = _('block device')
440 elif stat.S_ISFIFO(mode): kind = _('fifo')
411 elif stat.S_ISFIFO(mode): kind = _('fifo')
441 elif stat.S_ISSOCK(mode): kind = _('socket')
412 elif stat.S_ISSOCK(mode): kind = _('socket')
442 elif stat.S_ISDIR(mode): kind = _('directory')
413 elif stat.S_ISDIR(mode): kind = _('directory')
443 self._ui.warn(_('%s: unsupported file type (type is %s)\n')
414 self._ui.warn(_('%s: unsupported file type (type is %s)\n')
444 % (self.pathto(f), kind))
415 % (self.pathto(f), kind))
445 return False
416 return False
446
417
447 def _dirignore(self, f):
418 def _dirignore(self, f):
448 if f == '.':
419 if f == '.':
449 return False
420 return False
450 if self._ignore(f):
421 if self._ignore(f):
451 return True
422 return True
452 for p in _finddirs(f):
423 for p in _finddirs(f):
453 if self._ignore(p):
424 if self._ignore(p):
454 return True
425 return True
455 return False
426 return False
456
427
457 def walk(self, match, unknown, ignored):
428 def walk(self, match, unknown, ignored):
458 '''
429 '''
459 walk recursively through the directory tree, finding all files
430 walk recursively through the directory tree, finding all files
460 matched by the match function
431 matched by the match function
461
432
462 results are yielded in a tuple (filename, stat), where stat
433 results are yielded in a tuple (filename, stat), where stat
463 and st is the stat result if the file was found in the directory.
434 and st is the stat result if the file was found in the directory.
464 '''
435 '''
465
436
466 def fwarn(f, msg):
437 def fwarn(f, msg):
467 self._ui.warn('%s: %s\n' % (self.pathto(ff), msg))
438 self._ui.warn('%s: %s\n' % (self.pathto(ff), msg))
468 return False
439 return False
469 badfn = fwarn
440 badfn = fwarn
470 if hasattr(match, 'bad'):
441 if hasattr(match, 'bad'):
471 badfn = match.bad
442 badfn = match.bad
472
443
473 # walk all files by default
444 files = util.unique(match.files())
474 files = match.files()
445 if not files or '.' in files:
475 if not files:
446 files = ['']
476 files = ['.']
447 dc = self._map
477 dc = self._map.copy()
478 else:
479 files = util.unique(files)
480 dc = self._filter(files)
481
448
482 def imatch(file_):
449 def imatch(file_):
483 if file_ not in dc and self._ignore(file_):
450 if file_ not in dc and self._ignore(file_):
484 return False
451 return False
485 return match(file_)
452 return match(file_)
486
453
487 # TODO: don't walk unknown directories if unknown and ignored are False
454 # TODO: don't walk unknown directories if unknown and ignored are False
488 ignore = self._ignore
455 ignore = self._ignore
489 dirignore = self._dirignore
456 dirignore = self._dirignore
490 if ignored:
457 if ignored:
491 imatch = match
458 imatch = match
492 ignore = util.never
459 ignore = util.never
493 dirignore = util.never
460 dirignore = util.never
494
461
495 # self._root may end with a path separator when self._root == '/'
462 # self._root may end with a path separator when self._root == '/'
496 common_prefix_len = len(self._root)
463 common_prefix_len = len(self._root)
497 if not util.endswithsep(self._root):
464 if not util.endswithsep(self._root):
498 common_prefix_len += 1
465 common_prefix_len += 1
499
466
500 normpath = util.normpath
467 normpath = util.normpath
501 listdir = osutil.listdir
468 listdir = osutil.listdir
502 lstat = os.lstat
469 lstat = os.lstat
503 bisect_left = bisect.bisect_left
470 bisect_left = bisect.bisect_left
504 isdir = os.path.isdir
471 isdir = os.path.isdir
505 pconvert = util.pconvert
472 pconvert = util.pconvert
506 join = os.path.join
473 join = os.path.join
507 s_isdir = stat.S_ISDIR
474 s_isdir = stat.S_ISDIR
508 supported = self._supported
475 supported = self._supported
509 _join = self._join
476 _join = self._join
510 known = {'.hg': 1}
477 known = {'.hg': 1}
511
478
512 # recursion free walker, faster than os.walk.
479 # recursion free walker, faster than os.walk.
513 def findfiles(s):
480 def findfiles(s):
514 work = [s]
481 work = [s]
515 wadd = work.append
482 wadd = work.append
516 found = []
483 found = []
517 add = found.append
484 add = found.append
518 if hasattr(match, 'dir'):
485 if hasattr(match, 'dir'):
519 match.dir(normpath(s[common_prefix_len:]))
486 match.dir(normpath(s[common_prefix_len:]))
520 while work:
487 while work:
521 top = work.pop()
488 top = work.pop()
522 entries = listdir(top, stat=True)
489 entries = listdir(top, stat=True)
523 # nd is the top of the repository dir tree
490 # nd is the top of the repository dir tree
524 nd = normpath(top[common_prefix_len:])
491 nd = normpath(top[common_prefix_len:])
525 if nd == '.':
492 if nd == '.':
526 nd = ''
493 nd = ''
527 else:
494 else:
528 # do not recurse into a repo contained in this
495 # do not recurse into a repo contained in this
529 # one. use bisect to find .hg directory so speed
496 # one. use bisect to find .hg directory so speed
530 # is good on big directory.
497 # is good on big directory.
531 names = [e[0] for e in entries]
498 names = [e[0] for e in entries]
532 hg = bisect_left(names, '.hg')
499 hg = bisect_left(names, '.hg')
533 if hg < len(names) and names[hg] == '.hg':
500 if hg < len(names) and names[hg] == '.hg':
534 if isdir(join(top, '.hg')):
501 if isdir(join(top, '.hg')):
535 continue
502 continue
536 for f, kind, st in entries:
503 for f, kind, st in entries:
537 np = pconvert(join(nd, f))
504 np = pconvert(join(nd, f))
538 nn = self.normalize(np)
505 nn = self.normalize(np)
539 if np in known:
506 if np in known:
540 continue
507 continue
541 known[nn] = 1
508 known[nn] = 1
542 p = join(top, f)
509 p = join(top, f)
543 # don't trip over symlinks
510 # don't trip over symlinks
544 if kind == stat.S_IFDIR:
511 if kind == stat.S_IFDIR:
545 if not ignore(np):
512 if not ignore(np):
546 wadd(p)
513 wadd(p)
547 if hasattr(match, 'dir'):
514 if hasattr(match, 'dir'):
548 match.dir(np)
515 match.dir(np)
549 if np in dc and match(np):
516 if np in dc and match(np):
550 add((nn, None))
517 add((nn, None))
551 elif imatch(np):
518 elif imatch(np):
552 if supported(np, st.st_mode):
519 if supported(np, st.st_mode):
553 add((nn, st))
520 add((nn, st))
554 elif np in dc:
521 elif np in dc:
555 add((nn, None))
522 add((nn, None))
556 return util.sort(found)
523 return util.sort(found)
557
524
558 # step one, find all files that match our criteria
525 # step one, find all files that match our criteria
559 for ff in util.sort(files):
526 for ff in util.sort(files):
560 nf = normpath(ff)
527 nf = normpath(ff)
561 nn = self.normalize(nf)
528 nn = self.normalize(nf)
562 f = _join(ff)
529 f = _join(ff)
563 try:
530 try:
564 st = lstat(f)
531 st = lstat(f)
565 except OSError, inst:
532 except OSError, inst:
566 found = False
533 found = False
567 for fn in dc:
534 for fn in dc:
568 if nf == fn or (fn.startswith(nf) and fn[len(nf)] == '/'):
535 if nf == fn or (fn.startswith(nf) and fn[len(nf)] == '/'):
569 found = True
536 found = True
570 break
537 break
571 if not found:
538 if not found:
572 if inst.errno != errno.ENOENT:
539 if inst.errno != errno.ENOENT:
573 fwarn(ff, inst.strerror)
540 fwarn(ff, inst.strerror)
574 elif badfn(ff, inst.strerror) and imatch(nf):
541 elif badfn(ff, inst.strerror) and imatch(nf):
575 yield ff, None
542 yield ff, None
576 continue
543 continue
577 if s_isdir(st.st_mode):
544 if s_isdir(st.st_mode):
578 if not dirignore(nf):
545 if not dirignore(nf):
579 for e in findfiles(f):
546 for e in findfiles(f):
580 yield e
547 yield e
581 else:
548 else:
582 if nn in known:
549 if nn in known:
583 continue
550 continue
584 known[nn] = 1
551 known[nn] = 1
585 if match(nf):
552 if supported(ff, st.st_mode, verbose=True):
586 if supported(ff, st.st_mode, verbose=True):
553 yield self.normalize(nf), st
587 yield nn, st
554 elif ff in dc:
588 elif ff in dc:
555 yield nf, None
589 yield nf, None
590
556
591 # step two run through anything left in the dc hash and yield
557 # step two run through anything left in the dc hash and yield
592 # if we haven't already seen it
558 # if we haven't already seen it
593 for f in util.sort(dc):
559 for f in util.sort(dc):
594 if f in known:
560 if f in known or not match(f):
595 continue
561 continue
596 known[f] = 1
562 known[f] = 1
597 if imatch(f):
563 try:
598 try:
564 st = lstat(_join(f))
599 st = lstat(_join(f))
565 if supported(f, st.st_mode):
600 if supported(f, st.st_mode):
566 yield f, st
601 yield f, st
567 continue
602 continue
568 except OSError, inst:
603 except OSError, inst:
569 if inst.errno not in (errno.ENOENT, errno.ENOTDIR):
604 if inst.errno not in (errno.ENOENT, errno.ENOTDIR):
570 raise
605 raise
571 yield f, None
606 yield f, None
607
572
608 def status(self, match, ignored, clean, unknown):
573 def status(self, match, ignored, clean, unknown):
609 listignored, listclean, listunknown = ignored, clean, unknown
574 listignored, listclean, listunknown = ignored, clean, unknown
610 lookup, modified, added, unknown, ignored = [], [], [], [], []
575 lookup, modified, added, unknown, ignored = [], [], [], [], []
611 removed, deleted, clean = [], [], []
576 removed, deleted, clean = [], [], []
612
577
613 _join = self._join
578 _join = self._join
614 lstat = os.lstat
579 lstat = os.lstat
615 cmap = self._copymap
580 cmap = self._copymap
616 dmap = self._map
581 dmap = self._map
617 ladd = lookup.append
582 ladd = lookup.append
618 madd = modified.append
583 madd = modified.append
619 aadd = added.append
584 aadd = added.append
620 uadd = unknown.append
585 uadd = unknown.append
621 iadd = ignored.append
586 iadd = ignored.append
622 radd = removed.append
587 radd = removed.append
623 dadd = deleted.append
588 dadd = deleted.append
624 cadd = clean.append
589 cadd = clean.append
625
590
626 for fn, st in self.walk(match, listunknown, listignored):
591 for fn, st in self.walk(match, listunknown, listignored):
627 if fn not in dmap:
592 if fn not in dmap:
628 if (listignored or match.exact(fn)) and self._dirignore(fn):
593 if (listignored or match.exact(fn)) and self._dirignore(fn):
629 if listignored:
594 if listignored:
630 iadd(fn)
595 iadd(fn)
631 elif listunknown:
596 elif listunknown:
632 uadd(fn)
597 uadd(fn)
633 continue
598 continue
634
599
635 state, mode, size, time, foo = dmap[fn]
600 state, mode, size, time, foo = dmap[fn]
636
601
637 if not st and state in "nma":
602 if not st and state in "nma":
638 dadd(fn)
603 dadd(fn)
639 elif state == 'n':
604 elif state == 'n':
640 if (size >= 0 and
605 if (size >= 0 and
641 (size != st.st_size
606 (size != st.st_size
642 or ((mode ^ st.st_mode) & 0100 and self._checkexec))
607 or ((mode ^ st.st_mode) & 0100 and self._checkexec))
643 or size == -2
608 or size == -2
644 or fn in self._copymap):
609 or fn in self._copymap):
645 madd(fn)
610 madd(fn)
646 elif time != int(st.st_mtime):
611 elif time != int(st.st_mtime):
647 ladd(fn)
612 ladd(fn)
648 elif listclean:
613 elif listclean:
649 cadd(fn)
614 cadd(fn)
650 elif state == 'm':
615 elif state == 'm':
651 madd(fn)
616 madd(fn)
652 elif state == 'a':
617 elif state == 'a':
653 aadd(fn)
618 aadd(fn)
654 elif state == 'r':
619 elif state == 'r':
655 radd(fn)
620 radd(fn)
656
621
657 return (lookup, modified, added, removed, deleted, unknown, ignored,
622 return (lookup, modified, added, removed, deleted, unknown, ignored,
658 clean)
623 clean)
General Comments 0
You need to be logged in to leave comments. Login now