hgkw/keyword.py
branch0.9.3-compat
changeset 79 33eb5aa6f6e1
parent 78 474b415433a1
child 80 cee5fef33cf8
equal deleted inserted replaced
77:048153ccf040 79:33eb5aa6f6e1
     1 # keyword.py - keyword expansion for mercurial
     1 # keyword.py - keyword expansion for mercurial
     2 # $Id: keyword.py,v c4daf4753ed3 2007-01-08 03:08:24 +0100 blacktrash $
     2 # $Id$
     3 
     3 
     4 '''keyword expansion hack against the grain of a DSCM
     4 '''keyword expansion hack against the grain of a DSCM
     5 
     5 
     6 This extension lets you expand RCS/CVS-like keywords in a Mercurial
     6 This extension lets you expand RCS/CVS-like keywords in a Mercurial
     7 repository.
     7 repository.
   240 
   240 
   241             self.hook("commit", node=hex(n), parent1=xp1, parent2=xp2)
   241             self.hook("commit", node=hex(n), parent1=xp1, parent2=xp2)
   242             return n
   242             return n
   243 
   243 
   244     class kwfilelog(filelog.filelog):
   244     class kwfilelog(filelog.filelog):
       
   245         '''
       
   246         Superclass over filelog to customize it's read, add, cmp methods.
       
   247         Keywords are "stored" unexpanded, and expanded on reading.
       
   248         '''
   245         def __init__(self, opener, path, repo,
   249         def __init__(self, opener, path, repo,
   246                      defversion=revlog.REVLOG_DEFAULT_VERSION):
   250                      defversion=revlog.REVLOG_DEFAULT_VERSION):
   247             super(kwfilelog, self).__init__(opener, path, defversion)
   251             super(kwfilelog, self).__init__(opener, path, defversion)
   248             self._repo = repo
   252             self._repo = repo
   249             self._path = path
   253             self._path = path
   250 
   254 
       
   255         def iskwcandidate(self, data):
       
   256             '''Decides whether to act on keywords.'''
       
   257             return (kwfmatches(ui, self._repo, [self._path])
       
   258                     and not util.binary(data))
       
   259 
   251         def read(self, node):
   260         def read(self, node):
       
   261             '''Substitutes keywords when reading filelog.'''
   252             data = super(kwfilelog, self).read(node)
   262             data = super(kwfilelog, self).read(node)
   253             if not util.binary(data) and \
   263             if self.iskwcandidate(data):
   254                     kwfmatches(ui, self._repo, [self._path]):
       
   255                 return re_kw.sub(lambda m:
   264                 return re_kw.sub(lambda m:
   256                         kwexpand(m, self._repo, self._path,
   265                         kwexpand(m, self._repo, self._path,
   257                             fileid=node, filelog=self), data)
   266                             fileid=node, filelog=self), data)
   258             return data
   267             return data
   259 
   268 
       
   269         def add(self, text, meta, tr, link, p1=None, p2=None):
       
   270             '''Removes keyword substitutions when adding to filelog.'''
       
   271             if self.iskwcandidate(text):
       
   272                 text = re_kw.sub(r'$\1$', text)
       
   273             return super(kwfilelog, self).add(text,
       
   274                     meta, tr, link, p1=None, p2=None)
       
   275 
   260         def cmp(self, node, text):
   276         def cmp(self, node, text):
   261             '''Removes keyword substitution for comparison.'''
   277             '''Removes keyword substitutions for comparison.'''
   262             if not util.binary(text) and \
   278             if self.iskwcandidate(text):
   263                     kwfmatches(ui, self._repo, [self._path]):
       
   264                 text = re_kw.sub(r'$\1$', text)
   279                 text = re_kw.sub(r'$\1$', text)
   265             return super(kwfilelog, self).cmp(node, text)
   280             return super(kwfilelog, self).cmp(node, text)
   266 
   281 
   267     filelog.filelog = kwfilelog
   282     filelog.filelog = kwfilelog
   268     repo.__class__ = kwrepo
   283     repo.__class__ = kwrepo