comparison mercurial/formatter.py @ 36984:939e0983c1d9

formatter: unblock storing fctx as a template resource To keep templateformatter._renderitem() simple, a repo instance is looked through ctx if available. This is probably good for future subrepo support where ctx.repo() may be different from the global repo.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 11 Mar 2018 21:26:15 +0900
parents 036e4483d3a1
children de117f579431
comparison
equal deleted inserted replaced
36983:036e4483d3a1 36984:939e0983c1d9
186 # we have default template keyword, e.g. {item} 186 # we have default template keyword, e.g. {item}
187 return self._converter.formatlist(data, name, fmt, sep) 187 return self._converter.formatlist(data, name, fmt, sep)
188 def context(self, **ctxs): 188 def context(self, **ctxs):
189 '''insert context objects to be used to render template keywords''' 189 '''insert context objects to be used to render template keywords'''
190 ctxs = pycompat.byteskwargs(ctxs) 190 ctxs = pycompat.byteskwargs(ctxs)
191 assert all(k == 'ctx' for k in ctxs) 191 assert all(k in {'ctx', 'fctx'} for k in ctxs)
192 if self._converter.storecontext: 192 if self._converter.storecontext:
193 self._item.update(ctxs) 193 self._item.update(ctxs)
194 def data(self, **data): 194 def data(self, **data):
195 '''insert data into item that's not shown in default output''' 195 '''insert data into item that's not shown in default output'''
196 data = pycompat.byteskwargs(data) 196 data = pycompat.byteskwargs(data)
393 def _renderitem(self, part, item): 393 def _renderitem(self, part, item):
394 if part not in self._parts: 394 if part not in self._parts:
395 return 395 return
396 ref = self._parts[part] 396 ref = self._parts[part]
397 397
398 # TODO: add support for filectx
399 props = {} 398 props = {}
400 # explicitly-defined fields precede templatekw 399 # explicitly-defined fields precede templatekw
401 props.update(item) 400 props.update(item)
402 if 'ctx' in item: 401 if 'ctx' in item or 'fctx' in item:
403 # but template resources must be always available 402 # but template resources must be always available
404 props['repo'] = props['ctx'].repo()
405 props['revcache'] = {} 403 props['revcache'] = {}
406 props = pycompat.strkwargs(props) 404 props = pycompat.strkwargs(props)
407 g = self._t(ref, **props) 405 g = self._t(ref, **props)
408 self._out.write(templateutil.stringify(g)) 406 self._out.write(templateutil.stringify(g))
409 407
511 v = mapping.get(key) 509 v = mapping.get(key)
512 if v is not None: 510 if v is not None:
513 return v 511 return v
514 return resmap.get(key) 512 return resmap.get(key)
515 513
514 def getctx(context, mapping, key):
515 ctx = mapping.get('ctx')
516 if ctx is not None:
517 return ctx
518 fctx = mapping.get('fctx')
519 if fctx is not None:
520 return fctx.changectx()
521
522 def getrepo(context, mapping, key):
523 ctx = getctx(context, mapping, 'ctx')
524 if ctx is not None:
525 return ctx.repo()
526 return getsome(context, mapping, key)
527
516 return { 528 return {
517 'cache': getsome, 529 'cache': getsome,
518 'ctx': getsome, 530 'ctx': getctx,
519 'repo': getsome, 531 'fctx': getsome,
532 'repo': getrepo,
520 'revcache': getsome, # per-ctx cache; set later 533 'revcache': getsome, # per-ctx cache; set later
521 'ui': getsome, 534 'ui': getsome,
522 } 535 }
523 536
524 def formatter(ui, out, topic, opts): 537 def formatter(ui, out, topic, opts):