hgkw/keyword.py
branchkwmap-templates
changeset 144 19b3d1de3db0
parent 143 3485b0ef99c4
child 145 1c2cefa97b96
equal deleted inserted replaced
143:3485b0ef99c4 144:19b3d1de3db0
   159                     self.repo.wfile(f, 'w').write(data)
   159                     self.repo.wfile(f, 'w').write(data)
   160                     files.append(f)
   160                     files.append(f)
   161         if files:
   161         if files:
   162             self.repo.dirstate.update(files, 'n')
   162             self.repo.dirstate.update(files, 'n')
   163 
   163 
       
   164 class kwfilelog(filelog.filelog):
       
   165     '''
       
   166     Superclass over filelog to customize its read, add, cmp methods.
       
   167     Keywords are "stored" unexpanded, and expanded on reading.
       
   168     '''
       
   169     def __init__(self, opener, path, kwtemplater,
       
   170                  defversion=revlog.REVLOG_DEFAULT_VERSION):
       
   171         super(kwfilelog, self).__init__(opener, path, defversion)
       
   172         self.path = path
       
   173         self.kwtemplater = kwtemplater
       
   174 
       
   175     def read(self, node):
       
   176         '''Substitutes keywords when reading filelog.'''
       
   177         data = super(kwfilelog, self).read(node)
       
   178         return self.kwtemplater.expand(self.path, node, self, data)
       
   179 
       
   180     def add(self, text, meta, tr, link, p1=None, p2=None):
       
   181         '''Removes keyword substitutions when adding to filelog.'''
       
   182         text = self.kwtemplater.shrink(text)
       
   183         return super(kwfilelog, self).add(text,
       
   184                         meta, tr, link, p1=p1, p2=p2)
       
   185 
       
   186     def cmp(self, node, text):
       
   187         '''Removes keyword substitutions for comparison.'''
       
   188         text = self.kwtemplater.shrink(text)
       
   189         return super(kwfilelog, self).cmp(node, text)
       
   190 
   164 
   191 
   165 def reposetup(ui, repo):
   192 def reposetup(ui, repo):
   166     '''Sets up repo, and filelog especially, as kwrepo and kwfilelog
   193     '''Sets up repo as kwrepo for keyword substitution.
   167     for keyword substitution. This is done for local repos only.'''
   194     Overrides file method to return kwfilelog instead of filelog
       
   195     if file matches user configuration.
       
   196     Wraps commit to overwrite configured files with updated
       
   197     keyword substitutions.
       
   198     This is done for local repos only, and only if there are
       
   199     files configured at all for keyword substitution.'''
   168 
   200 
   169     if not repo.local():
   201     if not repo.local():
   170         return
   202         return
   171 
   203 
   172     inc, exc = [], ['.hg*']
   204     inc, exc = [], ['.hg*']
   182 
   214 
   183     class kwrepo(repo.__class__):
   215     class kwrepo(repo.__class__):
   184         def file(self, f):
   216         def file(self, f):
   185             if f[0] == '/':
   217             if f[0] == '/':
   186                 f = f[1:]
   218                 f = f[1:]
   187             return filelog.filelog(self.sopener, f, self, self.revlogversion)
   219             # only use kwfilelog when needed
       
   220             if self.kwfmatcher(f):
       
   221                 kwt = kwtemplater(self.ui, self)
       
   222                 return kwfilelog(self.sopener, f, kwt, self.revlogversion)
       
   223             else:
       
   224                 return filelog.filelog(self.sopener, f, self.revlogversion)
   188 
   225 
   189         def commit(self, files=None, text="", user=None, date=None,
   226         def commit(self, files=None, text="", user=None, date=None,
   190                 match=util.always, force=False, lock=None, wlock=None,
   227                 match=util.always, force=False, lock=None, wlock=None,
   191                 force_editor=False, p1=None, p2=None, extra={}):
   228                 force_editor=False, p1=None, p2=None, extra={}):
   192             '''Wraps commit, expanding keywords of committed and
   229             '''Wraps commit, expanding keywords of committed and
   217             finally:
   254             finally:
   218                 if wrelease:
   255                 if wrelease:
   219                     wlock.release()
   256                     wlock.release()
   220             return node
   257             return node
   221 
   258 
   222     class kwfilelog(filelog.filelog):
       
   223         '''
       
   224         Superclass over filelog to customize it's read, add, cmp methods.
       
   225         Keywords are "stored" unexpanded, and expanded on reading.
       
   226         '''
       
   227         def __init__(self, opener, path, repo=None,
       
   228                      defversion=revlog.REVLOG_DEFAULT_VERSION):
       
   229             super(kwfilelog, self).__init__(opener, path, defversion)
       
   230             self._repo = repo
       
   231             self._path = path
       
   232             # only init kwtemplater if needed
       
   233             if hasattr(repo, 'kwfmatcher') and repo.kwfmatcher(path):
       
   234                 self.kwt = kwtemplater(repo.ui, repo)
       
   235             else:
       
   236                 self.kwt = None
       
   237 
       
   238         def read(self, node):
       
   239             '''Substitutes keywords when reading filelog.'''
       
   240             data = super(kwfilelog, self).read(node)
       
   241             if self.kwt:
       
   242                 data = self.kwt.expand(self._path, node, self, data)
       
   243             return data
       
   244 
       
   245         def add(self, text, meta, tr, link, p1=None, p2=None):
       
   246             '''Removes keyword substitutions when adding to filelog.'''
       
   247             if self.kwt:
       
   248                 text = self.kwt.shrink(text)
       
   249             return super(kwfilelog, self).add(text,
       
   250                             meta, tr, link, p1=p1, p2=p2)
       
   251 
       
   252         def cmp(self, node, text):
       
   253             '''Removes keyword substitutions for comparison.'''
       
   254             if self.kwt:
       
   255                 text = self.kwt.shrink(text)
       
   256             return super(kwfilelog, self).cmp(node, text)
       
   257 
       
   258     filelog.filelog = kwfilelog
       
   259     repo.__class__ = kwrepo
   259     repo.__class__ = kwrepo