##// END OF EJS Templates
templater: use template context to render old-style list template...
Yuya Nishihara -
r37086:aa97e06a default
parent child Browse files
Show More
@@ -356,12 +356,11 b' def lfsfileset(mctx, x):'
356 return [f for f in mctx.subset
356 return [f for f in mctx.subset
357 if wrapper.pointerfromctx(mctx.ctx, f, removed=True) is not None]
357 if wrapper.pointerfromctx(mctx.ctx, f, removed=True) is not None]
358
358
359 @templatekeyword('lfs_files', requires={'ctx', 'templ'})
359 @templatekeyword('lfs_files', requires={'ctx'})
360 def lfsfiles(context, mapping):
360 def lfsfiles(context, mapping):
361 """List of strings. All files modified, added, or removed by this
361 """List of strings. All files modified, added, or removed by this
362 changeset."""
362 changeset."""
363 ctx = context.resource(mapping, 'ctx')
363 ctx = context.resource(mapping, 'ctx')
364 templ = context.resource(mapping, 'templ')
365
364
366 pointers = wrapper.pointersfromctx(ctx, removed=True) # {path: pointer}
365 pointers = wrapper.pointersfromctx(ctx, removed=True) # {path: pointer}
367 files = sorted(pointers.keys())
366 files = sorted(pointers.keys())
@@ -379,7 +378,7 b' def lfsfiles(context, mapping):'
379 }
378 }
380
379
381 # TODO: make the separator ', '?
380 # TODO: make the separator ', '?
382 f = templateutil._showlist('lfs_file', files, templ, mapping)
381 f = templateutil._showcompatlist(context, mapping, 'lfs_file', files)
383 return templateutil.hybrid(f, files, makemap, pycompat.identity)
382 return templateutil.hybrid(f, files, makemap, pycompat.identity)
384
383
385 @command('debuglfsupload',
384 @command('debuglfsupload',
@@ -230,7 +230,7 b' def reposetup(ui, repo):'
230 repo._remotenames.nodetobranch().get(node, []))
230 repo._remotenames.nodetobranch().get(node, []))
231 repo.names.addnamespace(remotebranchns)
231 repo.names.addnamespace(remotebranchns)
232
232
233 @templatekeyword('remotenames', requires={'repo', 'ctx', 'templ'})
233 @templatekeyword('remotenames', requires={'repo', 'ctx'})
234 def remotenameskw(context, mapping):
234 def remotenameskw(context, mapping):
235 """List of strings. Remote names associated with the changeset."""
235 """List of strings. Remote names associated with the changeset."""
236 repo = context.resource(mapping, 'repo')
236 repo = context.resource(mapping, 'repo')
@@ -246,7 +246,7 b' def remotenameskw(context, mapping):'
246 return templateutil.compatlist(context, mapping, 'remotename', remotenames,
246 return templateutil.compatlist(context, mapping, 'remotename', remotenames,
247 plural='remotenames')
247 plural='remotenames')
248
248
249 @templatekeyword('remotebookmarks', requires={'repo', 'ctx', 'templ'})
249 @templatekeyword('remotebookmarks', requires={'repo', 'ctx'})
250 def remotebookmarkskw(context, mapping):
250 def remotebookmarkskw(context, mapping):
251 """List of strings. Remote bookmarks associated with the changeset."""
251 """List of strings. Remote bookmarks associated with the changeset."""
252 repo = context.resource(mapping, 'repo')
252 repo = context.resource(mapping, 'repo')
@@ -259,7 +259,7 b' def remotebookmarkskw(context, mapping):'
259 return templateutil.compatlist(context, mapping, 'remotebookmark',
259 return templateutil.compatlist(context, mapping, 'remotebookmark',
260 remotebmarks, plural='remotebookmarks')
260 remotebmarks, plural='remotebookmarks')
261
261
262 @templatekeyword('remotebranches', requires={'repo', 'ctx', 'templ'})
262 @templatekeyword('remotebranches', requires={'repo', 'ctx'})
263 def remotebrancheskw(context, mapping):
263 def remotebrancheskw(context, mapping):
264 """List of strings. Remote branches associated with the changeset."""
264 """List of strings. Remote branches associated with the changeset."""
265 repo = context.resource(mapping, 'repo')
265 repo = context.resource(mapping, 'repo')
@@ -89,7 +89,7 b' class namespaces(object):'
89 # we only generate a template keyword if one does not already exist
89 # we only generate a template keyword if one does not already exist
90 if namespace.name not in templatekw.keywords:
90 if namespace.name not in templatekw.keywords:
91 templatekeyword = registrar.templatekeyword(templatekw.keywords)
91 templatekeyword = registrar.templatekeyword(templatekw.keywords)
92 @templatekeyword(namespace.name, requires={'repo', 'ctx', 'templ'})
92 @templatekeyword(namespace.name, requires={'repo', 'ctx'})
93 def generatekw(context, mapping):
93 def generatekw(context, mapping):
94 return templatekw.shownames(context, mapping, namespace.name)
94 return templatekw.shownames(context, mapping, namespace.name)
95
95
@@ -29,11 +29,26 b' from . import ('
29
29
30 _hybrid = templateutil.hybrid
30 _hybrid = templateutil.hybrid
31 _mappable = templateutil.mappable
31 _mappable = templateutil.mappable
32 _showlist = templateutil._showlist
33 hybriddict = templateutil.hybriddict
32 hybriddict = templateutil.hybriddict
34 hybridlist = templateutil.hybridlist
33 hybridlist = templateutil.hybridlist
35 compatdict = templateutil.compatdict
34 compatdict = templateutil.compatdict
36 compatlist = templateutil.compatlist
35 compatlist = templateutil.compatlist
36 _showcompatlist = templateutil._showcompatlist
37
38 # TODO: temporary hack for porting; will be removed soon
39 class _fakecontextwrapper(object):
40 def __init__(self, templ):
41 self._templ = templ
42
43 def preload(self, t):
44 return t in self._templ
45
46 def process(self, t, mapping):
47 return self._templ.generatenamed(t, mapping)
48
49 def _showlist(name, values, templ, mapping, plural=None, separator=' '):
50 context = _fakecontextwrapper(templ)
51 return _showcompatlist(context, mapping, name, values, plural, separator)
37
52
38 def showdict(name, data, mapping, plural=None, key='key', value='value',
53 def showdict(name, data, mapping, plural=None, key='key', value='value',
39 fmt=None, separator=' '):
54 fmt=None, separator=' '):
@@ -203,7 +218,7 b' def showbranch(context, mapping):'
203 ctx = context.resource(mapping, 'ctx')
218 ctx = context.resource(mapping, 'ctx')
204 return ctx.branch()
219 return ctx.branch()
205
220
206 @templatekeyword('branches', requires={'ctx', 'templ'})
221 @templatekeyword('branches', requires={'ctx'})
207 def showbranches(context, mapping):
222 def showbranches(context, mapping):
208 """List of strings. The name of the branch on which the
223 """List of strings. The name of the branch on which the
209 changeset was committed. Will be empty if the branch name was
224 changeset was committed. Will be empty if the branch name was
@@ -230,7 +245,7 b' def showbookmarks(context, mapping):'
230 f = _showlist('bookmark', bookmarks, templ, mapping)
245 f = _showlist('bookmark', bookmarks, templ, mapping)
231 return _hybrid(f, bookmarks, makemap, pycompat.identity)
246 return _hybrid(f, bookmarks, makemap, pycompat.identity)
232
247
233 @templatekeyword('children', requires={'ctx', 'templ'})
248 @templatekeyword('children', requires={'ctx'})
234 def showchildren(context, mapping):
249 def showchildren(context, mapping):
235 """List of strings. The children of the changeset."""
250 """List of strings. The children of the changeset."""
236 ctx = context.resource(mapping, 'ctx')
251 ctx = context.resource(mapping, 'ctx')
@@ -281,7 +296,7 b' def showdiffstat(context, mapping):'
281 maxname, maxtotal, adds, removes, binary = patch.diffstatsum(stats)
296 maxname, maxtotal, adds, removes, binary = patch.diffstatsum(stats)
282 return '%d: +%d/-%d' % (len(stats), adds, removes)
297 return '%d: +%d/-%d' % (len(stats), adds, removes)
283
298
284 @templatekeyword('envvars', requires={'ui', 'templ'})
299 @templatekeyword('envvars', requires={'ui'})
285 def showenvvars(context, mapping):
300 def showenvvars(context, mapping):
286 """A dictionary of environment variables. (EXPERIMENTAL)"""
301 """A dictionary of environment variables. (EXPERIMENTAL)"""
287 ui = context.resource(mapping, 'ui')
302 ui = context.resource(mapping, 'ui')
@@ -312,13 +327,13 b' def _showfilesbystat(context, mapping, n'
312 files = revcache['files'][index]
327 files = revcache['files'][index]
313 return compatlist(context, mapping, name, files, element='file')
328 return compatlist(context, mapping, name, files, element='file')
314
329
315 @templatekeyword('file_adds', requires={'repo', 'ctx', 'revcache', 'templ'})
330 @templatekeyword('file_adds', requires={'repo', 'ctx', 'revcache'})
316 def showfileadds(context, mapping):
331 def showfileadds(context, mapping):
317 """List of strings. Files added by this changeset."""
332 """List of strings. Files added by this changeset."""
318 return _showfilesbystat(context, mapping, 'file_add', 1)
333 return _showfilesbystat(context, mapping, 'file_add', 1)
319
334
320 @templatekeyword('file_copies',
335 @templatekeyword('file_copies',
321 requires={'repo', 'ctx', 'cache', 'revcache', 'templ'})
336 requires={'repo', 'ctx', 'cache', 'revcache'})
322 def showfilecopies(context, mapping):
337 def showfilecopies(context, mapping):
323 """List of strings. Files copied in this changeset with
338 """List of strings. Files copied in this changeset with
324 their sources.
339 their sources.
@@ -345,7 +360,7 b' def showfilecopies(context, mapping):'
345 # showfilecopiesswitch() displays file copies only if copy records are
360 # showfilecopiesswitch() displays file copies only if copy records are
346 # provided before calling the templater, usually with a --copies
361 # provided before calling the templater, usually with a --copies
347 # command line switch.
362 # command line switch.
348 @templatekeyword('file_copies_switch', requires={'revcache', 'templ'})
363 @templatekeyword('file_copies_switch', requires={'revcache'})
349 def showfilecopiesswitch(context, mapping):
364 def showfilecopiesswitch(context, mapping):
350 """List of strings. Like "file_copies" but displayed
365 """List of strings. Like "file_copies" but displayed
351 only if the --copied switch is set.
366 only if the --copied switch is set.
@@ -356,17 +371,17 b' def showfilecopiesswitch(context, mappin'
356 key='name', value='source', fmt='%s (%s)',
371 key='name', value='source', fmt='%s (%s)',
357 plural='file_copies')
372 plural='file_copies')
358
373
359 @templatekeyword('file_dels', requires={'repo', 'ctx', 'revcache', 'templ'})
374 @templatekeyword('file_dels', requires={'repo', 'ctx', 'revcache'})
360 def showfiledels(context, mapping):
375 def showfiledels(context, mapping):
361 """List of strings. Files removed by this changeset."""
376 """List of strings. Files removed by this changeset."""
362 return _showfilesbystat(context, mapping, 'file_del', 2)
377 return _showfilesbystat(context, mapping, 'file_del', 2)
363
378
364 @templatekeyword('file_mods', requires={'repo', 'ctx', 'revcache', 'templ'})
379 @templatekeyword('file_mods', requires={'repo', 'ctx', 'revcache'})
365 def showfilemods(context, mapping):
380 def showfilemods(context, mapping):
366 """List of strings. Files modified by this changeset."""
381 """List of strings. Files modified by this changeset."""
367 return _showfilesbystat(context, mapping, 'file_mod', 0)
382 return _showfilesbystat(context, mapping, 'file_mod', 0)
368
383
369 @templatekeyword('files', requires={'ctx', 'templ'})
384 @templatekeyword('files', requires={'ctx'})
370 def showfiles(context, mapping):
385 def showfiles(context, mapping):
371 """List of strings. All files modified, added, or removed by this
386 """List of strings. All files modified, added, or removed by this
372 changeset.
387 changeset.
@@ -465,11 +480,10 b' def _showchangessincetag(context, mappin'
465 # teach templater latesttags.changes is switched to (context, mapping) API
480 # teach templater latesttags.changes is switched to (context, mapping) API
466 _showchangessincetag._requires = {'repo', 'ctx'}
481 _showchangessincetag._requires = {'repo', 'ctx'}
467
482
468 @templatekeyword('manifest', requires={'repo', 'ctx', 'templ'})
483 @templatekeyword('manifest', requires={'repo', 'ctx'})
469 def showmanifest(context, mapping):
484 def showmanifest(context, mapping):
470 repo = context.resource(mapping, 'repo')
485 repo = context.resource(mapping, 'repo')
471 ctx = context.resource(mapping, 'ctx')
486 ctx = context.resource(mapping, 'ctx')
472 templ = context.resource(mapping, 'templ')
473 mnode = ctx.manifestnode()
487 mnode = ctx.manifestnode()
474 if mnode is None:
488 if mnode is None:
475 # just avoid crash, we might want to use the 'ff...' hash in future
489 # just avoid crash, we might want to use the 'ff...' hash in future
@@ -478,7 +492,7 b' def showmanifest(context, mapping):'
478 mhex = hex(mnode)
492 mhex = hex(mnode)
479 mapping = mapping.copy()
493 mapping = mapping.copy()
480 mapping.update({'rev': mrev, 'node': mhex})
494 mapping.update({'rev': mrev, 'node': mhex})
481 f = templ.generate('manifest', mapping)
495 f = context.process('manifest', mapping)
482 # TODO: perhaps 'ctx' should be dropped from mapping because manifest
496 # TODO: perhaps 'ctx' should be dropped from mapping because manifest
483 # rev and node are completely different from changeset's.
497 # rev and node are completely different from changeset's.
484 return _mappable(f, None, f, lambda x: {'rev': mrev, 'node': mhex})
498 return _mappable(f, None, f, lambda x: {'rev': mrev, 'node': mhex})
@@ -738,7 +752,7 b' def showrevslist(context, mapping, name,'
738 lambda x: {name: x, 'ctx': repo[x], 'revcache': {}},
752 lambda x: {name: x, 'ctx': repo[x], 'revcache': {}},
739 pycompat.identity, keytype=int)
753 pycompat.identity, keytype=int)
740
754
741 @templatekeyword('subrepos', requires={'ctx', 'templ'})
755 @templatekeyword('subrepos', requires={'ctx'})
742 def showsubrepos(context, mapping):
756 def showsubrepos(context, mapping):
743 """List of strings. Updated subrepositories in the changeset."""
757 """List of strings. Updated subrepositories in the changeset."""
744 ctx = context.resource(mapping, 'ctx')
758 ctx = context.resource(mapping, 'ctx')
@@ -758,7 +772,7 b' def showsubrepos(context, mapping):'
758 # don't remove "showtags" definition, even though namespaces will put
772 # don't remove "showtags" definition, even though namespaces will put
759 # a helper function for "tags" keyword into "keywords" map automatically,
773 # a helper function for "tags" keyword into "keywords" map automatically,
760 # because online help text is built without namespaces initialization
774 # because online help text is built without namespaces initialization
761 @templatekeyword('tags', requires={'repo', 'ctx', 'templ'})
775 @templatekeyword('tags', requires={'repo', 'ctx'})
762 def showtags(context, mapping):
776 def showtags(context, mapping):
763 """List of strings. Any tags associated with the changeset."""
777 """List of strings. Any tags associated with the changeset."""
764 return shownames(context, mapping, 'tags')
778 return shownames(context, mapping, 'tags')
@@ -769,7 +783,7 b' def showtermwidth(context, mapping):'
769 ui = context.resource(mapping, 'ui')
783 ui = context.resource(mapping, 'ui')
770 return ui.termwidth()
784 return ui.termwidth()
771
785
772 @templatekeyword('instabilities', requires={'ctx', 'templ'})
786 @templatekeyword('instabilities', requires={'ctx'})
773 def showinstabilities(context, mapping):
787 def showinstabilities(context, mapping):
774 """List of strings. Evolution instabilities affecting the changeset.
788 """List of strings. Evolution instabilities affecting the changeset.
775 (EXPERIMENTAL)
789 (EXPERIMENTAL)
@@ -148,8 +148,7 b' def compatdict(context, mapping, name, d'
148 hybriddict() for new template keywords.
148 hybriddict() for new template keywords.
149 """
149 """
150 c = [{key: k, value: v} for k, v in data.iteritems()]
150 c = [{key: k, value: v} for k, v in data.iteritems()]
151 t = context.resource(mapping, 'templ')
151 f = _showcompatlist(context, mapping, name, c, plural, separator)
152 f = _showlist(name, c, t, mapping, plural, separator)
153 return hybriddict(data, key=key, value=value, fmt=fmt, gen=f)
152 return hybriddict(data, key=key, value=value, fmt=fmt, gen=f)
154
153
155 def compatlist(context, mapping, name, data, element=None, fmt=None,
154 def compatlist(context, mapping, name, data, element=None, fmt=None,
@@ -159,12 +158,12 b' def compatlist(context, mapping, name, d'
159 This exists for backward compatibility with the old-style template. Use
158 This exists for backward compatibility with the old-style template. Use
160 hybridlist() for new template keywords.
159 hybridlist() for new template keywords.
161 """
160 """
162 t = context.resource(mapping, 'templ')
161 f = _showcompatlist(context, mapping, name, data, plural, separator)
163 f = _showlist(name, data, t, mapping, plural, separator)
164 return hybridlist(data, name=element or name, fmt=fmt, gen=f)
162 return hybridlist(data, name=element or name, fmt=fmt, gen=f)
165
163
166 def _showlist(name, values, templ, mapping, plural=None, separator=' '):
164 def _showcompatlist(context, mapping, name, values, plural=None, separator=' '):
167 '''expand set of values.
165 """Return a generator that renders old-style list template
166
168 name is name of key in template map.
167 name is name of key in template map.
169 values is list of strings or dicts.
168 values is list of strings or dicts.
170 plural is plural of name, if not simply name + 's'.
169 plural is plural of name, if not simply name + 's'.
@@ -183,15 +182,15 b' def _showlist(name, values, templ, mappi'
183 map, expand it instead of 'foo' for last key.
182 map, expand it instead of 'foo' for last key.
184
183
185 expand 'end_foos'.
184 expand 'end_foos'.
186 '''
185 """
187 if not plural:
186 if not plural:
188 plural = name + 's'
187 plural = name + 's'
189 if not values:
188 if not values:
190 noname = 'no_' + plural
189 noname = 'no_' + plural
191 if noname in templ:
190 if context.preload(noname):
192 yield templ.generate(noname, mapping)
191 yield context.process(noname, mapping)
193 return
192 return
194 if name not in templ:
193 if not context.preload(name):
195 if isinstance(values[0], bytes):
194 if isinstance(values[0], bytes):
196 yield separator.join(values)
195 yield separator.join(values)
197 else:
196 else:
@@ -201,8 +200,8 b' def _showlist(name, values, templ, mappi'
201 yield r
200 yield r
202 return
201 return
203 startname = 'start_' + plural
202 startname = 'start_' + plural
204 if startname in templ:
203 if context.preload(startname):
205 yield templ.generate(startname, mapping)
204 yield context.process(startname, mapping)
206 vmapping = mapping.copy()
205 vmapping = mapping.copy()
207 def one(v, tag=name):
206 def one(v, tag=name):
208 try:
207 try:
@@ -217,9 +216,9 b' def _showlist(name, values, templ, mappi'
217 vmapping[a] = b
216 vmapping[a] = b
218 except (TypeError, ValueError):
217 except (TypeError, ValueError):
219 vmapping[name] = v
218 vmapping[name] = v
220 return templ.generate(tag, vmapping)
219 return context.process(tag, vmapping)
221 lastname = 'last_' + name
220 lastname = 'last_' + name
222 if lastname in templ:
221 if context.preload(lastname):
223 last = values.pop()
222 last = values.pop()
224 else:
223 else:
225 last = None
224 last = None
@@ -228,8 +227,8 b' def _showlist(name, values, templ, mappi'
228 if last is not None:
227 if last is not None:
229 yield one(last, tag=lastname)
228 yield one(last, tag=lastname)
230 endname = 'end_' + plural
229 endname = 'end_' + plural
231 if endname in templ:
230 if context.preload(endname):
232 yield templ.generate(endname, mapping)
231 yield context.process(endname, mapping)
233
232
234 def stringify(thing):
233 def stringify(thing):
235 """Turn values into bytes by converting into text and concatenating them"""
234 """Turn values into bytes by converting into text and concatenating them"""
General Comments 0
You need to be logged in to leave comments. Login now