hgkw/keyword.py
branchsolo-extension
changeset 59 94b26168791d
parent 58 9b428ae6ff15
child 60 9771775ef322
equal deleted inserted replaced
58:9b428ae6ff15 59:94b26168791d
    41 
    41 
    42 from mercurial.i18n import _
    42 from mercurial.i18n import _
    43 from mercurial import cmdutil, commands, context, filelog, revlog, util
    43 from mercurial import cmdutil, commands, context, filelog, revlog, util
    44 import os.path, re, sys
    44 import os.path, re, sys
    45 
    45 
    46 # supported keywords for use in regexes
    46 
    47 hgkeywords = 'Id|Header|Author|Date|Revision|RCSFile|Source'
    47 re_kw = re.compile(
       
    48         r'\$(Id|Header|Author|Date|Revision|RCSFile|Source)[^$]*?\$')
       
    49 
    48 
    50 
    49 def kwexpand(matchobj, repo, path, changeid=None, fileid=None, filelog=None):
    51 def kwexpand(matchobj, repo, path, changeid=None, fileid=None, filelog=None):
    50     '''Called by kwfilelog.read and pretxnkw.
    52     '''Called by kwfilelog.read and pretxnkw.
    51     Sets supported keywords as local variables and evaluates them to
    53     Sets supported keywords as local variables and evaluates them to
    52     their expansion if matchobj is equal to string representation.'''
    54     their expansion if matchobj is equal to string representation.'''
    92                 for pat, opt in self._repo.ui.configitems('keyword'):
    94                 for pat, opt in self._repo.ui.configitems('keyword'):
    93                     if opt == 'expand':
    95                     if opt == 'expand':
    94                         mf = util.matcher(self._repo.root,
    96                         mf = util.matcher(self._repo.root,
    95                                 '', [pat], [], [])[1]
    97                                 '', [pat], [], [])[1]
    96                         if mf(self._path):
    98                         if mf(self._path):
    97                             re_kw = re.compile(r'\$(%s)\$' % hgkeywords)
       
    98                             return re_kw.sub(lambda m:
    99                             return re_kw.sub(lambda m:
    99                                     kwexpand(m, self._repo, self._path,
   100                                     kwexpand(m, self._repo, self._path,
   100                                         fileid=node, filelog=self),
   101                                         fileid=node, filelog=self),
   101                                     data)
   102                                     data)
   102             return data
   103             return data
   103 
   104 
   104         def add(self, text, meta, tr, link, p1=None, p2=None):
   105         def add(self, text, meta, tr, link, p1=None, p2=None):
   105             if not util.binary(text):
   106             if not util.binary(text):
   106                 re_kw = re.compile(r'\$(%s): [^$]+? \$' % hgkeywords)
       
   107                 text = re_kw.sub(r'$\1$', text)
   107                 text = re_kw.sub(r'$\1$', text)
   108             return super(kwfilelog, self).add(text, meta, tr, link, p1, p2)
   108             return super(kwfilelog, self).add(text, meta, tr, link, p1, p2)
   109 
   109 
   110         def size(self, rev):
   110         def size(self, rev):
   111             '''Overrides filelog's size() to use kwfilelog.read().'''
   111             '''Overrides filelog's size() to use kwfilelog.read().'''
   153                 if mf(candidate) and candidate not in files:
   153                 if mf(candidate) and candidate not in files:
   154                     files.append(candidate)
   154                     files.append(candidate)
   155     if not files:
   155     if not files:
   156         return False
   156         return False
   157 
   157 
   158     # update both expanded and unexpanded keywords
       
   159     re_kw = re.compile(r'\$(%s)(: [^$]+? )?\$' % hgkeywords)
       
   160 
       
   161     for f in files:
   158     for f in files:
   162         data = repo.wfile(f).read()
   159         data = repo.wfile(f).read()
   163         if not util.binary(data):
   160         if not util.binary(data):
   164             data, kwct = re_kw.subn(lambda m:
   161             data, kwct = re_kw.subn(lambda m:
   165                     kwexpand(m, repo, f, changeid=args['node']),
   162                     kwexpand(m, repo, f, changeid=args['node']),