hgkw/keyword.py
branchsolo-extension
changeset 57 f95c21f87e9c
parent 56 e8834575128b
child 58 9b428ae6ff15
equal deleted inserted replaced
56:e8834575128b 57:f95c21f87e9c
    44 import os.path, re, sys
    44 import os.path, re, sys
    45 
    45 
    46 # supported keywords for use in regexes
    46 # supported keywords for use in regexes
    47 hgkeywords = 'Id|Header|Author|Date|Revision|RCSFile|Source'
    47 hgkeywords = 'Id|Header|Author|Date|Revision|RCSFile|Source'
    48 
    48 
    49 def kwexpand(matchobj, repo, Revision, f, date, Author):
    49 def kwexpand(matchobj, repo, path, changeid=None, fileid=None, filelog=None):
    50     '''Called by kwfilelog.read and pretxnkw.
    50     '''Called by kwfilelog.read and pretxnkw.
    51     Sets supported keywords as local variables and evaluates them to
    51     Sets supported keywords as local variables and evaluates them to
    52     their expansion if matchobj is equal to string representation.'''
    52     their expansion if matchobj is equal to string representation.'''
    53 
    53 
    54     RCSFile = os.path.basename(f)+',v'
    54     c = context.filectx(repo, path,
    55     Source = repo.wjoin(f)+',v'
    55             changeid=changeid, fileid=fileid, filelog=filelog)
       
    56     date = c.date()
       
    57 
       
    58     Revision = c.changectx()
       
    59     Author = c.user()
       
    60     RCSFile = os.path.basename(path)+',v'
       
    61     Source = repo.wjoin(path)+',v'
    56     Date = util.datestr(date=date)
    62     Date = util.datestr(date=date)
    57     revdateauth = '%s %s %s' % (Revision,
    63     revdateauth = '%s %s %s' % (Revision,
    58             util.datestr(date=date, format=util.defaultdateformats[0]),
    64             util.datestr(date=date, format=util.defaultdateformats[0]),
    59             util.shortuser(Author))             # %Y-%m-%d %H:%M:%S
    65             util.shortuser(Author))             # %Y-%m-%d %H:%M:%S
    60     Header = '%s %s' % (Source, revdateauth)
    66     Header = '%s %s' % (Source, revdateauth)
    86                 for pat, opt in self._repo.ui.configitems('keyword'):
    92                 for pat, opt in self._repo.ui.configitems('keyword'):
    87                     if opt == 'expand':
    93                     if opt == 'expand':
    88                         mf = util.matcher(self._repo.root,
    94                         mf = util.matcher(self._repo.root,
    89                                 '', [pat], [], [])[1]
    95                                 '', [pat], [], [])[1]
    90                         if mf(self._path):
    96                         if mf(self._path):
    91                             c = context.filectx(self._repo, self._path,
       
    92                                     fileid=node, filelog=self)
       
    93 
       
    94                             def kwexpander(matchobj):
    97                             def kwexpander(matchobj):
    95                                 return kwexpand(matchobj,
    98                                 return kwexpand(matchobj, self._repo,
    96                                         self._repo, c.changectx(), self._path,
    99                                         self._path, fileid=node, filelog=self)
    97                                         c.date(), c.user())
       
    98 
       
    99                             re_kw = re.compile(r'\$(%s)\$' % hgkeywords)
   100                             re_kw = re.compile(r'\$(%s)\$' % hgkeywords)
   100                             return re_kw.sub(kwexpander, data)
   101                             return re_kw.sub(kwexpander, data)
   101             return data
   102             return data
   102 
   103 
   103         def add(self, text, meta, tr, link, p1=None, p2=None):
   104         def add(self, text, meta, tr, link, p1=None, p2=None):
   152                 if mf(candidate) and candidate not in files:
   153                 if mf(candidate) and candidate not in files:
   153                     files.append(candidate)
   154                     files.append(candidate)
   154     if not files:
   155     if not files:
   155         return False
   156         return False
   156 
   157 
   157     user, date = repo.changelog.read(repo.changelog.tip())[1:3]
       
   158     # update both expanded and unexpanded keywords
   158     # update both expanded and unexpanded keywords
   159     re_kw = re.compile(r'\$(%s)(: [^$]+? )?\$' % hgkeywords)
   159     re_kw = re.compile(r'\$(%s)(: [^$]+? )?\$' % hgkeywords)
   160 
   160 
   161     for f in files:
   161     for f in files:
   162         data = repo.wfile(f).read()
   162         data = repo.wfile(f).read()
   163         if not util.binary(data):
   163         if not util.binary(data):
   164 
       
   165             def kwexpander(matchobj):
   164             def kwexpander(matchobj):
   166                 return kwexpand(matchobj,
   165                 return kwexpand(matchobj, repo, f, changeid=args['node'])
   167                         repo, args['node'][:12], f, date, user)
       
   168 
       
   169             data, kwct = re_kw.subn(kwexpander, data)
   166             data, kwct = re_kw.subn(kwexpander, data)
   170             if kwct:
   167             if kwct:
   171                 ui.note(_('expanding keywords in %s\n' % f))
   168                 ui.note(_('expanding keywords in %s\n' % f))
   172                 repo.wfile(f, 'w').write(data)
   169                 repo.wfile(f, 'w').write(data)