hgkw/keyword.py
changeset 266 50ce25f8c676
parent 265 1b6b7835e3f9
child 268 14ebaa9eaf1d
equal deleted inserted replaced
265:1b6b7835e3f9 266:50ce25f8c676
   213             for f in files:
   213             for f in files:
   214                 self.repo.dirstate.normal(f)
   214                 self.repo.dirstate.normal(f)
   215         except AttributeError:
   215         except AttributeError:
   216             self.repo.dirstate.update(files, 'n')
   216             self.repo.dirstate.update(files, 'n')
   217 
   217 
   218     def _kwsub(self, mobj):
   218     def kwsub(self, mobj):
   219         '''Substitutes keyword using corresponding template.'''
   219         '''Substitutes keyword using corresponding template.'''
   220         kw = mobj.group(1)
   220         kw = mobj.group(1)
   221         self.t.use_template(self.templates[kw])
   221         self.t.use_template(self.templates[kw])
   222         self.ui.pushbuffer()
   222         self.ui.pushbuffer()
   223         self.t.show(changenode=self.node, root=self.repo.root, file=self.path)
   223         self.t.show(changenode=self.node, root=self.repo.root, file=self.path)
   224         keywordsub = templater.firstline(self.ui.popbuffer())
   224         keywordsub = templater.firstline(self.ui.popbuffer())
   225         return '$%s: %s $' % (kw, keywordsub)
   225         return '$%s: %s $' % (kw, keywordsub)
   226 
   226 
   227     def _substitute(self, node, data, subfunc):
   227     def substitute(self, node, data, subfunc):
   228         '''Obtains node if missing.
   228         '''Obtains node if missing.
   229         Ensures consistent templates regardless of ui.debugflag.
   229         Ensures consistent templates regardless of ui.debugflag.
   230         Calls given substitution function.'''
   230         Calls given substitution function.'''
   231         if not self.node:
   231         if not self.node:
   232             c = context.filectx(self.repo, self.path, fileid=node)
   232             c = context.filectx(self.repo, self.path, fileid=node)
   233             self.node = c.node()
   233             self.node = c.node()
   234         self.ui.debugflag = False
   234         self.ui.debugflag = False
   235         result = subfunc(self._kwsub, data)
   235         result = subfunc(self.kwsub, data)
   236         self.ui.debugflag = self.debug
   236         self.ui.debugflag = self.debug
   237         return result
   237         return result
   238 
   238 
   239     def expand(self, node, data):
   239     def expand(self, node, data):
   240         '''Returns data with keywords expanded.'''
   240         '''Returns data with keywords expanded.'''
   241         if util.binary(data):
   241         if util.binary(data):
   242             return data
   242             return data
   243         return self._substitute(node, data, self.re_kw.sub)
   243         return self.substitute(node, data, self.re_kw.sub)
   244 
   244 
   245     def process(self, node, data):
   245     def process(self, node, data):
   246         '''Returns a tuple: data, count.
   246         '''Returns a tuple: data, count.
   247         Count is number of keywords/keyword substitutions.
   247         Count is number of keywords/keyword substitutions.
   248         Keywords in data are expanded, if templater was initialized.'''
   248         Keywords in data are expanded, if templater was initialized.'''
   249         if util.binary(data):
   249         if util.binary(data):
   250             return data, None
   250             return data, None
   251         if self.t:
   251         if self.t:
   252             return self._substitute(node, data, self.re_kw.subn)
   252             return self.substitute(node, data, self.re_kw.subn)
   253         return data, self.re_kw.search(data)
   253         return data, self.re_kw.search(data)
   254 
   254 
   255     def shrink(self, text):
   255     def shrink(self, text):
   256         '''Returns text with all keyword substitutions removed.'''
   256         '''Returns text with all keyword substitutions removed.'''
   257         if util.binary(text):
   257         if util.binary(text):
   391     Extend current configuration by specifying maps as arguments
   391     Extend current configuration by specifying maps as arguments
   392     and optionally by reading from an additional hgrc file.
   392     and optionally by reading from an additional hgrc file.
   393 
   393 
   394     Override current keyword template maps with "default" option.
   394     Override current keyword template maps with "default" option.
   395     '''
   395     '''
   396     def _demostatus(stat):
   396     def demostatus(stat):
   397         ui.status(_('\n\t%s\n') % stat)
   397         ui.status(_('\n\t%s\n') % stat)
   398 
   398 
   399     def _showitems(section, items):
   399     def demoitems(section, items):
   400         ui.write('[%s]\n' % section)
   400         ui.write('[%s]\n' % section)
   401         for k, v in items:
   401         for k, v in items:
   402             ui.write('%s = %s\n' % (k, v))
   402             ui.write('%s = %s\n' % (k, v))
   403 
   403 
   404     msg = 'hg keyword config and expansion example'
   404     msg = 'hg keyword config and expansion example'
   432     reposetup(ui, repo)
   432     reposetup(ui, repo)
   433     for k, v in ui.configitems('extensions'):
   433     for k, v in ui.configitems('extensions'):
   434         if k.endswith('keyword'):
   434         if k.endswith('keyword'):
   435             extension = '%s = %s' % (k, v)
   435             extension = '%s = %s' % (k, v)
   436             break
   436             break
   437     _demostatus('config using %s keyword template maps' % kwstatus)
   437     demostatus('config using %s keyword template maps' % kwstatus)
   438     ui.write('[extensions]\n%s\n' % extension)
   438     ui.write('[extensions]\n%s\n' % extension)
   439     _showitems('keyword', ui.configitems('keyword'))
   439     demoitems('keyword', ui.configitems('keyword'))
   440     _showitems('keywordmaps', kwmaps.items())
   440     demoitems('keywordmaps', kwmaps.items())
   441     keywords = '$' + '$\n$'.join(kwmaps.keys()) + '$\n'
   441     keywords = '$' + '$\n$'.join(kwmaps.keys()) + '$\n'
   442     repo.wopener(fn, 'w').write(keywords)
   442     repo.wopener(fn, 'w').write(keywords)
   443     repo.add([fn])
   443     repo.add([fn])
   444     path = repo.wjoin(fn)
   444     path = repo.wjoin(fn)
   445     ui.note(_('\n%s keywords written to %s:\n') % (kwstatus, path))
   445     ui.note(_('\n%s keywords written to %s:\n') % (kwstatus, path))
   452     commands.branch(ui, repo, branchname)
   452     commands.branch(ui, repo, branchname)
   453     ui.quiet = quiet
   453     ui.quiet = quiet
   454     ui.note('hg -R "%s" ci -m "%s"\n' % (tmpdir, msg))
   454     ui.note('hg -R "%s" ci -m "%s"\n' % (tmpdir, msg))
   455     repo.commit(text=msg)
   455     repo.commit(text=msg)
   456     pathinfo = ('', ' in %s' % path)[ui.verbose]
   456     pathinfo = ('', ' in %s' % path)[ui.verbose]
   457     _demostatus('%s keywords expanded%s' % (kwstatus, pathinfo))
   457     demostatus('%s keywords expanded%s' % (kwstatus, pathinfo))
   458     ui.write(repo.wread(fn))
   458     ui.write(repo.wread(fn))
   459     ui.debug(_('\nremoving temporary repo %s\n') % tmpdir)
   459     ui.debug(_('\nremoving temporary repo %s\n') % tmpdir)
   460     shutil.rmtree(tmpdir, ignore_errors=True)
   460     shutil.rmtree(tmpdir, ignore_errors=True)
   461 
   461 
   462 
   462