hgkw/keyword.py
changeset 265 1b6b7835e3f9
parent 264 7c637046c0e2
child 266 50ce25f8c676
equal deleted inserted replaced
264:7c637046c0e2 265:1b6b7835e3f9
   265         expand = self.t is not None
   265         expand = self.t is not None
   266         action = ('shrinking', 'expanding')[expand]
   266         action = ('shrinking', 'expanding')[expand]
   267         notify = (self.ui.note, self.ui.debug)[commit]
   267         notify = (self.ui.note, self.ui.debug)[commit]
   268         overwritten = []
   268         overwritten = []
   269         for f in candidates:
   269         for f in candidates:
   270             fp = self.repo.file(f, kwexp=expand, kwcnt=True, kwmatch=True)
   270             fp = self.repo.file(f, kwexp=expand, kwmatch=True)
   271             data, kwfound = fp.read(man[f])
   271             data, kwfound = fp.kwctread(man[f])
   272             if kwfound:
   272             if kwfound:
   273                 notify(_('overwriting %s %s keywords\n') % (f, action))
   273                 notify(_('overwriting %s %s keywords\n') % (f, action))
   274                 self._wwrite(f, data, man)
   274                 self._wwrite(f, data, man)
   275                 overwritten.append(f)
   275                 overwritten.append(f)
   276         self._normal(overwritten)
   276         self._normal(overwritten)
   278 class kwfilelog(filelog.filelog):
   278 class kwfilelog(filelog.filelog):
   279     '''
   279     '''
   280     Subclass of filelog to hook into its read, add, cmp methods.
   280     Subclass of filelog to hook into its read, add, cmp methods.
   281     Keywords are "stored" unexpanded, and processed on reading.
   281     Keywords are "stored" unexpanded, and processed on reading.
   282     '''
   282     '''
   283     def __init__(self, opener, path, kwtemplater, kwcnt):
   283     def __init__(self, opener, path, kwtemplater):
   284         super(kwfilelog, self).__init__(opener, path)
   284         super(kwfilelog, self).__init__(opener, path)
   285         self.kwtemplater = kwtemplater
   285         self.kwtemplater = kwtemplater
   286         self.kwcnt = kwcnt
   286 
       
   287     def kwctread(self, node):
       
   288         '''Reads expanding and counting keywords
       
   289         (only called from kwtemplater.overwrite).'''
       
   290         data = super(kwfilelog, self).read(node)
       
   291         return self.kwtemplater.process(node, data)
   287 
   292 
   288     def read(self, node):
   293     def read(self, node):
   289         '''Passes data through kwemplater methods for
   294         '''Expands keywords when reading filelog.'''
   290         either unconditional keyword expansion
       
   291         or counting of keywords and substitution.'''
       
   292         data = super(kwfilelog, self).read(node)
   295         data = super(kwfilelog, self).read(node)
   293         if not self.kwcnt:
   296         return self.kwtemplater.expand(node, data)
   294             return self.kwtemplater.expand(node, data)
       
   295         return self.kwtemplater.process(node, data)
       
   296 
   297 
   297     def add(self, text, meta, tr, link, p1=None, p2=None):
   298     def add(self, text, meta, tr, link, p1=None, p2=None):
   298         '''Removes keyword substitutions when adding to filelog.'''
   299         '''Removes keyword substitutions when adding to filelog.'''
   299         text = self.kwtemplater.shrink(text)
   300         text = self.kwtemplater.shrink(text)
   300         return super(kwfilelog, self).add(text, meta, tr, link, p1=p1, p2=p2)
   301         return super(kwfilelog, self).add(text, meta, tr, link, p1=p1, p2=p2)
   485         return
   486         return
   486 
   487 
   487     ui.kwfmatcher = util.matcher(repo.root, inc=inc, exc=exc)[1]
   488     ui.kwfmatcher = util.matcher(repo.root, inc=inc, exc=exc)[1]
   488 
   489 
   489     class kwrepo(repo.__class__):
   490     class kwrepo(repo.__class__):
   490         def file(self, f, kwexp=True, kwcnt=False, kwmatch=False):
   491         def file(self, f, kwexp=True, kwmatch=False):
   491             if f[0] == '/':
   492             if f[0] == '/':
   492                 f = f[1:]
   493                 f = f[1:]
   493             if kwmatch or ui.kwfmatcher(f):
   494             if kwmatch or ui.kwfmatcher(f):
   494                 kwt = kwtemplater(ui, self, kwexp, path=f)
   495                 kwt = kwtemplater(ui, self, kwexp, path=f)
   495                 return kwfilelog(self.sopener, f, kwt, kwcnt)
   496                 return kwfilelog(self.sopener, f, kwt)
   496             return filelog.filelog(self.sopener, f)
   497             return filelog.filelog(self.sopener, f)
   497 
   498 
   498         def _commit(self, files, text, user, date, match, force, lock, wlock,
   499         def _commit(self, files, text, user, date, match, force, lock, wlock,
   499                     force_editor, p1, p2, extra):
   500                     force_editor, p1, p2, extra):
   500             '''Private commit wrapper for backwards compatibility.'''
   501             '''Private commit wrapper for backwards compatibility.'''