--- a/hgkw/keyword.py Wed Jan 03 14:14:19 2007 +0100
+++ b/hgkw/keyword.py Thu Jan 04 13:13:54 2007 +0100
@@ -48,6 +48,7 @@
pretxncommit.keyword = python:hgext.keyword.pretxnkw
'''
+from mercurial.i18n import _
from mercurial import context, util
import os.path, re
@@ -78,6 +79,12 @@
return '$%s: %s $' % (matchobj.group(1), eval(matchobj.group(1)))
+def kwfmatchers(ui, repo):
+ '''Returns filename matchers from ui keyword section.'''
+ return [util.matcher(repo.root, '', [pat], [], [])[1]
+ for pat, opt in ui.configitems('keyword')
+ if opt == 'expand']
+
def reposetup(ui, repo):
from mercurial import filelog, revlog
@@ -101,19 +108,19 @@
def read(self, node):
data = super(kwfilelog, self).read(node)
if not self._path.startswith('.hg') and not util.binary(data):
- for pat, opt in self._repo.ui.configitems('keyword'):
- if opt == 'expand':
- mf = util.matcher(self._repo.root,
- '', [pat], [], [])[1]
- if mf(self._path):
- return re_kw.sub(lambda m:
- kwexpand(m, self._repo, self._path,
- fileid=node, filelog=self),
- data)
+ for mf in kwfmatchers(ui, self._repo):
+ if mf(self._path):
+ ui.debug(_('expanding keywords in %s\n' % self._path))
+ return re_kw.sub(lambda m:
+ kwexpand(m, self._repo, self._path,
+ fileid=node, filelog=self),
+ data)
return data
def add(self, text, meta, tr, link, p1=None, p2=None):
if not util.binary(text):
+ ui.debug(_('removing keyword substitutions in %s\n')
+ % self._path)
text = re_kw.sub(r'$\1$', text)
return super(kwfilelog, self).add(text, meta, tr, link, p1, p2)
@@ -137,7 +144,6 @@
def pretxnkw(ui, repo, hooktype, **args):
'''pretxncommit hook that collects candidates for keyword expansion
on commit and expands keywords in working dir.'''
- from mercurial.i18n import _
from mercurial import cmdutil, commands
import sys
@@ -154,19 +160,16 @@
if not candidates:
return False
- files = []
- for pat, opt in repo.ui.configitems('keyword'):
- if opt == 'expand':
- mf = util.matcher(repo.root, '', [pat], [], [])[1]
- for candidate in candidates:
- if mf(candidate) and candidate not in files:
- files.append(candidate)
- for f in files:
- data = repo.wfile(f).read()
- if not util.binary(data):
- data, kwct = re_kw.subn(lambda m:
- kwexpand(m, repo, f, changeid=args['node']),
- data)
- if kwct:
- ui.note(_('expanding keywords in %s\n' % f))
- repo.wfile(f, 'w').write(data)
+ fmatchers = kwfmatchers(ui, repo)
+ for f in candidates:
+ for mf in fmatchers:
+ if mf(f):
+ data = repo.wfile(f).read()
+ if not util.binary(data):
+ data, kwct = re_kw.subn(lambda m:
+ kwexpand(m, repo, f, changeid=args['node']),
+ data)
+ if kwct:
+ ui.debug(_('overwriting %s expanding keywords\n' % f))
+ repo.wfile(f, 'w').write(data)
+ break