hgkw/keyword.py
branchsolo-extension
changeset 65 188849659487
parent 64 4cd7b993c5f8
child 66 8a8d30596640
equal deleted inserted replaced
64:4cd7b993c5f8 65:188849659487
    46     [hooks]
    46     [hooks]
    47     pretxncommit =
    47     pretxncommit =
    48     pretxncommit.keyword = python:hgext.keyword.pretxnkw
    48     pretxncommit.keyword = python:hgext.keyword.pretxnkw
    49 '''
    49 '''
    50 
    50 
       
    51 from mercurial.i18n import _
    51 from mercurial import context, util
    52 from mercurial import context, util
    52 import os.path, re
    53 import os.path, re
    53 
    54 
    54 
    55 
    55 re_kw = re.compile(
    56 re_kw = re.compile(
    76     Header = '%s %s' % (Source, revdateauth)
    77     Header = '%s %s' % (Source, revdateauth)
    77     Id = '%s %s' % (RCSFile, revdateauth)
    78     Id = '%s %s' % (RCSFile, revdateauth)
    78 
    79 
    79     return '$%s: %s $' % (matchobj.group(1), eval(matchobj.group(1)))
    80     return '$%s: %s $' % (matchobj.group(1), eval(matchobj.group(1)))
    80 
    81 
       
    82 def kwfmatchers(ui, repo):
       
    83     '''Returns filename matchers from ui keyword section.'''
       
    84     return [util.matcher(repo.root, '', [pat], [], [])[1]
       
    85             for pat, opt in ui.configitems('keyword')
       
    86             if opt == 'expand']
       
    87 
    81 
    88 
    82 def reposetup(ui, repo):
    89 def reposetup(ui, repo):
    83     from mercurial import filelog, revlog
    90     from mercurial import filelog, revlog
    84 
    91 
    85     if not repo.local():
    92     if not repo.local():
    99             self._path = path
   106             self._path = path
   100 
   107 
   101         def read(self, node):
   108         def read(self, node):
   102             data = super(kwfilelog, self).read(node)
   109             data = super(kwfilelog, self).read(node)
   103             if not self._path.startswith('.hg') and not util.binary(data):
   110             if not self._path.startswith('.hg') and not util.binary(data):
   104                 for pat, opt in self._repo.ui.configitems('keyword'):
   111                 for mf in kwfmatchers(ui, self._repo):
   105                     if opt == 'expand':
   112                     if mf(self._path):
   106                         mf = util.matcher(self._repo.root,
   113                         ui.debug(_('expanding keywords in %s\n' % self._path))
   107                                 '', [pat], [], [])[1]
   114                         return re_kw.sub(lambda m:
   108                         if mf(self._path):
   115                                 kwexpand(m, self._repo, self._path,
   109                             return re_kw.sub(lambda m:
   116                                     fileid=node, filelog=self),
   110                                     kwexpand(m, self._repo, self._path,
   117                                 data)
   111                                         fileid=node, filelog=self),
       
   112                                     data)
       
   113             return data
   118             return data
   114 
   119 
   115         def add(self, text, meta, tr, link, p1=None, p2=None):
   120         def add(self, text, meta, tr, link, p1=None, p2=None):
   116             if not util.binary(text):
   121             if not util.binary(text):
       
   122                 ui.debug(_('removing keyword substitutions in %s\n')
       
   123                         % self._path)
   117                 text = re_kw.sub(r'$\1$', text)
   124                 text = re_kw.sub(r'$\1$', text)
   118             return super(kwfilelog, self).add(text, meta, tr, link, p1, p2)
   125             return super(kwfilelog, self).add(text, meta, tr, link, p1, p2)
   119 
   126 
   120         def size(self, rev):
   127         def size(self, rev):
   121             '''Overrides filelog's size() to use kwfilelog.read().'''
   128             '''Overrides filelog's size() to use kwfilelog.read().'''
   135 
   142 
   136 
   143 
   137 def pretxnkw(ui, repo, hooktype, **args):
   144 def pretxnkw(ui, repo, hooktype, **args):
   138     '''pretxncommit hook that collects candidates for keyword expansion
   145     '''pretxncommit hook that collects candidates for keyword expansion
   139     on commit and expands keywords in working dir.'''
   146     on commit and expands keywords in working dir.'''
   140     from mercurial.i18n import _
       
   141     from mercurial import cmdutil, commands
   147     from mercurial import cmdutil, commands
   142     import sys
   148     import sys
   143 
   149 
   144     if hooktype != 'pretxncommit':
   150     if hooktype != 'pretxncommit':
   145         return True
   151         return True
   152     modified, added = repo.status(files=files, match=match)[:2]
   158     modified, added = repo.status(files=files, match=match)[:2]
   153     candidates = [f for f in modified + added if not f.startswith('.hg')]
   159     candidates = [f for f in modified + added if not f.startswith('.hg')]
   154     if not candidates:
   160     if not candidates:
   155         return False
   161         return False
   156 
   162 
   157     files = []
   163     fmatchers = kwfmatchers(ui, repo)
   158     for pat, opt in repo.ui.configitems('keyword'):
   164     for f in candidates:
   159         if opt == 'expand':
   165         for mf in fmatchers:
   160             mf = util.matcher(repo.root, '', [pat], [], [])[1]
   166             if mf(f):
   161             for candidate in candidates:
   167                 data = repo.wfile(f).read()
   162                 if mf(candidate) and candidate not in files:
   168                 if not util.binary(data):
   163                     files.append(candidate)
   169                     data, kwct = re_kw.subn(lambda m:
   164     for f in files:
   170                             kwexpand(m, repo, f, changeid=args['node']),
   165         data = repo.wfile(f).read()
   171                             data)
   166         if not util.binary(data):
   172                     if kwct:
   167             data, kwct = re_kw.subn(lambda m:
   173                         ui.debug(_('overwriting %s expanding keywords\n' % f))
   168                     kwexpand(m, repo, f, changeid=args['node']),
   174                         repo.wfile(f, 'w').write(data)
   169                     data)
   175                 break
   170             if kwct:
       
   171                 ui.note(_('expanding keywords in %s\n' % f))
       
   172                 repo.wfile(f, 'w').write(data)