[PATCH 3 of 3] template: unify template output to use abspath (BC)

Yuya Nishihara yuya at tcha.org
Fri Apr 15 12:29:09 EDT 2016


On Wed, 13 Apr 2016 12:14:36 -0500, timeless wrote:
> # HG changeset patch
> # User timeless <timeless at mozdev.org>
> # Date 1460548072 0
> #      Wed Apr 13 11:47:52 2016 +0000
> # Node ID 38d467dc57b027547d6fd201d2b0ef5261fbd192
> # Parent  4161598737bc141a062d8fa0d2c90da823e5b6c9
> template: unify template output to use abspath (BC)
> 
> Before this change, annotate used "file", which was inaccurate,
> the field is actually an abspath.
> 
> {file_dels}, {file_adds}, {file_copies}, {file_mods}, {files} are
> changed so their % field is {abspath} instead of {file}.
> 
> {file} is retained for backwards compatibility.

I don't see why {file} was bad, and should be encouraged to use "abspath",
even though {file} exists for years in log templates.

So I'm -0.5 for this.

> --- a/mercurial/commands.py
> +++ b/mercurial/commands.py
> @@ -422,18 +422,26 @@
>          hexfn = fm.hexfunc
>          formatrev = formathex = str
>  
> +    _abspath = lambda x: x[0].path()
>      opmap = [('user', ' ', lambda x: x[0].user(), ui.shortuser),
>               ('number', ' ', lambda x: x[0].rev(), formatrev),
>               ('changeset', ' ', lambda x: hexfn(x[0].node()), formathex),
>               ('date', ' ', lambda x: x[0].date(), util.cachefunc(datefunc)),
> -             ('file', ' ', lambda x: x[0].path(), str),
> +             ('abspath', ' ', _abspath, str),
> +             ('file', None, _abspath, str),
>               ('line_number', ':', lambda x: x[1], str),
>              ]
> -    fieldnamemap = {'number': 'rev', 'changeset': 'node'}
> +    fieldnamemap = {
> +             'changeset': 'node',
> +             'file': 'abspath',
> +             'number': 'rev',
> +            }
>  
>      if (not opts.get('user') and not opts.get('changeset')
>          and not opts.get('date') and not opts.get('file')):
>          opts['number'] = True
> +    if opts.get('file'):
> +        opts['abspath'] = True
>  
>      linenumber = opts.get('line_number') is not None
>      if linenumber and (not opts.get('changeset')) and (not opts.get('number')):
> @@ -449,7 +457,9 @@
>                 if opts.get(op)]
>      funcmap[0] = (funcmap[0][0], '') # no separator in front of first column
>      fields = ' '.join(fieldnamemap.get(op, op) for op, sep, get, fmt in opmap
> -                      if opts.get(op))
> +                      if opts.get(op) and sep is not None)
> +    datafields = [op for op, sep, get, fmt in opmap
> +                  if opts.get(op) and sep is None]
>  
>      def bad(x, y):
>          raise error.Abort("%s: %s" % (x, y))
> @@ -469,23 +479,37 @@
>                                diffopts=diffopts)
>          formats = []
>          pieces = []
> +        data = []
>  
>          for f, sep in funcmap:
>              l = [f(n) for n, dummy in lines]
>              if l:
>                  if fm:
> +                    if sep is None:
> +                        continue
>                      formats.append(['%s' for x in l])
>                  else:
> +                    if sep is None:
> +                        data.append(l)
> +                        continue
>                      sizes = [encoding.colwidth(x) for x in l]
>                      ml = max(sizes)
>                      formats.append([sep + ' ' * (ml - w) + '%s' for w in sizes])
>                  pieces.append(l)
>  
> -        for f, p, l in zip(zip(*formats), zip(*pieces), lines):
> -            fm.startitem()
> -            fm.write(fields, "".join(f), *p)
> -            fm.write('line', ": %s", l[1])
> -
> +        if not data:
> +            for f, p, l in zip(zip(*formats), zip(*pieces), lines):
> +                fm.startitem()
> +                fm.write(fields, "".join(f), *p)
> +                fm.write('line', ": %s", l[1])
> +        else:
> +            for f, p, d, l in zip(zip(*formats), zip(*pieces), zip(*data),
> +                                  lines):
> +                fm.startitem()
> +                fm.write(fields, "".join(f), *p)
> +                fm.write('line', ": %s", l[1])
> +                d = dict(zip(datafields, d))
> +                fm.data(**d)

Instead of abusing "sep", I would do:

  abspathindex = ...  # index in fields
  ...
  for f, p, l:
      ...
      if abspathindex >= 0:
          fm.data(file=p[abspathindex])


More information about the Mercurial-devel mailing list