# HG changeset patch # User Christian Ebert # Date 1166716708 -3600 # Node ID 0617e7d497f6f205d903d919254d6cd8321dad37 # Parent 5acf520f2115e0754aeedc3409a58602fd92f7bb Branch standalone extension, including pretxncommit hook function Slower, but easier to integrate. diff -r 5acf520f2115 -r 0617e7d497f6 hgkw/keyword.py --- a/hgkw/keyword.py Thu Dec 21 11:02:21 2006 +0100 +++ b/hgkw/keyword.py Thu Dec 21 16:58:28 2006 +0100 @@ -1,7 +1,26 @@ -from mercurial import hg, filelog, revlog, context, util -import os.path, re +from mercurial.i18n import _ +from mercurial import commands, cmdutil, context, filelog, revlog, util +import os.path, re, sys + +# supported keywords for use in regexes +hgkeywords = 'Id|Header|Author|Date|Revision|RCSFile|Source' + +def kwexpand(matchobj, repo, Revision, f, date, Author): + '''Called by kwfilelog.read and pretxnkw. + Sets supported keywords as local variables and evaluates them to + their expansion if matchobj is equal to string representation.''' -hgkeywords = 'Id|Header|Author|Date|Revision|RCSFile|Source' + RCSFile = os.path.basename(f)+',v' + Source = os.path.join(repo.root, f)+',v' + Date = util.datestr(date=date) + revdateauth = '%s %s %s' % (Revision, # %Y-%m-%d %H:%M:%S + util.datestr(date=date, format=util.defaultdateformats[0]), + util.shortuser(Author)) + Header = '%s %s' % (Source, revdateauth) + Id = '%s %s' % (RCSFile, revdateauth) + + return '$%s: %s $' % (matchobj.group(1), eval(matchobj.group(1))) + def reposetup(ui, repo): if not repo.local(): @@ -36,25 +55,14 @@ mf = util.matcher(self._repo.root, '', [pat], [], [])[1] if mf(f): - re_kw = re.compile(r'\$(%s)\$' % hgkeywords) - def kwexpand(matchobj): - RCSFile = os.path.basename(f)+',v' - Source = os.path.join(self._repo.root, f)+',v' - Revision = c.changectx() - Date = util.datestr(date=c.date()) - Author = c.user() - revdateauth = '%s %s %s' % ( - Revision, - util.datestr(date=c.date(), - format=util.defaultdateformats[0]), - util.shortuser(Author)) - Header = '%s %s' % (Source, revdateauth) - Id = '%s %s' % (RCSFile, revdateauth) - return '$%s: %s $' % ( - matchobj.group(1), eval(matchobj.group(1))) + def kwexpander(matchobj): + return kwexpand(matchobj, + self._repo, c.changectx(), f, + c.date(), c.user()) - return re_kw.sub(kwexpand, data) + re_kw = re.compile(r'\$(%s)\$' % hgkeywords) + return re_kw.sub(kwexpander, data) return data def add(self, text, meta, tr, link, p1=None, p2=None): @@ -66,3 +74,58 @@ filelog.filelog = kwfilelog repo.__class__ = kwrepo + + +def pretxnkw(ui, repo, hooktype, **args): + '''pretxncommit hook that collects candidates for keyword expansion + on commit and expands keywords in working dir.''' + + if hooktype != 'pretxncommit': # bail out with error + return True + + # reparse args, opts again as pretxncommit hook is silent about them + cmd, sysargs, globalopts, cmdopts = commands.parse(ui, sys.argv[1:])[1:] + # exclude tag and import + if repr(cmd).split()[1] in ('tag', 'import_'): + return False + + files, match, anypats = cmdutil.matchpats(repo, sysargs, cmdopts) + # validity checks should have been done already + modified, added = repo.status(files=files, match=match)[:2] + candidates = [f for f in modified + added if not f.startswith('.hg')] + + if not candidates: + return False + + # only check files that are configured in keyword section + files = [] + # python2.4: files = set() + 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) + # python2.4: + # if mf(candidate): files.add(candidate) + + if not files: + return False + + user, date = repo.changelog.read(repo.changelog.tip())[1:3] + # expand both expanded and unexpanded keywords + re_kw = re.compile(r'\$(%s)(: [^$]+? )?\$' % hgkeywords) + + for f in files: + data = repo.wfile(f).read() + if not util.binary(data): + + def kwexpander(matchobj): + return kwexpand(matchobj, + repo, args['node'][:12], f, date, user) + + data, kwct = re_kw.subn(kwexpander, data) + if kwct: + ui.note(_('expanding keywords in %s\n' % f)) + # backup file? + repo.wfile(f, 'w').write(data) diff -r 5acf520f2115 -r 0617e7d497f6 hgkw/pretxnkw.py --- a/hgkw/pretxnkw.py Thu Dec 21 11:02:21 2006 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,73 +0,0 @@ -from mercurial.i18n import _ -from mercurial import cmdutil, commands, util -import os.path, re, sys - -hgkeywords = 'Id|Header|Author|Date|Revision|RCSFile|Source' - -def pretxnkw(ui, repo, hooktype, **args): - '''Collects candidates for keyword expansion on commit - and expands keywords in working dir. - NOTE: for use in combination with hgext.keyword!''' - - if hooktype != 'pretxncommit': - # bail out with error - return True - - # reparse args, opts again as pretxncommit hook is silent about them - cmd, sysargs, globalopts, cmdopts = commands.parse(ui, sys.argv[1:])[1:] - # exclude tag and import - if repr(cmd).split()[1] in ('tag', 'import_'): - return False - files, match, anypats = cmdutil.matchpats(repo, sysargs, cmdopts) - - # validity checks should have been done already - modified, added = repo.status(files=files, match=match)[:2] - candidates = [f for f in modified + added if not f.startswith('.hg')] - - if not candidates: - return False - - # only check files that have hgkwencode assigned as encode filter - files = [] - # python2.4: files = set() - 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) - # python2.4: - # if mf(candidate): files.add(candidate) - - if not files: # nothing to do - return False - - user, date = repo.changelog.read(repo.changelog.tip())[1:3] - strdate = util.datestr(date=date) - shortuser = util.shortuser(user) - shortdate = util.datestr(date=date, format=util.defaultdateformats[0]) - # %Y-%m-%d %H:%M:%S - - re_kw = re.compile(r'\$(%s)(: [^$]+? )?\$' % hgkeywords) - - for f in files: - data = repo.wfile(f).read() - if not util.binary(data): - - def kwexpand(matchobj): - RCSFile = os.path.basename(f)+',v' - Source = os.path.join(repo.root, f)+',v' - Revision = args['node'][:12] - Date = strdate - Author = user - revdateauth = '%s %s %s' % (Revision, shortdate, shortuser) - Header = '%s %s' % (Source, revdateauth) - Id = '%s %s' % (RCSFile, revdateauth) - return '$%s: %s $' % ( - matchobj.group(1), eval(matchobj.group(1))) - - data, kwct = re_kw.subn(kwexpand, data) - if kwct: - ui.note(_('expanding keywords in %s\n' % f)) - # backup file? - repo.wfile(f, 'w').write(data)