diff -r fe37939db543 -r 15e8cd7f5295 hgkw/keyword.py --- a/hgkw/keyword.py Thu Feb 08 14:28:00 2007 +0100 +++ b/hgkw/keyword.py Thu Feb 08 14:40:20 2007 +0100 @@ -73,10 +73,9 @@ from mercurial.i18n import gettext as _ # above line for backwards compatibility of standalone version -from mercurial import commands, cmdutil, templater, util +from mercurial import cmdutil, templater, util from mercurial import context, filelog, revlog -from mercurial.node import bin -import os.path, re, sys, time +import os.path, re, time deftemplates = { 'Revision': '{node|short}', @@ -92,18 +91,6 @@ '''Returns hgdate in cvs-like UTC format.''' return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0])) -def getmodulename(): - '''Makes sure pretxncommit-hook can import keyword module - regardless of where its located.''' - for k, v in sys.modules.iteritems(): - if v is None or not hasattr(v, '__file__'): - continue - if v.__file__.startswith(__file__): - return k - else: - sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) - return os.path.splitext(os.path.basename(__file__))[0] - class kwtemplater(object): ''' Sets up keyword templates, corresponding keyword regex, and @@ -153,6 +140,39 @@ f = f[1:] return filelog.filelog(self.sopener, f, self, self.revlogversion) + def commit(self, files=None, text="", user=None, date=None, + match=util.always, force=False, lock=None, wlock=None, + force_editor=False, p1=None, p2=None, extra={}): + '''Wraps commit, expanding keywords of committed and + configured files in working directory.''' + + node = super(kwrepo, self).commit(files=files, + text=text, user=user, date=date, + match=match, force=force, lock=lock, wlock=wlock, + force_editor=force_editor, p1=p1, p2=p2, extra=extra) + if node is None: + return node + + candidates = self.changelog.read(node)[3] + candidates = [f for f in candidates + if self.kwfmatcher(f) and os.path.isfile(self.wjoin(f))] + if not candidates: + return node + + kwt = kwtemplater(self.ui, self) + overwrite = [] + for f in candidates: + data = self.wfile(f).read() + if not util.binary(data): + data, kwct = kwt.re_kw.subn(lambda m: + kwt.expand(m, f, node), data) + if kwct: + ui.debug(_('overwriting %s expanding keywords\n' % f)) + self.wfile(f, 'w').write(data) + overwrite.append(f) + self.dirstate.update(overwrite, 'n') + return node + class kwfilelog(filelog.filelog): ''' Superclass over filelog to customize it's read, add, cmp methods. @@ -198,31 +218,3 @@ filelog.filelog = kwfilelog repo.__class__ = kwrepo - # configure pretxncommit hook - repo.ui.setconfig('hooks', 'pretxncommit.keyword', - 'python:%s.pretxnkw' % getmodulename()) - - -def pretxnkw(ui, repo, hooktype, **args): - '''pretxncommit hook that collects candidates for keyword expansion - on commit and expands keywords in working dir.''' - - cmd, sysargs, globalopts, cmdopts = commands.parse(ui, sys.argv[1:])[1:] - if repr(cmd).split()[1] in ('tag', 'import_'): - return - - files, match, anypats = cmdutil.matchpats(repo, sysargs, cmdopts) - modified, added = repo.status(files=files, match=match)[:2] - candidates = [f for f in modified + added if repo.kwfmatcher(f)] - if not candidates: - return - - kwt = kwtemplater(ui, repo) - node = bin(args['node']) - for f in candidates: - data = repo.wfile(f).read() - if not util.binary(data): - data, kwct = kwt.re_kw.subn(lambda m: kwt.expand(m, f, node), data) - if kwct: - ui.debug(_('overwriting %s expanding keywords\n' % f)) - repo.wfile(f, 'w').write(data)