mercurial/templatekw.py
changeset 10060 f780b1098efc
parent 10058 c829563b3118
child 10260 fe699ca08a45
equal deleted inserted replaced
10059:9dd4e2859482 10060:f780b1098efc
     4 #
     4 #
     5 # This software may be used and distributed according to the terms of the
     5 # This software may be used and distributed according to the terms of the
     6 # GNU General Public License version 2, incorporated herein by reference.
     6 # GNU General Public License version 2, incorporated herein by reference.
     7 
     7 
     8 from node import hex
     8 from node import hex
     9 import encoding, patch, util
     9 import encoding, patch, util, error
    10 
    10 
    11 def showlist(templ, name, values, plural=None, **args):
    11 def showlist(templ, name, values, plural=None, **args):
    12     '''expand set of values.
    12     '''expand set of values.
    13     name is name of key in template map.
    13     name is name of key in template map.
    14     values is list of strings or dicts.
    14     values is list of strings or dicts.
   106             todo.extend(p.rev() for p in ctx.parents())
   106             todo.extend(p.rev() for p in ctx.parents())
   107             continue
   107             continue
   108         latesttags[rev] = pdate, pdist + 1, ptag
   108         latesttags[rev] = pdate, pdist + 1, ptag
   109     return latesttags[rev]
   109     return latesttags[rev]
   110 
   110 
       
   111 def getrenamedfn(repo, endrev=None):
       
   112     rcache = {}
       
   113     if endrev is None:
       
   114         endrev = len(repo)
       
   115 
       
   116     def getrenamed(fn, rev):
       
   117         '''looks up all renames for a file (up to endrev) the first
       
   118         time the file is given. It indexes on the changerev and only
       
   119         parses the manifest if linkrev != changerev.
       
   120         Returns rename info for fn at changerev rev.'''
       
   121         if fn not in rcache:
       
   122             rcache[fn] = {}
       
   123             fl = repo.file(fn)
       
   124             for i in fl:
       
   125                 lr = fl.linkrev(i)
       
   126                 renamed = fl.renamed(fl.node(i))
       
   127                 rcache[fn][lr] = renamed
       
   128                 if lr >= endrev:
       
   129                     break
       
   130         if rev in rcache[fn]:
       
   131             return rcache[fn][rev]
       
   132 
       
   133         # If linkrev != rev (i.e. rev not found in rcache) fallback to
       
   134         # filectx logic.
       
   135         try:
       
   136             return repo[rev][fn].renamed()
       
   137         except error.LookupError:
       
   138             return None
       
   139 
       
   140     return getrenamed
       
   141 
       
   142 
   111 def showauthor(repo, ctx, templ, **args):
   143 def showauthor(repo, ctx, templ, **args):
   112     return ctx.user()
   144     return ctx.user()
   113 
   145 
   114 def showbranches(repo, ctx, templ, **args):
   146 def showbranches(repo, ctx, templ, **args):
   115     branch = ctx.branch()
   147     branch = ctx.branch()
   139         yield templ('extra', **args)
   171         yield templ('extra', **args)
   140 
   172 
   141 def showfileadds(repo, ctx, templ, revcache, **args):
   173 def showfileadds(repo, ctx, templ, revcache, **args):
   142     return showlist(templ, 'file_add', getfiles(repo, ctx, revcache)[1], **args)
   174     return showlist(templ, 'file_add', getfiles(repo, ctx, revcache)[1], **args)
   143 
   175 
   144 def showfilecopies(repo, ctx, templ, revcache, **args):
   176 def showfilecopies(repo, ctx, templ, cache, revcache, **args):
   145     c = [{'name': x[0], 'source': x[1]} for x in revcache['copies']]
   177     copies = revcache.get('copies')
       
   178     if copies is None:
       
   179         if 'getrenamed' not in cache:
       
   180             cache['getrenamed'] = getrenamedfn(repo)
       
   181         copies = []
       
   182         getrenamed = cache['getrenamed']
       
   183         for fn in ctx.files():
       
   184             rename = getrenamed(fn, ctx.rev())
       
   185             if rename:
       
   186                 copies.append((fn, rename[0]))
       
   187             
       
   188     c = [{'name': x[0], 'source': x[1]} for x in copies]
       
   189     return showlist(templ, 'file_copy', c, plural='file_copies', **args)
       
   190 
       
   191 # showfilecopiesswitch() displays file copies only if copy records are
       
   192 # provided before calling the templater, usually with a --copies
       
   193 # command line switch.
       
   194 def showfilecopiesswitch(repo, ctx, templ, cache, revcache, **args):
       
   195     copies = revcache.get('copies') or []
       
   196     c = [{'name': x[0], 'source': x[1]} for x in copies]
   146     return showlist(templ, 'file_copy', c, plural='file_copies', **args)
   197     return showlist(templ, 'file_copy', c, plural='file_copies', **args)
   147 
   198 
   148 def showfiledels(repo, ctx, templ, revcache, **args):
   199 def showfiledels(repo, ctx, templ, revcache, **args):
   149     return showlist(templ, 'file_del', getfiles(repo, ctx, revcache)[2], **args)
   200     return showlist(templ, 'file_del', getfiles(repo, ctx, revcache)[2], **args)
   150 
   201 
   182     'desc': showdescription,
   233     'desc': showdescription,
   183     'diffstat': showdiffstat,
   234     'diffstat': showdiffstat,
   184     'extras': showextras,
   235     'extras': showextras,
   185     'file_adds': showfileadds,
   236     'file_adds': showfileadds,
   186     'file_copies': showfilecopies,
   237     'file_copies': showfilecopies,
       
   238     'file_copies_switch': showfilecopiesswitch,
   187     'file_dels': showfiledels,
   239     'file_dels': showfiledels,
   188     'file_mods': showfilemods,
   240     'file_mods': showfilemods,
   189     'files': showfiles,
   241     'files': showfiles,
   190     'latesttag': showlatesttag,
   242     'latesttag': showlatesttag,
   191     'latesttagdistance': showlatesttagdistance,
   243     'latesttagdistance': showlatesttagdistance,