mercurial/templatekw.py
changeset 17631 0b241d7a8c62
parent 17358 2917f82f6040
child 18715 c4ff927b6f68
equal deleted inserted replaced
17630:ff5ed1ecd43a 17631:0b241d7a8c62
     7 
     7 
     8 from node import hex
     8 from node import hex
     9 import patch, util, error
     9 import patch, util, error
    10 import hbisect
    10 import hbisect
    11 
    11 
    12 def showlist(name, values, plural=None, **args):
    12 # This helper class allows us to handle both:
       
    13 #  "{files}" (legacy command-line-specific list hack) and
       
    14 #  "{files % '{file}\n'}" (hgweb-style with inlining and function support)
       
    15 
       
    16 class _hybrid(object):
       
    17     def __init__(self, gen, values):
       
    18         self.gen = gen
       
    19         self.values = values
       
    20     def __iter__(self):
       
    21         return self.gen
       
    22     def __call__(self):
       
    23         for x in self.values:
       
    24             yield x
       
    25 
       
    26 def showlist(name, values, plural=None, element=None, **args):
       
    27     if not element:
       
    28         element = name
       
    29     f = _showlist(name, values, plural, **args)
       
    30     return _hybrid(f, [{element: x} for x in values])
       
    31 
       
    32 def _showlist(name, values, plural=None, **args):
    13     '''expand set of values.
    33     '''expand set of values.
    14     name is name of key in template map.
    34     name is name of key in template map.
    15     values is list of strings or dicts.
    35     values is list of strings or dicts.
    16     plural is plural of name, if not simply name + 's'.
    36     plural is plural of name, if not simply name + 's'.
    17 
    37 
   174 
   194 
   175 def showchildren(**args):
   195 def showchildren(**args):
   176     """:children: List of strings. The children of the changeset."""
   196     """:children: List of strings. The children of the changeset."""
   177     ctx = args['ctx']
   197     ctx = args['ctx']
   178     childrevs = ['%d:%s' % (cctx, cctx) for cctx in ctx.children()]
   198     childrevs = ['%d:%s' % (cctx, cctx) for cctx in ctx.children()]
   179     return showlist('children', childrevs, **args)
   199     return showlist('children', childrevs, element='child', **args)
   180 
   200 
   181 def showdate(repo, ctx, templ, **args):
   201 def showdate(repo, ctx, templ, **args):
   182     """:date: Date information. The date when the changeset was committed."""
   202     """:date: Date information. The date when the changeset was committed."""
   183     return ctx.date()
   203     return ctx.date()
   184 
   204 
   202         yield templ('extra', **args)
   222         yield templ('extra', **args)
   203 
   223 
   204 def showfileadds(**args):
   224 def showfileadds(**args):
   205     """:file_adds: List of strings. Files added by this changeset."""
   225     """:file_adds: List of strings. Files added by this changeset."""
   206     repo, ctx, revcache = args['repo'], args['ctx'], args['revcache']
   226     repo, ctx, revcache = args['repo'], args['ctx'], args['revcache']
   207     return showlist('file_add', getfiles(repo, ctx, revcache)[1], **args)
   227     return showlist('file_add', getfiles(repo, ctx, revcache)[1],
       
   228                     element='file', **args)
   208 
   229 
   209 def showfilecopies(**args):
   230 def showfilecopies(**args):
   210     """:file_copies: List of strings. Files copied in this changeset with
   231     """:file_copies: List of strings. Files copied in this changeset with
   211     their sources.
   232     their sources.
   212     """
   233     """
   221             rename = getrenamed(fn, ctx.rev())
   242             rename = getrenamed(fn, ctx.rev())
   222             if rename:
   243             if rename:
   223                 copies.append((fn, rename[0]))
   244                 copies.append((fn, rename[0]))
   224 
   245 
   225     c = [{'name': x[0], 'source': x[1]} for x in copies]
   246     c = [{'name': x[0], 'source': x[1]} for x in copies]
   226     return showlist('file_copy', c, plural='file_copies', **args)
   247     return showlist('file_copy', c, plural='file_copies',
       
   248                     element='file', **args)
   227 
   249 
   228 # showfilecopiesswitch() displays file copies only if copy records are
   250 # showfilecopiesswitch() displays file copies only if copy records are
   229 # provided before calling the templater, usually with a --copies
   251 # provided before calling the templater, usually with a --copies
   230 # command line switch.
   252 # command line switch.
   231 def showfilecopiesswitch(**args):
   253 def showfilecopiesswitch(**args):
   232     """:file_copies_switch: List of strings. Like "file_copies" but displayed
   254     """:file_copies_switch: List of strings. Like "file_copies" but displayed
   233     only if the --copied switch is set.
   255     only if the --copied switch is set.
   234     """
   256     """
   235     copies = args['revcache'].get('copies') or []
   257     copies = args['revcache'].get('copies') or []
   236     c = [{'name': x[0], 'source': x[1]} for x in copies]
   258     c = [{'name': x[0], 'source': x[1]} for x in copies]
   237     return showlist('file_copy', c, plural='file_copies', **args)
   259     return showlist('file_copy', c, plural='file_copies',
       
   260                     element='file', **args)
   238 
   261 
   239 def showfiledels(**args):
   262 def showfiledels(**args):
   240     """:file_dels: List of strings. Files removed by this changeset."""
   263     """:file_dels: List of strings. Files removed by this changeset."""
   241     repo, ctx, revcache = args['repo'], args['ctx'], args['revcache']
   264     repo, ctx, revcache = args['repo'], args['ctx'], args['revcache']
   242     return showlist('file_del', getfiles(repo, ctx, revcache)[2], **args)
   265     return showlist('file_del', getfiles(repo, ctx, revcache)[2],
       
   266                     element='file', **args)
   243 
   267 
   244 def showfilemods(**args):
   268 def showfilemods(**args):
   245     """:file_mods: List of strings. Files modified by this changeset."""
   269     """:file_mods: List of strings. Files modified by this changeset."""
   246     repo, ctx, revcache = args['repo'], args['ctx'], args['revcache']
   270     repo, ctx, revcache = args['repo'], args['ctx'], args['revcache']
   247     return showlist('file_mod', getfiles(repo, ctx, revcache)[0], **args)
   271     return showlist('file_mod', getfiles(repo, ctx, revcache)[0],
       
   272                     element='file', **args)
   248 
   273 
   249 def showfiles(**args):
   274 def showfiles(**args):
   250     """:files: List of strings. All files modified, added, or removed by this
   275     """:files: List of strings. All files modified, added, or removed by this
   251     changeset.
   276     changeset.
   252     """
   277     """