##// END OF EJS Templates
cachevfs: add a devel warning for cache access though 'vfs'...
Boris Feld -
r33538:54b36d3f default
parent child Browse files
Show More
@@ -1,2249 +1,2252 b''
1 1 # localrepo.py - read/write repository class for mercurial
2 2 #
3 3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 from __future__ import absolute_import
9 9
10 10 import errno
11 11 import hashlib
12 12 import inspect
13 13 import os
14 14 import random
15 15 import time
16 16 import weakref
17 17
18 18 from .i18n import _
19 19 from .node import (
20 20 hex,
21 21 nullid,
22 22 short,
23 23 )
24 24 from . import (
25 25 bookmarks,
26 26 branchmap,
27 27 bundle2,
28 28 changegroup,
29 29 changelog,
30 30 color,
31 31 context,
32 32 dirstate,
33 33 dirstateguard,
34 34 encoding,
35 35 error,
36 36 exchange,
37 37 extensions,
38 38 filelog,
39 39 hook,
40 40 lock as lockmod,
41 41 manifest,
42 42 match as matchmod,
43 43 merge as mergemod,
44 44 mergeutil,
45 45 namespaces,
46 46 obsolete,
47 47 pathutil,
48 48 peer,
49 49 phases,
50 50 pushkey,
51 51 pycompat,
52 52 repoview,
53 53 revset,
54 54 revsetlang,
55 55 scmutil,
56 56 sparse,
57 57 store,
58 58 subrepo,
59 59 tags as tagsmod,
60 60 transaction,
61 61 txnutil,
62 62 util,
63 63 vfs as vfsmod,
64 64 )
65 65
66 66 release = lockmod.release
67 67 urlerr = util.urlerr
68 68 urlreq = util.urlreq
69 69
70 70 # set of (path, vfs-location) tuples. vfs-location is:
71 71 # - 'plain for vfs relative paths
72 72 # - '' for svfs relative paths
73 73 _cachedfiles = set()
74 74
75 75 class _basefilecache(scmutil.filecache):
76 76 """All filecache usage on repo are done for logic that should be unfiltered
77 77 """
78 78 def __get__(self, repo, type=None):
79 79 if repo is None:
80 80 return self
81 81 return super(_basefilecache, self).__get__(repo.unfiltered(), type)
82 82 def __set__(self, repo, value):
83 83 return super(_basefilecache, self).__set__(repo.unfiltered(), value)
84 84 def __delete__(self, repo):
85 85 return super(_basefilecache, self).__delete__(repo.unfiltered())
86 86
87 87 class repofilecache(_basefilecache):
88 88 """filecache for files in .hg but outside of .hg/store"""
89 89 def __init__(self, *paths):
90 90 super(repofilecache, self).__init__(*paths)
91 91 for path in paths:
92 92 _cachedfiles.add((path, 'plain'))
93 93
94 94 def join(self, obj, fname):
95 95 return obj.vfs.join(fname)
96 96
97 97 class storecache(_basefilecache):
98 98 """filecache for files in the store"""
99 99 def __init__(self, *paths):
100 100 super(storecache, self).__init__(*paths)
101 101 for path in paths:
102 102 _cachedfiles.add((path, ''))
103 103
104 104 def join(self, obj, fname):
105 105 return obj.sjoin(fname)
106 106
107 107 def isfilecached(repo, name):
108 108 """check if a repo has already cached "name" filecache-ed property
109 109
110 110 This returns (cachedobj-or-None, iscached) tuple.
111 111 """
112 112 cacheentry = repo.unfiltered()._filecache.get(name, None)
113 113 if not cacheentry:
114 114 return None, False
115 115 return cacheentry.obj, True
116 116
117 117 class unfilteredpropertycache(util.propertycache):
118 118 """propertycache that apply to unfiltered repo only"""
119 119
120 120 def __get__(self, repo, type=None):
121 121 unfi = repo.unfiltered()
122 122 if unfi is repo:
123 123 return super(unfilteredpropertycache, self).__get__(unfi)
124 124 return getattr(unfi, self.name)
125 125
126 126 class filteredpropertycache(util.propertycache):
127 127 """propertycache that must take filtering in account"""
128 128
129 129 def cachevalue(self, obj, value):
130 130 object.__setattr__(obj, self.name, value)
131 131
132 132
133 133 def hasunfilteredcache(repo, name):
134 134 """check if a repo has an unfilteredpropertycache value for <name>"""
135 135 return name in vars(repo.unfiltered())
136 136
137 137 def unfilteredmethod(orig):
138 138 """decorate method that always need to be run on unfiltered version"""
139 139 def wrapper(repo, *args, **kwargs):
140 140 return orig(repo.unfiltered(), *args, **kwargs)
141 141 return wrapper
142 142
143 143 moderncaps = {'lookup', 'branchmap', 'pushkey', 'known', 'getbundle',
144 144 'unbundle'}
145 145 legacycaps = moderncaps.union({'changegroupsubset'})
146 146
147 147 class localpeer(peer.peerrepository):
148 148 '''peer for a local repo; reflects only the most recent API'''
149 149
150 150 def __init__(self, repo, caps=None):
151 151 if caps is None:
152 152 caps = moderncaps.copy()
153 153 peer.peerrepository.__init__(self)
154 154 self._repo = repo.filtered('served')
155 155 self.ui = repo.ui
156 156 self._caps = repo._restrictcapabilities(caps)
157 157 self.requirements = repo.requirements
158 158 self.supportedformats = repo.supportedformats
159 159
160 160 def close(self):
161 161 self._repo.close()
162 162
163 163 def _capabilities(self):
164 164 return self._caps
165 165
166 166 def local(self):
167 167 return self._repo
168 168
169 169 def canpush(self):
170 170 return True
171 171
172 172 def url(self):
173 173 return self._repo.url()
174 174
175 175 def lookup(self, key):
176 176 return self._repo.lookup(key)
177 177
178 178 def branchmap(self):
179 179 return self._repo.branchmap()
180 180
181 181 def heads(self):
182 182 return self._repo.heads()
183 183
184 184 def known(self, nodes):
185 185 return self._repo.known(nodes)
186 186
187 187 def getbundle(self, source, heads=None, common=None, bundlecaps=None,
188 188 **kwargs):
189 189 chunks = exchange.getbundlechunks(self._repo, source, heads=heads,
190 190 common=common, bundlecaps=bundlecaps,
191 191 **kwargs)
192 192 cb = util.chunkbuffer(chunks)
193 193
194 194 if exchange.bundle2requested(bundlecaps):
195 195 # When requesting a bundle2, getbundle returns a stream to make the
196 196 # wire level function happier. We need to build a proper object
197 197 # from it in local peer.
198 198 return bundle2.getunbundler(self.ui, cb)
199 199 else:
200 200 return changegroup.getunbundler('01', cb, None)
201 201
202 202 # TODO We might want to move the next two calls into legacypeer and add
203 203 # unbundle instead.
204 204
205 205 def unbundle(self, cg, heads, url):
206 206 """apply a bundle on a repo
207 207
208 208 This function handles the repo locking itself."""
209 209 try:
210 210 try:
211 211 cg = exchange.readbundle(self.ui, cg, None)
212 212 ret = exchange.unbundle(self._repo, cg, heads, 'push', url)
213 213 if util.safehasattr(ret, 'getchunks'):
214 214 # This is a bundle20 object, turn it into an unbundler.
215 215 # This little dance should be dropped eventually when the
216 216 # API is finally improved.
217 217 stream = util.chunkbuffer(ret.getchunks())
218 218 ret = bundle2.getunbundler(self.ui, stream)
219 219 return ret
220 220 except Exception as exc:
221 221 # If the exception contains output salvaged from a bundle2
222 222 # reply, we need to make sure it is printed before continuing
223 223 # to fail. So we build a bundle2 with such output and consume
224 224 # it directly.
225 225 #
226 226 # This is not very elegant but allows a "simple" solution for
227 227 # issue4594
228 228 output = getattr(exc, '_bundle2salvagedoutput', ())
229 229 if output:
230 230 bundler = bundle2.bundle20(self._repo.ui)
231 231 for out in output:
232 232 bundler.addpart(out)
233 233 stream = util.chunkbuffer(bundler.getchunks())
234 234 b = bundle2.getunbundler(self.ui, stream)
235 235 bundle2.processbundle(self._repo, b)
236 236 raise
237 237 except error.PushRaced as exc:
238 238 raise error.ResponseError(_('push failed:'), str(exc))
239 239
240 240 def lock(self):
241 241 return self._repo.lock()
242 242
243 243 def pushkey(self, namespace, key, old, new):
244 244 return self._repo.pushkey(namespace, key, old, new)
245 245
246 246 def listkeys(self, namespace):
247 247 return self._repo.listkeys(namespace)
248 248
249 249 def debugwireargs(self, one, two, three=None, four=None, five=None):
250 250 '''used to test argument passing over the wire'''
251 251 return "%s %s %s %s %s" % (one, two, three, four, five)
252 252
253 253 class locallegacypeer(localpeer):
254 254 '''peer extension which implements legacy methods too; used for tests with
255 255 restricted capabilities'''
256 256
257 257 def __init__(self, repo):
258 258 localpeer.__init__(self, repo, caps=legacycaps)
259 259
260 260 def branches(self, nodes):
261 261 return self._repo.branches(nodes)
262 262
263 263 def between(self, pairs):
264 264 return self._repo.between(pairs)
265 265
266 266 def changegroup(self, basenodes, source):
267 267 return changegroup.changegroup(self._repo, basenodes, source)
268 268
269 269 def changegroupsubset(self, bases, heads, source):
270 270 return changegroup.changegroupsubset(self._repo, bases, heads, source)
271 271
272 272 # Increment the sub-version when the revlog v2 format changes to lock out old
273 273 # clients.
274 274 REVLOGV2_REQUIREMENT = 'exp-revlogv2.0'
275 275
276 276 class localrepository(object):
277 277
278 278 supportedformats = {
279 279 'revlogv1',
280 280 'generaldelta',
281 281 'treemanifest',
282 282 'manifestv2',
283 283 REVLOGV2_REQUIREMENT,
284 284 }
285 285 _basesupported = supportedformats | {
286 286 'store',
287 287 'fncache',
288 288 'shared',
289 289 'relshared',
290 290 'dotencode',
291 291 }
292 292 openerreqs = {
293 293 'revlogv1',
294 294 'generaldelta',
295 295 'treemanifest',
296 296 'manifestv2',
297 297 }
298 298
299 299 # a list of (ui, featureset) functions.
300 300 # only functions defined in module of enabled extensions are invoked
301 301 featuresetupfuncs = set()
302 302
303 303 # list of prefix for file which can be written without 'wlock'
304 304 # Extensions should extend this list when needed
305 305 _wlockfreeprefix = {
306 306 # We migh consider requiring 'wlock' for the next
307 307 # two, but pretty much all the existing code assume
308 308 # wlock is not needed so we keep them excluded for
309 309 # now.
310 310 'hgrc',
311 311 'requires',
312 312 # XXX cache is a complicatged business someone
313 313 # should investigate this in depth at some point
314 314 'cache/',
315 315 # XXX shouldn't be dirstate covered by the wlock?
316 316 'dirstate',
317 317 # XXX bisect was still a bit too messy at the time
318 318 # this changeset was introduced. Someone should fix
319 319 # the remainig bit and drop this line
320 320 'bisect.state',
321 321 }
322 322
323 323 def __init__(self, baseui, path, create=False):
324 324 self.requirements = set()
325 325 self.filtername = None
326 326 # wvfs: rooted at the repository root, used to access the working copy
327 327 self.wvfs = vfsmod.vfs(path, expandpath=True, realpath=True)
328 328 # vfs: rooted at .hg, used to access repo files outside of .hg/store
329 329 self.vfs = None
330 330 # svfs: usually rooted at .hg/store, used to access repository history
331 331 # If this is a shared repository, this vfs may point to another
332 332 # repository's .hg/store directory.
333 333 self.svfs = None
334 334 self.root = self.wvfs.base
335 335 self.path = self.wvfs.join(".hg")
336 336 self.origroot = path
337 337 # These auditor are not used by the vfs,
338 338 # only used when writing this comment: basectx.match
339 339 self.auditor = pathutil.pathauditor(self.root, self._checknested)
340 340 self.nofsauditor = pathutil.pathauditor(self.root, self._checknested,
341 341 realfs=False)
342 342 self.baseui = baseui
343 343 self.ui = baseui.copy()
344 344 self.ui.copy = baseui.copy # prevent copying repo configuration
345 345 self.vfs = vfsmod.vfs(self.path)
346 346 if (self.ui.configbool('devel', 'all-warnings') or
347 347 self.ui.configbool('devel', 'check-locks')):
348 348 self.vfs.audit = self._getvfsward(self.vfs.audit)
349 349 # A list of callback to shape the phase if no data were found.
350 350 # Callback are in the form: func(repo, roots) --> processed root.
351 351 # This list it to be filled by extension during repo setup
352 352 self._phasedefaults = []
353 353 try:
354 354 self.ui.readconfig(self.vfs.join("hgrc"), self.root)
355 355 self._loadextensions()
356 356 except IOError:
357 357 pass
358 358
359 359 if self.featuresetupfuncs:
360 360 self.supported = set(self._basesupported) # use private copy
361 361 extmods = set(m.__name__ for n, m
362 362 in extensions.extensions(self.ui))
363 363 for setupfunc in self.featuresetupfuncs:
364 364 if setupfunc.__module__ in extmods:
365 365 setupfunc(self.ui, self.supported)
366 366 else:
367 367 self.supported = self._basesupported
368 368 color.setup(self.ui)
369 369
370 370 # Add compression engines.
371 371 for name in util.compengines:
372 372 engine = util.compengines[name]
373 373 if engine.revlogheader():
374 374 self.supported.add('exp-compression-%s' % name)
375 375
376 376 if not self.vfs.isdir():
377 377 if create:
378 378 self.requirements = newreporequirements(self)
379 379
380 380 if not self.wvfs.exists():
381 381 self.wvfs.makedirs()
382 382 self.vfs.makedir(notindexed=True)
383 383
384 384 if 'store' in self.requirements:
385 385 self.vfs.mkdir("store")
386 386
387 387 # create an invalid changelog
388 388 self.vfs.append(
389 389 "00changelog.i",
390 390 '\0\0\0\2' # represents revlogv2
391 391 ' dummy changelog to prevent using the old repo layout'
392 392 )
393 393 else:
394 394 raise error.RepoError(_("repository %s not found") % path)
395 395 elif create:
396 396 raise error.RepoError(_("repository %s already exists") % path)
397 397 else:
398 398 try:
399 399 self.requirements = scmutil.readrequires(
400 400 self.vfs, self.supported)
401 401 except IOError as inst:
402 402 if inst.errno != errno.ENOENT:
403 403 raise
404 404
405 405 self.sharedpath = self.path
406 406 try:
407 407 sharedpath = self.vfs.read("sharedpath").rstrip('\n')
408 408 if 'relshared' in self.requirements:
409 409 sharedpath = self.vfs.join(sharedpath)
410 410 vfs = vfsmod.vfs(sharedpath, realpath=True)
411 411 s = vfs.base
412 412 if not vfs.exists():
413 413 raise error.RepoError(
414 414 _('.hg/sharedpath points to nonexistent directory %s') % s)
415 415 self.sharedpath = s
416 416 except IOError as inst:
417 417 if inst.errno != errno.ENOENT:
418 418 raise
419 419
420 420 self.store = store.store(
421 421 self.requirements, self.sharedpath, vfsmod.vfs)
422 422 self.spath = self.store.path
423 423 self.svfs = self.store.vfs
424 424 self.sjoin = self.store.join
425 425 self.vfs.createmode = self.store.createmode
426 426 self.cachevfs = vfsmod.vfs(self.vfs.join('cache'))
427 427 self.cachevfs.createmode = self.store.createmode
428 428 if (self.ui.configbool('devel', 'all-warnings') or
429 429 self.ui.configbool('devel', 'check-locks')):
430 430 if util.safehasattr(self.svfs, 'vfs'): # this is filtervfs
431 431 self.svfs.vfs.audit = self._getsvfsward(self.svfs.vfs.audit)
432 432 else: # standard vfs
433 433 self.svfs.audit = self._getsvfsward(self.svfs.audit)
434 434 self._applyopenerreqs()
435 435 if create:
436 436 self._writerequirements()
437 437
438 438 self._dirstatevalidatewarned = False
439 439
440 440 self._branchcaches = {}
441 441 self._revbranchcache = None
442 442 self.filterpats = {}
443 443 self._datafilters = {}
444 444 self._transref = self._lockref = self._wlockref = None
445 445
446 446 # A cache for various files under .hg/ that tracks file changes,
447 447 # (used by the filecache decorator)
448 448 #
449 449 # Maps a property name to its util.filecacheentry
450 450 self._filecache = {}
451 451
452 452 # hold sets of revision to be filtered
453 453 # should be cleared when something might have changed the filter value:
454 454 # - new changesets,
455 455 # - phase change,
456 456 # - new obsolescence marker,
457 457 # - working directory parent change,
458 458 # - bookmark changes
459 459 self.filteredrevcache = {}
460 460
461 461 # post-dirstate-status hooks
462 462 self._postdsstatus = []
463 463
464 464 # Cache of types representing filtered repos.
465 465 self._filteredrepotypes = weakref.WeakKeyDictionary()
466 466
467 467 # generic mapping between names and nodes
468 468 self.names = namespaces.namespaces()
469 469
470 470 # Key to signature value.
471 471 self._sparsesignaturecache = {}
472 472 # Signature to cached matcher instance.
473 473 self._sparsematchercache = {}
474 474
475 475 def _getvfsward(self, origfunc):
476 476 """build a ward for self.vfs"""
477 477 rref = weakref.ref(self)
478 478 def checkvfs(path, mode=None):
479 479 ret = origfunc(path, mode=mode)
480 480 repo = rref()
481 481 if (repo is None
482 482 or not util.safehasattr(repo, '_wlockref')
483 483 or not util.safehasattr(repo, '_lockref')):
484 484 return
485 485 if mode in (None, 'r', 'rb'):
486 486 return
487 487 if path.startswith(repo.path):
488 488 # truncate name relative to the repository (.hg)
489 489 path = path[len(repo.path) + 1:]
490 if path.startswith('cache/'):
491 msg = 'accessing cache with vfs instead of cachevfs: "%s"'
492 repo.ui.develwarn(msg % path, stacklevel=2, config="cache-vfs")
490 493 if path.startswith('journal.'):
491 494 # journal is covered by 'lock'
492 495 if repo._currentlock(repo._lockref) is None:
493 496 repo.ui.develwarn('write with no lock: "%s"' % path,
494 497 stacklevel=2, config='check-locks')
495 498 elif repo._currentlock(repo._wlockref) is None:
496 499 # rest of vfs files are covered by 'wlock'
497 500 #
498 501 # exclude special files
499 502 for prefix in self._wlockfreeprefix:
500 503 if path.startswith(prefix):
501 504 return
502 505 repo.ui.develwarn('write with no wlock: "%s"' % path,
503 506 stacklevel=2, config='check-locks')
504 507 return ret
505 508 return checkvfs
506 509
507 510 def _getsvfsward(self, origfunc):
508 511 """build a ward for self.svfs"""
509 512 rref = weakref.ref(self)
510 513 def checksvfs(path, mode=None):
511 514 ret = origfunc(path, mode=mode)
512 515 repo = rref()
513 516 if repo is None or not util.safehasattr(repo, '_lockref'):
514 517 return
515 518 if mode in (None, 'r', 'rb'):
516 519 return
517 520 if path.startswith(repo.sharedpath):
518 521 # truncate name relative to the repository (.hg)
519 522 path = path[len(repo.sharedpath) + 1:]
520 523 if repo._currentlock(repo._lockref) is None:
521 524 repo.ui.develwarn('write with no lock: "%s"' % path,
522 525 stacklevel=3)
523 526 return ret
524 527 return checksvfs
525 528
526 529 def close(self):
527 530 self._writecaches()
528 531
529 532 def _loadextensions(self):
530 533 extensions.loadall(self.ui)
531 534
532 535 def _writecaches(self):
533 536 if self._revbranchcache:
534 537 self._revbranchcache.write()
535 538
536 539 def _restrictcapabilities(self, caps):
537 540 if self.ui.configbool('experimental', 'bundle2-advertise'):
538 541 caps = set(caps)
539 542 capsblob = bundle2.encodecaps(bundle2.getrepocaps(self))
540 543 caps.add('bundle2=' + urlreq.quote(capsblob))
541 544 return caps
542 545
543 546 def _applyopenerreqs(self):
544 547 self.svfs.options = dict((r, 1) for r in self.requirements
545 548 if r in self.openerreqs)
546 549 # experimental config: format.chunkcachesize
547 550 chunkcachesize = self.ui.configint('format', 'chunkcachesize')
548 551 if chunkcachesize is not None:
549 552 self.svfs.options['chunkcachesize'] = chunkcachesize
550 553 # experimental config: format.maxchainlen
551 554 maxchainlen = self.ui.configint('format', 'maxchainlen')
552 555 if maxchainlen is not None:
553 556 self.svfs.options['maxchainlen'] = maxchainlen
554 557 # experimental config: format.manifestcachesize
555 558 manifestcachesize = self.ui.configint('format', 'manifestcachesize')
556 559 if manifestcachesize is not None:
557 560 self.svfs.options['manifestcachesize'] = manifestcachesize
558 561 # experimental config: format.aggressivemergedeltas
559 562 aggressivemergedeltas = self.ui.configbool('format',
560 563 'aggressivemergedeltas')
561 564 self.svfs.options['aggressivemergedeltas'] = aggressivemergedeltas
562 565 self.svfs.options['lazydeltabase'] = not scmutil.gddeltaconfig(self.ui)
563 566 chainspan = self.ui.configbytes('experimental', 'maxdeltachainspan', -1)
564 567 if 0 <= chainspan:
565 568 self.svfs.options['maxdeltachainspan'] = chainspan
566 569
567 570 for r in self.requirements:
568 571 if r.startswith('exp-compression-'):
569 572 self.svfs.options['compengine'] = r[len('exp-compression-'):]
570 573
571 574 # TODO move "revlogv2" to openerreqs once finalized.
572 575 if REVLOGV2_REQUIREMENT in self.requirements:
573 576 self.svfs.options['revlogv2'] = True
574 577
575 578 def _writerequirements(self):
576 579 scmutil.writerequires(self.vfs, self.requirements)
577 580
578 581 def _checknested(self, path):
579 582 """Determine if path is a legal nested repository."""
580 583 if not path.startswith(self.root):
581 584 return False
582 585 subpath = path[len(self.root) + 1:]
583 586 normsubpath = util.pconvert(subpath)
584 587
585 588 # XXX: Checking against the current working copy is wrong in
586 589 # the sense that it can reject things like
587 590 #
588 591 # $ hg cat -r 10 sub/x.txt
589 592 #
590 593 # if sub/ is no longer a subrepository in the working copy
591 594 # parent revision.
592 595 #
593 596 # However, it can of course also allow things that would have
594 597 # been rejected before, such as the above cat command if sub/
595 598 # is a subrepository now, but was a normal directory before.
596 599 # The old path auditor would have rejected by mistake since it
597 600 # panics when it sees sub/.hg/.
598 601 #
599 602 # All in all, checking against the working copy seems sensible
600 603 # since we want to prevent access to nested repositories on
601 604 # the filesystem *now*.
602 605 ctx = self[None]
603 606 parts = util.splitpath(subpath)
604 607 while parts:
605 608 prefix = '/'.join(parts)
606 609 if prefix in ctx.substate:
607 610 if prefix == normsubpath:
608 611 return True
609 612 else:
610 613 sub = ctx.sub(prefix)
611 614 return sub.checknested(subpath[len(prefix) + 1:])
612 615 else:
613 616 parts.pop()
614 617 return False
615 618
616 619 def peer(self):
617 620 return localpeer(self) # not cached to avoid reference cycle
618 621
619 622 def unfiltered(self):
620 623 """Return unfiltered version of the repository
621 624
622 625 Intended to be overwritten by filtered repo."""
623 626 return self
624 627
625 628 def filtered(self, name):
626 629 """Return a filtered version of a repository"""
627 630 # Python <3.4 easily leaks types via __mro__. See
628 631 # https://bugs.python.org/issue17950. We cache dynamically
629 632 # created types so this method doesn't leak on every
630 633 # invocation.
631 634
632 635 key = self.unfiltered().__class__
633 636 if key not in self._filteredrepotypes:
634 637 # Build a new type with the repoview mixin and the base
635 638 # class of this repo. Give it a name containing the
636 639 # filter name to aid debugging.
637 640 bases = (repoview.repoview, key)
638 641 cls = type(r'%sfilteredrepo' % name, bases, {})
639 642 self._filteredrepotypes[key] = cls
640 643
641 644 return self._filteredrepotypes[key](self, name)
642 645
643 646 @repofilecache('bookmarks', 'bookmarks.current')
644 647 def _bookmarks(self):
645 648 return bookmarks.bmstore(self)
646 649
647 650 @property
648 651 def _activebookmark(self):
649 652 return self._bookmarks.active
650 653
651 654 # _phaserevs and _phasesets depend on changelog. what we need is to
652 655 # call _phasecache.invalidate() if '00changelog.i' was changed, but it
653 656 # can't be easily expressed in filecache mechanism.
654 657 @storecache('phaseroots', '00changelog.i')
655 658 def _phasecache(self):
656 659 return phases.phasecache(self, self._phasedefaults)
657 660
658 661 @storecache('obsstore')
659 662 def obsstore(self):
660 663 return obsolete.makestore(self.ui, self)
661 664
662 665 @storecache('00changelog.i')
663 666 def changelog(self):
664 667 return changelog.changelog(self.svfs,
665 668 trypending=txnutil.mayhavepending(self.root))
666 669
667 670 def _constructmanifest(self):
668 671 # This is a temporary function while we migrate from manifest to
669 672 # manifestlog. It allows bundlerepo and unionrepo to intercept the
670 673 # manifest creation.
671 674 return manifest.manifestrevlog(self.svfs)
672 675
673 676 @storecache('00manifest.i')
674 677 def manifestlog(self):
675 678 return manifest.manifestlog(self.svfs, self)
676 679
677 680 @repofilecache('dirstate')
678 681 def dirstate(self):
679 682 sparsematchfn = lambda: sparse.matcher(self)
680 683
681 684 return dirstate.dirstate(self.vfs, self.ui, self.root,
682 685 self._dirstatevalidate, sparsematchfn)
683 686
684 687 def _dirstatevalidate(self, node):
685 688 try:
686 689 self.changelog.rev(node)
687 690 return node
688 691 except error.LookupError:
689 692 if not self._dirstatevalidatewarned:
690 693 self._dirstatevalidatewarned = True
691 694 self.ui.warn(_("warning: ignoring unknown"
692 695 " working parent %s!\n") % short(node))
693 696 return nullid
694 697
695 698 def __getitem__(self, changeid):
696 699 if changeid is None:
697 700 return context.workingctx(self)
698 701 if isinstance(changeid, slice):
699 702 # wdirrev isn't contiguous so the slice shouldn't include it
700 703 return [context.changectx(self, i)
701 704 for i in xrange(*changeid.indices(len(self)))
702 705 if i not in self.changelog.filteredrevs]
703 706 try:
704 707 return context.changectx(self, changeid)
705 708 except error.WdirUnsupported:
706 709 return context.workingctx(self)
707 710
708 711 def __contains__(self, changeid):
709 712 """True if the given changeid exists
710 713
711 714 error.LookupError is raised if an ambiguous node specified.
712 715 """
713 716 try:
714 717 self[changeid]
715 718 return True
716 719 except error.RepoLookupError:
717 720 return False
718 721
719 722 def __nonzero__(self):
720 723 return True
721 724
722 725 __bool__ = __nonzero__
723 726
724 727 def __len__(self):
725 728 return len(self.changelog)
726 729
727 730 def __iter__(self):
728 731 return iter(self.changelog)
729 732
730 733 def revs(self, expr, *args):
731 734 '''Find revisions matching a revset.
732 735
733 736 The revset is specified as a string ``expr`` that may contain
734 737 %-formatting to escape certain types. See ``revsetlang.formatspec``.
735 738
736 739 Revset aliases from the configuration are not expanded. To expand
737 740 user aliases, consider calling ``scmutil.revrange()`` or
738 741 ``repo.anyrevs([expr], user=True)``.
739 742
740 743 Returns a revset.abstractsmartset, which is a list-like interface
741 744 that contains integer revisions.
742 745 '''
743 746 expr = revsetlang.formatspec(expr, *args)
744 747 m = revset.match(None, expr)
745 748 return m(self)
746 749
747 750 def set(self, expr, *args):
748 751 '''Find revisions matching a revset and emit changectx instances.
749 752
750 753 This is a convenience wrapper around ``revs()`` that iterates the
751 754 result and is a generator of changectx instances.
752 755
753 756 Revset aliases from the configuration are not expanded. To expand
754 757 user aliases, consider calling ``scmutil.revrange()``.
755 758 '''
756 759 for r in self.revs(expr, *args):
757 760 yield self[r]
758 761
759 762 def anyrevs(self, specs, user=False, localalias=None):
760 763 '''Find revisions matching one of the given revsets.
761 764
762 765 Revset aliases from the configuration are not expanded by default. To
763 766 expand user aliases, specify ``user=True``. To provide some local
764 767 definitions overriding user aliases, set ``localalias`` to
765 768 ``{name: definitionstring}``.
766 769 '''
767 770 if user:
768 771 m = revset.matchany(self.ui, specs, repo=self,
769 772 localalias=localalias)
770 773 else:
771 774 m = revset.matchany(None, specs, localalias=localalias)
772 775 return m(self)
773 776
774 777 def url(self):
775 778 return 'file:' + self.root
776 779
777 780 def hook(self, name, throw=False, **args):
778 781 """Call a hook, passing this repo instance.
779 782
780 783 This a convenience method to aid invoking hooks. Extensions likely
781 784 won't call this unless they have registered a custom hook or are
782 785 replacing code that is expected to call a hook.
783 786 """
784 787 return hook.hook(self.ui, self, name, throw, **args)
785 788
786 789 @filteredpropertycache
787 790 def _tagscache(self):
788 791 '''Returns a tagscache object that contains various tags related
789 792 caches.'''
790 793
791 794 # This simplifies its cache management by having one decorated
792 795 # function (this one) and the rest simply fetch things from it.
793 796 class tagscache(object):
794 797 def __init__(self):
795 798 # These two define the set of tags for this repository. tags
796 799 # maps tag name to node; tagtypes maps tag name to 'global' or
797 800 # 'local'. (Global tags are defined by .hgtags across all
798 801 # heads, and local tags are defined in .hg/localtags.)
799 802 # They constitute the in-memory cache of tags.
800 803 self.tags = self.tagtypes = None
801 804
802 805 self.nodetagscache = self.tagslist = None
803 806
804 807 cache = tagscache()
805 808 cache.tags, cache.tagtypes = self._findtags()
806 809
807 810 return cache
808 811
809 812 def tags(self):
810 813 '''return a mapping of tag to node'''
811 814 t = {}
812 815 if self.changelog.filteredrevs:
813 816 tags, tt = self._findtags()
814 817 else:
815 818 tags = self._tagscache.tags
816 819 for k, v in tags.iteritems():
817 820 try:
818 821 # ignore tags to unknown nodes
819 822 self.changelog.rev(v)
820 823 t[k] = v
821 824 except (error.LookupError, ValueError):
822 825 pass
823 826 return t
824 827
825 828 def _findtags(self):
826 829 '''Do the hard work of finding tags. Return a pair of dicts
827 830 (tags, tagtypes) where tags maps tag name to node, and tagtypes
828 831 maps tag name to a string like \'global\' or \'local\'.
829 832 Subclasses or extensions are free to add their own tags, but
830 833 should be aware that the returned dicts will be retained for the
831 834 duration of the localrepo object.'''
832 835
833 836 # XXX what tagtype should subclasses/extensions use? Currently
834 837 # mq and bookmarks add tags, but do not set the tagtype at all.
835 838 # Should each extension invent its own tag type? Should there
836 839 # be one tagtype for all such "virtual" tags? Or is the status
837 840 # quo fine?
838 841
839 842
840 843 # map tag name to (node, hist)
841 844 alltags = tagsmod.findglobaltags(self.ui, self)
842 845 # map tag name to tag type
843 846 tagtypes = dict((tag, 'global') for tag in alltags)
844 847
845 848 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
846 849
847 850 # Build the return dicts. Have to re-encode tag names because
848 851 # the tags module always uses UTF-8 (in order not to lose info
849 852 # writing to the cache), but the rest of Mercurial wants them in
850 853 # local encoding.
851 854 tags = {}
852 855 for (name, (node, hist)) in alltags.iteritems():
853 856 if node != nullid:
854 857 tags[encoding.tolocal(name)] = node
855 858 tags['tip'] = self.changelog.tip()
856 859 tagtypes = dict([(encoding.tolocal(name), value)
857 860 for (name, value) in tagtypes.iteritems()])
858 861 return (tags, tagtypes)
859 862
860 863 def tagtype(self, tagname):
861 864 '''
862 865 return the type of the given tag. result can be:
863 866
864 867 'local' : a local tag
865 868 'global' : a global tag
866 869 None : tag does not exist
867 870 '''
868 871
869 872 return self._tagscache.tagtypes.get(tagname)
870 873
871 874 def tagslist(self):
872 875 '''return a list of tags ordered by revision'''
873 876 if not self._tagscache.tagslist:
874 877 l = []
875 878 for t, n in self.tags().iteritems():
876 879 l.append((self.changelog.rev(n), t, n))
877 880 self._tagscache.tagslist = [(t, n) for r, t, n in sorted(l)]
878 881
879 882 return self._tagscache.tagslist
880 883
881 884 def nodetags(self, node):
882 885 '''return the tags associated with a node'''
883 886 if not self._tagscache.nodetagscache:
884 887 nodetagscache = {}
885 888 for t, n in self._tagscache.tags.iteritems():
886 889 nodetagscache.setdefault(n, []).append(t)
887 890 for tags in nodetagscache.itervalues():
888 891 tags.sort()
889 892 self._tagscache.nodetagscache = nodetagscache
890 893 return self._tagscache.nodetagscache.get(node, [])
891 894
892 895 def nodebookmarks(self, node):
893 896 """return the list of bookmarks pointing to the specified node"""
894 897 marks = []
895 898 for bookmark, n in self._bookmarks.iteritems():
896 899 if n == node:
897 900 marks.append(bookmark)
898 901 return sorted(marks)
899 902
900 903 def branchmap(self):
901 904 '''returns a dictionary {branch: [branchheads]} with branchheads
902 905 ordered by increasing revision number'''
903 906 branchmap.updatecache(self)
904 907 return self._branchcaches[self.filtername]
905 908
906 909 @unfilteredmethod
907 910 def revbranchcache(self):
908 911 if not self._revbranchcache:
909 912 self._revbranchcache = branchmap.revbranchcache(self.unfiltered())
910 913 return self._revbranchcache
911 914
912 915 def branchtip(self, branch, ignoremissing=False):
913 916 '''return the tip node for a given branch
914 917
915 918 If ignoremissing is True, then this method will not raise an error.
916 919 This is helpful for callers that only expect None for a missing branch
917 920 (e.g. namespace).
918 921
919 922 '''
920 923 try:
921 924 return self.branchmap().branchtip(branch)
922 925 except KeyError:
923 926 if not ignoremissing:
924 927 raise error.RepoLookupError(_("unknown branch '%s'") % branch)
925 928 else:
926 929 pass
927 930
928 931 def lookup(self, key):
929 932 return self[key].node()
930 933
931 934 def lookupbranch(self, key, remote=None):
932 935 repo = remote or self
933 936 if key in repo.branchmap():
934 937 return key
935 938
936 939 repo = (remote and remote.local()) and remote or self
937 940 return repo[key].branch()
938 941
939 942 def known(self, nodes):
940 943 cl = self.changelog
941 944 nm = cl.nodemap
942 945 filtered = cl.filteredrevs
943 946 result = []
944 947 for n in nodes:
945 948 r = nm.get(n)
946 949 resp = not (r is None or r in filtered)
947 950 result.append(resp)
948 951 return result
949 952
950 953 def local(self):
951 954 return self
952 955
953 956 def publishing(self):
954 957 # it's safe (and desirable) to trust the publish flag unconditionally
955 958 # so that we don't finalize changes shared between users via ssh or nfs
956 959 return self.ui.configbool('phases', 'publish', untrusted=True)
957 960
958 961 def cancopy(self):
959 962 # so statichttprepo's override of local() works
960 963 if not self.local():
961 964 return False
962 965 if not self.publishing():
963 966 return True
964 967 # if publishing we can't copy if there is filtered content
965 968 return not self.filtered('visible').changelog.filteredrevs
966 969
967 970 def shared(self):
968 971 '''the type of shared repository (None if not shared)'''
969 972 if self.sharedpath != self.path:
970 973 return 'store'
971 974 return None
972 975
973 976 def wjoin(self, f, *insidef):
974 977 return self.vfs.reljoin(self.root, f, *insidef)
975 978
976 979 def file(self, f):
977 980 if f[0] == '/':
978 981 f = f[1:]
979 982 return filelog.filelog(self.svfs, f)
980 983
981 984 def changectx(self, changeid):
982 985 return self[changeid]
983 986
984 987 def setparents(self, p1, p2=nullid):
985 988 with self.dirstate.parentchange():
986 989 copies = self.dirstate.setparents(p1, p2)
987 990 pctx = self[p1]
988 991 if copies:
989 992 # Adjust copy records, the dirstate cannot do it, it
990 993 # requires access to parents manifests. Preserve them
991 994 # only for entries added to first parent.
992 995 for f in copies:
993 996 if f not in pctx and copies[f] in pctx:
994 997 self.dirstate.copy(copies[f], f)
995 998 if p2 == nullid:
996 999 for f, s in sorted(self.dirstate.copies().items()):
997 1000 if f not in pctx and s not in pctx:
998 1001 self.dirstate.copy(None, f)
999 1002
1000 1003 def filectx(self, path, changeid=None, fileid=None):
1001 1004 """changeid can be a changeset revision, node, or tag.
1002 1005 fileid can be a file revision or node."""
1003 1006 return context.filectx(self, path, changeid, fileid)
1004 1007
1005 1008 def getcwd(self):
1006 1009 return self.dirstate.getcwd()
1007 1010
1008 1011 def pathto(self, f, cwd=None):
1009 1012 return self.dirstate.pathto(f, cwd)
1010 1013
1011 1014 def _loadfilter(self, filter):
1012 1015 if filter not in self.filterpats:
1013 1016 l = []
1014 1017 for pat, cmd in self.ui.configitems(filter):
1015 1018 if cmd == '!':
1016 1019 continue
1017 1020 mf = matchmod.match(self.root, '', [pat])
1018 1021 fn = None
1019 1022 params = cmd
1020 1023 for name, filterfn in self._datafilters.iteritems():
1021 1024 if cmd.startswith(name):
1022 1025 fn = filterfn
1023 1026 params = cmd[len(name):].lstrip()
1024 1027 break
1025 1028 if not fn:
1026 1029 fn = lambda s, c, **kwargs: util.filter(s, c)
1027 1030 # Wrap old filters not supporting keyword arguments
1028 1031 if not inspect.getargspec(fn)[2]:
1029 1032 oldfn = fn
1030 1033 fn = lambda s, c, **kwargs: oldfn(s, c)
1031 1034 l.append((mf, fn, params))
1032 1035 self.filterpats[filter] = l
1033 1036 return self.filterpats[filter]
1034 1037
1035 1038 def _filter(self, filterpats, filename, data):
1036 1039 for mf, fn, cmd in filterpats:
1037 1040 if mf(filename):
1038 1041 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
1039 1042 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
1040 1043 break
1041 1044
1042 1045 return data
1043 1046
1044 1047 @unfilteredpropertycache
1045 1048 def _encodefilterpats(self):
1046 1049 return self._loadfilter('encode')
1047 1050
1048 1051 @unfilteredpropertycache
1049 1052 def _decodefilterpats(self):
1050 1053 return self._loadfilter('decode')
1051 1054
1052 1055 def adddatafilter(self, name, filter):
1053 1056 self._datafilters[name] = filter
1054 1057
1055 1058 def wread(self, filename):
1056 1059 if self.wvfs.islink(filename):
1057 1060 data = self.wvfs.readlink(filename)
1058 1061 else:
1059 1062 data = self.wvfs.read(filename)
1060 1063 return self._filter(self._encodefilterpats, filename, data)
1061 1064
1062 1065 def wwrite(self, filename, data, flags, backgroundclose=False):
1063 1066 """write ``data`` into ``filename`` in the working directory
1064 1067
1065 1068 This returns length of written (maybe decoded) data.
1066 1069 """
1067 1070 data = self._filter(self._decodefilterpats, filename, data)
1068 1071 if 'l' in flags:
1069 1072 self.wvfs.symlink(data, filename)
1070 1073 else:
1071 1074 self.wvfs.write(filename, data, backgroundclose=backgroundclose)
1072 1075 if 'x' in flags:
1073 1076 self.wvfs.setflags(filename, False, True)
1074 1077 return len(data)
1075 1078
1076 1079 def wwritedata(self, filename, data):
1077 1080 return self._filter(self._decodefilterpats, filename, data)
1078 1081
1079 1082 def currenttransaction(self):
1080 1083 """return the current transaction or None if non exists"""
1081 1084 if self._transref:
1082 1085 tr = self._transref()
1083 1086 else:
1084 1087 tr = None
1085 1088
1086 1089 if tr and tr.running():
1087 1090 return tr
1088 1091 return None
1089 1092
1090 1093 def transaction(self, desc, report=None):
1091 1094 if (self.ui.configbool('devel', 'all-warnings')
1092 1095 or self.ui.configbool('devel', 'check-locks')):
1093 1096 if self._currentlock(self._lockref) is None:
1094 1097 raise error.ProgrammingError('transaction requires locking')
1095 1098 tr = self.currenttransaction()
1096 1099 if tr is not None:
1097 1100 return tr.nest()
1098 1101
1099 1102 # abort here if the journal already exists
1100 1103 if self.svfs.exists("journal"):
1101 1104 raise error.RepoError(
1102 1105 _("abandoned transaction found"),
1103 1106 hint=_("run 'hg recover' to clean up transaction"))
1104 1107
1105 1108 idbase = "%.40f#%f" % (random.random(), time.time())
1106 1109 ha = hex(hashlib.sha1(idbase).digest())
1107 1110 txnid = 'TXN:' + ha
1108 1111 self.hook('pretxnopen', throw=True, txnname=desc, txnid=txnid)
1109 1112
1110 1113 self._writejournal(desc)
1111 1114 renames = [(vfs, x, undoname(x)) for vfs, x in self._journalfiles()]
1112 1115 if report:
1113 1116 rp = report
1114 1117 else:
1115 1118 rp = self.ui.warn
1116 1119 vfsmap = {'plain': self.vfs} # root of .hg/
1117 1120 # we must avoid cyclic reference between repo and transaction.
1118 1121 reporef = weakref.ref(self)
1119 1122 # Code to track tag movement
1120 1123 #
1121 1124 # Since tags are all handled as file content, it is actually quite hard
1122 1125 # to track these movement from a code perspective. So we fallback to a
1123 1126 # tracking at the repository level. One could envision to track changes
1124 1127 # to the '.hgtags' file through changegroup apply but that fails to
1125 1128 # cope with case where transaction expose new heads without changegroup
1126 1129 # being involved (eg: phase movement).
1127 1130 #
1128 1131 # For now, We gate the feature behind a flag since this likely comes
1129 1132 # with performance impacts. The current code run more often than needed
1130 1133 # and do not use caches as much as it could. The current focus is on
1131 1134 # the behavior of the feature so we disable it by default. The flag
1132 1135 # will be removed when we are happy with the performance impact.
1133 1136 #
1134 1137 # Once this feature is no longer experimental move the following
1135 1138 # documentation to the appropriate help section:
1136 1139 #
1137 1140 # The ``HG_TAG_MOVED`` variable will be set if the transaction touched
1138 1141 # tags (new or changed or deleted tags). In addition the details of
1139 1142 # these changes are made available in a file at:
1140 1143 # ``REPOROOT/.hg/changes/tags.changes``.
1141 1144 # Make sure you check for HG_TAG_MOVED before reading that file as it
1142 1145 # might exist from a previous transaction even if no tag were touched
1143 1146 # in this one. Changes are recorded in a line base format::
1144 1147 #
1145 1148 # <action> <hex-node> <tag-name>\n
1146 1149 #
1147 1150 # Actions are defined as follow:
1148 1151 # "-R": tag is removed,
1149 1152 # "+A": tag is added,
1150 1153 # "-M": tag is moved (old value),
1151 1154 # "+M": tag is moved (new value),
1152 1155 tracktags = lambda x: None
1153 1156 # experimental config: experimental.hook-track-tags
1154 1157 shouldtracktags = self.ui.configbool('experimental', 'hook-track-tags')
1155 1158 if desc != 'strip' and shouldtracktags:
1156 1159 oldheads = self.changelog.headrevs()
1157 1160 def tracktags(tr2):
1158 1161 repo = reporef()
1159 1162 oldfnodes = tagsmod.fnoderevs(repo.ui, repo, oldheads)
1160 1163 newheads = repo.changelog.headrevs()
1161 1164 newfnodes = tagsmod.fnoderevs(repo.ui, repo, newheads)
1162 1165 # notes: we compare lists here.
1163 1166 # As we do it only once buiding set would not be cheaper
1164 1167 changes = tagsmod.difftags(repo.ui, repo, oldfnodes, newfnodes)
1165 1168 if changes:
1166 1169 tr2.hookargs['tag_moved'] = '1'
1167 1170 with repo.vfs('changes/tags.changes', 'w',
1168 1171 atomictemp=True) as changesfile:
1169 1172 # note: we do not register the file to the transaction
1170 1173 # because we needs it to still exist on the transaction
1171 1174 # is close (for txnclose hooks)
1172 1175 tagsmod.writediff(changesfile, changes)
1173 1176 def validate(tr2):
1174 1177 """will run pre-closing hooks"""
1175 1178 # XXX the transaction API is a bit lacking here so we take a hacky
1176 1179 # path for now
1177 1180 #
1178 1181 # We cannot add this as a "pending" hooks since the 'tr.hookargs'
1179 1182 # dict is copied before these run. In addition we needs the data
1180 1183 # available to in memory hooks too.
1181 1184 #
1182 1185 # Moreover, we also need to make sure this runs before txnclose
1183 1186 # hooks and there is no "pending" mechanism that would execute
1184 1187 # logic only if hooks are about to run.
1185 1188 #
1186 1189 # Fixing this limitation of the transaction is also needed to track
1187 1190 # other families of changes (bookmarks, phases, obsolescence).
1188 1191 #
1189 1192 # This will have to be fixed before we remove the experimental
1190 1193 # gating.
1191 1194 tracktags(tr2)
1192 1195 reporef().hook('pretxnclose', throw=True,
1193 1196 txnname=desc, **pycompat.strkwargs(tr.hookargs))
1194 1197 def releasefn(tr, success):
1195 1198 repo = reporef()
1196 1199 if success:
1197 1200 # this should be explicitly invoked here, because
1198 1201 # in-memory changes aren't written out at closing
1199 1202 # transaction, if tr.addfilegenerator (via
1200 1203 # dirstate.write or so) isn't invoked while
1201 1204 # transaction running
1202 1205 repo.dirstate.write(None)
1203 1206 else:
1204 1207 # discard all changes (including ones already written
1205 1208 # out) in this transaction
1206 1209 repo.dirstate.restorebackup(None, 'journal.dirstate')
1207 1210
1208 1211 repo.invalidate(clearfilecache=True)
1209 1212
1210 1213 tr = transaction.transaction(rp, self.svfs, vfsmap,
1211 1214 "journal",
1212 1215 "undo",
1213 1216 aftertrans(renames),
1214 1217 self.store.createmode,
1215 1218 validator=validate,
1216 1219 releasefn=releasefn,
1217 1220 checkambigfiles=_cachedfiles)
1218 1221 tr.changes['revs'] = set()
1219 1222 tr.changes['obsmarkers'] = set()
1220 1223 tr.changes['phases'] = {}
1221 1224 tr.changes['bookmarks'] = {}
1222 1225
1223 1226 tr.hookargs['txnid'] = txnid
1224 1227 # note: writing the fncache only during finalize mean that the file is
1225 1228 # outdated when running hooks. As fncache is used for streaming clone,
1226 1229 # this is not expected to break anything that happen during the hooks.
1227 1230 tr.addfinalize('flush-fncache', self.store.write)
1228 1231 def txnclosehook(tr2):
1229 1232 """To be run if transaction is successful, will schedule a hook run
1230 1233 """
1231 1234 # Don't reference tr2 in hook() so we don't hold a reference.
1232 1235 # This reduces memory consumption when there are multiple
1233 1236 # transactions per lock. This can likely go away if issue5045
1234 1237 # fixes the function accumulation.
1235 1238 hookargs = tr2.hookargs
1236 1239
1237 1240 def hook():
1238 1241 reporef().hook('txnclose', throw=False, txnname=desc,
1239 1242 **pycompat.strkwargs(hookargs))
1240 1243 reporef()._afterlock(hook)
1241 1244 tr.addfinalize('txnclose-hook', txnclosehook)
1242 1245 tr.addpostclose('warms-cache', self._buildcacheupdater(tr))
1243 1246 def txnaborthook(tr2):
1244 1247 """To be run if transaction is aborted
1245 1248 """
1246 1249 reporef().hook('txnabort', throw=False, txnname=desc,
1247 1250 **tr2.hookargs)
1248 1251 tr.addabort('txnabort-hook', txnaborthook)
1249 1252 # avoid eager cache invalidation. in-memory data should be identical
1250 1253 # to stored data if transaction has no error.
1251 1254 tr.addpostclose('refresh-filecachestats', self._refreshfilecachestats)
1252 1255 self._transref = weakref.ref(tr)
1253 1256 return tr
1254 1257
1255 1258 def _journalfiles(self):
1256 1259 return ((self.svfs, 'journal'),
1257 1260 (self.vfs, 'journal.dirstate'),
1258 1261 (self.vfs, 'journal.branch'),
1259 1262 (self.vfs, 'journal.desc'),
1260 1263 (self.vfs, 'journal.bookmarks'),
1261 1264 (self.svfs, 'journal.phaseroots'))
1262 1265
1263 1266 def undofiles(self):
1264 1267 return [(vfs, undoname(x)) for vfs, x in self._journalfiles()]
1265 1268
1266 1269 @unfilteredmethod
1267 1270 def _writejournal(self, desc):
1268 1271 self.dirstate.savebackup(None, 'journal.dirstate')
1269 1272 self.vfs.write("journal.branch",
1270 1273 encoding.fromlocal(self.dirstate.branch()))
1271 1274 self.vfs.write("journal.desc",
1272 1275 "%d\n%s\n" % (len(self), desc))
1273 1276 self.vfs.write("journal.bookmarks",
1274 1277 self.vfs.tryread("bookmarks"))
1275 1278 self.svfs.write("journal.phaseroots",
1276 1279 self.svfs.tryread("phaseroots"))
1277 1280
1278 1281 def recover(self):
1279 1282 with self.lock():
1280 1283 if self.svfs.exists("journal"):
1281 1284 self.ui.status(_("rolling back interrupted transaction\n"))
1282 1285 vfsmap = {'': self.svfs,
1283 1286 'plain': self.vfs,}
1284 1287 transaction.rollback(self.svfs, vfsmap, "journal",
1285 1288 self.ui.warn,
1286 1289 checkambigfiles=_cachedfiles)
1287 1290 self.invalidate()
1288 1291 return True
1289 1292 else:
1290 1293 self.ui.warn(_("no interrupted transaction available\n"))
1291 1294 return False
1292 1295
1293 1296 def rollback(self, dryrun=False, force=False):
1294 1297 wlock = lock = dsguard = None
1295 1298 try:
1296 1299 wlock = self.wlock()
1297 1300 lock = self.lock()
1298 1301 if self.svfs.exists("undo"):
1299 1302 dsguard = dirstateguard.dirstateguard(self, 'rollback')
1300 1303
1301 1304 return self._rollback(dryrun, force, dsguard)
1302 1305 else:
1303 1306 self.ui.warn(_("no rollback information available\n"))
1304 1307 return 1
1305 1308 finally:
1306 1309 release(dsguard, lock, wlock)
1307 1310
1308 1311 @unfilteredmethod # Until we get smarter cache management
1309 1312 def _rollback(self, dryrun, force, dsguard):
1310 1313 ui = self.ui
1311 1314 try:
1312 1315 args = self.vfs.read('undo.desc').splitlines()
1313 1316 (oldlen, desc, detail) = (int(args[0]), args[1], None)
1314 1317 if len(args) >= 3:
1315 1318 detail = args[2]
1316 1319 oldtip = oldlen - 1
1317 1320
1318 1321 if detail and ui.verbose:
1319 1322 msg = (_('repository tip rolled back to revision %d'
1320 1323 ' (undo %s: %s)\n')
1321 1324 % (oldtip, desc, detail))
1322 1325 else:
1323 1326 msg = (_('repository tip rolled back to revision %d'
1324 1327 ' (undo %s)\n')
1325 1328 % (oldtip, desc))
1326 1329 except IOError:
1327 1330 msg = _('rolling back unknown transaction\n')
1328 1331 desc = None
1329 1332
1330 1333 if not force and self['.'] != self['tip'] and desc == 'commit':
1331 1334 raise error.Abort(
1332 1335 _('rollback of last commit while not checked out '
1333 1336 'may lose data'), hint=_('use -f to force'))
1334 1337
1335 1338 ui.status(msg)
1336 1339 if dryrun:
1337 1340 return 0
1338 1341
1339 1342 parents = self.dirstate.parents()
1340 1343 self.destroying()
1341 1344 vfsmap = {'plain': self.vfs, '': self.svfs}
1342 1345 transaction.rollback(self.svfs, vfsmap, 'undo', ui.warn,
1343 1346 checkambigfiles=_cachedfiles)
1344 1347 if self.vfs.exists('undo.bookmarks'):
1345 1348 self.vfs.rename('undo.bookmarks', 'bookmarks', checkambig=True)
1346 1349 if self.svfs.exists('undo.phaseroots'):
1347 1350 self.svfs.rename('undo.phaseroots', 'phaseroots', checkambig=True)
1348 1351 self.invalidate()
1349 1352
1350 1353 parentgone = (parents[0] not in self.changelog.nodemap or
1351 1354 parents[1] not in self.changelog.nodemap)
1352 1355 if parentgone:
1353 1356 # prevent dirstateguard from overwriting already restored one
1354 1357 dsguard.close()
1355 1358
1356 1359 self.dirstate.restorebackup(None, 'undo.dirstate')
1357 1360 try:
1358 1361 branch = self.vfs.read('undo.branch')
1359 1362 self.dirstate.setbranch(encoding.tolocal(branch))
1360 1363 except IOError:
1361 1364 ui.warn(_('named branch could not be reset: '
1362 1365 'current branch is still \'%s\'\n')
1363 1366 % self.dirstate.branch())
1364 1367
1365 1368 parents = tuple([p.rev() for p in self[None].parents()])
1366 1369 if len(parents) > 1:
1367 1370 ui.status(_('working directory now based on '
1368 1371 'revisions %d and %d\n') % parents)
1369 1372 else:
1370 1373 ui.status(_('working directory now based on '
1371 1374 'revision %d\n') % parents)
1372 1375 mergemod.mergestate.clean(self, self['.'].node())
1373 1376
1374 1377 # TODO: if we know which new heads may result from this rollback, pass
1375 1378 # them to destroy(), which will prevent the branchhead cache from being
1376 1379 # invalidated.
1377 1380 self.destroyed()
1378 1381 return 0
1379 1382
1380 1383 def _buildcacheupdater(self, newtransaction):
1381 1384 """called during transaction to build the callback updating cache
1382 1385
1383 1386 Lives on the repository to help extension who might want to augment
1384 1387 this logic. For this purpose, the created transaction is passed to the
1385 1388 method.
1386 1389 """
1387 1390 # we must avoid cyclic reference between repo and transaction.
1388 1391 reporef = weakref.ref(self)
1389 1392 def updater(tr):
1390 1393 repo = reporef()
1391 1394 repo.updatecaches(tr)
1392 1395 return updater
1393 1396
1394 1397 @unfilteredmethod
1395 1398 def updatecaches(self, tr=None):
1396 1399 """warm appropriate caches
1397 1400
1398 1401 If this function is called after a transaction closed. The transaction
1399 1402 will be available in the 'tr' argument. This can be used to selectively
1400 1403 update caches relevant to the changes in that transaction.
1401 1404 """
1402 1405 if tr is not None and tr.hookargs.get('source') == 'strip':
1403 1406 # During strip, many caches are invalid but
1404 1407 # later call to `destroyed` will refresh them.
1405 1408 return
1406 1409
1407 1410 if tr is None or tr.changes['revs']:
1408 1411 # updating the unfiltered branchmap should refresh all the others,
1409 1412 self.ui.debug('updating the branch cache\n')
1410 1413 branchmap.updatecache(self.filtered('served'))
1411 1414
1412 1415 def invalidatecaches(self):
1413 1416
1414 1417 if '_tagscache' in vars(self):
1415 1418 # can't use delattr on proxy
1416 1419 del self.__dict__['_tagscache']
1417 1420
1418 1421 self.unfiltered()._branchcaches.clear()
1419 1422 self.invalidatevolatilesets()
1420 1423 self._sparsesignaturecache.clear()
1421 1424
1422 1425 def invalidatevolatilesets(self):
1423 1426 self.filteredrevcache.clear()
1424 1427 obsolete.clearobscaches(self)
1425 1428
1426 1429 def invalidatedirstate(self):
1427 1430 '''Invalidates the dirstate, causing the next call to dirstate
1428 1431 to check if it was modified since the last time it was read,
1429 1432 rereading it if it has.
1430 1433
1431 1434 This is different to dirstate.invalidate() that it doesn't always
1432 1435 rereads the dirstate. Use dirstate.invalidate() if you want to
1433 1436 explicitly read the dirstate again (i.e. restoring it to a previous
1434 1437 known good state).'''
1435 1438 if hasunfilteredcache(self, 'dirstate'):
1436 1439 for k in self.dirstate._filecache:
1437 1440 try:
1438 1441 delattr(self.dirstate, k)
1439 1442 except AttributeError:
1440 1443 pass
1441 1444 delattr(self.unfiltered(), 'dirstate')
1442 1445
1443 1446 def invalidate(self, clearfilecache=False):
1444 1447 '''Invalidates both store and non-store parts other than dirstate
1445 1448
1446 1449 If a transaction is running, invalidation of store is omitted,
1447 1450 because discarding in-memory changes might cause inconsistency
1448 1451 (e.g. incomplete fncache causes unintentional failure, but
1449 1452 redundant one doesn't).
1450 1453 '''
1451 1454 unfiltered = self.unfiltered() # all file caches are stored unfiltered
1452 1455 for k in list(self._filecache.keys()):
1453 1456 # dirstate is invalidated separately in invalidatedirstate()
1454 1457 if k == 'dirstate':
1455 1458 continue
1456 1459
1457 1460 if clearfilecache:
1458 1461 del self._filecache[k]
1459 1462 try:
1460 1463 delattr(unfiltered, k)
1461 1464 except AttributeError:
1462 1465 pass
1463 1466 self.invalidatecaches()
1464 1467 if not self.currenttransaction():
1465 1468 # TODO: Changing contents of store outside transaction
1466 1469 # causes inconsistency. We should make in-memory store
1467 1470 # changes detectable, and abort if changed.
1468 1471 self.store.invalidatecaches()
1469 1472
1470 1473 def invalidateall(self):
1471 1474 '''Fully invalidates both store and non-store parts, causing the
1472 1475 subsequent operation to reread any outside changes.'''
1473 1476 # extension should hook this to invalidate its caches
1474 1477 self.invalidate()
1475 1478 self.invalidatedirstate()
1476 1479
1477 1480 @unfilteredmethod
1478 1481 def _refreshfilecachestats(self, tr):
1479 1482 """Reload stats of cached files so that they are flagged as valid"""
1480 1483 for k, ce in self._filecache.items():
1481 1484 if k == 'dirstate' or k not in self.__dict__:
1482 1485 continue
1483 1486 ce.refresh()
1484 1487
1485 1488 def _lock(self, vfs, lockname, wait, releasefn, acquirefn, desc,
1486 1489 inheritchecker=None, parentenvvar=None):
1487 1490 parentlock = None
1488 1491 # the contents of parentenvvar are used by the underlying lock to
1489 1492 # determine whether it can be inherited
1490 1493 if parentenvvar is not None:
1491 1494 parentlock = encoding.environ.get(parentenvvar)
1492 1495 try:
1493 1496 l = lockmod.lock(vfs, lockname, 0, releasefn=releasefn,
1494 1497 acquirefn=acquirefn, desc=desc,
1495 1498 inheritchecker=inheritchecker,
1496 1499 parentlock=parentlock)
1497 1500 except error.LockHeld as inst:
1498 1501 if not wait:
1499 1502 raise
1500 1503 # show more details for new-style locks
1501 1504 if ':' in inst.locker:
1502 1505 host, pid = inst.locker.split(":", 1)
1503 1506 self.ui.warn(
1504 1507 _("waiting for lock on %s held by process %r "
1505 1508 "on host %r\n") % (desc, pid, host))
1506 1509 else:
1507 1510 self.ui.warn(_("waiting for lock on %s held by %r\n") %
1508 1511 (desc, inst.locker))
1509 1512 # default to 600 seconds timeout
1510 1513 l = lockmod.lock(vfs, lockname,
1511 1514 int(self.ui.config("ui", "timeout")),
1512 1515 releasefn=releasefn, acquirefn=acquirefn,
1513 1516 desc=desc)
1514 1517 self.ui.warn(_("got lock after %s seconds\n") % l.delay)
1515 1518 return l
1516 1519
1517 1520 def _afterlock(self, callback):
1518 1521 """add a callback to be run when the repository is fully unlocked
1519 1522
1520 1523 The callback will be executed when the outermost lock is released
1521 1524 (with wlock being higher level than 'lock')."""
1522 1525 for ref in (self._wlockref, self._lockref):
1523 1526 l = ref and ref()
1524 1527 if l and l.held:
1525 1528 l.postrelease.append(callback)
1526 1529 break
1527 1530 else: # no lock have been found.
1528 1531 callback()
1529 1532
1530 1533 def lock(self, wait=True):
1531 1534 '''Lock the repository store (.hg/store) and return a weak reference
1532 1535 to the lock. Use this before modifying the store (e.g. committing or
1533 1536 stripping). If you are opening a transaction, get a lock as well.)
1534 1537
1535 1538 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
1536 1539 'wlock' first to avoid a dead-lock hazard.'''
1537 1540 l = self._currentlock(self._lockref)
1538 1541 if l is not None:
1539 1542 l.lock()
1540 1543 return l
1541 1544
1542 1545 l = self._lock(self.svfs, "lock", wait, None,
1543 1546 self.invalidate, _('repository %s') % self.origroot)
1544 1547 self._lockref = weakref.ref(l)
1545 1548 return l
1546 1549
1547 1550 def _wlockchecktransaction(self):
1548 1551 if self.currenttransaction() is not None:
1549 1552 raise error.LockInheritanceContractViolation(
1550 1553 'wlock cannot be inherited in the middle of a transaction')
1551 1554
1552 1555 def wlock(self, wait=True):
1553 1556 '''Lock the non-store parts of the repository (everything under
1554 1557 .hg except .hg/store) and return a weak reference to the lock.
1555 1558
1556 1559 Use this before modifying files in .hg.
1557 1560
1558 1561 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
1559 1562 'wlock' first to avoid a dead-lock hazard.'''
1560 1563 l = self._wlockref and self._wlockref()
1561 1564 if l is not None and l.held:
1562 1565 l.lock()
1563 1566 return l
1564 1567
1565 1568 # We do not need to check for non-waiting lock acquisition. Such
1566 1569 # acquisition would not cause dead-lock as they would just fail.
1567 1570 if wait and (self.ui.configbool('devel', 'all-warnings')
1568 1571 or self.ui.configbool('devel', 'check-locks')):
1569 1572 if self._currentlock(self._lockref) is not None:
1570 1573 self.ui.develwarn('"wlock" acquired after "lock"')
1571 1574
1572 1575 def unlock():
1573 1576 if self.dirstate.pendingparentchange():
1574 1577 self.dirstate.invalidate()
1575 1578 else:
1576 1579 self.dirstate.write(None)
1577 1580
1578 1581 self._filecache['dirstate'].refresh()
1579 1582
1580 1583 l = self._lock(self.vfs, "wlock", wait, unlock,
1581 1584 self.invalidatedirstate, _('working directory of %s') %
1582 1585 self.origroot,
1583 1586 inheritchecker=self._wlockchecktransaction,
1584 1587 parentenvvar='HG_WLOCK_LOCKER')
1585 1588 self._wlockref = weakref.ref(l)
1586 1589 return l
1587 1590
1588 1591 def _currentlock(self, lockref):
1589 1592 """Returns the lock if it's held, or None if it's not."""
1590 1593 if lockref is None:
1591 1594 return None
1592 1595 l = lockref()
1593 1596 if l is None or not l.held:
1594 1597 return None
1595 1598 return l
1596 1599
1597 1600 def currentwlock(self):
1598 1601 """Returns the wlock if it's held, or None if it's not."""
1599 1602 return self._currentlock(self._wlockref)
1600 1603
1601 1604 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
1602 1605 """
1603 1606 commit an individual file as part of a larger transaction
1604 1607 """
1605 1608
1606 1609 fname = fctx.path()
1607 1610 fparent1 = manifest1.get(fname, nullid)
1608 1611 fparent2 = manifest2.get(fname, nullid)
1609 1612 if isinstance(fctx, context.filectx):
1610 1613 node = fctx.filenode()
1611 1614 if node in [fparent1, fparent2]:
1612 1615 self.ui.debug('reusing %s filelog entry\n' % fname)
1613 1616 if manifest1.flags(fname) != fctx.flags():
1614 1617 changelist.append(fname)
1615 1618 return node
1616 1619
1617 1620 flog = self.file(fname)
1618 1621 meta = {}
1619 1622 copy = fctx.renamed()
1620 1623 if copy and copy[0] != fname:
1621 1624 # Mark the new revision of this file as a copy of another
1622 1625 # file. This copy data will effectively act as a parent
1623 1626 # of this new revision. If this is a merge, the first
1624 1627 # parent will be the nullid (meaning "look up the copy data")
1625 1628 # and the second one will be the other parent. For example:
1626 1629 #
1627 1630 # 0 --- 1 --- 3 rev1 changes file foo
1628 1631 # \ / rev2 renames foo to bar and changes it
1629 1632 # \- 2 -/ rev3 should have bar with all changes and
1630 1633 # should record that bar descends from
1631 1634 # bar in rev2 and foo in rev1
1632 1635 #
1633 1636 # this allows this merge to succeed:
1634 1637 #
1635 1638 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
1636 1639 # \ / merging rev3 and rev4 should use bar@rev2
1637 1640 # \- 2 --- 4 as the merge base
1638 1641 #
1639 1642
1640 1643 cfname = copy[0]
1641 1644 crev = manifest1.get(cfname)
1642 1645 newfparent = fparent2
1643 1646
1644 1647 if manifest2: # branch merge
1645 1648 if fparent2 == nullid or crev is None: # copied on remote side
1646 1649 if cfname in manifest2:
1647 1650 crev = manifest2[cfname]
1648 1651 newfparent = fparent1
1649 1652
1650 1653 # Here, we used to search backwards through history to try to find
1651 1654 # where the file copy came from if the source of a copy was not in
1652 1655 # the parent directory. However, this doesn't actually make sense to
1653 1656 # do (what does a copy from something not in your working copy even
1654 1657 # mean?) and it causes bugs (eg, issue4476). Instead, we will warn
1655 1658 # the user that copy information was dropped, so if they didn't
1656 1659 # expect this outcome it can be fixed, but this is the correct
1657 1660 # behavior in this circumstance.
1658 1661
1659 1662 if crev:
1660 1663 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev)))
1661 1664 meta["copy"] = cfname
1662 1665 meta["copyrev"] = hex(crev)
1663 1666 fparent1, fparent2 = nullid, newfparent
1664 1667 else:
1665 1668 self.ui.warn(_("warning: can't find ancestor for '%s' "
1666 1669 "copied from '%s'!\n") % (fname, cfname))
1667 1670
1668 1671 elif fparent1 == nullid:
1669 1672 fparent1, fparent2 = fparent2, nullid
1670 1673 elif fparent2 != nullid:
1671 1674 # is one parent an ancestor of the other?
1672 1675 fparentancestors = flog.commonancestorsheads(fparent1, fparent2)
1673 1676 if fparent1 in fparentancestors:
1674 1677 fparent1, fparent2 = fparent2, nullid
1675 1678 elif fparent2 in fparentancestors:
1676 1679 fparent2 = nullid
1677 1680
1678 1681 # is the file changed?
1679 1682 text = fctx.data()
1680 1683 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
1681 1684 changelist.append(fname)
1682 1685 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
1683 1686 # are just the flags changed during merge?
1684 1687 elif fname in manifest1 and manifest1.flags(fname) != fctx.flags():
1685 1688 changelist.append(fname)
1686 1689
1687 1690 return fparent1
1688 1691
1689 1692 def checkcommitpatterns(self, wctx, vdirs, match, status, fail):
1690 1693 """check for commit arguments that aren't committable"""
1691 1694 if match.isexact() or match.prefix():
1692 1695 matched = set(status.modified + status.added + status.removed)
1693 1696
1694 1697 for f in match.files():
1695 1698 f = self.dirstate.normalize(f)
1696 1699 if f == '.' or f in matched or f in wctx.substate:
1697 1700 continue
1698 1701 if f in status.deleted:
1699 1702 fail(f, _('file not found!'))
1700 1703 if f in vdirs: # visited directory
1701 1704 d = f + '/'
1702 1705 for mf in matched:
1703 1706 if mf.startswith(d):
1704 1707 break
1705 1708 else:
1706 1709 fail(f, _("no match under directory!"))
1707 1710 elif f not in self.dirstate:
1708 1711 fail(f, _("file not tracked!"))
1709 1712
1710 1713 @unfilteredmethod
1711 1714 def commit(self, text="", user=None, date=None, match=None, force=False,
1712 1715 editor=False, extra=None):
1713 1716 """Add a new revision to current repository.
1714 1717
1715 1718 Revision information is gathered from the working directory,
1716 1719 match can be used to filter the committed files. If editor is
1717 1720 supplied, it is called to get a commit message.
1718 1721 """
1719 1722 if extra is None:
1720 1723 extra = {}
1721 1724
1722 1725 def fail(f, msg):
1723 1726 raise error.Abort('%s: %s' % (f, msg))
1724 1727
1725 1728 if not match:
1726 1729 match = matchmod.always(self.root, '')
1727 1730
1728 1731 if not force:
1729 1732 vdirs = []
1730 1733 match.explicitdir = vdirs.append
1731 1734 match.bad = fail
1732 1735
1733 1736 wlock = lock = tr = None
1734 1737 try:
1735 1738 wlock = self.wlock()
1736 1739 lock = self.lock() # for recent changelog (see issue4368)
1737 1740
1738 1741 wctx = self[None]
1739 1742 merge = len(wctx.parents()) > 1
1740 1743
1741 1744 if not force and merge and not match.always():
1742 1745 raise error.Abort(_('cannot partially commit a merge '
1743 1746 '(do not specify files or patterns)'))
1744 1747
1745 1748 status = self.status(match=match, clean=force)
1746 1749 if force:
1747 1750 status.modified.extend(status.clean) # mq may commit clean files
1748 1751
1749 1752 # check subrepos
1750 1753 subs = []
1751 1754 commitsubs = set()
1752 1755 newstate = wctx.substate.copy()
1753 1756 # only manage subrepos and .hgsubstate if .hgsub is present
1754 1757 if '.hgsub' in wctx:
1755 1758 # we'll decide whether to track this ourselves, thanks
1756 1759 for c in status.modified, status.added, status.removed:
1757 1760 if '.hgsubstate' in c:
1758 1761 c.remove('.hgsubstate')
1759 1762
1760 1763 # compare current state to last committed state
1761 1764 # build new substate based on last committed state
1762 1765 oldstate = wctx.p1().substate
1763 1766 for s in sorted(newstate.keys()):
1764 1767 if not match(s):
1765 1768 # ignore working copy, use old state if present
1766 1769 if s in oldstate:
1767 1770 newstate[s] = oldstate[s]
1768 1771 continue
1769 1772 if not force:
1770 1773 raise error.Abort(
1771 1774 _("commit with new subrepo %s excluded") % s)
1772 1775 dirtyreason = wctx.sub(s).dirtyreason(True)
1773 1776 if dirtyreason:
1774 1777 if not self.ui.configbool('ui', 'commitsubrepos'):
1775 1778 raise error.Abort(dirtyreason,
1776 1779 hint=_("use --subrepos for recursive commit"))
1777 1780 subs.append(s)
1778 1781 commitsubs.add(s)
1779 1782 else:
1780 1783 bs = wctx.sub(s).basestate()
1781 1784 newstate[s] = (newstate[s][0], bs, newstate[s][2])
1782 1785 if oldstate.get(s, (None, None, None))[1] != bs:
1783 1786 subs.append(s)
1784 1787
1785 1788 # check for removed subrepos
1786 1789 for p in wctx.parents():
1787 1790 r = [s for s in p.substate if s not in newstate]
1788 1791 subs += [s for s in r if match(s)]
1789 1792 if subs:
1790 1793 if (not match('.hgsub') and
1791 1794 '.hgsub' in (wctx.modified() + wctx.added())):
1792 1795 raise error.Abort(
1793 1796 _("can't commit subrepos without .hgsub"))
1794 1797 status.modified.insert(0, '.hgsubstate')
1795 1798
1796 1799 elif '.hgsub' in status.removed:
1797 1800 # clean up .hgsubstate when .hgsub is removed
1798 1801 if ('.hgsubstate' in wctx and
1799 1802 '.hgsubstate' not in (status.modified + status.added +
1800 1803 status.removed)):
1801 1804 status.removed.insert(0, '.hgsubstate')
1802 1805
1803 1806 # make sure all explicit patterns are matched
1804 1807 if not force:
1805 1808 self.checkcommitpatterns(wctx, vdirs, match, status, fail)
1806 1809
1807 1810 cctx = context.workingcommitctx(self, status,
1808 1811 text, user, date, extra)
1809 1812
1810 1813 # internal config: ui.allowemptycommit
1811 1814 allowemptycommit = (wctx.branch() != wctx.p1().branch()
1812 1815 or extra.get('close') or merge or cctx.files()
1813 1816 or self.ui.configbool('ui', 'allowemptycommit'))
1814 1817 if not allowemptycommit:
1815 1818 return None
1816 1819
1817 1820 if merge and cctx.deleted():
1818 1821 raise error.Abort(_("cannot commit merge with missing files"))
1819 1822
1820 1823 ms = mergemod.mergestate.read(self)
1821 1824 mergeutil.checkunresolved(ms)
1822 1825
1823 1826 if editor:
1824 1827 cctx._text = editor(self, cctx, subs)
1825 1828 edited = (text != cctx._text)
1826 1829
1827 1830 # Save commit message in case this transaction gets rolled back
1828 1831 # (e.g. by a pretxncommit hook). Leave the content alone on
1829 1832 # the assumption that the user will use the same editor again.
1830 1833 msgfn = self.savecommitmessage(cctx._text)
1831 1834
1832 1835 # commit subs and write new state
1833 1836 if subs:
1834 1837 for s in sorted(commitsubs):
1835 1838 sub = wctx.sub(s)
1836 1839 self.ui.status(_('committing subrepository %s\n') %
1837 1840 subrepo.subrelpath(sub))
1838 1841 sr = sub.commit(cctx._text, user, date)
1839 1842 newstate[s] = (newstate[s][0], sr)
1840 1843 subrepo.writestate(self, newstate)
1841 1844
1842 1845 p1, p2 = self.dirstate.parents()
1843 1846 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
1844 1847 try:
1845 1848 self.hook("precommit", throw=True, parent1=hookp1,
1846 1849 parent2=hookp2)
1847 1850 tr = self.transaction('commit')
1848 1851 ret = self.commitctx(cctx, True)
1849 1852 except: # re-raises
1850 1853 if edited:
1851 1854 self.ui.write(
1852 1855 _('note: commit message saved in %s\n') % msgfn)
1853 1856 raise
1854 1857 # update bookmarks, dirstate and mergestate
1855 1858 bookmarks.update(self, [p1, p2], ret)
1856 1859 cctx.markcommitted(ret)
1857 1860 ms.reset()
1858 1861 tr.close()
1859 1862
1860 1863 finally:
1861 1864 lockmod.release(tr, lock, wlock)
1862 1865
1863 1866 def commithook(node=hex(ret), parent1=hookp1, parent2=hookp2):
1864 1867 # hack for command that use a temporary commit (eg: histedit)
1865 1868 # temporary commit got stripped before hook release
1866 1869 if self.changelog.hasnode(ret):
1867 1870 self.hook("commit", node=node, parent1=parent1,
1868 1871 parent2=parent2)
1869 1872 self._afterlock(commithook)
1870 1873 return ret
1871 1874
1872 1875 @unfilteredmethod
1873 1876 def commitctx(self, ctx, error=False):
1874 1877 """Add a new revision to current repository.
1875 1878 Revision information is passed via the context argument.
1876 1879 """
1877 1880
1878 1881 tr = None
1879 1882 p1, p2 = ctx.p1(), ctx.p2()
1880 1883 user = ctx.user()
1881 1884
1882 1885 lock = self.lock()
1883 1886 try:
1884 1887 tr = self.transaction("commit")
1885 1888 trp = weakref.proxy(tr)
1886 1889
1887 1890 if ctx.manifestnode():
1888 1891 # reuse an existing manifest revision
1889 1892 mn = ctx.manifestnode()
1890 1893 files = ctx.files()
1891 1894 elif ctx.files():
1892 1895 m1ctx = p1.manifestctx()
1893 1896 m2ctx = p2.manifestctx()
1894 1897 mctx = m1ctx.copy()
1895 1898
1896 1899 m = mctx.read()
1897 1900 m1 = m1ctx.read()
1898 1901 m2 = m2ctx.read()
1899 1902
1900 1903 # check in files
1901 1904 added = []
1902 1905 changed = []
1903 1906 removed = list(ctx.removed())
1904 1907 linkrev = len(self)
1905 1908 self.ui.note(_("committing files:\n"))
1906 1909 for f in sorted(ctx.modified() + ctx.added()):
1907 1910 self.ui.note(f + "\n")
1908 1911 try:
1909 1912 fctx = ctx[f]
1910 1913 if fctx is None:
1911 1914 removed.append(f)
1912 1915 else:
1913 1916 added.append(f)
1914 1917 m[f] = self._filecommit(fctx, m1, m2, linkrev,
1915 1918 trp, changed)
1916 1919 m.setflag(f, fctx.flags())
1917 1920 except OSError as inst:
1918 1921 self.ui.warn(_("trouble committing %s!\n") % f)
1919 1922 raise
1920 1923 except IOError as inst:
1921 1924 errcode = getattr(inst, 'errno', errno.ENOENT)
1922 1925 if error or errcode and errcode != errno.ENOENT:
1923 1926 self.ui.warn(_("trouble committing %s!\n") % f)
1924 1927 raise
1925 1928
1926 1929 # update manifest
1927 1930 self.ui.note(_("committing manifest\n"))
1928 1931 removed = [f for f in sorted(removed) if f in m1 or f in m2]
1929 1932 drop = [f for f in removed if f in m]
1930 1933 for f in drop:
1931 1934 del m[f]
1932 1935 mn = mctx.write(trp, linkrev,
1933 1936 p1.manifestnode(), p2.manifestnode(),
1934 1937 added, drop)
1935 1938 files = changed + removed
1936 1939 else:
1937 1940 mn = p1.manifestnode()
1938 1941 files = []
1939 1942
1940 1943 # update changelog
1941 1944 self.ui.note(_("committing changelog\n"))
1942 1945 self.changelog.delayupdate(tr)
1943 1946 n = self.changelog.add(mn, files, ctx.description(),
1944 1947 trp, p1.node(), p2.node(),
1945 1948 user, ctx.date(), ctx.extra().copy())
1946 1949 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
1947 1950 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
1948 1951 parent2=xp2)
1949 1952 # set the new commit is proper phase
1950 1953 targetphase = subrepo.newcommitphase(self.ui, ctx)
1951 1954 if targetphase:
1952 1955 # retract boundary do not alter parent changeset.
1953 1956 # if a parent have higher the resulting phase will
1954 1957 # be compliant anyway
1955 1958 #
1956 1959 # if minimal phase was 0 we don't need to retract anything
1957 1960 phases.registernew(self, tr, targetphase, [n])
1958 1961 tr.close()
1959 1962 return n
1960 1963 finally:
1961 1964 if tr:
1962 1965 tr.release()
1963 1966 lock.release()
1964 1967
1965 1968 @unfilteredmethod
1966 1969 def destroying(self):
1967 1970 '''Inform the repository that nodes are about to be destroyed.
1968 1971 Intended for use by strip and rollback, so there's a common
1969 1972 place for anything that has to be done before destroying history.
1970 1973
1971 1974 This is mostly useful for saving state that is in memory and waiting
1972 1975 to be flushed when the current lock is released. Because a call to
1973 1976 destroyed is imminent, the repo will be invalidated causing those
1974 1977 changes to stay in memory (waiting for the next unlock), or vanish
1975 1978 completely.
1976 1979 '''
1977 1980 # When using the same lock to commit and strip, the phasecache is left
1978 1981 # dirty after committing. Then when we strip, the repo is invalidated,
1979 1982 # causing those changes to disappear.
1980 1983 if '_phasecache' in vars(self):
1981 1984 self._phasecache.write()
1982 1985
1983 1986 @unfilteredmethod
1984 1987 def destroyed(self):
1985 1988 '''Inform the repository that nodes have been destroyed.
1986 1989 Intended for use by strip and rollback, so there's a common
1987 1990 place for anything that has to be done after destroying history.
1988 1991 '''
1989 1992 # When one tries to:
1990 1993 # 1) destroy nodes thus calling this method (e.g. strip)
1991 1994 # 2) use phasecache somewhere (e.g. commit)
1992 1995 #
1993 1996 # then 2) will fail because the phasecache contains nodes that were
1994 1997 # removed. We can either remove phasecache from the filecache,
1995 1998 # causing it to reload next time it is accessed, or simply filter
1996 1999 # the removed nodes now and write the updated cache.
1997 2000 self._phasecache.filterunknown(self)
1998 2001 self._phasecache.write()
1999 2002
2000 2003 # refresh all repository caches
2001 2004 self.updatecaches()
2002 2005
2003 2006 # Ensure the persistent tag cache is updated. Doing it now
2004 2007 # means that the tag cache only has to worry about destroyed
2005 2008 # heads immediately after a strip/rollback. That in turn
2006 2009 # guarantees that "cachetip == currenttip" (comparing both rev
2007 2010 # and node) always means no nodes have been added or destroyed.
2008 2011
2009 2012 # XXX this is suboptimal when qrefresh'ing: we strip the current
2010 2013 # head, refresh the tag cache, then immediately add a new head.
2011 2014 # But I think doing it this way is necessary for the "instant
2012 2015 # tag cache retrieval" case to work.
2013 2016 self.invalidate()
2014 2017
2015 2018 def walk(self, match, node=None):
2016 2019 '''
2017 2020 walk recursively through the directory tree or a given
2018 2021 changeset, finding all files matched by the match
2019 2022 function
2020 2023 '''
2021 2024 self.ui.deprecwarn('use repo[node].walk instead of repo.walk', '4.3')
2022 2025 return self[node].walk(match)
2023 2026
2024 2027 def status(self, node1='.', node2=None, match=None,
2025 2028 ignored=False, clean=False, unknown=False,
2026 2029 listsubrepos=False):
2027 2030 '''a convenience method that calls node1.status(node2)'''
2028 2031 return self[node1].status(node2, match, ignored, clean, unknown,
2029 2032 listsubrepos)
2030 2033
2031 2034 def addpostdsstatus(self, ps):
2032 2035 """Add a callback to run within the wlock, at the point at which status
2033 2036 fixups happen.
2034 2037
2035 2038 On status completion, callback(wctx, status) will be called with the
2036 2039 wlock held, unless the dirstate has changed from underneath or the wlock
2037 2040 couldn't be grabbed.
2038 2041
2039 2042 Callbacks should not capture and use a cached copy of the dirstate --
2040 2043 it might change in the meanwhile. Instead, they should access the
2041 2044 dirstate via wctx.repo().dirstate.
2042 2045
2043 2046 This list is emptied out after each status run -- extensions should
2044 2047 make sure it adds to this list each time dirstate.status is called.
2045 2048 Extensions should also make sure they don't call this for statuses
2046 2049 that don't involve the dirstate.
2047 2050 """
2048 2051
2049 2052 # The list is located here for uniqueness reasons -- it is actually
2050 2053 # managed by the workingctx, but that isn't unique per-repo.
2051 2054 self._postdsstatus.append(ps)
2052 2055
2053 2056 def postdsstatus(self):
2054 2057 """Used by workingctx to get the list of post-dirstate-status hooks."""
2055 2058 return self._postdsstatus
2056 2059
2057 2060 def clearpostdsstatus(self):
2058 2061 """Used by workingctx to clear post-dirstate-status hooks."""
2059 2062 del self._postdsstatus[:]
2060 2063
2061 2064 def heads(self, start=None):
2062 2065 if start is None:
2063 2066 cl = self.changelog
2064 2067 headrevs = reversed(cl.headrevs())
2065 2068 return [cl.node(rev) for rev in headrevs]
2066 2069
2067 2070 heads = self.changelog.heads(start)
2068 2071 # sort the output in rev descending order
2069 2072 return sorted(heads, key=self.changelog.rev, reverse=True)
2070 2073
2071 2074 def branchheads(self, branch=None, start=None, closed=False):
2072 2075 '''return a (possibly filtered) list of heads for the given branch
2073 2076
2074 2077 Heads are returned in topological order, from newest to oldest.
2075 2078 If branch is None, use the dirstate branch.
2076 2079 If start is not None, return only heads reachable from start.
2077 2080 If closed is True, return heads that are marked as closed as well.
2078 2081 '''
2079 2082 if branch is None:
2080 2083 branch = self[None].branch()
2081 2084 branches = self.branchmap()
2082 2085 if branch not in branches:
2083 2086 return []
2084 2087 # the cache returns heads ordered lowest to highest
2085 2088 bheads = list(reversed(branches.branchheads(branch, closed=closed)))
2086 2089 if start is not None:
2087 2090 # filter out the heads that cannot be reached from startrev
2088 2091 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
2089 2092 bheads = [h for h in bheads if h in fbheads]
2090 2093 return bheads
2091 2094
2092 2095 def branches(self, nodes):
2093 2096 if not nodes:
2094 2097 nodes = [self.changelog.tip()]
2095 2098 b = []
2096 2099 for n in nodes:
2097 2100 t = n
2098 2101 while True:
2099 2102 p = self.changelog.parents(n)
2100 2103 if p[1] != nullid or p[0] == nullid:
2101 2104 b.append((t, n, p[0], p[1]))
2102 2105 break
2103 2106 n = p[0]
2104 2107 return b
2105 2108
2106 2109 def between(self, pairs):
2107 2110 r = []
2108 2111
2109 2112 for top, bottom in pairs:
2110 2113 n, l, i = top, [], 0
2111 2114 f = 1
2112 2115
2113 2116 while n != bottom and n != nullid:
2114 2117 p = self.changelog.parents(n)[0]
2115 2118 if i == f:
2116 2119 l.append(n)
2117 2120 f = f * 2
2118 2121 n = p
2119 2122 i += 1
2120 2123
2121 2124 r.append(l)
2122 2125
2123 2126 return r
2124 2127
2125 2128 def checkpush(self, pushop):
2126 2129 """Extensions can override this function if additional checks have
2127 2130 to be performed before pushing, or call it if they override push
2128 2131 command.
2129 2132 """
2130 2133 pass
2131 2134
2132 2135 @unfilteredpropertycache
2133 2136 def prepushoutgoinghooks(self):
2134 2137 """Return util.hooks consists of a pushop with repo, remote, outgoing
2135 2138 methods, which are called before pushing changesets.
2136 2139 """
2137 2140 return util.hooks()
2138 2141
2139 2142 def pushkey(self, namespace, key, old, new):
2140 2143 try:
2141 2144 tr = self.currenttransaction()
2142 2145 hookargs = {}
2143 2146 if tr is not None:
2144 2147 hookargs.update(tr.hookargs)
2145 2148 hookargs['namespace'] = namespace
2146 2149 hookargs['key'] = key
2147 2150 hookargs['old'] = old
2148 2151 hookargs['new'] = new
2149 2152 self.hook('prepushkey', throw=True, **hookargs)
2150 2153 except error.HookAbort as exc:
2151 2154 self.ui.write_err(_("pushkey-abort: %s\n") % exc)
2152 2155 if exc.hint:
2153 2156 self.ui.write_err(_("(%s)\n") % exc.hint)
2154 2157 return False
2155 2158 self.ui.debug('pushing key for "%s:%s"\n' % (namespace, key))
2156 2159 ret = pushkey.push(self, namespace, key, old, new)
2157 2160 def runhook():
2158 2161 self.hook('pushkey', namespace=namespace, key=key, old=old, new=new,
2159 2162 ret=ret)
2160 2163 self._afterlock(runhook)
2161 2164 return ret
2162 2165
2163 2166 def listkeys(self, namespace):
2164 2167 self.hook('prelistkeys', throw=True, namespace=namespace)
2165 2168 self.ui.debug('listing keys for "%s"\n' % namespace)
2166 2169 values = pushkey.list(self, namespace)
2167 2170 self.hook('listkeys', namespace=namespace, values=values)
2168 2171 return values
2169 2172
2170 2173 def debugwireargs(self, one, two, three=None, four=None, five=None):
2171 2174 '''used to test argument passing over the wire'''
2172 2175 return "%s %s %s %s %s" % (one, two, three, four, five)
2173 2176
2174 2177 def savecommitmessage(self, text):
2175 2178 fp = self.vfs('last-message.txt', 'wb')
2176 2179 try:
2177 2180 fp.write(text)
2178 2181 finally:
2179 2182 fp.close()
2180 2183 return self.pathto(fp.name[len(self.root) + 1:])
2181 2184
2182 2185 # used to avoid circular references so destructors work
2183 2186 def aftertrans(files):
2184 2187 renamefiles = [tuple(t) for t in files]
2185 2188 def a():
2186 2189 for vfs, src, dest in renamefiles:
2187 2190 # if src and dest refer to a same file, vfs.rename is a no-op,
2188 2191 # leaving both src and dest on disk. delete dest to make sure
2189 2192 # the rename couldn't be such a no-op.
2190 2193 vfs.tryunlink(dest)
2191 2194 try:
2192 2195 vfs.rename(src, dest)
2193 2196 except OSError: # journal file does not yet exist
2194 2197 pass
2195 2198 return a
2196 2199
2197 2200 def undoname(fn):
2198 2201 base, name = os.path.split(fn)
2199 2202 assert name.startswith('journal')
2200 2203 return os.path.join(base, name.replace('journal', 'undo', 1))
2201 2204
2202 2205 def instance(ui, path, create):
2203 2206 return localrepository(ui, util.urllocalpath(path), create)
2204 2207
2205 2208 def islocal(path):
2206 2209 return True
2207 2210
2208 2211 def newreporequirements(repo):
2209 2212 """Determine the set of requirements for a new local repository.
2210 2213
2211 2214 Extensions can wrap this function to specify custom requirements for
2212 2215 new repositories.
2213 2216 """
2214 2217 ui = repo.ui
2215 2218 requirements = {'revlogv1'}
2216 2219 if ui.configbool('format', 'usestore'):
2217 2220 requirements.add('store')
2218 2221 if ui.configbool('format', 'usefncache'):
2219 2222 requirements.add('fncache')
2220 2223 if ui.configbool('format', 'dotencode'):
2221 2224 requirements.add('dotencode')
2222 2225
2223 2226 compengine = ui.config('experimental', 'format.compression')
2224 2227 if compengine not in util.compengines:
2225 2228 raise error.Abort(_('compression engine %s defined by '
2226 2229 'experimental.format.compression not available') %
2227 2230 compengine,
2228 2231 hint=_('run "hg debuginstall" to list available '
2229 2232 'compression engines'))
2230 2233
2231 2234 # zlib is the historical default and doesn't need an explicit requirement.
2232 2235 if compengine != 'zlib':
2233 2236 requirements.add('exp-compression-%s' % compengine)
2234 2237
2235 2238 if scmutil.gdinitconfig(ui):
2236 2239 requirements.add('generaldelta')
2237 2240 if ui.configbool('experimental', 'treemanifest'):
2238 2241 requirements.add('treemanifest')
2239 2242 if ui.configbool('experimental', 'manifestv2'):
2240 2243 requirements.add('manifestv2')
2241 2244
2242 2245 revlogv2 = ui.config('experimental', 'revlogv2')
2243 2246 if revlogv2 == 'enable-unstable-format-and-corrupt-my-data':
2244 2247 requirements.remove('revlogv1')
2245 2248 # generaldelta is implied by revlogv2.
2246 2249 requirements.discard('generaldelta')
2247 2250 requirements.add(REVLOGV2_REQUIREMENT)
2248 2251
2249 2252 return requirements
General Comments 0
You need to be logged in to leave comments. Login now