comparison mercurial/formatter.py @ 39582:28f974d83c0a

templater: remove unused context argument from most resourcemapper functions While working on demand loading of ctx/fctx objects, I noticed that it's quite easy to create infinite recursion by carelessly using the template context in the resource mapper. Let's make that not happen.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 07 Jun 2018 23:27:54 +0900
parents 5b04a0c30f3f
children ee1e74ee037c
comparison
equal deleted inserted replaced
39581:68ce242c8b4b 39582:28f974d83c0a
546 'cache': {}, # for templatekw/funcs to store reusable data 546 'cache': {}, # for templatekw/funcs to store reusable data
547 'repo': repo, 547 'repo': repo,
548 'ui': ui, 548 'ui': ui,
549 } 549 }
550 550
551 def availablekeys(self, context, mapping): 551 def availablekeys(self, mapping):
552 return {k for k, g in self._gettermap.iteritems() 552 return {k for k, g in self._gettermap.iteritems()
553 if g(self, context, mapping, k) is not None} 553 if g(self, mapping, k) is not None}
554 554
555 def knownkeys(self): 555 def knownkeys(self):
556 return self._knownkeys 556 return self._knownkeys
557 557
558 def lookup(self, context, mapping, key): 558 def lookup(self, mapping, key):
559 get = self._gettermap.get(key) 559 get = self._gettermap.get(key)
560 if not get: 560 if not get:
561 return None 561 return None
562 return get(self, context, mapping, key) 562 return get(self, mapping, key)
563 563
564 def populatemap(self, context, origmapping, newmapping): 564 def populatemap(self, context, origmapping, newmapping):
565 mapping = {} 565 mapping = {}
566 if self._hasctx(newmapping): 566 if self._hasctx(newmapping):
567 mapping['revcache'] = {} # per-ctx cache 567 mapping['revcache'] = {} # per-ctx cache
569 and ('node' in newmapping or self._hasctx(newmapping))): 569 and ('node' in newmapping or self._hasctx(newmapping))):
570 orignode = templateutil.runsymbol(context, origmapping, 'node') 570 orignode = templateutil.runsymbol(context, origmapping, 'node')
571 mapping['originalnode'] = orignode 571 mapping['originalnode'] = orignode
572 return mapping 572 return mapping
573 573
574 def _getsome(self, context, mapping, key): 574 def _getsome(self, mapping, key):
575 v = mapping.get(key) 575 v = mapping.get(key)
576 if v is not None: 576 if v is not None:
577 return v 577 return v
578 return self._resmap.get(key) 578 return self._resmap.get(key)
579 579
580 def _hasctx(self, mapping): 580 def _hasctx(self, mapping):
581 return 'ctx' in mapping or 'fctx' in mapping 581 return 'ctx' in mapping or 'fctx' in mapping
582 582
583 def _getctx(self, context, mapping, key): 583 def _getctx(self, mapping, key):
584 ctx = mapping.get('ctx') 584 ctx = mapping.get('ctx')
585 if ctx is not None: 585 if ctx is not None:
586 return ctx 586 return ctx
587 fctx = mapping.get('fctx') 587 fctx = mapping.get('fctx')
588 if fctx is not None: 588 if fctx is not None:
589 return fctx.changectx() 589 return fctx.changectx()
590 590
591 def _getrepo(self, context, mapping, key): 591 def _getrepo(self, mapping, key):
592 ctx = self._getctx(context, mapping, 'ctx') 592 ctx = self._getctx(mapping, 'ctx')
593 if ctx is not None: 593 if ctx is not None:
594 return ctx.repo() 594 return ctx.repo()
595 return self._getsome(context, mapping, key) 595 return self._getsome(mapping, key)
596 596
597 _gettermap = { 597 _gettermap = {
598 'cache': _getsome, 598 'cache': _getsome,
599 'ctx': _getctx, 599 'ctx': _getctx,
600 'fctx': _getsome, 600 'fctx': _getsome,