hgkw/keyword.py
branchmodular
changeset 46 67e9fb23a32b
parent 44 dc6e7d0e607f
equal deleted inserted replaced
45:5acf520f2115 46:67e9fb23a32b
     1 from mercurial import hg, filelog, revlog, context, util
     1 from mercurial.i18n import _
     2 import os.path, re
     2 from mercurial import filelog, revlog, context, util
     3 
     3 import re
     4 hgkeywords = 'Id|Header|Author|Date|Revision|RCSFile|Source'
       
     5 
     4 
     6 def reposetup(ui, repo):
     5 def reposetup(ui, repo):
       
     6     try:
       
     7         from hgkw import kwutil
       
     8     except ImportError, e:
       
     9         raise util.Abort(_('%s\nkeyword extension needs package hgkw\n') % e)
       
    10 
     7     if not repo.local():
    11     if not repo.local():
     8         return
    12         return
     9 
    13 
    10     class kwrepo(repo.__class__):
    14     class kwrepo(repo.__class__):
    11         def file(self, f):
    15         def file(self, f):
    34             for pat, opt in self._repo.ui.configitems('keyword'):
    38             for pat, opt in self._repo.ui.configitems('keyword'):
    35                 if opt == 'expand':
    39                 if opt == 'expand':
    36                     mf = util.matcher(self._repo.root,
    40                     mf = util.matcher(self._repo.root,
    37                             '', [pat], [], [])[1]
    41                             '', [pat], [], [])[1]
    38                     if mf(f):
    42                     if mf(f):
    39                         re_kw = re.compile(r'\$(%s)\$' % hgkeywords)
       
    40 
    43 
    41                         def kwexpand(matchobj):
    44                         def kwexpand(matchobj):
    42                             RCSFile = os.path.basename(f)+',v'
    45                             return kwutil.kwexpand(matchobj,
    43                             Source = os.path.join(self._repo.root, f)+',v'
    46                                     self._repo, c.changectx(), f,
    44                             Revision = c.changectx()
    47                                     c.date(), c.user())
    45                             Date = util.datestr(date=c.date())
       
    46                             Author = c.user()
       
    47                             revdateauth = '%s %s %s' % (
       
    48                                     Revision,
       
    49                                     util.datestr(date=c.date(),
       
    50                                         format=util.defaultdateformats[0]),
       
    51                                     util.shortuser(Author))
       
    52                             Header = '%s %s' % (Source, revdateauth)
       
    53                             Id = '%s %s' % (RCSFile, revdateauth)
       
    54                             return '$%s: %s $' % (
       
    55                                     matchobj.group(1), eval(matchobj.group(1)))
       
    56 
    48 
       
    49                         re_kw = re.compile(r'\$(%s)\$' % kwutil.hgkeywords)
    57                         return re_kw.sub(kwexpand, data)
    50                         return re_kw.sub(kwexpand, data)
    58             return data
    51             return data
    59 
    52 
    60         def add(self, text, meta, tr, link, p1=None, p2=None):
    53         def add(self, text, meta, tr, link, p1=None, p2=None):
    61             if (not util.binary(text) and
    54             if (not util.binary(text) and
    62                    self._repo.ui.config('keyword', 'remove', True)):
    55                    self._repo.ui.config('keyword', 'remove', True)):
    63                 re_kw = re.compile(r'\$(%s): [^$]+? \$' % hgkeywords)
    56                 re_kw = re.compile(r'\$(%s): [^$]+? \$' % kwutil.hgkeywords)
    64                 text = re_kw.sub(r'$\1$', text)
    57                 text = re_kw.sub(r'$\1$', text)
    65             return super(kwfilelog, self).add(text, meta, tr, link, p1, p2)
    58             return super(kwfilelog, self).add(text, meta, tr, link, p1, p2)
    66 
    59 
    67     filelog.filelog = kwfilelog
    60     filelog.filelog = kwfilelog
    68     repo.__class__ = kwrepo
    61     repo.__class__ = kwrepo