hgkw/keyword.py
branchself_initializing_hook
changeset 96 682e684a15e9
parent 94 d34486cc99b0
child 98 121da9c0a325
equal deleted inserted replaced
94:d34486cc99b0 96:682e684a15e9
    36 from mercurial import context, util
    36 from mercurial import context, util
    37 import os.path, re, sys, time
    37 import os.path, re, sys, time
    38 
    38 
    39 re_kw = re.compile(
    39 re_kw = re.compile(
    40         r'\$(Id|Header|Author|Date|Revision|RCSFile|Source)[^$]*?\$')
    40         r'\$(Id|Header|Author|Date|Revision|RCSFile|Source)[^$]*?\$')
    41 
       
    42 def kwexpand(mobj, kwfctx):
       
    43     '''Called by kwfilelog.read and pretxnkw.
       
    44     Expands keywords according to file context.'''
       
    45     return '$%s: %s $' % (mobj.group(1), kwfctx.expand(mobj.group(1)))
       
    46 
    41 
    47 def kwfmatches(ui, repo, files):
    42 def kwfmatches(ui, repo, files):
    48     '''Selects candidates for keyword substitution
    43     '''Selects candidates for keyword substitution
    49     configured in keyword section in hgrc.'''
    44     configured in keyword section in hgrc.'''
    50     files = [f for f in files if not f.startswith('.hg')]
    45     files = [f for f in files if not f.startswith('.hg')]
    85         return ' '.join(
    80         return ' '.join(
    86                 [self.Source(), self.Revision(), self.Date(), self.Author()])
    81                 [self.Source(), self.Revision(), self.Date(), self.Author()])
    87     def Id(self):
    82     def Id(self):
    88         return ' '.join(
    83         return ' '.join(
    89                 [self.RCSFile(), self.Revision(), self.Date(), self.Author()])
    84                 [self.RCSFile(), self.Revision(), self.Date(), self.Author()])
    90     def expand(self, kw):
    85     def expand(self, mobj):
    91         '''Called from kwexpand, evaluates keyword.'''
    86         '''Called from kwexpand, evaluates keyword.'''
    92         return eval('self.%s()' % kw)
    87         kw = mobj.group(1)
       
    88         return '$%s: %s $' % (kw, eval('self.%s()' % kw))
    93 
    89 
    94 
    90 
    95 def reposetup(ui, repo):
    91 def reposetup(ui, repo):
    96     from mercurial import filelog, revlog
    92     from mercurial import filelog, revlog
    97 
    93 
   124             '''Substitutes keywords when reading filelog.'''
   120             '''Substitutes keywords when reading filelog.'''
   125             data = super(kwfilelog, self).read(node)
   121             data = super(kwfilelog, self).read(node)
   126             if self.iskwcandidate(data):
   122             if self.iskwcandidate(data):
   127                 kwfctx = kwfilectx(self._repo, self._path,
   123                 kwfctx = kwfilectx(self._repo, self._path,
   128                             fileid=node, filelog=self)
   124                             fileid=node, filelog=self)
   129                 return re_kw.sub(lambda m: kwexpand(m, kwfctx), data)
   125                 return re_kw.sub(kwfctx.expand, data)
   130             return data
   126             return data
   131 
   127 
   132         def add(self, text, meta, tr, link, p1=None, p2=None):
   128         def add(self, text, meta, tr, link, p1=None, p2=None):
   133             '''Removes keyword substitutions when adding to filelog.'''
   129             '''Removes keyword substitutions when adding to filelog.'''
   134             if self.iskwcandidate(text):
   130             if self.iskwcandidate(text):
   181 
   177 
   182     for f in kwfmatches(ui, repo, modified+added):
   178     for f in kwfmatches(ui, repo, modified+added):
   183         data = repo.wfile(f).read()
   179         data = repo.wfile(f).read()
   184         if not util.binary(data):
   180         if not util.binary(data):
   185             kwfctx = kwfilectx(repo, f, changeid=args['node'])
   181             kwfctx = kwfilectx(repo, f, changeid=args['node'])
   186             data, kwct = re_kw.subn(lambda m: kwexpand(m, kwfctx), data)
   182             data, kwct = re_kw.subn(kwfctx.expand, data)
   187             if kwct:
   183             if kwct:
   188                 ui.debug(_('overwriting %s expanding keywords\n' % f))
   184                 ui.debug(_('overwriting %s expanding keywords\n' % f))
   189                 repo.wfile(f, 'w').write(data)
   185                 repo.wfile(f, 'w').write(data)