##// END OF EJS Templates
dirstate.py: when comparing mtimes, use only the integer part....
Alexis S. L. Carvalho -
r2962:882e703e default
parent child Browse files
Show More
@@ -1,533 +1,533 b''
1 1 """
2 2 dirstate.py - working directory tracking for mercurial
3 3
4 4 Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
5 5
6 6 This software may be used and distributed according to the terms
7 7 of the GNU General Public License, incorporated herein by reference.
8 8 """
9 9
10 10 from node import *
11 11 from i18n import gettext as _
12 12 from demandload import *
13 13 demandload(globals(), "struct os time bisect stat strutil util re errno")
14 14
15 15 class dirstate(object):
16 16 format = ">cllll"
17 17
18 18 def __init__(self, opener, ui, root):
19 19 self.opener = opener
20 20 self.root = root
21 21 self.dirty = 0
22 22 self.ui = ui
23 23 self.map = None
24 24 self.pl = None
25 25 self.dirs = None
26 26 self.copies = {}
27 27 self.ignorefunc = None
28 28 self.blockignore = False
29 29
30 30 def wjoin(self, f):
31 31 return os.path.join(self.root, f)
32 32
33 33 def getcwd(self):
34 34 cwd = os.getcwd()
35 35 if cwd == self.root: return ''
36 36 return cwd[len(self.root) + 1:]
37 37
38 38 def hgignore(self):
39 39 '''return the contents of .hgignore files as a list of patterns.
40 40
41 41 the files parsed for patterns include:
42 42 .hgignore in the repository root
43 43 any additional files specified in the [ui] section of ~/.hgrc
44 44
45 45 trailing white space is dropped.
46 46 the escape character is backslash.
47 47 comments start with #.
48 48 empty lines are skipped.
49 49
50 50 lines can be of the following formats:
51 51
52 52 syntax: regexp # defaults following lines to non-rooted regexps
53 53 syntax: glob # defaults following lines to non-rooted globs
54 54 re:pattern # non-rooted regular expression
55 55 glob:pattern # non-rooted glob
56 56 pattern # pattern of the current default type'''
57 57 syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'}
58 58 def parselines(fp):
59 59 for line in fp:
60 60 escape = False
61 61 for i in xrange(len(line)):
62 62 if escape: escape = False
63 63 elif line[i] == '\\': escape = True
64 64 elif line[i] == '#': break
65 65 line = line[:i].rstrip()
66 66 if line: yield line
67 67 repoignore = self.wjoin('.hgignore')
68 68 files = [repoignore]
69 69 files.extend(self.ui.hgignorefiles())
70 70 pats = {}
71 71 for f in files:
72 72 try:
73 73 pats[f] = []
74 74 fp = open(f)
75 75 syntax = 'relre:'
76 76 for line in parselines(fp):
77 77 if line.startswith('syntax:'):
78 78 s = line[7:].strip()
79 79 try:
80 80 syntax = syntaxes[s]
81 81 except KeyError:
82 82 self.ui.warn(_("%s: ignoring invalid "
83 83 "syntax '%s'\n") % (f, s))
84 84 continue
85 85 pat = syntax + line
86 86 for s in syntaxes.values():
87 87 if line.startswith(s):
88 88 pat = line
89 89 break
90 90 pats[f].append(pat)
91 91 except IOError, inst:
92 92 if f != repoignore:
93 93 self.ui.warn(_("skipping unreadable ignore file"
94 94 " '%s': %s\n") % (f, inst.strerror))
95 95 return pats
96 96
97 97 def ignore(self, fn):
98 98 '''default match function used by dirstate and
99 99 localrepository. this honours the repository .hgignore file
100 100 and any other files specified in the [ui] section of .hgrc.'''
101 101 if self.blockignore:
102 102 return False
103 103 if not self.ignorefunc:
104 104 ignore = self.hgignore()
105 105 allpats = []
106 106 [allpats.extend(patlist) for patlist in ignore.values()]
107 107 if allpats:
108 108 try:
109 109 files, self.ignorefunc, anypats = (
110 110 util.matcher(self.root, inc=allpats, src='.hgignore'))
111 111 except util.Abort:
112 112 # Re-raise an exception where the src is the right file
113 113 for f, patlist in ignore.items():
114 114 files, self.ignorefunc, anypats = (
115 115 util.matcher(self.root, inc=patlist, src=f))
116 116 else:
117 117 self.ignorefunc = util.never
118 118 return self.ignorefunc(fn)
119 119
120 120 def __del__(self):
121 121 if self.dirty:
122 122 self.write()
123 123
124 124 def __getitem__(self, key):
125 125 try:
126 126 return self.map[key]
127 127 except TypeError:
128 128 self.lazyread()
129 129 return self[key]
130 130
131 131 def __contains__(self, key):
132 132 self.lazyread()
133 133 return key in self.map
134 134
135 135 def parents(self):
136 136 self.lazyread()
137 137 return self.pl
138 138
139 139 def markdirty(self):
140 140 if not self.dirty:
141 141 self.dirty = 1
142 142
143 143 def setparents(self, p1, p2=nullid):
144 144 self.lazyread()
145 145 self.markdirty()
146 146 self.pl = p1, p2
147 147
148 148 def state(self, key):
149 149 try:
150 150 return self[key][0]
151 151 except KeyError:
152 152 return "?"
153 153
154 154 def lazyread(self):
155 155 if self.map is None:
156 156 self.read()
157 157
158 158 def parse(self, st):
159 159 self.pl = [st[:20], st[20: 40]]
160 160
161 161 # deref fields so they will be local in loop
162 162 map = self.map
163 163 copies = self.copies
164 164 format = self.format
165 165 unpack = struct.unpack
166 166
167 167 pos = 40
168 168 e_size = struct.calcsize(format)
169 169
170 170 while pos < len(st):
171 171 newpos = pos + e_size
172 172 e = unpack(format, st[pos:newpos])
173 173 l = e[4]
174 174 pos = newpos
175 175 newpos = pos + l
176 176 f = st[pos:newpos]
177 177 if '\0' in f:
178 178 f, c = f.split('\0')
179 179 copies[f] = c
180 180 map[f] = e[:4]
181 181 pos = newpos
182 182
183 183 def read(self):
184 184 self.map = {}
185 185 self.pl = [nullid, nullid]
186 186 try:
187 187 st = self.opener("dirstate").read()
188 188 if st:
189 189 self.parse(st)
190 190 except IOError, err:
191 191 if err.errno != errno.ENOENT: raise
192 192
193 193 def copy(self, source, dest):
194 194 self.lazyread()
195 195 self.markdirty()
196 196 self.copies[dest] = source
197 197
198 198 def copied(self, file):
199 199 return self.copies.get(file, None)
200 200
201 201 def initdirs(self):
202 202 if self.dirs is None:
203 203 self.dirs = {}
204 204 for f in self.map:
205 205 self.updatedirs(f, 1)
206 206
207 207 def updatedirs(self, path, delta):
208 208 if self.dirs is not None:
209 209 for c in strutil.findall(path, '/'):
210 210 pc = path[:c]
211 211 self.dirs.setdefault(pc, 0)
212 212 self.dirs[pc] += delta
213 213
214 214 def checkshadows(self, files):
215 215 def prefixes(f):
216 216 for c in strutil.rfindall(f, '/'):
217 217 yield f[:c]
218 218 self.lazyread()
219 219 self.initdirs()
220 220 seendirs = {}
221 221 for f in files:
222 222 if self.dirs.get(f):
223 223 raise util.Abort(_('directory named %r already in dirstate') %
224 224 f)
225 225 for d in prefixes(f):
226 226 if d in seendirs:
227 227 break
228 228 if d in self.map:
229 229 raise util.Abort(_('file named %r already in dirstate') %
230 230 d)
231 231 seendirs[d] = True
232 232
233 233 def update(self, files, state, **kw):
234 234 ''' current states:
235 235 n normal
236 236 m needs merging
237 237 r marked for removal
238 238 a marked for addition'''
239 239
240 240 if not files: return
241 241 self.lazyread()
242 242 self.markdirty()
243 243 if state == "a":
244 244 self.initdirs()
245 245 self.checkshadows(files)
246 246 for f in files:
247 247 if state == "r":
248 248 self.map[f] = ('r', 0, 0, 0)
249 249 self.updatedirs(f, -1)
250 250 else:
251 251 if state == "a":
252 252 self.updatedirs(f, 1)
253 253 s = os.lstat(self.wjoin(f))
254 254 st_size = kw.get('st_size', s.st_size)
255 255 st_mtime = kw.get('st_mtime', s.st_mtime)
256 256 self.map[f] = (state, s.st_mode, st_size, st_mtime)
257 257 if self.copies.has_key(f):
258 258 del self.copies[f]
259 259
260 260 def forget(self, files):
261 261 if not files: return
262 262 self.lazyread()
263 263 self.markdirty()
264 264 self.initdirs()
265 265 for f in files:
266 266 try:
267 267 del self.map[f]
268 268 self.updatedirs(f, -1)
269 269 except KeyError:
270 270 self.ui.warn(_("not in dirstate: %s!\n") % f)
271 271 pass
272 272
273 273 def clear(self):
274 274 self.map = {}
275 275 self.copies = {}
276 276 self.dirs = None
277 277 self.markdirty()
278 278
279 279 def rebuild(self, parent, files):
280 280 self.clear()
281 281 umask = os.umask(0)
282 282 os.umask(umask)
283 283 for f in files:
284 284 if files.execf(f):
285 285 self.map[f] = ('n', ~umask, -1, 0)
286 286 else:
287 287 self.map[f] = ('n', ~umask & 0666, -1, 0)
288 288 self.pl = (parent, nullid)
289 289 self.markdirty()
290 290
291 291 def write(self):
292 292 if not self.dirty:
293 293 return
294 294 st = self.opener("dirstate", "w", atomic=True)
295 295 st.write("".join(self.pl))
296 296 for f, e in self.map.items():
297 297 c = self.copied(f)
298 298 if c:
299 299 f = f + "\0" + c
300 300 e = struct.pack(self.format, e[0], e[1], e[2], e[3], len(f))
301 301 st.write(e + f)
302 302 self.dirty = 0
303 303
304 304 def filterfiles(self, files):
305 305 ret = {}
306 306 unknown = []
307 307
308 308 for x in files:
309 309 if x == '.':
310 310 return self.map.copy()
311 311 if x not in self.map:
312 312 unknown.append(x)
313 313 else:
314 314 ret[x] = self.map[x]
315 315
316 316 if not unknown:
317 317 return ret
318 318
319 319 b = self.map.keys()
320 320 b.sort()
321 321 blen = len(b)
322 322
323 323 for x in unknown:
324 324 bs = bisect.bisect(b, "%s%s" % (x, '/'))
325 325 while bs < blen:
326 326 s = b[bs]
327 327 if len(s) > len(x) and s.startswith(x):
328 328 ret[s] = self.map[s]
329 329 else:
330 330 break
331 331 bs += 1
332 332 return ret
333 333
334 334 def supported_type(self, f, st, verbose=False):
335 335 if stat.S_ISREG(st.st_mode):
336 336 return True
337 337 if verbose:
338 338 kind = 'unknown'
339 339 if stat.S_ISCHR(st.st_mode): kind = _('character device')
340 340 elif stat.S_ISBLK(st.st_mode): kind = _('block device')
341 341 elif stat.S_ISFIFO(st.st_mode): kind = _('fifo')
342 342 elif stat.S_ISLNK(st.st_mode): kind = _('symbolic link')
343 343 elif stat.S_ISSOCK(st.st_mode): kind = _('socket')
344 344 elif stat.S_ISDIR(st.st_mode): kind = _('directory')
345 345 self.ui.warn(_('%s: unsupported file type (type is %s)\n') % (
346 346 util.pathto(self.getcwd(), f),
347 347 kind))
348 348 return False
349 349
350 350 def statwalk(self, files=None, match=util.always, dc=None, ignored=False,
351 351 badmatch=None):
352 352 self.lazyread()
353 353
354 354 # walk all files by default
355 355 if not files:
356 356 files = [self.root]
357 357 if not dc:
358 358 dc = self.map.copy()
359 359 elif not dc:
360 360 dc = self.filterfiles(files)
361 361
362 362 def statmatch(file_, stat):
363 363 file_ = util.pconvert(file_)
364 364 if not ignored and file_ not in dc and self.ignore(file_):
365 365 return False
366 366 return match(file_)
367 367
368 368 return self.walkhelper(files=files, statmatch=statmatch, dc=dc,
369 369 badmatch=badmatch)
370 370
371 371 def walk(self, files=None, match=util.always, dc=None, badmatch=None):
372 372 # filter out the stat
373 373 for src, f, st in self.statwalk(files, match, dc, badmatch=badmatch):
374 374 yield src, f
375 375
376 376 # walk recursively through the directory tree, finding all files
377 377 # matched by the statmatch function
378 378 #
379 379 # results are yielded in a tuple (src, filename, st), where src
380 380 # is one of:
381 381 # 'f' the file was found in the directory tree
382 382 # 'm' the file was only in the dirstate and not in the tree
383 383 # and st is the stat result if the file was found in the directory.
384 384 #
385 385 # dc is an optional arg for the current dirstate. dc is not modified
386 386 # directly by this function, but might be modified by your statmatch call.
387 387 #
388 388 def walkhelper(self, files, statmatch, dc, badmatch=None):
389 389 # self.root may end with a path separator when self.root == '/'
390 390 common_prefix_len = len(self.root)
391 391 if not self.root.endswith('/'):
392 392 common_prefix_len += 1
393 393 # recursion free walker, faster than os.walk.
394 394 def findfiles(s):
395 395 work = [s]
396 396 while work:
397 397 top = work.pop()
398 398 names = os.listdir(top)
399 399 names.sort()
400 400 # nd is the top of the repository dir tree
401 401 nd = util.normpath(top[common_prefix_len:])
402 402 if nd == '.':
403 403 nd = ''
404 404 else:
405 405 # do not recurse into a repo contained in this
406 406 # one. use bisect to find .hg directory so speed
407 407 # is good on big directory.
408 408 hg = bisect.bisect_left(names, '.hg')
409 409 if hg < len(names) and names[hg] == '.hg':
410 410 if os.path.isdir(os.path.join(top, '.hg')):
411 411 continue
412 412 for f in names:
413 413 np = util.pconvert(os.path.join(nd, f))
414 414 if seen(np):
415 415 continue
416 416 p = os.path.join(top, f)
417 417 # don't trip over symlinks
418 418 st = os.lstat(p)
419 419 if stat.S_ISDIR(st.st_mode):
420 420 ds = os.path.join(nd, f +'/')
421 421 if statmatch(ds, st):
422 422 work.append(p)
423 423 if statmatch(np, st) and np in dc:
424 424 yield 'm', np, st
425 425 elif statmatch(np, st):
426 426 if self.supported_type(np, st):
427 427 yield 'f', np, st
428 428 elif np in dc:
429 429 yield 'm', np, st
430 430
431 431 known = {'.hg': 1}
432 432 def seen(fn):
433 433 if fn in known: return True
434 434 known[fn] = 1
435 435
436 436 # step one, find all files that match our criteria
437 437 files.sort()
438 438 for ff in util.unique(files):
439 439 f = self.wjoin(ff)
440 440 try:
441 441 st = os.lstat(f)
442 442 except OSError, inst:
443 443 nf = util.normpath(ff)
444 444 found = False
445 445 for fn in dc:
446 446 if nf == fn or (fn.startswith(nf) and fn[len(nf)] == '/'):
447 447 found = True
448 448 break
449 449 if not found:
450 450 if inst.errno != errno.ENOENT or not badmatch:
451 451 self.ui.warn('%s: %s\n' % (
452 452 util.pathto(self.getcwd(), ff),
453 453 inst.strerror))
454 454 elif badmatch and badmatch(ff) and statmatch(ff, None):
455 455 yield 'b', ff, None
456 456 continue
457 457 if stat.S_ISDIR(st.st_mode):
458 458 cmp1 = (lambda x, y: cmp(x[1], y[1]))
459 459 sorted_ = [ x for x in findfiles(f) ]
460 460 sorted_.sort(cmp1)
461 461 for e in sorted_:
462 462 yield e
463 463 else:
464 464 ff = util.normpath(ff)
465 465 if seen(ff):
466 466 continue
467 467 self.blockignore = True
468 468 if statmatch(ff, st):
469 469 if self.supported_type(ff, st, verbose=True):
470 470 yield 'f', ff, st
471 471 elif ff in dc:
472 472 yield 'm', ff, st
473 473 self.blockignore = False
474 474
475 475 # step two run through anything left in the dc hash and yield
476 476 # if we haven't already seen it
477 477 ks = dc.keys()
478 478 ks.sort()
479 479 for k in ks:
480 480 if not seen(k) and (statmatch(k, None)):
481 481 yield 'm', k, None
482 482
483 483 def status(self, files=None, match=util.always, list_ignored=False,
484 484 list_clean=False):
485 485 lookup, modified, added, unknown, ignored = [], [], [], [], []
486 486 removed, deleted, clean = [], [], []
487 487
488 488 for src, fn, st in self.statwalk(files, match, ignored=list_ignored):
489 489 try:
490 490 type_, mode, size, time = self[fn]
491 491 except KeyError:
492 492 if list_ignored and self.ignore(fn):
493 493 ignored.append(fn)
494 494 else:
495 495 unknown.append(fn)
496 496 continue
497 497 if src == 'm':
498 498 nonexistent = True
499 499 if not st:
500 500 try:
501 501 st = os.lstat(self.wjoin(fn))
502 502 except OSError, inst:
503 503 if inst.errno != errno.ENOENT:
504 504 raise
505 505 st = None
506 506 # We need to re-check that it is a valid file
507 507 if st and self.supported_type(fn, st):
508 508 nonexistent = False
509 509 # XXX: what to do with file no longer present in the fs
510 510 # who are not removed in the dirstate ?
511 511 if nonexistent and type_ in "nm":
512 512 deleted.append(fn)
513 513 continue
514 514 # check the common case first
515 515 if type_ == 'n':
516 516 if not st:
517 517 st = os.lstat(self.wjoin(fn))
518 518 if size >= 0 and (size != st.st_size
519 519 or (mode ^ st.st_mode) & 0100):
520 520 modified.append(fn)
521 elif time != st.st_mtime:
521 elif time != int(st.st_mtime):
522 522 lookup.append(fn)
523 523 elif list_clean:
524 524 clean.append(fn)
525 525 elif type_ == 'm':
526 526 modified.append(fn)
527 527 elif type_ == 'a':
528 528 added.append(fn)
529 529 elif type_ == 'r':
530 530 removed.append(fn)
531 531
532 532 return (lookup, modified, added, removed, deleted, unknown, ignored,
533 533 clean)
General Comments 0
You need to be logged in to leave comments. Login now