eggs/mercurial-1.7.3-py2.6-linux-x86_64.egg/mercurial/templatekw.py
changeset 69 c6bca38c1cbf
equal deleted inserted replaced
68:5ff1fc726848 69:c6bca38c1cbf
       
     1 # templatekw.py - common changeset template keywords
       
     2 #
       
     3 # Copyright 2005-2009 Matt Mackall <mpm@selenic.com>
       
     4 #
       
     5 # This software may be used and distributed according to the terms of the
       
     6 # GNU General Public License version 2 or any later version.
       
     7 
       
     8 from node import hex
       
     9 import encoding, patch, util, error
       
    10 
       
    11 def showlist(name, values, plural=None, **args):
       
    12     '''expand set of values.
       
    13     name is name of key in template map.
       
    14     values is list of strings or dicts.
       
    15     plural is plural of name, if not simply name + 's'.
       
    16 
       
    17     expansion works like this, given name 'foo'.
       
    18 
       
    19     if values is empty, expand 'no_foos'.
       
    20 
       
    21     if 'foo' not in template map, return values as a string,
       
    22     joined by space.
       
    23 
       
    24     expand 'start_foos'.
       
    25 
       
    26     for each value, expand 'foo'. if 'last_foo' in template
       
    27     map, expand it instead of 'foo' for last key.
       
    28 
       
    29     expand 'end_foos'.
       
    30     '''
       
    31     templ = args['templ']
       
    32     if plural:
       
    33         names = plural
       
    34     else: names = name + 's'
       
    35     if not values:
       
    36         noname = 'no_' + names
       
    37         if noname in templ:
       
    38             yield templ(noname, **args)
       
    39         return
       
    40     if name not in templ:
       
    41         if isinstance(values[0], str):
       
    42             yield ' '.join(values)
       
    43         else:
       
    44             for v in values:
       
    45                 yield dict(v, **args)
       
    46         return
       
    47     startname = 'start_' + names
       
    48     if startname in templ:
       
    49         yield templ(startname, **args)
       
    50     vargs = args.copy()
       
    51     def one(v, tag=name):
       
    52         try:
       
    53             vargs.update(v)
       
    54         except (AttributeError, ValueError):
       
    55             try:
       
    56                 for a, b in v:
       
    57                     vargs[a] = b
       
    58             except ValueError:
       
    59                 vargs[name] = v
       
    60         return templ(tag, **vargs)
       
    61     lastname = 'last_' + name
       
    62     if lastname in templ:
       
    63         last = values.pop()
       
    64     else:
       
    65         last = None
       
    66     for v in values:
       
    67         yield one(v)
       
    68     if last is not None:
       
    69         yield one(last, tag=lastname)
       
    70     endname = 'end_' + names
       
    71     if endname in templ:
       
    72         yield templ(endname, **args)
       
    73 
       
    74 def getfiles(repo, ctx, revcache):
       
    75     if 'files' not in revcache:
       
    76         revcache['files'] = repo.status(ctx.parents()[0].node(),
       
    77                                         ctx.node())[:3]
       
    78     return revcache['files']
       
    79 
       
    80 def getlatesttags(repo, ctx, cache):
       
    81     '''return date, distance and name for the latest tag of rev'''
       
    82 
       
    83     if 'latesttags' not in cache:
       
    84         # Cache mapping from rev to a tuple with tag date, tag
       
    85         # distance and tag name
       
    86         cache['latesttags'] = {-1: (0, 0, 'null')}
       
    87     latesttags = cache['latesttags']
       
    88 
       
    89     rev = ctx.rev()
       
    90     todo = [rev]
       
    91     while todo:
       
    92         rev = todo.pop()
       
    93         if rev in latesttags:
       
    94             continue
       
    95         ctx = repo[rev]
       
    96         tags = [t for t in ctx.tags() if repo.tagtype(t) == 'global']
       
    97         if tags:
       
    98             latesttags[rev] = ctx.date()[0], 0, ':'.join(sorted(tags))
       
    99             continue
       
   100         try:
       
   101             # The tuples are laid out so the right one can be found by
       
   102             # comparison.
       
   103             pdate, pdist, ptag = max(
       
   104                 latesttags[p.rev()] for p in ctx.parents())
       
   105         except KeyError:
       
   106             # Cache miss - recurse
       
   107             todo.append(rev)
       
   108             todo.extend(p.rev() for p in ctx.parents())
       
   109             continue
       
   110         latesttags[rev] = pdate, pdist + 1, ptag
       
   111     return latesttags[rev]
       
   112 
       
   113 def getrenamedfn(repo, endrev=None):
       
   114     rcache = {}
       
   115     if endrev is None:
       
   116         endrev = len(repo)
       
   117 
       
   118     def getrenamed(fn, rev):
       
   119         '''looks up all renames for a file (up to endrev) the first
       
   120         time the file is given. It indexes on the changerev and only
       
   121         parses the manifest if linkrev != changerev.
       
   122         Returns rename info for fn at changerev rev.'''
       
   123         if fn not in rcache:
       
   124             rcache[fn] = {}
       
   125             fl = repo.file(fn)
       
   126             for i in fl:
       
   127                 lr = fl.linkrev(i)
       
   128                 renamed = fl.renamed(fl.node(i))
       
   129                 rcache[fn][lr] = renamed
       
   130                 if lr >= endrev:
       
   131                     break
       
   132         if rev in rcache[fn]:
       
   133             return rcache[fn][rev]
       
   134 
       
   135         # If linkrev != rev (i.e. rev not found in rcache) fallback to
       
   136         # filectx logic.
       
   137         try:
       
   138             return repo[rev][fn].renamed()
       
   139         except error.LookupError:
       
   140             return None
       
   141 
       
   142     return getrenamed
       
   143 
       
   144 
       
   145 def showauthor(repo, ctx, templ, **args):
       
   146     return ctx.user()
       
   147 
       
   148 def showbranches(**args):
       
   149     branch = args['ctx'].branch()
       
   150     if branch != 'default':
       
   151         branch = encoding.tolocal(branch)
       
   152         return showlist('branch', [branch], plural='branches', **args)
       
   153 
       
   154 def showchildren(**args):
       
   155     ctx = args['ctx']
       
   156     childrevs = ['%d:%s' % (cctx, cctx) for cctx in ctx.children()]
       
   157     return showlist('children', childrevs, **args)
       
   158 
       
   159 def showdate(repo, ctx, templ, **args):
       
   160     return ctx.date()
       
   161 
       
   162 def showdescription(repo, ctx, templ, **args):
       
   163     return ctx.description().strip()
       
   164 
       
   165 def showdiffstat(repo, ctx, templ, **args):
       
   166     diff = patch.diff(repo, ctx.parents()[0].node(), ctx.node())
       
   167     files, adds, removes = 0, 0, 0
       
   168     for i in patch.diffstatdata(util.iterlines(diff)):
       
   169         files += 1
       
   170         adds += i[1]
       
   171         removes += i[2]
       
   172     return '%s: +%s/-%s' % (files, adds, removes)
       
   173 
       
   174 def showextras(**args):
       
   175     templ = args['templ']
       
   176     for key, value in sorted(args['ctx'].extra().items()):
       
   177         args = args.copy()
       
   178         args.update(dict(key=key, value=value))
       
   179         yield templ('extra', **args)
       
   180 
       
   181 def showfileadds(**args):
       
   182     repo, ctx, revcache = args['repo'], args['ctx'], args['revcache']
       
   183     return showlist('file_add', getfiles(repo, ctx, revcache)[1], **args)
       
   184 
       
   185 def showfilecopies(**args):
       
   186     cache, ctx = args['cache'], args['ctx']
       
   187     copies = args['revcache'].get('copies')
       
   188     if copies is None:
       
   189         if 'getrenamed' not in cache:
       
   190             cache['getrenamed'] = getrenamedfn(args['repo'])
       
   191         copies = []
       
   192         getrenamed = cache['getrenamed']
       
   193         for fn in ctx.files():
       
   194             rename = getrenamed(fn, ctx.rev())
       
   195             if rename:
       
   196                 copies.append((fn, rename[0]))
       
   197 
       
   198     c = [{'name': x[0], 'source': x[1]} for x in copies]
       
   199     return showlist('file_copy', c, plural='file_copies', **args)
       
   200 
       
   201 # showfilecopiesswitch() displays file copies only if copy records are
       
   202 # provided before calling the templater, usually with a --copies
       
   203 # command line switch.
       
   204 def showfilecopiesswitch(**args):
       
   205     copies = args['revcache'].get('copies') or []
       
   206     c = [{'name': x[0], 'source': x[1]} for x in copies]
       
   207     return showlist('file_copy', c, plural='file_copies', **args)
       
   208 
       
   209 def showfiledels(**args):
       
   210     repo, ctx, revcache = args['repo'], args['ctx'], args['revcache']
       
   211     return showlist('file_del', getfiles(repo, ctx, revcache)[2], **args)
       
   212 
       
   213 def showfilemods(**args):
       
   214     repo, ctx, revcache = args['repo'], args['ctx'], args['revcache']
       
   215     return showlist('file_mod', getfiles(repo, ctx, revcache)[0], **args)
       
   216 
       
   217 def showfiles(**args):
       
   218     return showlist('file', args['ctx'].files(), **args)
       
   219 
       
   220 def showlatesttag(repo, ctx, templ, cache, **args):
       
   221     return getlatesttags(repo, ctx, cache)[2]
       
   222 
       
   223 def showlatesttagdistance(repo, ctx, templ, cache, **args):
       
   224     return getlatesttags(repo, ctx, cache)[1]
       
   225 
       
   226 def showmanifest(**args):
       
   227     repo, ctx, templ = args['repo'], args['ctx'], args['templ']
       
   228     args = args.copy()
       
   229     args.update(dict(rev=repo.manifest.rev(ctx.changeset()[0]),
       
   230                      node=hex(ctx.changeset()[0])))
       
   231     return templ('manifest', **args)
       
   232 
       
   233 def shownode(repo, ctx, templ, **args):
       
   234     return ctx.hex()
       
   235 
       
   236 def showrev(repo, ctx, templ, **args):
       
   237     return ctx.rev()
       
   238 
       
   239 def showtags(**args):
       
   240     return showlist('tag', args['ctx'].tags(), **args)
       
   241 
       
   242 # keywords are callables like:
       
   243 # fn(repo, ctx, templ, cache, revcache, **args)
       
   244 # with:
       
   245 # repo - current repository instance
       
   246 # ctx - the changectx being displayed
       
   247 # templ - the templater instance
       
   248 # cache - a cache dictionary for the whole templater run
       
   249 # revcache - a cache dictionary for the current revision
       
   250 keywords = {
       
   251     'author': showauthor,
       
   252     'branches': showbranches,
       
   253     'children': showchildren,
       
   254     'date': showdate,
       
   255     'desc': showdescription,
       
   256     'diffstat': showdiffstat,
       
   257     'extras': showextras,
       
   258     'file_adds': showfileadds,
       
   259     'file_copies': showfilecopies,
       
   260     'file_copies_switch': showfilecopiesswitch,
       
   261     'file_dels': showfiledels,
       
   262     'file_mods': showfilemods,
       
   263     'files': showfiles,
       
   264     'latesttag': showlatesttag,
       
   265     'latesttagdistance': showlatesttagdistance,
       
   266     'manifest': showmanifest,
       
   267     'node': shownode,
       
   268     'rev': showrev,
       
   269     'tags': showtags,
       
   270 }
       
   271