##// END OF EJS Templates
dirstate: simplify the ambiguity clearing at write time...
marmoute -
r48869:84e7a86e default
parent child Browse files
Show More
@@ -1,1568 +1,1568 b''
1 # dirstate.py - working directory tracking for mercurial
1 # dirstate.py - working directory tracking for mercurial
2 #
2 #
3 # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com>
3 # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import collections
10 import collections
11 import contextlib
11 import contextlib
12 import errno
12 import errno
13 import os
13 import os
14 import stat
14 import stat
15
15
16 from .i18n import _
16 from .i18n import _
17 from .pycompat import delattr
17 from .pycompat import delattr
18
18
19 from hgdemandimport import tracing
19 from hgdemandimport import tracing
20
20
21 from . import (
21 from . import (
22 dirstatemap,
22 dirstatemap,
23 encoding,
23 encoding,
24 error,
24 error,
25 match as matchmod,
25 match as matchmod,
26 pathutil,
26 pathutil,
27 policy,
27 policy,
28 pycompat,
28 pycompat,
29 scmutil,
29 scmutil,
30 sparse,
30 sparse,
31 util,
31 util,
32 )
32 )
33
33
34 from .interfaces import (
34 from .interfaces import (
35 dirstate as intdirstate,
35 dirstate as intdirstate,
36 util as interfaceutil,
36 util as interfaceutil,
37 )
37 )
38
38
39 parsers = policy.importmod('parsers')
39 parsers = policy.importmod('parsers')
40 rustmod = policy.importrust('dirstate')
40 rustmod = policy.importrust('dirstate')
41
41
42 SUPPORTS_DIRSTATE_V2 = rustmod is not None
42 SUPPORTS_DIRSTATE_V2 = rustmod is not None
43
43
44 propertycache = util.propertycache
44 propertycache = util.propertycache
45 filecache = scmutil.filecache
45 filecache = scmutil.filecache
46 _rangemask = dirstatemap.rangemask
46 _rangemask = dirstatemap.rangemask
47
47
48 DirstateItem = dirstatemap.DirstateItem
48 DirstateItem = dirstatemap.DirstateItem
49
49
50
50
51 class repocache(filecache):
51 class repocache(filecache):
52 """filecache for files in .hg/"""
52 """filecache for files in .hg/"""
53
53
54 def join(self, obj, fname):
54 def join(self, obj, fname):
55 return obj._opener.join(fname)
55 return obj._opener.join(fname)
56
56
57
57
58 class rootcache(filecache):
58 class rootcache(filecache):
59 """filecache for files in the repository root"""
59 """filecache for files in the repository root"""
60
60
61 def join(self, obj, fname):
61 def join(self, obj, fname):
62 return obj._join(fname)
62 return obj._join(fname)
63
63
64
64
65 def _getfsnow(vfs):
65 def _getfsnow(vfs):
66 '''Get "now" timestamp on filesystem'''
66 '''Get "now" timestamp on filesystem'''
67 tmpfd, tmpname = vfs.mkstemp()
67 tmpfd, tmpname = vfs.mkstemp()
68 try:
68 try:
69 return os.fstat(tmpfd)[stat.ST_MTIME]
69 return os.fstat(tmpfd)[stat.ST_MTIME]
70 finally:
70 finally:
71 os.close(tmpfd)
71 os.close(tmpfd)
72 vfs.unlink(tmpname)
72 vfs.unlink(tmpname)
73
73
74
74
75 def requires_parents_change(func):
75 def requires_parents_change(func):
76 def wrap(self, *args, **kwargs):
76 def wrap(self, *args, **kwargs):
77 if not self.pendingparentchange():
77 if not self.pendingparentchange():
78 msg = 'calling `%s` outside of a parentchange context'
78 msg = 'calling `%s` outside of a parentchange context'
79 msg %= func.__name__
79 msg %= func.__name__
80 raise error.ProgrammingError(msg)
80 raise error.ProgrammingError(msg)
81 return func(self, *args, **kwargs)
81 return func(self, *args, **kwargs)
82
82
83 return wrap
83 return wrap
84
84
85
85
86 def requires_no_parents_change(func):
86 def requires_no_parents_change(func):
87 def wrap(self, *args, **kwargs):
87 def wrap(self, *args, **kwargs):
88 if self.pendingparentchange():
88 if self.pendingparentchange():
89 msg = 'calling `%s` inside of a parentchange context'
89 msg = 'calling `%s` inside of a parentchange context'
90 msg %= func.__name__
90 msg %= func.__name__
91 raise error.ProgrammingError(msg)
91 raise error.ProgrammingError(msg)
92 return func(self, *args, **kwargs)
92 return func(self, *args, **kwargs)
93
93
94 return wrap
94 return wrap
95
95
96
96
97 @interfaceutil.implementer(intdirstate.idirstate)
97 @interfaceutil.implementer(intdirstate.idirstate)
98 class dirstate(object):
98 class dirstate(object):
99 def __init__(
99 def __init__(
100 self,
100 self,
101 opener,
101 opener,
102 ui,
102 ui,
103 root,
103 root,
104 validate,
104 validate,
105 sparsematchfn,
105 sparsematchfn,
106 nodeconstants,
106 nodeconstants,
107 use_dirstate_v2,
107 use_dirstate_v2,
108 ):
108 ):
109 """Create a new dirstate object.
109 """Create a new dirstate object.
110
110
111 opener is an open()-like callable that can be used to open the
111 opener is an open()-like callable that can be used to open the
112 dirstate file; root is the root of the directory tracked by
112 dirstate file; root is the root of the directory tracked by
113 the dirstate.
113 the dirstate.
114 """
114 """
115 self._use_dirstate_v2 = use_dirstate_v2
115 self._use_dirstate_v2 = use_dirstate_v2
116 self._nodeconstants = nodeconstants
116 self._nodeconstants = nodeconstants
117 self._opener = opener
117 self._opener = opener
118 self._validate = validate
118 self._validate = validate
119 self._root = root
119 self._root = root
120 self._sparsematchfn = sparsematchfn
120 self._sparsematchfn = sparsematchfn
121 # ntpath.join(root, '') of Python 2.7.9 does not add sep if root is
121 # ntpath.join(root, '') of Python 2.7.9 does not add sep if root is
122 # UNC path pointing to root share (issue4557)
122 # UNC path pointing to root share (issue4557)
123 self._rootdir = pathutil.normasprefix(root)
123 self._rootdir = pathutil.normasprefix(root)
124 self._dirty = False
124 self._dirty = False
125 self._lastnormaltime = 0
125 self._lastnormaltime = 0
126 self._ui = ui
126 self._ui = ui
127 self._filecache = {}
127 self._filecache = {}
128 self._parentwriters = 0
128 self._parentwriters = 0
129 self._filename = b'dirstate'
129 self._filename = b'dirstate'
130 self._pendingfilename = b'%s.pending' % self._filename
130 self._pendingfilename = b'%s.pending' % self._filename
131 self._plchangecallbacks = {}
131 self._plchangecallbacks = {}
132 self._origpl = None
132 self._origpl = None
133 self._updatedfiles = set()
133 self._updatedfiles = set()
134 self._mapcls = dirstatemap.dirstatemap
134 self._mapcls = dirstatemap.dirstatemap
135 # Access and cache cwd early, so we don't access it for the first time
135 # Access and cache cwd early, so we don't access it for the first time
136 # after a working-copy update caused it to not exist (accessing it then
136 # after a working-copy update caused it to not exist (accessing it then
137 # raises an exception).
137 # raises an exception).
138 self._cwd
138 self._cwd
139
139
140 def prefetch_parents(self):
140 def prefetch_parents(self):
141 """make sure the parents are loaded
141 """make sure the parents are loaded
142
142
143 Used to avoid a race condition.
143 Used to avoid a race condition.
144 """
144 """
145 self._pl
145 self._pl
146
146
147 @contextlib.contextmanager
147 @contextlib.contextmanager
148 def parentchange(self):
148 def parentchange(self):
149 """Context manager for handling dirstate parents.
149 """Context manager for handling dirstate parents.
150
150
151 If an exception occurs in the scope of the context manager,
151 If an exception occurs in the scope of the context manager,
152 the incoherent dirstate won't be written when wlock is
152 the incoherent dirstate won't be written when wlock is
153 released.
153 released.
154 """
154 """
155 self._parentwriters += 1
155 self._parentwriters += 1
156 yield
156 yield
157 # Typically we want the "undo" step of a context manager in a
157 # Typically we want the "undo" step of a context manager in a
158 # finally block so it happens even when an exception
158 # finally block so it happens even when an exception
159 # occurs. In this case, however, we only want to decrement
159 # occurs. In this case, however, we only want to decrement
160 # parentwriters if the code in the with statement exits
160 # parentwriters if the code in the with statement exits
161 # normally, so we don't have a try/finally here on purpose.
161 # normally, so we don't have a try/finally here on purpose.
162 self._parentwriters -= 1
162 self._parentwriters -= 1
163
163
164 def pendingparentchange(self):
164 def pendingparentchange(self):
165 """Returns true if the dirstate is in the middle of a set of changes
165 """Returns true if the dirstate is in the middle of a set of changes
166 that modify the dirstate parent.
166 that modify the dirstate parent.
167 """
167 """
168 return self._parentwriters > 0
168 return self._parentwriters > 0
169
169
170 @propertycache
170 @propertycache
171 def _map(self):
171 def _map(self):
172 """Return the dirstate contents (see documentation for dirstatemap)."""
172 """Return the dirstate contents (see documentation for dirstatemap)."""
173 self._map = self._mapcls(
173 self._map = self._mapcls(
174 self._ui,
174 self._ui,
175 self._opener,
175 self._opener,
176 self._root,
176 self._root,
177 self._nodeconstants,
177 self._nodeconstants,
178 self._use_dirstate_v2,
178 self._use_dirstate_v2,
179 )
179 )
180 return self._map
180 return self._map
181
181
182 @property
182 @property
183 def _sparsematcher(self):
183 def _sparsematcher(self):
184 """The matcher for the sparse checkout.
184 """The matcher for the sparse checkout.
185
185
186 The working directory may not include every file from a manifest. The
186 The working directory may not include every file from a manifest. The
187 matcher obtained by this property will match a path if it is to be
187 matcher obtained by this property will match a path if it is to be
188 included in the working directory.
188 included in the working directory.
189 """
189 """
190 # TODO there is potential to cache this property. For now, the matcher
190 # TODO there is potential to cache this property. For now, the matcher
191 # is resolved on every access. (But the called function does use a
191 # is resolved on every access. (But the called function does use a
192 # cache to keep the lookup fast.)
192 # cache to keep the lookup fast.)
193 return self._sparsematchfn()
193 return self._sparsematchfn()
194
194
195 @repocache(b'branch')
195 @repocache(b'branch')
196 def _branch(self):
196 def _branch(self):
197 try:
197 try:
198 return self._opener.read(b"branch").strip() or b"default"
198 return self._opener.read(b"branch").strip() or b"default"
199 except IOError as inst:
199 except IOError as inst:
200 if inst.errno != errno.ENOENT:
200 if inst.errno != errno.ENOENT:
201 raise
201 raise
202 return b"default"
202 return b"default"
203
203
204 @property
204 @property
205 def _pl(self):
205 def _pl(self):
206 return self._map.parents()
206 return self._map.parents()
207
207
208 def hasdir(self, d):
208 def hasdir(self, d):
209 return self._map.hastrackeddir(d)
209 return self._map.hastrackeddir(d)
210
210
211 @rootcache(b'.hgignore')
211 @rootcache(b'.hgignore')
212 def _ignore(self):
212 def _ignore(self):
213 files = self._ignorefiles()
213 files = self._ignorefiles()
214 if not files:
214 if not files:
215 return matchmod.never()
215 return matchmod.never()
216
216
217 pats = [b'include:%s' % f for f in files]
217 pats = [b'include:%s' % f for f in files]
218 return matchmod.match(self._root, b'', [], pats, warn=self._ui.warn)
218 return matchmod.match(self._root, b'', [], pats, warn=self._ui.warn)
219
219
220 @propertycache
220 @propertycache
221 def _slash(self):
221 def _slash(self):
222 return self._ui.configbool(b'ui', b'slash') and pycompat.ossep != b'/'
222 return self._ui.configbool(b'ui', b'slash') and pycompat.ossep != b'/'
223
223
224 @propertycache
224 @propertycache
225 def _checklink(self):
225 def _checklink(self):
226 return util.checklink(self._root)
226 return util.checklink(self._root)
227
227
228 @propertycache
228 @propertycache
229 def _checkexec(self):
229 def _checkexec(self):
230 return bool(util.checkexec(self._root))
230 return bool(util.checkexec(self._root))
231
231
232 @propertycache
232 @propertycache
233 def _checkcase(self):
233 def _checkcase(self):
234 return not util.fscasesensitive(self._join(b'.hg'))
234 return not util.fscasesensitive(self._join(b'.hg'))
235
235
236 def _join(self, f):
236 def _join(self, f):
237 # much faster than os.path.join()
237 # much faster than os.path.join()
238 # it's safe because f is always a relative path
238 # it's safe because f is always a relative path
239 return self._rootdir + f
239 return self._rootdir + f
240
240
241 def flagfunc(self, buildfallback):
241 def flagfunc(self, buildfallback):
242 if self._checklink and self._checkexec:
242 if self._checklink and self._checkexec:
243
243
244 def f(x):
244 def f(x):
245 try:
245 try:
246 st = os.lstat(self._join(x))
246 st = os.lstat(self._join(x))
247 if util.statislink(st):
247 if util.statislink(st):
248 return b'l'
248 return b'l'
249 if util.statisexec(st):
249 if util.statisexec(st):
250 return b'x'
250 return b'x'
251 except OSError:
251 except OSError:
252 pass
252 pass
253 return b''
253 return b''
254
254
255 return f
255 return f
256
256
257 fallback = buildfallback()
257 fallback = buildfallback()
258 if self._checklink:
258 if self._checklink:
259
259
260 def f(x):
260 def f(x):
261 if os.path.islink(self._join(x)):
261 if os.path.islink(self._join(x)):
262 return b'l'
262 return b'l'
263 if b'x' in fallback(x):
263 if b'x' in fallback(x):
264 return b'x'
264 return b'x'
265 return b''
265 return b''
266
266
267 return f
267 return f
268 if self._checkexec:
268 if self._checkexec:
269
269
270 def f(x):
270 def f(x):
271 if b'l' in fallback(x):
271 if b'l' in fallback(x):
272 return b'l'
272 return b'l'
273 if util.isexec(self._join(x)):
273 if util.isexec(self._join(x)):
274 return b'x'
274 return b'x'
275 return b''
275 return b''
276
276
277 return f
277 return f
278 else:
278 else:
279 return fallback
279 return fallback
280
280
281 @propertycache
281 @propertycache
282 def _cwd(self):
282 def _cwd(self):
283 # internal config: ui.forcecwd
283 # internal config: ui.forcecwd
284 forcecwd = self._ui.config(b'ui', b'forcecwd')
284 forcecwd = self._ui.config(b'ui', b'forcecwd')
285 if forcecwd:
285 if forcecwd:
286 return forcecwd
286 return forcecwd
287 return encoding.getcwd()
287 return encoding.getcwd()
288
288
289 def getcwd(self):
289 def getcwd(self):
290 """Return the path from which a canonical path is calculated.
290 """Return the path from which a canonical path is calculated.
291
291
292 This path should be used to resolve file patterns or to convert
292 This path should be used to resolve file patterns or to convert
293 canonical paths back to file paths for display. It shouldn't be
293 canonical paths back to file paths for display. It shouldn't be
294 used to get real file paths. Use vfs functions instead.
294 used to get real file paths. Use vfs functions instead.
295 """
295 """
296 cwd = self._cwd
296 cwd = self._cwd
297 if cwd == self._root:
297 if cwd == self._root:
298 return b''
298 return b''
299 # self._root ends with a path separator if self._root is '/' or 'C:\'
299 # self._root ends with a path separator if self._root is '/' or 'C:\'
300 rootsep = self._root
300 rootsep = self._root
301 if not util.endswithsep(rootsep):
301 if not util.endswithsep(rootsep):
302 rootsep += pycompat.ossep
302 rootsep += pycompat.ossep
303 if cwd.startswith(rootsep):
303 if cwd.startswith(rootsep):
304 return cwd[len(rootsep) :]
304 return cwd[len(rootsep) :]
305 else:
305 else:
306 # we're outside the repo. return an absolute path.
306 # we're outside the repo. return an absolute path.
307 return cwd
307 return cwd
308
308
309 def pathto(self, f, cwd=None):
309 def pathto(self, f, cwd=None):
310 if cwd is None:
310 if cwd is None:
311 cwd = self.getcwd()
311 cwd = self.getcwd()
312 path = util.pathto(self._root, cwd, f)
312 path = util.pathto(self._root, cwd, f)
313 if self._slash:
313 if self._slash:
314 return util.pconvert(path)
314 return util.pconvert(path)
315 return path
315 return path
316
316
317 def __getitem__(self, key):
317 def __getitem__(self, key):
318 """Return the current state of key (a filename) in the dirstate.
318 """Return the current state of key (a filename) in the dirstate.
319
319
320 States are:
320 States are:
321 n normal
321 n normal
322 m needs merging
322 m needs merging
323 r marked for removal
323 r marked for removal
324 a marked for addition
324 a marked for addition
325 ? not tracked
325 ? not tracked
326
326
327 XXX The "state" is a bit obscure to be in the "public" API. we should
327 XXX The "state" is a bit obscure to be in the "public" API. we should
328 consider migrating all user of this to going through the dirstate entry
328 consider migrating all user of this to going through the dirstate entry
329 instead.
329 instead.
330 """
330 """
331 entry = self._map.get(key)
331 entry = self._map.get(key)
332 if entry is not None:
332 if entry is not None:
333 return entry.state
333 return entry.state
334 return b'?'
334 return b'?'
335
335
336 def __contains__(self, key):
336 def __contains__(self, key):
337 return key in self._map
337 return key in self._map
338
338
339 def __iter__(self):
339 def __iter__(self):
340 return iter(sorted(self._map))
340 return iter(sorted(self._map))
341
341
342 def items(self):
342 def items(self):
343 return pycompat.iteritems(self._map)
343 return pycompat.iteritems(self._map)
344
344
345 iteritems = items
345 iteritems = items
346
346
347 def parents(self):
347 def parents(self):
348 return [self._validate(p) for p in self._pl]
348 return [self._validate(p) for p in self._pl]
349
349
350 def p1(self):
350 def p1(self):
351 return self._validate(self._pl[0])
351 return self._validate(self._pl[0])
352
352
353 def p2(self):
353 def p2(self):
354 return self._validate(self._pl[1])
354 return self._validate(self._pl[1])
355
355
356 @property
356 @property
357 def in_merge(self):
357 def in_merge(self):
358 """True if a merge is in progress"""
358 """True if a merge is in progress"""
359 return self._pl[1] != self._nodeconstants.nullid
359 return self._pl[1] != self._nodeconstants.nullid
360
360
361 def branch(self):
361 def branch(self):
362 return encoding.tolocal(self._branch)
362 return encoding.tolocal(self._branch)
363
363
364 def setparents(self, p1, p2=None):
364 def setparents(self, p1, p2=None):
365 """Set dirstate parents to p1 and p2.
365 """Set dirstate parents to p1 and p2.
366
366
367 When moving from two parents to one, "merged" entries a
367 When moving from two parents to one, "merged" entries a
368 adjusted to normal and previous copy records discarded and
368 adjusted to normal and previous copy records discarded and
369 returned by the call.
369 returned by the call.
370
370
371 See localrepo.setparents()
371 See localrepo.setparents()
372 """
372 """
373 if p2 is None:
373 if p2 is None:
374 p2 = self._nodeconstants.nullid
374 p2 = self._nodeconstants.nullid
375 if self._parentwriters == 0:
375 if self._parentwriters == 0:
376 raise ValueError(
376 raise ValueError(
377 b"cannot set dirstate parent outside of "
377 b"cannot set dirstate parent outside of "
378 b"dirstate.parentchange context manager"
378 b"dirstate.parentchange context manager"
379 )
379 )
380
380
381 self._dirty = True
381 self._dirty = True
382 oldp2 = self._pl[1]
382 oldp2 = self._pl[1]
383 if self._origpl is None:
383 if self._origpl is None:
384 self._origpl = self._pl
384 self._origpl = self._pl
385 self._map.setparents(p1, p2)
385 self._map.setparents(p1, p2)
386 copies = {}
386 copies = {}
387 nullid = self._nodeconstants.nullid
387 nullid = self._nodeconstants.nullid
388 if oldp2 != nullid and p2 == nullid:
388 if oldp2 != nullid and p2 == nullid:
389 candidatefiles = self._map.non_normal_or_other_parent_paths()
389 candidatefiles = self._map.non_normal_or_other_parent_paths()
390
390
391 for f in candidatefiles:
391 for f in candidatefiles:
392 s = self._map.get(f)
392 s = self._map.get(f)
393 if s is None:
393 if s is None:
394 continue
394 continue
395
395
396 # Discard "merged" markers when moving away from a merge state
396 # Discard "merged" markers when moving away from a merge state
397 if s.merged:
397 if s.merged:
398 source = self._map.copymap.get(f)
398 source = self._map.copymap.get(f)
399 if source:
399 if source:
400 copies[f] = source
400 copies[f] = source
401 self._map.reset_state(
401 self._map.reset_state(
402 f,
402 f,
403 wc_tracked=True,
403 wc_tracked=True,
404 p1_tracked=True,
404 p1_tracked=True,
405 possibly_dirty=True,
405 possibly_dirty=True,
406 )
406 )
407 # Also fix up otherparent markers
407 # Also fix up otherparent markers
408 elif s.from_p2:
408 elif s.from_p2:
409 source = self._map.copymap.get(f)
409 source = self._map.copymap.get(f)
410 if source:
410 if source:
411 copies[f] = source
411 copies[f] = source
412 self._check_new_tracked_filename(f)
412 self._check_new_tracked_filename(f)
413 self._updatedfiles.add(f)
413 self._updatedfiles.add(f)
414 self._map.reset_state(
414 self._map.reset_state(
415 f,
415 f,
416 p1_tracked=False,
416 p1_tracked=False,
417 wc_tracked=True,
417 wc_tracked=True,
418 )
418 )
419 return copies
419 return copies
420
420
421 def setbranch(self, branch):
421 def setbranch(self, branch):
422 self.__class__._branch.set(self, encoding.fromlocal(branch))
422 self.__class__._branch.set(self, encoding.fromlocal(branch))
423 f = self._opener(b'branch', b'w', atomictemp=True, checkambig=True)
423 f = self._opener(b'branch', b'w', atomictemp=True, checkambig=True)
424 try:
424 try:
425 f.write(self._branch + b'\n')
425 f.write(self._branch + b'\n')
426 f.close()
426 f.close()
427
427
428 # make sure filecache has the correct stat info for _branch after
428 # make sure filecache has the correct stat info for _branch after
429 # replacing the underlying file
429 # replacing the underlying file
430 ce = self._filecache[b'_branch']
430 ce = self._filecache[b'_branch']
431 if ce:
431 if ce:
432 ce.refresh()
432 ce.refresh()
433 except: # re-raises
433 except: # re-raises
434 f.discard()
434 f.discard()
435 raise
435 raise
436
436
437 def invalidate(self):
437 def invalidate(self):
438 """Causes the next access to reread the dirstate.
438 """Causes the next access to reread the dirstate.
439
439
440 This is different from localrepo.invalidatedirstate() because it always
440 This is different from localrepo.invalidatedirstate() because it always
441 rereads the dirstate. Use localrepo.invalidatedirstate() if you want to
441 rereads the dirstate. Use localrepo.invalidatedirstate() if you want to
442 check whether the dirstate has changed before rereading it."""
442 check whether the dirstate has changed before rereading it."""
443
443
444 for a in ("_map", "_branch", "_ignore"):
444 for a in ("_map", "_branch", "_ignore"):
445 if a in self.__dict__:
445 if a in self.__dict__:
446 delattr(self, a)
446 delattr(self, a)
447 self._lastnormaltime = 0
447 self._lastnormaltime = 0
448 self._dirty = False
448 self._dirty = False
449 self._updatedfiles.clear()
449 self._updatedfiles.clear()
450 self._parentwriters = 0
450 self._parentwriters = 0
451 self._origpl = None
451 self._origpl = None
452
452
453 def copy(self, source, dest):
453 def copy(self, source, dest):
454 """Mark dest as a copy of source. Unmark dest if source is None."""
454 """Mark dest as a copy of source. Unmark dest if source is None."""
455 if source == dest:
455 if source == dest:
456 return
456 return
457 self._dirty = True
457 self._dirty = True
458 if source is not None:
458 if source is not None:
459 self._map.copymap[dest] = source
459 self._map.copymap[dest] = source
460 self._updatedfiles.add(source)
460 self._updatedfiles.add(source)
461 self._updatedfiles.add(dest)
461 self._updatedfiles.add(dest)
462 elif self._map.copymap.pop(dest, None):
462 elif self._map.copymap.pop(dest, None):
463 self._updatedfiles.add(dest)
463 self._updatedfiles.add(dest)
464
464
465 def copied(self, file):
465 def copied(self, file):
466 return self._map.copymap.get(file, None)
466 return self._map.copymap.get(file, None)
467
467
468 def copies(self):
468 def copies(self):
469 return self._map.copymap
469 return self._map.copymap
470
470
471 @requires_no_parents_change
471 @requires_no_parents_change
472 def set_tracked(self, filename):
472 def set_tracked(self, filename):
473 """a "public" method for generic code to mark a file as tracked
473 """a "public" method for generic code to mark a file as tracked
474
474
475 This function is to be called outside of "update/merge" case. For
475 This function is to be called outside of "update/merge" case. For
476 example by a command like `hg add X`.
476 example by a command like `hg add X`.
477
477
478 return True the file was previously untracked, False otherwise.
478 return True the file was previously untracked, False otherwise.
479 """
479 """
480 self._dirty = True
480 self._dirty = True
481 self._updatedfiles.add(filename)
481 self._updatedfiles.add(filename)
482 entry = self._map.get(filename)
482 entry = self._map.get(filename)
483 if entry is None or not entry.tracked:
483 if entry is None or not entry.tracked:
484 self._check_new_tracked_filename(filename)
484 self._check_new_tracked_filename(filename)
485 return self._map.set_tracked(filename)
485 return self._map.set_tracked(filename)
486
486
487 @requires_no_parents_change
487 @requires_no_parents_change
488 def set_untracked(self, filename):
488 def set_untracked(self, filename):
489 """a "public" method for generic code to mark a file as untracked
489 """a "public" method for generic code to mark a file as untracked
490
490
491 This function is to be called outside of "update/merge" case. For
491 This function is to be called outside of "update/merge" case. For
492 example by a command like `hg remove X`.
492 example by a command like `hg remove X`.
493
493
494 return True the file was previously tracked, False otherwise.
494 return True the file was previously tracked, False otherwise.
495 """
495 """
496 ret = self._map.set_untracked(filename)
496 ret = self._map.set_untracked(filename)
497 if ret:
497 if ret:
498 self._dirty = True
498 self._dirty = True
499 self._updatedfiles.add(filename)
499 self._updatedfiles.add(filename)
500 return ret
500 return ret
501
501
502 @requires_no_parents_change
502 @requires_no_parents_change
503 def set_clean(self, filename, parentfiledata=None):
503 def set_clean(self, filename, parentfiledata=None):
504 """record that the current state of the file on disk is known to be clean"""
504 """record that the current state of the file on disk is known to be clean"""
505 self._dirty = True
505 self._dirty = True
506 self._updatedfiles.add(filename)
506 self._updatedfiles.add(filename)
507 if parentfiledata:
507 if parentfiledata:
508 (mode, size, mtime) = parentfiledata
508 (mode, size, mtime) = parentfiledata
509 else:
509 else:
510 (mode, size, mtime) = self._get_filedata(filename)
510 (mode, size, mtime) = self._get_filedata(filename)
511 if not self._map[filename].tracked:
511 if not self._map[filename].tracked:
512 self._check_new_tracked_filename(filename)
512 self._check_new_tracked_filename(filename)
513 self._map.set_clean(filename, mode, size, mtime)
513 self._map.set_clean(filename, mode, size, mtime)
514 if mtime > self._lastnormaltime:
514 if mtime > self._lastnormaltime:
515 # Remember the most recent modification timeslot for status(),
515 # Remember the most recent modification timeslot for status(),
516 # to make sure we won't miss future size-preserving file content
516 # to make sure we won't miss future size-preserving file content
517 # modifications that happen within the same timeslot.
517 # modifications that happen within the same timeslot.
518 self._lastnormaltime = mtime
518 self._lastnormaltime = mtime
519
519
520 @requires_no_parents_change
520 @requires_no_parents_change
521 def set_possibly_dirty(self, filename):
521 def set_possibly_dirty(self, filename):
522 """record that the current state of the file on disk is unknown"""
522 """record that the current state of the file on disk is unknown"""
523 self._dirty = True
523 self._dirty = True
524 self._updatedfiles.add(filename)
524 self._updatedfiles.add(filename)
525 self._map.set_possibly_dirty(filename)
525 self._map.set_possibly_dirty(filename)
526
526
527 @requires_parents_change
527 @requires_parents_change
528 def update_file_p1(
528 def update_file_p1(
529 self,
529 self,
530 filename,
530 filename,
531 p1_tracked,
531 p1_tracked,
532 ):
532 ):
533 """Set a file as tracked in the parent (or not)
533 """Set a file as tracked in the parent (or not)
534
534
535 This is to be called when adjust the dirstate to a new parent after an history
535 This is to be called when adjust the dirstate to a new parent after an history
536 rewriting operation.
536 rewriting operation.
537
537
538 It should not be called during a merge (p2 != nullid) and only within
538 It should not be called during a merge (p2 != nullid) and only within
539 a `with dirstate.parentchange():` context.
539 a `with dirstate.parentchange():` context.
540 """
540 """
541 if self.in_merge:
541 if self.in_merge:
542 msg = b'update_file_reference should not be called when merging'
542 msg = b'update_file_reference should not be called when merging'
543 raise error.ProgrammingError(msg)
543 raise error.ProgrammingError(msg)
544 entry = self._map.get(filename)
544 entry = self._map.get(filename)
545 if entry is None:
545 if entry is None:
546 wc_tracked = False
546 wc_tracked = False
547 else:
547 else:
548 wc_tracked = entry.tracked
548 wc_tracked = entry.tracked
549 possibly_dirty = False
549 possibly_dirty = False
550 if p1_tracked and wc_tracked:
550 if p1_tracked and wc_tracked:
551 # the underlying reference might have changed, we will have to
551 # the underlying reference might have changed, we will have to
552 # check it.
552 # check it.
553 possibly_dirty = True
553 possibly_dirty = True
554 elif not (p1_tracked or wc_tracked):
554 elif not (p1_tracked or wc_tracked):
555 # the file is no longer relevant to anyone
555 # the file is no longer relevant to anyone
556 if self._map.get(filename) is not None:
556 if self._map.get(filename) is not None:
557 self._map.reset_state(filename)
557 self._map.reset_state(filename)
558 self._dirty = True
558 self._dirty = True
559 self._updatedfiles.add(filename)
559 self._updatedfiles.add(filename)
560 elif (not p1_tracked) and wc_tracked:
560 elif (not p1_tracked) and wc_tracked:
561 if entry is not None and entry.added:
561 if entry is not None and entry.added:
562 return # avoid dropping copy information (maybe?)
562 return # avoid dropping copy information (maybe?)
563 elif p1_tracked and not wc_tracked:
563 elif p1_tracked and not wc_tracked:
564 pass
564 pass
565 else:
565 else:
566 assert False, 'unreachable'
566 assert False, 'unreachable'
567
567
568 # this mean we are doing call for file we do not really care about the
568 # this mean we are doing call for file we do not really care about the
569 # data (eg: added or removed), however this should be a minor overhead
569 # data (eg: added or removed), however this should be a minor overhead
570 # compared to the overall update process calling this.
570 # compared to the overall update process calling this.
571 parentfiledata = None
571 parentfiledata = None
572 if wc_tracked:
572 if wc_tracked:
573 parentfiledata = self._get_filedata(filename)
573 parentfiledata = self._get_filedata(filename)
574
574
575 self._updatedfiles.add(filename)
575 self._updatedfiles.add(filename)
576 self._map.reset_state(
576 self._map.reset_state(
577 filename,
577 filename,
578 wc_tracked,
578 wc_tracked,
579 p1_tracked,
579 p1_tracked,
580 possibly_dirty=possibly_dirty,
580 possibly_dirty=possibly_dirty,
581 parentfiledata=parentfiledata,
581 parentfiledata=parentfiledata,
582 )
582 )
583 if (
583 if (
584 parentfiledata is not None
584 parentfiledata is not None
585 and parentfiledata[2] > self._lastnormaltime
585 and parentfiledata[2] > self._lastnormaltime
586 ):
586 ):
587 # Remember the most recent modification timeslot for status(),
587 # Remember the most recent modification timeslot for status(),
588 # to make sure we won't miss future size-preserving file content
588 # to make sure we won't miss future size-preserving file content
589 # modifications that happen within the same timeslot.
589 # modifications that happen within the same timeslot.
590 self._lastnormaltime = parentfiledata[2]
590 self._lastnormaltime = parentfiledata[2]
591
591
592 @requires_parents_change
592 @requires_parents_change
593 def update_file(
593 def update_file(
594 self,
594 self,
595 filename,
595 filename,
596 wc_tracked,
596 wc_tracked,
597 p1_tracked,
597 p1_tracked,
598 p2_tracked=False,
598 p2_tracked=False,
599 merged=False,
599 merged=False,
600 clean_p1=False,
600 clean_p1=False,
601 clean_p2=False,
601 clean_p2=False,
602 possibly_dirty=False,
602 possibly_dirty=False,
603 parentfiledata=None,
603 parentfiledata=None,
604 ):
604 ):
605 """update the information about a file in the dirstate
605 """update the information about a file in the dirstate
606
606
607 This is to be called when the direstates parent changes to keep track
607 This is to be called when the direstates parent changes to keep track
608 of what is the file situation in regards to the working copy and its parent.
608 of what is the file situation in regards to the working copy and its parent.
609
609
610 This function must be called within a `dirstate.parentchange` context.
610 This function must be called within a `dirstate.parentchange` context.
611
611
612 note: the API is at an early stage and we might need to adjust it
612 note: the API is at an early stage and we might need to adjust it
613 depending of what information ends up being relevant and useful to
613 depending of what information ends up being relevant and useful to
614 other processing.
614 other processing.
615 """
615 """
616 if merged and (clean_p1 or clean_p2):
616 if merged and (clean_p1 or clean_p2):
617 msg = b'`merged` argument incompatible with `clean_p1`/`clean_p2`'
617 msg = b'`merged` argument incompatible with `clean_p1`/`clean_p2`'
618 raise error.ProgrammingError(msg)
618 raise error.ProgrammingError(msg)
619
619
620 # note: I do not think we need to double check name clash here since we
620 # note: I do not think we need to double check name clash here since we
621 # are in a update/merge case that should already have taken care of
621 # are in a update/merge case that should already have taken care of
622 # this. The test agrees
622 # this. The test agrees
623
623
624 self._dirty = True
624 self._dirty = True
625 self._updatedfiles.add(filename)
625 self._updatedfiles.add(filename)
626
626
627 need_parent_file_data = (
627 need_parent_file_data = (
628 not (possibly_dirty or clean_p2 or merged)
628 not (possibly_dirty or clean_p2 or merged)
629 and wc_tracked
629 and wc_tracked
630 and p1_tracked
630 and p1_tracked
631 )
631 )
632
632
633 # this mean we are doing call for file we do not really care about the
633 # this mean we are doing call for file we do not really care about the
634 # data (eg: added or removed), however this should be a minor overhead
634 # data (eg: added or removed), however this should be a minor overhead
635 # compared to the overall update process calling this.
635 # compared to the overall update process calling this.
636 if need_parent_file_data:
636 if need_parent_file_data:
637 if parentfiledata is None:
637 if parentfiledata is None:
638 parentfiledata = self._get_filedata(filename)
638 parentfiledata = self._get_filedata(filename)
639 mtime = parentfiledata[2]
639 mtime = parentfiledata[2]
640
640
641 if mtime > self._lastnormaltime:
641 if mtime > self._lastnormaltime:
642 # Remember the most recent modification timeslot for
642 # Remember the most recent modification timeslot for
643 # status(), to make sure we won't miss future
643 # status(), to make sure we won't miss future
644 # size-preserving file content modifications that happen
644 # size-preserving file content modifications that happen
645 # within the same timeslot.
645 # within the same timeslot.
646 self._lastnormaltime = mtime
646 self._lastnormaltime = mtime
647
647
648 self._map.reset_state(
648 self._map.reset_state(
649 filename,
649 filename,
650 wc_tracked,
650 wc_tracked,
651 p1_tracked,
651 p1_tracked,
652 p2_tracked=p2_tracked,
652 p2_tracked=p2_tracked,
653 merged=merged,
653 merged=merged,
654 clean_p1=clean_p1,
654 clean_p1=clean_p1,
655 clean_p2=clean_p2,
655 clean_p2=clean_p2,
656 possibly_dirty=possibly_dirty,
656 possibly_dirty=possibly_dirty,
657 parentfiledata=parentfiledata,
657 parentfiledata=parentfiledata,
658 )
658 )
659 if (
659 if (
660 parentfiledata is not None
660 parentfiledata is not None
661 and parentfiledata[2] > self._lastnormaltime
661 and parentfiledata[2] > self._lastnormaltime
662 ):
662 ):
663 # Remember the most recent modification timeslot for status(),
663 # Remember the most recent modification timeslot for status(),
664 # to make sure we won't miss future size-preserving file content
664 # to make sure we won't miss future size-preserving file content
665 # modifications that happen within the same timeslot.
665 # modifications that happen within the same timeslot.
666 self._lastnormaltime = parentfiledata[2]
666 self._lastnormaltime = parentfiledata[2]
667
667
668 def _check_new_tracked_filename(self, filename):
668 def _check_new_tracked_filename(self, filename):
669 scmutil.checkfilename(filename)
669 scmutil.checkfilename(filename)
670 if self._map.hastrackeddir(filename):
670 if self._map.hastrackeddir(filename):
671 msg = _(b'directory %r already in dirstate')
671 msg = _(b'directory %r already in dirstate')
672 msg %= pycompat.bytestr(filename)
672 msg %= pycompat.bytestr(filename)
673 raise error.Abort(msg)
673 raise error.Abort(msg)
674 # shadows
674 # shadows
675 for d in pathutil.finddirs(filename):
675 for d in pathutil.finddirs(filename):
676 if self._map.hastrackeddir(d):
676 if self._map.hastrackeddir(d):
677 break
677 break
678 entry = self._map.get(d)
678 entry = self._map.get(d)
679 if entry is not None and not entry.removed:
679 if entry is not None and not entry.removed:
680 msg = _(b'file %r in dirstate clashes with %r')
680 msg = _(b'file %r in dirstate clashes with %r')
681 msg %= (pycompat.bytestr(d), pycompat.bytestr(filename))
681 msg %= (pycompat.bytestr(d), pycompat.bytestr(filename))
682 raise error.Abort(msg)
682 raise error.Abort(msg)
683
683
684 def _get_filedata(self, filename):
684 def _get_filedata(self, filename):
685 """returns"""
685 """returns"""
686 s = os.lstat(self._join(filename))
686 s = os.lstat(self._join(filename))
687 mode = s.st_mode
687 mode = s.st_mode
688 size = s.st_size
688 size = s.st_size
689 mtime = s[stat.ST_MTIME]
689 mtime = s[stat.ST_MTIME]
690 return (mode, size, mtime)
690 return (mode, size, mtime)
691
691
692 def _discoverpath(self, path, normed, ignoremissing, exists, storemap):
692 def _discoverpath(self, path, normed, ignoremissing, exists, storemap):
693 if exists is None:
693 if exists is None:
694 exists = os.path.lexists(os.path.join(self._root, path))
694 exists = os.path.lexists(os.path.join(self._root, path))
695 if not exists:
695 if not exists:
696 # Maybe a path component exists
696 # Maybe a path component exists
697 if not ignoremissing and b'/' in path:
697 if not ignoremissing and b'/' in path:
698 d, f = path.rsplit(b'/', 1)
698 d, f = path.rsplit(b'/', 1)
699 d = self._normalize(d, False, ignoremissing, None)
699 d = self._normalize(d, False, ignoremissing, None)
700 folded = d + b"/" + f
700 folded = d + b"/" + f
701 else:
701 else:
702 # No path components, preserve original case
702 # No path components, preserve original case
703 folded = path
703 folded = path
704 else:
704 else:
705 # recursively normalize leading directory components
705 # recursively normalize leading directory components
706 # against dirstate
706 # against dirstate
707 if b'/' in normed:
707 if b'/' in normed:
708 d, f = normed.rsplit(b'/', 1)
708 d, f = normed.rsplit(b'/', 1)
709 d = self._normalize(d, False, ignoremissing, True)
709 d = self._normalize(d, False, ignoremissing, True)
710 r = self._root + b"/" + d
710 r = self._root + b"/" + d
711 folded = d + b"/" + util.fspath(f, r)
711 folded = d + b"/" + util.fspath(f, r)
712 else:
712 else:
713 folded = util.fspath(normed, self._root)
713 folded = util.fspath(normed, self._root)
714 storemap[normed] = folded
714 storemap[normed] = folded
715
715
716 return folded
716 return folded
717
717
718 def _normalizefile(self, path, isknown, ignoremissing=False, exists=None):
718 def _normalizefile(self, path, isknown, ignoremissing=False, exists=None):
719 normed = util.normcase(path)
719 normed = util.normcase(path)
720 folded = self._map.filefoldmap.get(normed, None)
720 folded = self._map.filefoldmap.get(normed, None)
721 if folded is None:
721 if folded is None:
722 if isknown:
722 if isknown:
723 folded = path
723 folded = path
724 else:
724 else:
725 folded = self._discoverpath(
725 folded = self._discoverpath(
726 path, normed, ignoremissing, exists, self._map.filefoldmap
726 path, normed, ignoremissing, exists, self._map.filefoldmap
727 )
727 )
728 return folded
728 return folded
729
729
730 def _normalize(self, path, isknown, ignoremissing=False, exists=None):
730 def _normalize(self, path, isknown, ignoremissing=False, exists=None):
731 normed = util.normcase(path)
731 normed = util.normcase(path)
732 folded = self._map.filefoldmap.get(normed, None)
732 folded = self._map.filefoldmap.get(normed, None)
733 if folded is None:
733 if folded is None:
734 folded = self._map.dirfoldmap.get(normed, None)
734 folded = self._map.dirfoldmap.get(normed, None)
735 if folded is None:
735 if folded is None:
736 if isknown:
736 if isknown:
737 folded = path
737 folded = path
738 else:
738 else:
739 # store discovered result in dirfoldmap so that future
739 # store discovered result in dirfoldmap so that future
740 # normalizefile calls don't start matching directories
740 # normalizefile calls don't start matching directories
741 folded = self._discoverpath(
741 folded = self._discoverpath(
742 path, normed, ignoremissing, exists, self._map.dirfoldmap
742 path, normed, ignoremissing, exists, self._map.dirfoldmap
743 )
743 )
744 return folded
744 return folded
745
745
746 def normalize(self, path, isknown=False, ignoremissing=False):
746 def normalize(self, path, isknown=False, ignoremissing=False):
747 """
747 """
748 normalize the case of a pathname when on a casefolding filesystem
748 normalize the case of a pathname when on a casefolding filesystem
749
749
750 isknown specifies whether the filename came from walking the
750 isknown specifies whether the filename came from walking the
751 disk, to avoid extra filesystem access.
751 disk, to avoid extra filesystem access.
752
752
753 If ignoremissing is True, missing path are returned
753 If ignoremissing is True, missing path are returned
754 unchanged. Otherwise, we try harder to normalize possibly
754 unchanged. Otherwise, we try harder to normalize possibly
755 existing path components.
755 existing path components.
756
756
757 The normalized case is determined based on the following precedence:
757 The normalized case is determined based on the following precedence:
758
758
759 - version of name already stored in the dirstate
759 - version of name already stored in the dirstate
760 - version of name stored on disk
760 - version of name stored on disk
761 - version provided via command arguments
761 - version provided via command arguments
762 """
762 """
763
763
764 if self._checkcase:
764 if self._checkcase:
765 return self._normalize(path, isknown, ignoremissing)
765 return self._normalize(path, isknown, ignoremissing)
766 return path
766 return path
767
767
768 def clear(self):
768 def clear(self):
769 self._map.clear()
769 self._map.clear()
770 self._lastnormaltime = 0
770 self._lastnormaltime = 0
771 self._updatedfiles.clear()
771 self._updatedfiles.clear()
772 self._dirty = True
772 self._dirty = True
773
773
774 def rebuild(self, parent, allfiles, changedfiles=None):
774 def rebuild(self, parent, allfiles, changedfiles=None):
775 if changedfiles is None:
775 if changedfiles is None:
776 # Rebuild entire dirstate
776 # Rebuild entire dirstate
777 to_lookup = allfiles
777 to_lookup = allfiles
778 to_drop = []
778 to_drop = []
779 lastnormaltime = self._lastnormaltime
779 lastnormaltime = self._lastnormaltime
780 self.clear()
780 self.clear()
781 self._lastnormaltime = lastnormaltime
781 self._lastnormaltime = lastnormaltime
782 elif len(changedfiles) < 10:
782 elif len(changedfiles) < 10:
783 # Avoid turning allfiles into a set, which can be expensive if it's
783 # Avoid turning allfiles into a set, which can be expensive if it's
784 # large.
784 # large.
785 to_lookup = []
785 to_lookup = []
786 to_drop = []
786 to_drop = []
787 for f in changedfiles:
787 for f in changedfiles:
788 if f in allfiles:
788 if f in allfiles:
789 to_lookup.append(f)
789 to_lookup.append(f)
790 else:
790 else:
791 to_drop.append(f)
791 to_drop.append(f)
792 else:
792 else:
793 changedfilesset = set(changedfiles)
793 changedfilesset = set(changedfiles)
794 to_lookup = changedfilesset & set(allfiles)
794 to_lookup = changedfilesset & set(allfiles)
795 to_drop = changedfilesset - to_lookup
795 to_drop = changedfilesset - to_lookup
796
796
797 if self._origpl is None:
797 if self._origpl is None:
798 self._origpl = self._pl
798 self._origpl = self._pl
799 self._map.setparents(parent, self._nodeconstants.nullid)
799 self._map.setparents(parent, self._nodeconstants.nullid)
800
800
801 for f in to_lookup:
801 for f in to_lookup:
802
802
803 if self.in_merge:
803 if self.in_merge:
804 self.set_tracked(f)
804 self.set_tracked(f)
805 else:
805 else:
806 self._map.reset_state(
806 self._map.reset_state(
807 f,
807 f,
808 wc_tracked=True,
808 wc_tracked=True,
809 p1_tracked=True,
809 p1_tracked=True,
810 possibly_dirty=True,
810 possibly_dirty=True,
811 )
811 )
812 self._updatedfiles.add(f)
812 self._updatedfiles.add(f)
813 for f in to_drop:
813 for f in to_drop:
814 self._map.reset_state(f)
814 self._map.reset_state(f)
815 self._updatedfiles.add(f)
815 self._updatedfiles.add(f)
816
816
817 self._dirty = True
817 self._dirty = True
818
818
819 def identity(self):
819 def identity(self):
820 """Return identity of dirstate itself to detect changing in storage
820 """Return identity of dirstate itself to detect changing in storage
821
821
822 If identity of previous dirstate is equal to this, writing
822 If identity of previous dirstate is equal to this, writing
823 changes based on the former dirstate out can keep consistency.
823 changes based on the former dirstate out can keep consistency.
824 """
824 """
825 return self._map.identity
825 return self._map.identity
826
826
827 def write(self, tr):
827 def write(self, tr):
828 if not self._dirty:
828 if not self._dirty:
829 return
829 return
830
830
831 filename = self._filename
831 filename = self._filename
832 if tr:
832 if tr:
833 # 'dirstate.write()' is not only for writing in-memory
833 # 'dirstate.write()' is not only for writing in-memory
834 # changes out, but also for dropping ambiguous timestamp.
834 # changes out, but also for dropping ambiguous timestamp.
835 # delayed writing re-raise "ambiguous timestamp issue".
835 # delayed writing re-raise "ambiguous timestamp issue".
836 # See also the wiki page below for detail:
836 # See also the wiki page below for detail:
837 # https://www.mercurial-scm.org/wiki/DirstateTransactionPlan
837 # https://www.mercurial-scm.org/wiki/DirstateTransactionPlan
838
838
839 # emulate dropping timestamp in 'parsers.pack_dirstate'
839 # record when mtime start to be ambiguous
840 now = _getfsnow(self._opener)
840 now = _getfsnow(self._opener)
841 self._map.clearambiguoustimes(self._updatedfiles, now)
842
841
843 # emulate that all 'dirstate.normal' results are written out
842 # emulate that all 'dirstate.normal' results are written out
844 self._lastnormaltime = 0
845 self._updatedfiles.clear()
843 self._updatedfiles.clear()
846
844
847 # delay writing in-memory changes out
845 # delay writing in-memory changes out
848 tr.addfilegenerator(
846 tr.addfilegenerator(
849 b'dirstate',
847 b'dirstate',
850 (self._filename,),
848 (self._filename,),
851 lambda f: self._writedirstate(tr, f),
849 lambda f: self._writedirstate(tr, f, now=now),
852 location=b'plain',
850 location=b'plain',
853 )
851 )
854 return
852 return
855
853
856 st = self._opener(filename, b"w", atomictemp=True, checkambig=True)
854 st = self._opener(filename, b"w", atomictemp=True, checkambig=True)
857 self._writedirstate(tr, st)
855 self._writedirstate(tr, st)
858
856
859 def addparentchangecallback(self, category, callback):
857 def addparentchangecallback(self, category, callback):
860 """add a callback to be called when the wd parents are changed
858 """add a callback to be called when the wd parents are changed
861
859
862 Callback will be called with the following arguments:
860 Callback will be called with the following arguments:
863 dirstate, (oldp1, oldp2), (newp1, newp2)
861 dirstate, (oldp1, oldp2), (newp1, newp2)
864
862
865 Category is a unique identifier to allow overwriting an old callback
863 Category is a unique identifier to allow overwriting an old callback
866 with a newer callback.
864 with a newer callback.
867 """
865 """
868 self._plchangecallbacks[category] = callback
866 self._plchangecallbacks[category] = callback
869
867
870 def _writedirstate(self, tr, st):
868 def _writedirstate(self, tr, st, now=None):
871 # notify callbacks about parents change
869 # notify callbacks about parents change
872 if self._origpl is not None and self._origpl != self._pl:
870 if self._origpl is not None and self._origpl != self._pl:
873 for c, callback in sorted(
871 for c, callback in sorted(
874 pycompat.iteritems(self._plchangecallbacks)
872 pycompat.iteritems(self._plchangecallbacks)
875 ):
873 ):
876 callback(self, self._origpl, self._pl)
874 callback(self, self._origpl, self._pl)
877 self._origpl = None
875 self._origpl = None
878 # use the modification time of the newly created temporary file as the
876
879 # filesystem's notion of 'now'
877 if now is None:
880 now = util.fstat(st)[stat.ST_MTIME] & _rangemask
878 # use the modification time of the newly created temporary file as the
879 # filesystem's notion of 'now'
880 now = util.fstat(st)[stat.ST_MTIME] & _rangemask
881
881
882 # enough 'delaywrite' prevents 'pack_dirstate' from dropping
882 # enough 'delaywrite' prevents 'pack_dirstate' from dropping
883 # timestamp of each entries in dirstate, because of 'now > mtime'
883 # timestamp of each entries in dirstate, because of 'now > mtime'
884 delaywrite = self._ui.configint(b'debug', b'dirstate.delaywrite')
884 delaywrite = self._ui.configint(b'debug', b'dirstate.delaywrite')
885 if delaywrite > 0:
885 if delaywrite > 0:
886 # do we have any files to delay for?
886 # do we have any files to delay for?
887 for f, e in pycompat.iteritems(self._map):
887 for f, e in pycompat.iteritems(self._map):
888 if e.need_delay(now):
888 if e.need_delay(now):
889 import time # to avoid useless import
889 import time # to avoid useless import
890
890
891 # rather than sleep n seconds, sleep until the next
891 # rather than sleep n seconds, sleep until the next
892 # multiple of n seconds
892 # multiple of n seconds
893 clock = time.time()
893 clock = time.time()
894 start = int(clock) - (int(clock) % delaywrite)
894 start = int(clock) - (int(clock) % delaywrite)
895 end = start + delaywrite
895 end = start + delaywrite
896 time.sleep(end - clock)
896 time.sleep(end - clock)
897 now = end # trust our estimate that the end is near now
897 now = end # trust our estimate that the end is near now
898 break
898 break
899
899
900 self._map.write(tr, st, now)
900 self._map.write(tr, st, now)
901 self._lastnormaltime = 0
901 self._lastnormaltime = 0
902 self._dirty = False
902 self._dirty = False
903
903
904 def _dirignore(self, f):
904 def _dirignore(self, f):
905 if self._ignore(f):
905 if self._ignore(f):
906 return True
906 return True
907 for p in pathutil.finddirs(f):
907 for p in pathutil.finddirs(f):
908 if self._ignore(p):
908 if self._ignore(p):
909 return True
909 return True
910 return False
910 return False
911
911
912 def _ignorefiles(self):
912 def _ignorefiles(self):
913 files = []
913 files = []
914 if os.path.exists(self._join(b'.hgignore')):
914 if os.path.exists(self._join(b'.hgignore')):
915 files.append(self._join(b'.hgignore'))
915 files.append(self._join(b'.hgignore'))
916 for name, path in self._ui.configitems(b"ui"):
916 for name, path in self._ui.configitems(b"ui"):
917 if name == b'ignore' or name.startswith(b'ignore.'):
917 if name == b'ignore' or name.startswith(b'ignore.'):
918 # we need to use os.path.join here rather than self._join
918 # we need to use os.path.join here rather than self._join
919 # because path is arbitrary and user-specified
919 # because path is arbitrary and user-specified
920 files.append(os.path.join(self._rootdir, util.expandpath(path)))
920 files.append(os.path.join(self._rootdir, util.expandpath(path)))
921 return files
921 return files
922
922
923 def _ignorefileandline(self, f):
923 def _ignorefileandline(self, f):
924 files = collections.deque(self._ignorefiles())
924 files = collections.deque(self._ignorefiles())
925 visited = set()
925 visited = set()
926 while files:
926 while files:
927 i = files.popleft()
927 i = files.popleft()
928 patterns = matchmod.readpatternfile(
928 patterns = matchmod.readpatternfile(
929 i, self._ui.warn, sourceinfo=True
929 i, self._ui.warn, sourceinfo=True
930 )
930 )
931 for pattern, lineno, line in patterns:
931 for pattern, lineno, line in patterns:
932 kind, p = matchmod._patsplit(pattern, b'glob')
932 kind, p = matchmod._patsplit(pattern, b'glob')
933 if kind == b"subinclude":
933 if kind == b"subinclude":
934 if p not in visited:
934 if p not in visited:
935 files.append(p)
935 files.append(p)
936 continue
936 continue
937 m = matchmod.match(
937 m = matchmod.match(
938 self._root, b'', [], [pattern], warn=self._ui.warn
938 self._root, b'', [], [pattern], warn=self._ui.warn
939 )
939 )
940 if m(f):
940 if m(f):
941 return (i, lineno, line)
941 return (i, lineno, line)
942 visited.add(i)
942 visited.add(i)
943 return (None, -1, b"")
943 return (None, -1, b"")
944
944
945 def _walkexplicit(self, match, subrepos):
945 def _walkexplicit(self, match, subrepos):
946 """Get stat data about the files explicitly specified by match.
946 """Get stat data about the files explicitly specified by match.
947
947
948 Return a triple (results, dirsfound, dirsnotfound).
948 Return a triple (results, dirsfound, dirsnotfound).
949 - results is a mapping from filename to stat result. It also contains
949 - results is a mapping from filename to stat result. It also contains
950 listings mapping subrepos and .hg to None.
950 listings mapping subrepos and .hg to None.
951 - dirsfound is a list of files found to be directories.
951 - dirsfound is a list of files found to be directories.
952 - dirsnotfound is a list of files that the dirstate thinks are
952 - dirsnotfound is a list of files that the dirstate thinks are
953 directories and that were not found."""
953 directories and that were not found."""
954
954
955 def badtype(mode):
955 def badtype(mode):
956 kind = _(b'unknown')
956 kind = _(b'unknown')
957 if stat.S_ISCHR(mode):
957 if stat.S_ISCHR(mode):
958 kind = _(b'character device')
958 kind = _(b'character device')
959 elif stat.S_ISBLK(mode):
959 elif stat.S_ISBLK(mode):
960 kind = _(b'block device')
960 kind = _(b'block device')
961 elif stat.S_ISFIFO(mode):
961 elif stat.S_ISFIFO(mode):
962 kind = _(b'fifo')
962 kind = _(b'fifo')
963 elif stat.S_ISSOCK(mode):
963 elif stat.S_ISSOCK(mode):
964 kind = _(b'socket')
964 kind = _(b'socket')
965 elif stat.S_ISDIR(mode):
965 elif stat.S_ISDIR(mode):
966 kind = _(b'directory')
966 kind = _(b'directory')
967 return _(b'unsupported file type (type is %s)') % kind
967 return _(b'unsupported file type (type is %s)') % kind
968
968
969 badfn = match.bad
969 badfn = match.bad
970 dmap = self._map
970 dmap = self._map
971 lstat = os.lstat
971 lstat = os.lstat
972 getkind = stat.S_IFMT
972 getkind = stat.S_IFMT
973 dirkind = stat.S_IFDIR
973 dirkind = stat.S_IFDIR
974 regkind = stat.S_IFREG
974 regkind = stat.S_IFREG
975 lnkkind = stat.S_IFLNK
975 lnkkind = stat.S_IFLNK
976 join = self._join
976 join = self._join
977 dirsfound = []
977 dirsfound = []
978 foundadd = dirsfound.append
978 foundadd = dirsfound.append
979 dirsnotfound = []
979 dirsnotfound = []
980 notfoundadd = dirsnotfound.append
980 notfoundadd = dirsnotfound.append
981
981
982 if not match.isexact() and self._checkcase:
982 if not match.isexact() and self._checkcase:
983 normalize = self._normalize
983 normalize = self._normalize
984 else:
984 else:
985 normalize = None
985 normalize = None
986
986
987 files = sorted(match.files())
987 files = sorted(match.files())
988 subrepos.sort()
988 subrepos.sort()
989 i, j = 0, 0
989 i, j = 0, 0
990 while i < len(files) and j < len(subrepos):
990 while i < len(files) and j < len(subrepos):
991 subpath = subrepos[j] + b"/"
991 subpath = subrepos[j] + b"/"
992 if files[i] < subpath:
992 if files[i] < subpath:
993 i += 1
993 i += 1
994 continue
994 continue
995 while i < len(files) and files[i].startswith(subpath):
995 while i < len(files) and files[i].startswith(subpath):
996 del files[i]
996 del files[i]
997 j += 1
997 j += 1
998
998
999 if not files or b'' in files:
999 if not files or b'' in files:
1000 files = [b'']
1000 files = [b'']
1001 # constructing the foldmap is expensive, so don't do it for the
1001 # constructing the foldmap is expensive, so don't do it for the
1002 # common case where files is ['']
1002 # common case where files is ['']
1003 normalize = None
1003 normalize = None
1004 results = dict.fromkeys(subrepos)
1004 results = dict.fromkeys(subrepos)
1005 results[b'.hg'] = None
1005 results[b'.hg'] = None
1006
1006
1007 for ff in files:
1007 for ff in files:
1008 if normalize:
1008 if normalize:
1009 nf = normalize(ff, False, True)
1009 nf = normalize(ff, False, True)
1010 else:
1010 else:
1011 nf = ff
1011 nf = ff
1012 if nf in results:
1012 if nf in results:
1013 continue
1013 continue
1014
1014
1015 try:
1015 try:
1016 st = lstat(join(nf))
1016 st = lstat(join(nf))
1017 kind = getkind(st.st_mode)
1017 kind = getkind(st.st_mode)
1018 if kind == dirkind:
1018 if kind == dirkind:
1019 if nf in dmap:
1019 if nf in dmap:
1020 # file replaced by dir on disk but still in dirstate
1020 # file replaced by dir on disk but still in dirstate
1021 results[nf] = None
1021 results[nf] = None
1022 foundadd((nf, ff))
1022 foundadd((nf, ff))
1023 elif kind == regkind or kind == lnkkind:
1023 elif kind == regkind or kind == lnkkind:
1024 results[nf] = st
1024 results[nf] = st
1025 else:
1025 else:
1026 badfn(ff, badtype(kind))
1026 badfn(ff, badtype(kind))
1027 if nf in dmap:
1027 if nf in dmap:
1028 results[nf] = None
1028 results[nf] = None
1029 except OSError as inst: # nf not found on disk - it is dirstate only
1029 except OSError as inst: # nf not found on disk - it is dirstate only
1030 if nf in dmap: # does it exactly match a missing file?
1030 if nf in dmap: # does it exactly match a missing file?
1031 results[nf] = None
1031 results[nf] = None
1032 else: # does it match a missing directory?
1032 else: # does it match a missing directory?
1033 if self._map.hasdir(nf):
1033 if self._map.hasdir(nf):
1034 notfoundadd(nf)
1034 notfoundadd(nf)
1035 else:
1035 else:
1036 badfn(ff, encoding.strtolocal(inst.strerror))
1036 badfn(ff, encoding.strtolocal(inst.strerror))
1037
1037
1038 # match.files() may contain explicitly-specified paths that shouldn't
1038 # match.files() may contain explicitly-specified paths that shouldn't
1039 # be taken; drop them from the list of files found. dirsfound/notfound
1039 # be taken; drop them from the list of files found. dirsfound/notfound
1040 # aren't filtered here because they will be tested later.
1040 # aren't filtered here because they will be tested later.
1041 if match.anypats():
1041 if match.anypats():
1042 for f in list(results):
1042 for f in list(results):
1043 if f == b'.hg' or f in subrepos:
1043 if f == b'.hg' or f in subrepos:
1044 # keep sentinel to disable further out-of-repo walks
1044 # keep sentinel to disable further out-of-repo walks
1045 continue
1045 continue
1046 if not match(f):
1046 if not match(f):
1047 del results[f]
1047 del results[f]
1048
1048
1049 # Case insensitive filesystems cannot rely on lstat() failing to detect
1049 # Case insensitive filesystems cannot rely on lstat() failing to detect
1050 # a case-only rename. Prune the stat object for any file that does not
1050 # a case-only rename. Prune the stat object for any file that does not
1051 # match the case in the filesystem, if there are multiple files that
1051 # match the case in the filesystem, if there are multiple files that
1052 # normalize to the same path.
1052 # normalize to the same path.
1053 if match.isexact() and self._checkcase:
1053 if match.isexact() and self._checkcase:
1054 normed = {}
1054 normed = {}
1055
1055
1056 for f, st in pycompat.iteritems(results):
1056 for f, st in pycompat.iteritems(results):
1057 if st is None:
1057 if st is None:
1058 continue
1058 continue
1059
1059
1060 nc = util.normcase(f)
1060 nc = util.normcase(f)
1061 paths = normed.get(nc)
1061 paths = normed.get(nc)
1062
1062
1063 if paths is None:
1063 if paths is None:
1064 paths = set()
1064 paths = set()
1065 normed[nc] = paths
1065 normed[nc] = paths
1066
1066
1067 paths.add(f)
1067 paths.add(f)
1068
1068
1069 for norm, paths in pycompat.iteritems(normed):
1069 for norm, paths in pycompat.iteritems(normed):
1070 if len(paths) > 1:
1070 if len(paths) > 1:
1071 for path in paths:
1071 for path in paths:
1072 folded = self._discoverpath(
1072 folded = self._discoverpath(
1073 path, norm, True, None, self._map.dirfoldmap
1073 path, norm, True, None, self._map.dirfoldmap
1074 )
1074 )
1075 if path != folded:
1075 if path != folded:
1076 results[path] = None
1076 results[path] = None
1077
1077
1078 return results, dirsfound, dirsnotfound
1078 return results, dirsfound, dirsnotfound
1079
1079
1080 def walk(self, match, subrepos, unknown, ignored, full=True):
1080 def walk(self, match, subrepos, unknown, ignored, full=True):
1081 """
1081 """
1082 Walk recursively through the directory tree, finding all files
1082 Walk recursively through the directory tree, finding all files
1083 matched by match.
1083 matched by match.
1084
1084
1085 If full is False, maybe skip some known-clean files.
1085 If full is False, maybe skip some known-clean files.
1086
1086
1087 Return a dict mapping filename to stat-like object (either
1087 Return a dict mapping filename to stat-like object (either
1088 mercurial.osutil.stat instance or return value of os.stat()).
1088 mercurial.osutil.stat instance or return value of os.stat()).
1089
1089
1090 """
1090 """
1091 # full is a flag that extensions that hook into walk can use -- this
1091 # full is a flag that extensions that hook into walk can use -- this
1092 # implementation doesn't use it at all. This satisfies the contract
1092 # implementation doesn't use it at all. This satisfies the contract
1093 # because we only guarantee a "maybe".
1093 # because we only guarantee a "maybe".
1094
1094
1095 if ignored:
1095 if ignored:
1096 ignore = util.never
1096 ignore = util.never
1097 dirignore = util.never
1097 dirignore = util.never
1098 elif unknown:
1098 elif unknown:
1099 ignore = self._ignore
1099 ignore = self._ignore
1100 dirignore = self._dirignore
1100 dirignore = self._dirignore
1101 else:
1101 else:
1102 # if not unknown and not ignored, drop dir recursion and step 2
1102 # if not unknown and not ignored, drop dir recursion and step 2
1103 ignore = util.always
1103 ignore = util.always
1104 dirignore = util.always
1104 dirignore = util.always
1105
1105
1106 matchfn = match.matchfn
1106 matchfn = match.matchfn
1107 matchalways = match.always()
1107 matchalways = match.always()
1108 matchtdir = match.traversedir
1108 matchtdir = match.traversedir
1109 dmap = self._map
1109 dmap = self._map
1110 listdir = util.listdir
1110 listdir = util.listdir
1111 lstat = os.lstat
1111 lstat = os.lstat
1112 dirkind = stat.S_IFDIR
1112 dirkind = stat.S_IFDIR
1113 regkind = stat.S_IFREG
1113 regkind = stat.S_IFREG
1114 lnkkind = stat.S_IFLNK
1114 lnkkind = stat.S_IFLNK
1115 join = self._join
1115 join = self._join
1116
1116
1117 exact = skipstep3 = False
1117 exact = skipstep3 = False
1118 if match.isexact(): # match.exact
1118 if match.isexact(): # match.exact
1119 exact = True
1119 exact = True
1120 dirignore = util.always # skip step 2
1120 dirignore = util.always # skip step 2
1121 elif match.prefix(): # match.match, no patterns
1121 elif match.prefix(): # match.match, no patterns
1122 skipstep3 = True
1122 skipstep3 = True
1123
1123
1124 if not exact and self._checkcase:
1124 if not exact and self._checkcase:
1125 normalize = self._normalize
1125 normalize = self._normalize
1126 normalizefile = self._normalizefile
1126 normalizefile = self._normalizefile
1127 skipstep3 = False
1127 skipstep3 = False
1128 else:
1128 else:
1129 normalize = self._normalize
1129 normalize = self._normalize
1130 normalizefile = None
1130 normalizefile = None
1131
1131
1132 # step 1: find all explicit files
1132 # step 1: find all explicit files
1133 results, work, dirsnotfound = self._walkexplicit(match, subrepos)
1133 results, work, dirsnotfound = self._walkexplicit(match, subrepos)
1134 if matchtdir:
1134 if matchtdir:
1135 for d in work:
1135 for d in work:
1136 matchtdir(d[0])
1136 matchtdir(d[0])
1137 for d in dirsnotfound:
1137 for d in dirsnotfound:
1138 matchtdir(d)
1138 matchtdir(d)
1139
1139
1140 skipstep3 = skipstep3 and not (work or dirsnotfound)
1140 skipstep3 = skipstep3 and not (work or dirsnotfound)
1141 work = [d for d in work if not dirignore(d[0])]
1141 work = [d for d in work if not dirignore(d[0])]
1142
1142
1143 # step 2: visit subdirectories
1143 # step 2: visit subdirectories
1144 def traverse(work, alreadynormed):
1144 def traverse(work, alreadynormed):
1145 wadd = work.append
1145 wadd = work.append
1146 while work:
1146 while work:
1147 tracing.counter('dirstate.walk work', len(work))
1147 tracing.counter('dirstate.walk work', len(work))
1148 nd = work.pop()
1148 nd = work.pop()
1149 visitentries = match.visitchildrenset(nd)
1149 visitentries = match.visitchildrenset(nd)
1150 if not visitentries:
1150 if not visitentries:
1151 continue
1151 continue
1152 if visitentries == b'this' or visitentries == b'all':
1152 if visitentries == b'this' or visitentries == b'all':
1153 visitentries = None
1153 visitentries = None
1154 skip = None
1154 skip = None
1155 if nd != b'':
1155 if nd != b'':
1156 skip = b'.hg'
1156 skip = b'.hg'
1157 try:
1157 try:
1158 with tracing.log('dirstate.walk.traverse listdir %s', nd):
1158 with tracing.log('dirstate.walk.traverse listdir %s', nd):
1159 entries = listdir(join(nd), stat=True, skip=skip)
1159 entries = listdir(join(nd), stat=True, skip=skip)
1160 except OSError as inst:
1160 except OSError as inst:
1161 if inst.errno in (errno.EACCES, errno.ENOENT):
1161 if inst.errno in (errno.EACCES, errno.ENOENT):
1162 match.bad(
1162 match.bad(
1163 self.pathto(nd), encoding.strtolocal(inst.strerror)
1163 self.pathto(nd), encoding.strtolocal(inst.strerror)
1164 )
1164 )
1165 continue
1165 continue
1166 raise
1166 raise
1167 for f, kind, st in entries:
1167 for f, kind, st in entries:
1168 # Some matchers may return files in the visitentries set,
1168 # Some matchers may return files in the visitentries set,
1169 # instead of 'this', if the matcher explicitly mentions them
1169 # instead of 'this', if the matcher explicitly mentions them
1170 # and is not an exactmatcher. This is acceptable; we do not
1170 # and is not an exactmatcher. This is acceptable; we do not
1171 # make any hard assumptions about file-or-directory below
1171 # make any hard assumptions about file-or-directory below
1172 # based on the presence of `f` in visitentries. If
1172 # based on the presence of `f` in visitentries. If
1173 # visitchildrenset returned a set, we can always skip the
1173 # visitchildrenset returned a set, we can always skip the
1174 # entries *not* in the set it provided regardless of whether
1174 # entries *not* in the set it provided regardless of whether
1175 # they're actually a file or a directory.
1175 # they're actually a file or a directory.
1176 if visitentries and f not in visitentries:
1176 if visitentries and f not in visitentries:
1177 continue
1177 continue
1178 if normalizefile:
1178 if normalizefile:
1179 # even though f might be a directory, we're only
1179 # even though f might be a directory, we're only
1180 # interested in comparing it to files currently in the
1180 # interested in comparing it to files currently in the
1181 # dmap -- therefore normalizefile is enough
1181 # dmap -- therefore normalizefile is enough
1182 nf = normalizefile(
1182 nf = normalizefile(
1183 nd and (nd + b"/" + f) or f, True, True
1183 nd and (nd + b"/" + f) or f, True, True
1184 )
1184 )
1185 else:
1185 else:
1186 nf = nd and (nd + b"/" + f) or f
1186 nf = nd and (nd + b"/" + f) or f
1187 if nf not in results:
1187 if nf not in results:
1188 if kind == dirkind:
1188 if kind == dirkind:
1189 if not ignore(nf):
1189 if not ignore(nf):
1190 if matchtdir:
1190 if matchtdir:
1191 matchtdir(nf)
1191 matchtdir(nf)
1192 wadd(nf)
1192 wadd(nf)
1193 if nf in dmap and (matchalways or matchfn(nf)):
1193 if nf in dmap and (matchalways or matchfn(nf)):
1194 results[nf] = None
1194 results[nf] = None
1195 elif kind == regkind or kind == lnkkind:
1195 elif kind == regkind or kind == lnkkind:
1196 if nf in dmap:
1196 if nf in dmap:
1197 if matchalways or matchfn(nf):
1197 if matchalways or matchfn(nf):
1198 results[nf] = st
1198 results[nf] = st
1199 elif (matchalways or matchfn(nf)) and not ignore(
1199 elif (matchalways or matchfn(nf)) and not ignore(
1200 nf
1200 nf
1201 ):
1201 ):
1202 # unknown file -- normalize if necessary
1202 # unknown file -- normalize if necessary
1203 if not alreadynormed:
1203 if not alreadynormed:
1204 nf = normalize(nf, False, True)
1204 nf = normalize(nf, False, True)
1205 results[nf] = st
1205 results[nf] = st
1206 elif nf in dmap and (matchalways or matchfn(nf)):
1206 elif nf in dmap and (matchalways or matchfn(nf)):
1207 results[nf] = None
1207 results[nf] = None
1208
1208
1209 for nd, d in work:
1209 for nd, d in work:
1210 # alreadynormed means that processwork doesn't have to do any
1210 # alreadynormed means that processwork doesn't have to do any
1211 # expensive directory normalization
1211 # expensive directory normalization
1212 alreadynormed = not normalize or nd == d
1212 alreadynormed = not normalize or nd == d
1213 traverse([d], alreadynormed)
1213 traverse([d], alreadynormed)
1214
1214
1215 for s in subrepos:
1215 for s in subrepos:
1216 del results[s]
1216 del results[s]
1217 del results[b'.hg']
1217 del results[b'.hg']
1218
1218
1219 # step 3: visit remaining files from dmap
1219 # step 3: visit remaining files from dmap
1220 if not skipstep3 and not exact:
1220 if not skipstep3 and not exact:
1221 # If a dmap file is not in results yet, it was either
1221 # If a dmap file is not in results yet, it was either
1222 # a) not matching matchfn b) ignored, c) missing, or d) under a
1222 # a) not matching matchfn b) ignored, c) missing, or d) under a
1223 # symlink directory.
1223 # symlink directory.
1224 if not results and matchalways:
1224 if not results and matchalways:
1225 visit = [f for f in dmap]
1225 visit = [f for f in dmap]
1226 else:
1226 else:
1227 visit = [f for f in dmap if f not in results and matchfn(f)]
1227 visit = [f for f in dmap if f not in results and matchfn(f)]
1228 visit.sort()
1228 visit.sort()
1229
1229
1230 if unknown:
1230 if unknown:
1231 # unknown == True means we walked all dirs under the roots
1231 # unknown == True means we walked all dirs under the roots
1232 # that wasn't ignored, and everything that matched was stat'ed
1232 # that wasn't ignored, and everything that matched was stat'ed
1233 # and is already in results.
1233 # and is already in results.
1234 # The rest must thus be ignored or under a symlink.
1234 # The rest must thus be ignored or under a symlink.
1235 audit_path = pathutil.pathauditor(self._root, cached=True)
1235 audit_path = pathutil.pathauditor(self._root, cached=True)
1236
1236
1237 for nf in iter(visit):
1237 for nf in iter(visit):
1238 # If a stat for the same file was already added with a
1238 # If a stat for the same file was already added with a
1239 # different case, don't add one for this, since that would
1239 # different case, don't add one for this, since that would
1240 # make it appear as if the file exists under both names
1240 # make it appear as if the file exists under both names
1241 # on disk.
1241 # on disk.
1242 if (
1242 if (
1243 normalizefile
1243 normalizefile
1244 and normalizefile(nf, True, True) in results
1244 and normalizefile(nf, True, True) in results
1245 ):
1245 ):
1246 results[nf] = None
1246 results[nf] = None
1247 # Report ignored items in the dmap as long as they are not
1247 # Report ignored items in the dmap as long as they are not
1248 # under a symlink directory.
1248 # under a symlink directory.
1249 elif audit_path.check(nf):
1249 elif audit_path.check(nf):
1250 try:
1250 try:
1251 results[nf] = lstat(join(nf))
1251 results[nf] = lstat(join(nf))
1252 # file was just ignored, no links, and exists
1252 # file was just ignored, no links, and exists
1253 except OSError:
1253 except OSError:
1254 # file doesn't exist
1254 # file doesn't exist
1255 results[nf] = None
1255 results[nf] = None
1256 else:
1256 else:
1257 # It's either missing or under a symlink directory
1257 # It's either missing or under a symlink directory
1258 # which we in this case report as missing
1258 # which we in this case report as missing
1259 results[nf] = None
1259 results[nf] = None
1260 else:
1260 else:
1261 # We may not have walked the full directory tree above,
1261 # We may not have walked the full directory tree above,
1262 # so stat and check everything we missed.
1262 # so stat and check everything we missed.
1263 iv = iter(visit)
1263 iv = iter(visit)
1264 for st in util.statfiles([join(i) for i in visit]):
1264 for st in util.statfiles([join(i) for i in visit]):
1265 results[next(iv)] = st
1265 results[next(iv)] = st
1266 return results
1266 return results
1267
1267
1268 def _rust_status(self, matcher, list_clean, list_ignored, list_unknown):
1268 def _rust_status(self, matcher, list_clean, list_ignored, list_unknown):
1269 # Force Rayon (Rust parallelism library) to respect the number of
1269 # Force Rayon (Rust parallelism library) to respect the number of
1270 # workers. This is a temporary workaround until Rust code knows
1270 # workers. This is a temporary workaround until Rust code knows
1271 # how to read the config file.
1271 # how to read the config file.
1272 numcpus = self._ui.configint(b"worker", b"numcpus")
1272 numcpus = self._ui.configint(b"worker", b"numcpus")
1273 if numcpus is not None:
1273 if numcpus is not None:
1274 encoding.environ.setdefault(b'RAYON_NUM_THREADS', b'%d' % numcpus)
1274 encoding.environ.setdefault(b'RAYON_NUM_THREADS', b'%d' % numcpus)
1275
1275
1276 workers_enabled = self._ui.configbool(b"worker", b"enabled", True)
1276 workers_enabled = self._ui.configbool(b"worker", b"enabled", True)
1277 if not workers_enabled:
1277 if not workers_enabled:
1278 encoding.environ[b"RAYON_NUM_THREADS"] = b"1"
1278 encoding.environ[b"RAYON_NUM_THREADS"] = b"1"
1279
1279
1280 (
1280 (
1281 lookup,
1281 lookup,
1282 modified,
1282 modified,
1283 added,
1283 added,
1284 removed,
1284 removed,
1285 deleted,
1285 deleted,
1286 clean,
1286 clean,
1287 ignored,
1287 ignored,
1288 unknown,
1288 unknown,
1289 warnings,
1289 warnings,
1290 bad,
1290 bad,
1291 traversed,
1291 traversed,
1292 dirty,
1292 dirty,
1293 ) = rustmod.status(
1293 ) = rustmod.status(
1294 self._map._rustmap,
1294 self._map._rustmap,
1295 matcher,
1295 matcher,
1296 self._rootdir,
1296 self._rootdir,
1297 self._ignorefiles(),
1297 self._ignorefiles(),
1298 self._checkexec,
1298 self._checkexec,
1299 self._lastnormaltime,
1299 self._lastnormaltime,
1300 bool(list_clean),
1300 bool(list_clean),
1301 bool(list_ignored),
1301 bool(list_ignored),
1302 bool(list_unknown),
1302 bool(list_unknown),
1303 bool(matcher.traversedir),
1303 bool(matcher.traversedir),
1304 )
1304 )
1305
1305
1306 self._dirty |= dirty
1306 self._dirty |= dirty
1307
1307
1308 if matcher.traversedir:
1308 if matcher.traversedir:
1309 for dir in traversed:
1309 for dir in traversed:
1310 matcher.traversedir(dir)
1310 matcher.traversedir(dir)
1311
1311
1312 if self._ui.warn:
1312 if self._ui.warn:
1313 for item in warnings:
1313 for item in warnings:
1314 if isinstance(item, tuple):
1314 if isinstance(item, tuple):
1315 file_path, syntax = item
1315 file_path, syntax = item
1316 msg = _(b"%s: ignoring invalid syntax '%s'\n") % (
1316 msg = _(b"%s: ignoring invalid syntax '%s'\n") % (
1317 file_path,
1317 file_path,
1318 syntax,
1318 syntax,
1319 )
1319 )
1320 self._ui.warn(msg)
1320 self._ui.warn(msg)
1321 else:
1321 else:
1322 msg = _(b"skipping unreadable pattern file '%s': %s\n")
1322 msg = _(b"skipping unreadable pattern file '%s': %s\n")
1323 self._ui.warn(
1323 self._ui.warn(
1324 msg
1324 msg
1325 % (
1325 % (
1326 pathutil.canonpath(
1326 pathutil.canonpath(
1327 self._rootdir, self._rootdir, item
1327 self._rootdir, self._rootdir, item
1328 ),
1328 ),
1329 b"No such file or directory",
1329 b"No such file or directory",
1330 )
1330 )
1331 )
1331 )
1332
1332
1333 for (fn, message) in bad:
1333 for (fn, message) in bad:
1334 matcher.bad(fn, encoding.strtolocal(message))
1334 matcher.bad(fn, encoding.strtolocal(message))
1335
1335
1336 status = scmutil.status(
1336 status = scmutil.status(
1337 modified=modified,
1337 modified=modified,
1338 added=added,
1338 added=added,
1339 removed=removed,
1339 removed=removed,
1340 deleted=deleted,
1340 deleted=deleted,
1341 unknown=unknown,
1341 unknown=unknown,
1342 ignored=ignored,
1342 ignored=ignored,
1343 clean=clean,
1343 clean=clean,
1344 )
1344 )
1345 return (lookup, status)
1345 return (lookup, status)
1346
1346
1347 def status(self, match, subrepos, ignored, clean, unknown):
1347 def status(self, match, subrepos, ignored, clean, unknown):
1348 """Determine the status of the working copy relative to the
1348 """Determine the status of the working copy relative to the
1349 dirstate and return a pair of (unsure, status), where status is of type
1349 dirstate and return a pair of (unsure, status), where status is of type
1350 scmutil.status and:
1350 scmutil.status and:
1351
1351
1352 unsure:
1352 unsure:
1353 files that might have been modified since the dirstate was
1353 files that might have been modified since the dirstate was
1354 written, but need to be read to be sure (size is the same
1354 written, but need to be read to be sure (size is the same
1355 but mtime differs)
1355 but mtime differs)
1356 status.modified:
1356 status.modified:
1357 files that have definitely been modified since the dirstate
1357 files that have definitely been modified since the dirstate
1358 was written (different size or mode)
1358 was written (different size or mode)
1359 status.clean:
1359 status.clean:
1360 files that have definitely not been modified since the
1360 files that have definitely not been modified since the
1361 dirstate was written
1361 dirstate was written
1362 """
1362 """
1363 listignored, listclean, listunknown = ignored, clean, unknown
1363 listignored, listclean, listunknown = ignored, clean, unknown
1364 lookup, modified, added, unknown, ignored = [], [], [], [], []
1364 lookup, modified, added, unknown, ignored = [], [], [], [], []
1365 removed, deleted, clean = [], [], []
1365 removed, deleted, clean = [], [], []
1366
1366
1367 dmap = self._map
1367 dmap = self._map
1368 dmap.preload()
1368 dmap.preload()
1369
1369
1370 use_rust = True
1370 use_rust = True
1371
1371
1372 allowed_matchers = (
1372 allowed_matchers = (
1373 matchmod.alwaysmatcher,
1373 matchmod.alwaysmatcher,
1374 matchmod.exactmatcher,
1374 matchmod.exactmatcher,
1375 matchmod.includematcher,
1375 matchmod.includematcher,
1376 )
1376 )
1377
1377
1378 if rustmod is None:
1378 if rustmod is None:
1379 use_rust = False
1379 use_rust = False
1380 elif self._checkcase:
1380 elif self._checkcase:
1381 # Case-insensitive filesystems are not handled yet
1381 # Case-insensitive filesystems are not handled yet
1382 use_rust = False
1382 use_rust = False
1383 elif subrepos:
1383 elif subrepos:
1384 use_rust = False
1384 use_rust = False
1385 elif sparse.enabled:
1385 elif sparse.enabled:
1386 use_rust = False
1386 use_rust = False
1387 elif not isinstance(match, allowed_matchers):
1387 elif not isinstance(match, allowed_matchers):
1388 # Some matchers have yet to be implemented
1388 # Some matchers have yet to be implemented
1389 use_rust = False
1389 use_rust = False
1390
1390
1391 if use_rust:
1391 if use_rust:
1392 try:
1392 try:
1393 return self._rust_status(
1393 return self._rust_status(
1394 match, listclean, listignored, listunknown
1394 match, listclean, listignored, listunknown
1395 )
1395 )
1396 except rustmod.FallbackError:
1396 except rustmod.FallbackError:
1397 pass
1397 pass
1398
1398
1399 def noop(f):
1399 def noop(f):
1400 pass
1400 pass
1401
1401
1402 dcontains = dmap.__contains__
1402 dcontains = dmap.__contains__
1403 dget = dmap.__getitem__
1403 dget = dmap.__getitem__
1404 ladd = lookup.append # aka "unsure"
1404 ladd = lookup.append # aka "unsure"
1405 madd = modified.append
1405 madd = modified.append
1406 aadd = added.append
1406 aadd = added.append
1407 uadd = unknown.append if listunknown else noop
1407 uadd = unknown.append if listunknown else noop
1408 iadd = ignored.append if listignored else noop
1408 iadd = ignored.append if listignored else noop
1409 radd = removed.append
1409 radd = removed.append
1410 dadd = deleted.append
1410 dadd = deleted.append
1411 cadd = clean.append if listclean else noop
1411 cadd = clean.append if listclean else noop
1412 mexact = match.exact
1412 mexact = match.exact
1413 dirignore = self._dirignore
1413 dirignore = self._dirignore
1414 checkexec = self._checkexec
1414 checkexec = self._checkexec
1415 copymap = self._map.copymap
1415 copymap = self._map.copymap
1416 lastnormaltime = self._lastnormaltime
1416 lastnormaltime = self._lastnormaltime
1417
1417
1418 # We need to do full walks when either
1418 # We need to do full walks when either
1419 # - we're listing all clean files, or
1419 # - we're listing all clean files, or
1420 # - match.traversedir does something, because match.traversedir should
1420 # - match.traversedir does something, because match.traversedir should
1421 # be called for every dir in the working dir
1421 # be called for every dir in the working dir
1422 full = listclean or match.traversedir is not None
1422 full = listclean or match.traversedir is not None
1423 for fn, st in pycompat.iteritems(
1423 for fn, st in pycompat.iteritems(
1424 self.walk(match, subrepos, listunknown, listignored, full=full)
1424 self.walk(match, subrepos, listunknown, listignored, full=full)
1425 ):
1425 ):
1426 if not dcontains(fn):
1426 if not dcontains(fn):
1427 if (listignored or mexact(fn)) and dirignore(fn):
1427 if (listignored or mexact(fn)) and dirignore(fn):
1428 if listignored:
1428 if listignored:
1429 iadd(fn)
1429 iadd(fn)
1430 else:
1430 else:
1431 uadd(fn)
1431 uadd(fn)
1432 continue
1432 continue
1433
1433
1434 # This is equivalent to 'state, mode, size, time = dmap[fn]' but not
1434 # This is equivalent to 'state, mode, size, time = dmap[fn]' but not
1435 # written like that for performance reasons. dmap[fn] is not a
1435 # written like that for performance reasons. dmap[fn] is not a
1436 # Python tuple in compiled builds. The CPython UNPACK_SEQUENCE
1436 # Python tuple in compiled builds. The CPython UNPACK_SEQUENCE
1437 # opcode has fast paths when the value to be unpacked is a tuple or
1437 # opcode has fast paths when the value to be unpacked is a tuple or
1438 # a list, but falls back to creating a full-fledged iterator in
1438 # a list, but falls back to creating a full-fledged iterator in
1439 # general. That is much slower than simply accessing and storing the
1439 # general. That is much slower than simply accessing and storing the
1440 # tuple members one by one.
1440 # tuple members one by one.
1441 t = dget(fn)
1441 t = dget(fn)
1442 mode = t.mode
1442 mode = t.mode
1443 size = t.size
1443 size = t.size
1444 time = t.mtime
1444 time = t.mtime
1445
1445
1446 if not st and t.tracked:
1446 if not st and t.tracked:
1447 dadd(fn)
1447 dadd(fn)
1448 elif t.merged:
1448 elif t.merged:
1449 madd(fn)
1449 madd(fn)
1450 elif t.added:
1450 elif t.added:
1451 aadd(fn)
1451 aadd(fn)
1452 elif t.removed:
1452 elif t.removed:
1453 radd(fn)
1453 radd(fn)
1454 elif t.tracked:
1454 elif t.tracked:
1455 if (
1455 if (
1456 size >= 0
1456 size >= 0
1457 and (
1457 and (
1458 (size != st.st_size and size != st.st_size & _rangemask)
1458 (size != st.st_size and size != st.st_size & _rangemask)
1459 or ((mode ^ st.st_mode) & 0o100 and checkexec)
1459 or ((mode ^ st.st_mode) & 0o100 and checkexec)
1460 )
1460 )
1461 or t.from_p2
1461 or t.from_p2
1462 or fn in copymap
1462 or fn in copymap
1463 ):
1463 ):
1464 if stat.S_ISLNK(st.st_mode) and size != st.st_size:
1464 if stat.S_ISLNK(st.st_mode) and size != st.st_size:
1465 # issue6456: Size returned may be longer due to
1465 # issue6456: Size returned may be longer due to
1466 # encryption on EXT-4 fscrypt, undecided.
1466 # encryption on EXT-4 fscrypt, undecided.
1467 ladd(fn)
1467 ladd(fn)
1468 else:
1468 else:
1469 madd(fn)
1469 madd(fn)
1470 elif (
1470 elif (
1471 time != st[stat.ST_MTIME]
1471 time != st[stat.ST_MTIME]
1472 and time != st[stat.ST_MTIME] & _rangemask
1472 and time != st[stat.ST_MTIME] & _rangemask
1473 ):
1473 ):
1474 ladd(fn)
1474 ladd(fn)
1475 elif st[stat.ST_MTIME] == lastnormaltime:
1475 elif st[stat.ST_MTIME] == lastnormaltime:
1476 # fn may have just been marked as normal and it may have
1476 # fn may have just been marked as normal and it may have
1477 # changed in the same second without changing its size.
1477 # changed in the same second without changing its size.
1478 # This can happen if we quickly do multiple commits.
1478 # This can happen if we quickly do multiple commits.
1479 # Force lookup, so we don't miss such a racy file change.
1479 # Force lookup, so we don't miss such a racy file change.
1480 ladd(fn)
1480 ladd(fn)
1481 elif listclean:
1481 elif listclean:
1482 cadd(fn)
1482 cadd(fn)
1483 status = scmutil.status(
1483 status = scmutil.status(
1484 modified, added, removed, deleted, unknown, ignored, clean
1484 modified, added, removed, deleted, unknown, ignored, clean
1485 )
1485 )
1486 return (lookup, status)
1486 return (lookup, status)
1487
1487
1488 def matches(self, match):
1488 def matches(self, match):
1489 """
1489 """
1490 return files in the dirstate (in whatever state) filtered by match
1490 return files in the dirstate (in whatever state) filtered by match
1491 """
1491 """
1492 dmap = self._map
1492 dmap = self._map
1493 if rustmod is not None:
1493 if rustmod is not None:
1494 dmap = self._map._rustmap
1494 dmap = self._map._rustmap
1495
1495
1496 if match.always():
1496 if match.always():
1497 return dmap.keys()
1497 return dmap.keys()
1498 files = match.files()
1498 files = match.files()
1499 if match.isexact():
1499 if match.isexact():
1500 # fast path -- filter the other way around, since typically files is
1500 # fast path -- filter the other way around, since typically files is
1501 # much smaller than dmap
1501 # much smaller than dmap
1502 return [f for f in files if f in dmap]
1502 return [f for f in files if f in dmap]
1503 if match.prefix() and all(fn in dmap for fn in files):
1503 if match.prefix() and all(fn in dmap for fn in files):
1504 # fast path -- all the values are known to be files, so just return
1504 # fast path -- all the values are known to be files, so just return
1505 # that
1505 # that
1506 return list(files)
1506 return list(files)
1507 return [f for f in dmap if match(f)]
1507 return [f for f in dmap if match(f)]
1508
1508
1509 def _actualfilename(self, tr):
1509 def _actualfilename(self, tr):
1510 if tr:
1510 if tr:
1511 return self._pendingfilename
1511 return self._pendingfilename
1512 else:
1512 else:
1513 return self._filename
1513 return self._filename
1514
1514
1515 def savebackup(self, tr, backupname):
1515 def savebackup(self, tr, backupname):
1516 '''Save current dirstate into backup file'''
1516 '''Save current dirstate into backup file'''
1517 filename = self._actualfilename(tr)
1517 filename = self._actualfilename(tr)
1518 assert backupname != filename
1518 assert backupname != filename
1519
1519
1520 # use '_writedirstate' instead of 'write' to write changes certainly,
1520 # use '_writedirstate' instead of 'write' to write changes certainly,
1521 # because the latter omits writing out if transaction is running.
1521 # because the latter omits writing out if transaction is running.
1522 # output file will be used to create backup of dirstate at this point.
1522 # output file will be used to create backup of dirstate at this point.
1523 if self._dirty or not self._opener.exists(filename):
1523 if self._dirty or not self._opener.exists(filename):
1524 self._writedirstate(
1524 self._writedirstate(
1525 tr,
1525 tr,
1526 self._opener(filename, b"w", atomictemp=True, checkambig=True),
1526 self._opener(filename, b"w", atomictemp=True, checkambig=True),
1527 )
1527 )
1528
1528
1529 if tr:
1529 if tr:
1530 # ensure that subsequent tr.writepending returns True for
1530 # ensure that subsequent tr.writepending returns True for
1531 # changes written out above, even if dirstate is never
1531 # changes written out above, even if dirstate is never
1532 # changed after this
1532 # changed after this
1533 tr.addfilegenerator(
1533 tr.addfilegenerator(
1534 b'dirstate',
1534 b'dirstate',
1535 (self._filename,),
1535 (self._filename,),
1536 lambda f: self._writedirstate(tr, f),
1536 lambda f: self._writedirstate(tr, f),
1537 location=b'plain',
1537 location=b'plain',
1538 )
1538 )
1539
1539
1540 # ensure that pending file written above is unlinked at
1540 # ensure that pending file written above is unlinked at
1541 # failure, even if tr.writepending isn't invoked until the
1541 # failure, even if tr.writepending isn't invoked until the
1542 # end of this transaction
1542 # end of this transaction
1543 tr.registertmp(filename, location=b'plain')
1543 tr.registertmp(filename, location=b'plain')
1544
1544
1545 self._opener.tryunlink(backupname)
1545 self._opener.tryunlink(backupname)
1546 # hardlink backup is okay because _writedirstate is always called
1546 # hardlink backup is okay because _writedirstate is always called
1547 # with an "atomictemp=True" file.
1547 # with an "atomictemp=True" file.
1548 util.copyfile(
1548 util.copyfile(
1549 self._opener.join(filename),
1549 self._opener.join(filename),
1550 self._opener.join(backupname),
1550 self._opener.join(backupname),
1551 hardlink=True,
1551 hardlink=True,
1552 )
1552 )
1553
1553
1554 def restorebackup(self, tr, backupname):
1554 def restorebackup(self, tr, backupname):
1555 '''Restore dirstate by backup file'''
1555 '''Restore dirstate by backup file'''
1556 # this "invalidate()" prevents "wlock.release()" from writing
1556 # this "invalidate()" prevents "wlock.release()" from writing
1557 # changes of dirstate out after restoring from backup file
1557 # changes of dirstate out after restoring from backup file
1558 self.invalidate()
1558 self.invalidate()
1559 filename = self._actualfilename(tr)
1559 filename = self._actualfilename(tr)
1560 o = self._opener
1560 o = self._opener
1561 if util.samefile(o.join(backupname), o.join(filename)):
1561 if util.samefile(o.join(backupname), o.join(filename)):
1562 o.unlink(backupname)
1562 o.unlink(backupname)
1563 else:
1563 else:
1564 o.rename(backupname, filename, checkambig=True)
1564 o.rename(backupname, filename, checkambig=True)
1565
1565
1566 def clearbackup(self, tr, backupname):
1566 def clearbackup(self, tr, backupname):
1567 '''Clear backup file'''
1567 '''Clear backup file'''
1568 self._opener.unlink(backupname)
1568 self._opener.unlink(backupname)
General Comments 0
You need to be logged in to leave comments. Login now