hgkw/keyword.py
branchkwmap-templates
changeset 138 bf8622c50309
parent 137 bfea48fbcb88
child 139 0ea07bb56ff6
equal deleted inserted replaced
137:bfea48fbcb88 138:bf8622c50309
   136         '''Returns data with expanded keywords.'''
   136         '''Returns data with expanded keywords.'''
   137         if util.binary(data):
   137         if util.binary(data):
   138             return data
   138             return data
   139         return self.re_kw.sub(lambda m: self.kwsub(m, path, node), data)
   139         return self.re_kw.sub(lambda m: self.kwsub(m, path, node), data)
   140 
   140 
   141     def expandn(self, path, node, data):
       
   142         '''Returns data with expanded keywords and number of expansions.'''
       
   143         if util.binary(data):
       
   144             return data, None
       
   145         return self.re_kw.subn(lambda m: self.kwsub(m, path, node), data)
       
   146 
       
   147     def shrink(self, text):
   141     def shrink(self, text):
   148         '''Returns text with all keyword substitutions removed.'''
   142         '''Returns text with all keyword substitutions removed.'''
   149         if util.binary(text):
   143         if util.binary(text):
   150             return text
   144             return text
   151         return self.re_kw.sub(r'$\1$', text)
   145         return self.re_kw.sub(r'$\1$', text)
       
   146 
       
   147     def overwrite(self, candidates, node):
       
   148         '''Overwrites candidates in working dir expanding keywords.'''
       
   149         files = []
       
   150         for f in candidates:
       
   151             data = self.repo.wfile(f).read()
       
   152             if not util.binary(data):
       
   153                 data, kwct = self.re_kw.subn(lambda m:
       
   154                         self.kwsub(m, f, node), data)
       
   155                 if kwct:
       
   156                     self.ui.debug(_('overwriting %s expanding keywords\n') % f)
       
   157                     self.repo.wfile(f, 'w').write(data)
       
   158                     files.append(f)
       
   159         if files:
       
   160             self.repo.dirstate.update(files, 'n')
   152 
   161 
   153 
   162 
   154 def reposetup(ui, repo):
   163 def reposetup(ui, repo):
   155     '''Sets up repo, and filelog especially, as kwrepo and kwfilelog
   164     '''Sets up repo, and filelog especially, as kwrepo and kwfilelog
   156     for keyword substitution. This is done for local repos only.'''
   165     for keyword substitution. This is done for local repos only.'''
   183 
   192 
   184             node = super(kwrepo, self).commit(files=files,
   193             node = super(kwrepo, self).commit(files=files,
   185                     text=text, user=user, date=date,
   194                     text=text, user=user, date=date,
   186                     match=match, force=force, lock=lock, wlock=wlock,
   195                     match=match, force=force, lock=lock, wlock=wlock,
   187                     force_editor=force_editor, p1=p1, p2=p2, extra=extra)
   196                     force_editor=force_editor, p1=p1, p2=p2, extra=extra)
   188 
       
   189             if node is None:
   197             if node is None:
   190                 return node
   198                 return node
   191 
   199 
   192             candidates = self.changelog.read(node)[3]
   200             candidates = self.changelog.read(node)[3]
   193             candidates = [f for f in candidates
   201             candidates = [f for f in candidates
   194                     if self.kwfmatcher(f) and os.path.isfile(self.wjoin(f))]
   202                     if self.kwfmatcher(f) and os.path.isfile(self.wjoin(f))]
   195             if not candidates:
   203             if not candidates:
   196                 return node
   204                 return node
   197 
   205 
   198             kwt = kwtemplater(self.ui, self)
   206             kwt = kwtemplater(self.ui, self)
   199             overwrite = []
   207             kwt.overwrite(candidates, node)
   200             for f in candidates:
       
   201                 data = self.wfile(f).read()
       
   202                 data, kwct = kwt.expandn(f, node, data)
       
   203                 if kwct:
       
   204                     ui.debug(_('overwriting %s expanding keywords\n' % f))
       
   205                     self.wfile(f, 'w').write(data)
       
   206                     overwrite.append(f)
       
   207             if overwrite:
       
   208                 self.dirstate.update(overwrite, 'n')
       
   209             return node
   208             return node
   210 
   209 
   211     class kwfilelog(filelog.filelog):
   210     class kwfilelog(filelog.filelog):
   212         '''
   211         '''
   213         Superclass over filelog to customize it's read, add, cmp methods.
   212         Superclass over filelog to customize it's read, add, cmp methods.