hgkw/keyword.py
branchsolo-extension
changeset 78 474b415433a1
parent 77 048153ccf040
child 79 33eb5aa6f6e1
equal deleted inserted replaced
76:d9c80746d727 78:474b415433a1
     1 # keyword.py - keyword expansion for mercurial
     1 # keyword.py - keyword expansion for mercurial
     2 # $Id: keyword.py,v 85c345cd495b 2007-01-08 03:05:22 +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.
   244 
   244 
   245             self.hook("commit", node=hex(n), parent1=xp1, parent2=xp2)
   245             self.hook("commit", node=hex(n), parent1=xp1, parent2=xp2)
   246             return n
   246             return n
   247 
   247 
   248     class kwfilelog(filelog.filelog):
   248     class kwfilelog(filelog.filelog):
       
   249         '''
       
   250         Superclass over filelog to customize it's read, add, cmp methods.
       
   251         Keywords are "stored" unexpanded, and expanded on reading.
       
   252         '''
   249         def __init__(self, opener, path, repo,
   253         def __init__(self, opener, path, repo,
   250                      defversion=revlog.REVLOG_DEFAULT_VERSION):
   254                      defversion=revlog.REVLOG_DEFAULT_VERSION):
   251             super(kwfilelog, self).__init__(opener, path, defversion)
   255             super(kwfilelog, self).__init__(opener, path, defversion)
   252             self._repo = repo
   256             self._repo = repo
   253             self._path = path
   257             self._path = path
   254 
   258 
       
   259         def iskwcandidate(self, data):
       
   260             '''Decides whether to act on keywords.'''
       
   261             return (kwfmatches(ui, self._repo, [self._path])
       
   262                     and not util.binary(data))
       
   263 
   255         def read(self, node):
   264         def read(self, node):
       
   265             '''Substitutes keywords when reading filelog.'''
   256             data = super(kwfilelog, self).read(node)
   266             data = super(kwfilelog, self).read(node)
   257             if not util.binary(data) and \
   267             if self.iskwcandidate(data):
   258                     kwfmatches(ui, self._repo, [self._path]):
       
   259                 return re_kw.sub(lambda m:
   268                 return re_kw.sub(lambda m:
   260                         kwexpand(m, self._repo, self._path,
   269                         kwexpand(m, self._repo, self._path,
   261                             fileid=node, filelog=self), data)
   270                             fileid=node, filelog=self), data)
   262             return data
   271             return data
   263 
   272 
       
   273         def add(self, text, meta, tr, link, p1=None, p2=None):
       
   274             '''Removes keyword substitutions when adding to filelog.'''
       
   275             if self.iskwcandidate(text):
       
   276                 text = re_kw.sub(r'$\1$', text)
       
   277             return super(kwfilelog, self).add(text,
       
   278                     meta, tr, link, p1=None, p2=None)
       
   279 
   264         def cmp(self, node, text):
   280         def cmp(self, node, text):
   265             '''Removes keyword substitution for comparison.'''
   281             '''Removes keyword substitutions for comparison.'''
   266             if not util.binary(text) and \
   282             if self.iskwcandidate(text):
   267                     kwfmatches(ui, self._repo, [self._path]):
       
   268                 text = re_kw.sub(r'$\1$', text)
   283                 text = re_kw.sub(r'$\1$', text)
   269             return super(kwfilelog, self).cmp(node, text)
   284             return super(kwfilelog, self).cmp(node, text)
   270 
   285 
   271     filelog.filelog = kwfilelog
   286     filelog.filelog = kwfilelog
   272     repo.__class__ = kwrepo
   287     repo.__class__ = kwrepo