##// END OF EJS Templates
dirstate: add copies function...
Matt Mackall -
r3154:b1f10d32 default
parent child Browse files
Show More
@@ -1,533 +1,536 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 self.copies = {}
26 self.copymap = {}
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 copies = self.copies
163 copymap = self.copymap
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 copies[f] = c
179 copymap[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 self.copies[dest] = source
196 self.copymap[dest] = source
197 197
198 198 def copied(self, file):
199 return self.copies.get(file, None)
199 return self.copymap.get(file, None)
200
201 def copies(self):
202 return self.copymap
200 203
201 204 def initdirs(self):
202 205 if self.dirs is None:
203 206 self.dirs = {}
204 207 for f in self.map:
205 208 self.updatedirs(f, 1)
206 209
207 210 def updatedirs(self, path, delta):
208 211 if self.dirs is not None:
209 212 for c in strutil.findall(path, '/'):
210 213 pc = path[:c]
211 214 self.dirs.setdefault(pc, 0)
212 215 self.dirs[pc] += delta
213 216
214 217 def checkshadows(self, files):
215 218 def prefixes(f):
216 219 for c in strutil.rfindall(f, '/'):
217 220 yield f[:c]
218 221 self.lazyread()
219 222 self.initdirs()
220 223 seendirs = {}
221 224 for f in files:
222 225 if self.dirs.get(f):
223 226 raise util.Abort(_('directory named %r already in dirstate') %
224 227 f)
225 228 for d in prefixes(f):
226 229 if d in seendirs:
227 230 break
228 231 if d in self.map:
229 232 raise util.Abort(_('file named %r already in dirstate') %
230 233 d)
231 234 seendirs[d] = True
232 235
233 236 def update(self, files, state, **kw):
234 237 ''' current states:
235 238 n normal
236 239 m needs merging
237 240 r marked for removal
238 241 a marked for addition'''
239 242
240 243 if not files: return
241 244 self.lazyread()
242 245 self.markdirty()
243 246 if state == "a":
244 247 self.initdirs()
245 248 self.checkshadows(files)
246 249 for f in files:
247 250 if state == "r":
248 251 self.map[f] = ('r', 0, 0, 0)
249 252 self.updatedirs(f, -1)
250 253 else:
251 254 if state == "a":
252 255 self.updatedirs(f, 1)
253 256 s = os.lstat(self.wjoin(f))
254 257 st_size = kw.get('st_size', s.st_size)
255 258 st_mtime = kw.get('st_mtime', s.st_mtime)
256 259 self.map[f] = (state, s.st_mode, st_size, st_mtime)
257 if self.copies.has_key(f):
258 del self.copies[f]
260 if self.copymap.has_key(f):
261 del self.copymap[f]
259 262
260 263 def forget(self, files):
261 264 if not files: return
262 265 self.lazyread()
263 266 self.markdirty()
264 267 self.initdirs()
265 268 for f in files:
266 269 try:
267 270 del self.map[f]
268 271 self.updatedirs(f, -1)
269 272 except KeyError:
270 273 self.ui.warn(_("not in dirstate: %s!\n") % f)
271 274 pass
272 275
273 276 def clear(self):
274 277 self.map = {}
275 self.copies = {}
278 self.copymap = {}
276 279 self.dirs = None
277 280 self.markdirty()
278 281
279 282 def rebuild(self, parent, files):
280 283 self.clear()
281 284 umask = os.umask(0)
282 285 os.umask(umask)
283 286 for f in files:
284 287 if files.execf(f):
285 288 self.map[f] = ('n', ~umask, -1, 0)
286 289 else:
287 290 self.map[f] = ('n', ~umask & 0666, -1, 0)
288 291 self.pl = (parent, nullid)
289 292 self.markdirty()
290 293
291 294 def write(self):
292 295 if not self.dirty:
293 296 return
294 297 st = self.opener("dirstate", "w", atomic=True)
295 298 st.write("".join(self.pl))
296 299 for f, e in self.map.items():
297 300 c = self.copied(f)
298 301 if c:
299 302 f = f + "\0" + c
300 303 e = struct.pack(self.format, e[0], e[1], e[2], e[3], len(f))
301 304 st.write(e + f)
302 305 self.dirty = 0
303 306
304 307 def filterfiles(self, files):
305 308 ret = {}
306 309 unknown = []
307 310
308 311 for x in files:
309 312 if x == '.':
310 313 return self.map.copy()
311 314 if x not in self.map:
312 315 unknown.append(x)
313 316 else:
314 317 ret[x] = self.map[x]
315 318
316 319 if not unknown:
317 320 return ret
318 321
319 322 b = self.map.keys()
320 323 b.sort()
321 324 blen = len(b)
322 325
323 326 for x in unknown:
324 327 bs = bisect.bisect(b, "%s%s" % (x, '/'))
325 328 while bs < blen:
326 329 s = b[bs]
327 330 if len(s) > len(x) and s.startswith(x):
328 331 ret[s] = self.map[s]
329 332 else:
330 333 break
331 334 bs += 1
332 335 return ret
333 336
334 337 def supported_type(self, f, st, verbose=False):
335 338 if stat.S_ISREG(st.st_mode):
336 339 return True
337 340 if verbose:
338 341 kind = 'unknown'
339 342 if stat.S_ISCHR(st.st_mode): kind = _('character device')
340 343 elif stat.S_ISBLK(st.st_mode): kind = _('block device')
341 344 elif stat.S_ISFIFO(st.st_mode): kind = _('fifo')
342 345 elif stat.S_ISLNK(st.st_mode): kind = _('symbolic link')
343 346 elif stat.S_ISSOCK(st.st_mode): kind = _('socket')
344 347 elif stat.S_ISDIR(st.st_mode): kind = _('directory')
345 348 self.ui.warn(_('%s: unsupported file type (type is %s)\n') % (
346 349 util.pathto(self.getcwd(), f),
347 350 kind))
348 351 return False
349 352
350 353 def statwalk(self, files=None, match=util.always, dc=None, ignored=False,
351 354 badmatch=None):
352 355 self.lazyread()
353 356
354 357 # walk all files by default
355 358 if not files:
356 359 files = [self.root]
357 360 if not dc:
358 361 dc = self.map.copy()
359 362 elif not dc:
360 363 dc = self.filterfiles(files)
361 364
362 365 def statmatch(file_, stat):
363 366 file_ = util.pconvert(file_)
364 367 if not ignored and file_ not in dc and self.ignore(file_):
365 368 return False
366 369 return match(file_)
367 370
368 371 return self.walkhelper(files=files, statmatch=statmatch, dc=dc,
369 372 badmatch=badmatch)
370 373
371 374 def walk(self, files=None, match=util.always, dc=None, badmatch=None):
372 375 # filter out the stat
373 376 for src, f, st in self.statwalk(files, match, dc, badmatch=badmatch):
374 377 yield src, f
375 378
376 379 # walk recursively through the directory tree, finding all files
377 380 # matched by the statmatch function
378 381 #
379 382 # results are yielded in a tuple (src, filename, st), where src
380 383 # is one of:
381 384 # 'f' the file was found in the directory tree
382 385 # 'm' the file was only in the dirstate and not in the tree
383 386 # and st is the stat result if the file was found in the directory.
384 387 #
385 388 # dc is an optional arg for the current dirstate. dc is not modified
386 389 # directly by this function, but might be modified by your statmatch call.
387 390 #
388 391 def walkhelper(self, files, statmatch, dc, badmatch=None):
389 392 # self.root may end with a path separator when self.root == '/'
390 393 common_prefix_len = len(self.root)
391 394 if not self.root.endswith('/'):
392 395 common_prefix_len += 1
393 396 # recursion free walker, faster than os.walk.
394 397 def findfiles(s):
395 398 work = [s]
396 399 while work:
397 400 top = work.pop()
398 401 names = os.listdir(top)
399 402 names.sort()
400 403 # nd is the top of the repository dir tree
401 404 nd = util.normpath(top[common_prefix_len:])
402 405 if nd == '.':
403 406 nd = ''
404 407 else:
405 408 # do not recurse into a repo contained in this
406 409 # one. use bisect to find .hg directory so speed
407 410 # is good on big directory.
408 411 hg = bisect.bisect_left(names, '.hg')
409 412 if hg < len(names) and names[hg] == '.hg':
410 413 if os.path.isdir(os.path.join(top, '.hg')):
411 414 continue
412 415 for f in names:
413 416 np = util.pconvert(os.path.join(nd, f))
414 417 if seen(np):
415 418 continue
416 419 p = os.path.join(top, f)
417 420 # don't trip over symlinks
418 421 st = os.lstat(p)
419 422 if stat.S_ISDIR(st.st_mode):
420 423 ds = os.path.join(nd, f +'/')
421 424 if statmatch(ds, st):
422 425 work.append(p)
423 426 if statmatch(np, st) and np in dc:
424 427 yield 'm', np, st
425 428 elif statmatch(np, st):
426 429 if self.supported_type(np, st):
427 430 yield 'f', np, st
428 431 elif np in dc:
429 432 yield 'm', np, st
430 433
431 434 known = {'.hg': 1}
432 435 def seen(fn):
433 436 if fn in known: return True
434 437 known[fn] = 1
435 438
436 439 # step one, find all files that match our criteria
437 440 files.sort()
438 441 for ff in util.unique(files):
439 442 f = self.wjoin(ff)
440 443 try:
441 444 st = os.lstat(f)
442 445 except OSError, inst:
443 446 nf = util.normpath(ff)
444 447 found = False
445 448 for fn in dc:
446 449 if nf == fn or (fn.startswith(nf) and fn[len(nf)] == '/'):
447 450 found = True
448 451 break
449 452 if not found:
450 453 if inst.errno != errno.ENOENT or not badmatch:
451 454 self.ui.warn('%s: %s\n' % (
452 455 util.pathto(self.getcwd(), ff),
453 456 inst.strerror))
454 457 elif badmatch and badmatch(ff) and statmatch(ff, None):
455 458 yield 'b', ff, None
456 459 continue
457 460 if stat.S_ISDIR(st.st_mode):
458 461 cmp1 = (lambda x, y: cmp(x[1], y[1]))
459 462 sorted_ = [ x for x in findfiles(f) ]
460 463 sorted_.sort(cmp1)
461 464 for e in sorted_:
462 465 yield e
463 466 else:
464 467 ff = util.normpath(ff)
465 468 if seen(ff):
466 469 continue
467 470 self.blockignore = True
468 471 if statmatch(ff, st):
469 472 if self.supported_type(ff, st, verbose=True):
470 473 yield 'f', ff, st
471 474 elif ff in dc:
472 475 yield 'm', ff, st
473 476 self.blockignore = False
474 477
475 478 # step two run through anything left in the dc hash and yield
476 479 # if we haven't already seen it
477 480 ks = dc.keys()
478 481 ks.sort()
479 482 for k in ks:
480 483 if not seen(k) and (statmatch(k, None)):
481 484 yield 'm', k, None
482 485
483 486 def status(self, files=None, match=util.always, list_ignored=False,
484 487 list_clean=False):
485 488 lookup, modified, added, unknown, ignored = [], [], [], [], []
486 489 removed, deleted, clean = [], [], []
487 490
488 491 for src, fn, st in self.statwalk(files, match, ignored=list_ignored):
489 492 try:
490 493 type_, mode, size, time = self[fn]
491 494 except KeyError:
492 495 if list_ignored and self.ignore(fn):
493 496 ignored.append(fn)
494 497 else:
495 498 unknown.append(fn)
496 499 continue
497 500 if src == 'm':
498 501 nonexistent = True
499 502 if not st:
500 503 try:
501 504 st = os.lstat(self.wjoin(fn))
502 505 except OSError, inst:
503 506 if inst.errno != errno.ENOENT:
504 507 raise
505 508 st = None
506 509 # We need to re-check that it is a valid file
507 510 if st and self.supported_type(fn, st):
508 511 nonexistent = False
509 512 # XXX: what to do with file no longer present in the fs
510 513 # who are not removed in the dirstate ?
511 514 if nonexistent and type_ in "nm":
512 515 deleted.append(fn)
513 516 continue
514 517 # check the common case first
515 518 if type_ == 'n':
516 519 if not st:
517 520 st = os.lstat(self.wjoin(fn))
518 521 if size >= 0 and (size != st.st_size
519 522 or (mode ^ st.st_mode) & 0100):
520 523 modified.append(fn)
521 524 elif time != int(st.st_mtime):
522 525 lookup.append(fn)
523 526 elif list_clean:
524 527 clean.append(fn)
525 528 elif type_ == 'm':
526 529 modified.append(fn)
527 530 elif type_ == 'a':
528 531 added.append(fn)
529 532 elif type_ == 'r':
530 533 removed.append(fn)
531 534
532 535 return (lookup, modified, added, removed, deleted, unknown, ignored,
533 536 clean)
General Comments 0
You need to be logged in to leave comments. Login now