hgkw/keyword.py
changeset 222 f956a8301c25
parent 221 2990d6aa10ff
child 224 fd98a9bffcbe
equal deleted inserted replaced
221:2990d6aa10ff 222:f956a8301c25
   102 class kwtemplater(object):
   102 class kwtemplater(object):
   103     '''
   103     '''
   104     Sets up keyword templates, corresponding keyword regex, and
   104     Sets up keyword templates, corresponding keyword regex, and
   105     provides keyword substitution functions.
   105     provides keyword substitution functions.
   106     '''
   106     '''
   107     deftemplates = {
   107     templates = {
   108         'Revision': '{node|short}',
   108         'Revision': '{node|short}',
   109         'Author': '{author|user}',
   109         'Author': '{author|user}',
   110         'Date': '{date|utcdate}',
   110         'Date': '{date|utcdate}',
   111         'RCSFile': '{file|basename},v',
   111         'RCSFile': '{file|basename},v',
   112         'Source': '{root}/{file},v',
   112         'Source': '{root}/{file},v',
   119         self.repo = repo
   119         self.repo = repo
   120         self.t = expand or None
   120         self.t = expand or None
   121         self.path = path
   121         self.path = path
   122         self.node = node
   122         self.node = node
   123 
   123 
   124         templates = dict(ui.configitems('keywordmaps'))
   124         kwmaps = self.ui.configitems('keywordmaps')
   125         if templates:
   125         if kwmaps:
   126             for k in templates.keys():
   126             self.templates = {}
   127                 templates[k] = templater.parsestring(templates[k],
   127             for k, v in kwmaps:
   128                                                      quoted=False)
   128                 self.templates[k] = templater.parsestring(v, quoted=False)
   129         self.templates = templates or self.deftemplates
   129         escaped = [re.escape(k) for k in self.templates.keys()]
       
   130         rawkeyword = r'\$(%s)(: [^$\n\r]*? )??\$'
       
   131         self.re_kw = re.compile(rawkeyword % '|'.join(escaped))
       
   132 
   130         if self.t:
   133         if self.t:
   131             templater.common_filters['utcdate'] = utcdate
   134             templater.common_filters['utcdate'] = utcdate
   132             try:
   135             try:
   133                 self.t = cmdutil.changeset_templater(self.ui, self.repo,
   136                 self.t = cmdutil.changeset_templater(self.ui, self.repo,
   134                                                      False, '', False)
   137                                                      False, '', False)
   135             except TypeError:
   138             except TypeError:
   136                 # depending on hg rev changeset_templater has extra "brinfo" arg
   139                 # depending on hg rev changeset_templater has extra "brinfo" arg
   137                 self.t = cmdutil.changeset_templater(self.ui, self.repo,
   140                 self.t = cmdutil.changeset_templater(self.ui, self.repo,
   138                                                      False, None, '', False)
   141                                                      False, None, '', False)
   139         escaped = [re.escape(k) for k in self.templates.keys()]
       
   140         rawkeyword = r'\$(%s)(: [^$\n\r]*? )??\$'
       
   141         self.re_kw = re.compile(rawkeyword % '|'.join(escaped))
       
   142 
   142 
   143     def _ctxnode(self, node):
   143     def _ctxnode(self, node):
   144         '''Obtains missing node from file context.'''
   144         '''Obtains missing node from file context.'''
   145         if not self.node:
   145         if not self.node:
   146             c = context.filectx(self.repo, self.path, fileid=node)
   146             c = context.filectx(self.repo, self.path, fileid=node)
   336         kwstatus = 'custom'
   336         kwstatus = 'custom'
   337     if opts['rcfile']:
   337     if opts['rcfile']:
   338         ui.readconfig(opts['rcfile'])
   338         ui.readconfig(opts['rcfile'])
   339     if opts['default']:
   339     if opts['default']:
   340         kwstatus = 'default'
   340         kwstatus = 'default'
   341         kwmaps = kwtemplater.deftemplates
   341         kwmaps = kwtemplater.templates
   342         if ui.configitems('keywordmaps'):
   342         if ui.configitems('keywordmaps'):
   343             # override maps from optional rcfile
   343             # override maps from optional rcfile
   344             for k, v in kwmaps.items():
   344             for k, v in kwmaps.items():
   345                 ui.setconfig('keywordmaps', k, v)
   345                 ui.setconfig('keywordmaps', k, v)
   346     elif args:
   346     elif args:
   349         fp = repo.opener('hgrc', 'w')
   349         fp = repo.opener('hgrc', 'w')
   350         fp.writelines(rcmaps)
   350         fp.writelines(rcmaps)
   351         fp.close()
   351         fp.close()
   352         ui.readconfig(repo.join('hgrc'))
   352         ui.readconfig(repo.join('hgrc'))
   353     if not opts['default']:
   353     if not opts['default']:
   354         kwmaps = (dict(ui.configitems('keywordmaps')) or
   354         kwmaps = dict(ui.configitems('keywordmaps')) or kwtemplater.templates
   355                   kwtemplater.deftemplates)
       
   356     reposetup(ui, repo)
   355     reposetup(ui, repo)
   357     for k, v in ui.configitems('extensions'):
   356     for k, v in ui.configitems('extensions'):
   358         if k.endswith('keyword'):
   357         if k.endswith('keyword'):
   359             extension = '%s = %s' % (k, v)
   358             extension = '%s = %s' % (k, v)
   360             break
   359             break