hgkw/keyword.py
branchkwmap-templates
changeset 101 98a65d978098
parent 100 4f1129ddbdb8
child 102 393d6f8ea83c
equal deleted inserted replaced
100:4f1129ddbdb8 101:98a65d978098
    82 class kwtemplater(object):
    82 class kwtemplater(object):
    83     '''
    83     '''
    84     Sets up keyword templates, corresponding keyword regex, and
    84     Sets up keyword templates, corresponding keyword regex, and
    85     provides keyword expansion function.
    85     provides keyword expansion function.
    86     '''
    86     '''
    87     def __init__(self, ui, repo, node=None):
    87     def __init__(self, ui, repo):
    88         self.ui = ui
    88         self.ui = ui
    89         self.repo = repo
    89         self.repo = repo
    90         self.node = node 
       
    91         templates = {}
    90         templates = {}
    92         for k, v in self.ui.configitems('keywordmaps'):
    91         for k, v in self.ui.configitems('keywordmaps'):
    93             templates[k] = v
    92             templates[k] = v
    94         self.templates = templates or deftemplates
    93         self.templates = templates or deftemplates
    95         self.re_kw = re.compile(r'\$(%s)[^$]*?\$' %
    94         self.re_kw = re.compile(r'\$(%s)[^$]*?\$' %
    96                 '|'.join(re.escape(k) for k in self.templates.keys()))
    95                 '|'.join(re.escape(k) for k in self.templates.keys()))
    97         self.t = cmdutil.changeset_templater(self.ui, self.repo,
    96         self.t = cmdutil.changeset_templater(self.ui, self.repo,
    98                 False, '', False)
    97                 False, '', False)
    99 
    98 
   100     def expand(self, mobj, path, node=None):
    99     def expand(self, mobj, path, node):
   101         self.node = self.node or node
       
   102         kw = mobj.group(1)
   100         kw = mobj.group(1)
   103         template = templater.parsestring(self.templates[kw], quoted=False)
   101         template = templater.parsestring(self.templates[kw], quoted=False)
   104         self.t.use_template(template)
   102         self.t.use_template(template)
   105         self.ui.pushbuffer()
   103         self.ui.pushbuffer()
   106         self.t.show(changenode=self.node,
   104         self.t.show(changenode=node, root=self.repo.root, file=path)
   107                 changes=self.repo.changelog.read(self.node),
       
   108                 root=self.repo.root, file=path)
       
   109         return '$%s: %s $' % (kw, self.ui.popbuffer())
   105         return '$%s: %s $' % (kw, self.ui.popbuffer())
   110 
   106 
   111 
   107 
   112 def reposetup(ui, repo):
   108 def reposetup(ui, repo):
   113     from mercurial import context, filelog, revlog
   109     from mercurial import context, filelog, revlog
   142             data = super(kwfilelog, self).read(node)
   138             data = super(kwfilelog, self).read(node)
   143             if self.iskwcandidate(data):
   139             if self.iskwcandidate(data):
   144                 c = context.filectx(self._repo, self._path,
   140                 c = context.filectx(self._repo, self._path,
   145                                     fileid=node, filelog=self)
   141                                     fileid=node, filelog=self)
   146                 return self.kwt.re_kw.sub(lambda m:
   142                 return self.kwt.re_kw.sub(lambda m:
   147                         self.kwt.expand(m, self._path, node=c.node()), data)
   143                         self.kwt.expand(m, self._path, c.node()), data)
   148             return data
   144             return data
   149 
   145 
   150         def add(self, text, meta, tr, link, p1=None, p2=None):
   146         def add(self, text, meta, tr, link, p1=None, p2=None):
   151             '''Removes keyword substitutions when adding to filelog.'''
   147             '''Removes keyword substitutions when adding to filelog.'''
   152             if self.iskwcandidate(text):
   148             if self.iskwcandidate(text):
   195     modified, added = repo.status(files=files, match=match)[:2]
   191     modified, added = repo.status(files=files, match=match)[:2]
   196     candidates = kwfmatches(ui, repo, modified+added)
   192     candidates = kwfmatches(ui, repo, modified+added)
   197     if not candidates:
   193     if not candidates:
   198         return
   194         return
   199 
   195 
   200     kwt = kwtemplater(ui, repo, node=bin(args['node']))
   196     kwt = kwtemplater(ui, repo)
       
   197     node = bin(args['node'])
   201     for f in candidates:
   198     for f in candidates:
   202         data = repo.wfile(f).read()
   199         data = repo.wfile(f).read()
   203         if not util.binary(data):
   200         if not util.binary(data):
   204             data, kwct = kwt.re_kw.subn(lambda m: kwt.expand(m, f), data)
   201             data, kwct = kwt.re_kw.subn(lambda m: kwt.expand(m, f, node), data)
   205             if kwct:
   202             if kwct:
   206                 ui.debug(_('overwriting %s expanding keywords\n' % f))
   203                 ui.debug(_('overwriting %s expanding keywords\n' % f))
   207                 repo.wfile(f, 'w').write(data)
   204                 repo.wfile(f, 'w').write(data)