comparison mercurial/formatter.py @ 39623:34ecc0a09c76

formatter: populate ctx from repo and node value This will basically replace the fm.contexthint() API. I originally thought this would be too complicated, and I wrote 8399438bc7ef "formatter: provide hint of context keys required by template" because of that. However, I had to add a similar mechanism for fctx templates, and the overall machinery became way simpler than my original patch. The test output slightly changed as {author} is no longer available in the {manifest} context, which isn't the point this test targeted on.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 07 Jun 2018 21:48:11 +0900
parents b1239aeef4d9
children 713085b45810
comparison
equal deleted inserted replaced
39622:20dbca581727 39623:34ecc0a09c76
577 if self._hasnodespec(newmapping): 577 if self._hasnodespec(newmapping):
578 mapping['revcache'] = {} # per-ctx cache 578 mapping['revcache'] = {} # per-ctx cache
579 if self._hasnodespec(origmapping) and self._hasnodespec(newmapping): 579 if self._hasnodespec(origmapping) and self._hasnodespec(newmapping):
580 orignode = templateutil.runsymbol(context, origmapping, 'node') 580 orignode = templateutil.runsymbol(context, origmapping, 'node')
581 mapping['originalnode'] = orignode 581 mapping['originalnode'] = orignode
582 # put marker to override 'fctx' in mapping if any, and flag 582 # put marker to override 'ctx'/'fctx' in mapping if any, and flag
583 # its existence to be reported by availablekeys() 583 # its existence to be reported by availablekeys()
584 if 'ctx' not in newmapping and self._hasliteral(newmapping, 'node'):
585 mapping['ctx'] = _placeholder
584 if 'fctx' not in newmapping and self._hasliteral(newmapping, 'path'): 586 if 'fctx' not in newmapping and self._hasliteral(newmapping, 'path'):
585 mapping['fctx'] = _placeholder 587 mapping['fctx'] = _placeholder
586 return mapping 588 return mapping
587 589
588 def _getsome(self, mapping, key): 590 def _getsome(self, mapping, key):
603 return v 605 return v
604 606
605 def _hasnodespec(self, mapping): 607 def _hasnodespec(self, mapping):
606 """Test if context revision is set or unset in the given mapping""" 608 """Test if context revision is set or unset in the given mapping"""
607 return 'node' in mapping or 'ctx' in mapping 609 return 'node' in mapping or 'ctx' in mapping
610
611 def _loadctx(self, mapping):
612 repo = self._getsome(mapping, 'repo')
613 node = self._getliteral(mapping, 'node')
614 if repo is None or node is None:
615 return
616 try:
617 return repo[node]
618 except error.RepoLookupError:
619 return None # maybe hidden/non-existent node
608 620
609 def _loadfctx(self, mapping): 621 def _loadfctx(self, mapping):
610 ctx = self._getsome(mapping, 'ctx') 622 ctx = self._getsome(mapping, 'ctx')
611 path = self._getliteral(mapping, 'path') 623 path = self._getliteral(mapping, 'path')
612 if ctx is None or path is None: 624 if ctx is None or path is None:
615 return ctx[path] 627 return ctx[path]
616 except error.LookupError: 628 except error.LookupError:
617 return None # maybe removed file? 629 return None # maybe removed file?
618 630
619 _loadermap = { 631 _loadermap = {
632 'ctx': _loadctx,
620 'fctx': _loadfctx, 633 'fctx': _loadfctx,
621 } 634 }
622 635
623 def formatter(ui, out, topic, opts): 636 def formatter(ui, out, topic, opts):
624 template = opts.get("template", "") 637 template = opts.get("template", "")