# HG changeset patch # User Christian Ebert # Date 1184917478 -7200 # Node ID 2d089b691b31c10704616dcb09d1bc6efb3a605d # Parent 20eaa614739104e0fc828928ff0f4bbcc1a04396 Add grep, init to nokwcommands; make variables, getcmd local Remove checking of ParseError - was done before, and might be in cmdutil. nokwcommands as list, can be appended to. diff -r 20eaa6147391 -r 2d089b691b31 hgkw/keyword.py --- a/hgkw/keyword.py Thu Jul 19 16:16:09 2007 +0200 +++ b/hgkw/keyword.py Fri Jul 20 09:44:38 2007 +0200 @@ -94,37 +94,10 @@ commands.optionalrepo += ' kwdemo' -deftemplates = { - 'Revision': '{node|short}', - 'Author': '{author|user}', - 'Date': '{date|utcdate}', - 'RCSFile': '{file|basename},v', - 'Source': '{root}/{file},v', - 'Id': '{file|basename},v {node|short} {date|utcdate} {author|user}', - 'Header': '{root}/{file},v {node|short} {date|utcdate} {author|user}', -} - -nokwcommands = ('add', 'addremove', 'bundle', 'clone', 'copy', 'export', - 'incoming', 'outgoing', 'push', 'remove', 'rename', 'rollback') - -keyword_raw = r'\$(%s)[^$\n\r]*?\$' - def utcdate(date): '''Returns hgdate in cvs-like UTC format.''' return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0])) -def getcmd(ui): - '''Returns current hg command.''' - # commands.parse(ui, sys.argv[1:])[0] breaks "hg diff -r" - try: - args = fancyopts.fancyopts(sys.argv[1:], commands.globalopts, {}) - except fancyopts.getopt.GetoptError, inst: - raise commands.ParseError(None, inst) - if args: - cmd = args[0] - aliases, i = findcmd(ui, cmd) - return aliases[0] - def keywordmatcher(ui, repo): '''Collects include/exclude filename patterns for expansion candidates of current configuration. Returns filename matching @@ -144,30 +117,41 @@ Sets up keyword templates, corresponding keyword regex, and provides keyword substitution functions. ''' + deftemplates = { + 'Revision': '{node|short}', + 'Author': '{author|user}', + 'Date': '{date|utcdate}', + 'RCSFile': '{file|basename},v', + 'Source': '{root}/{file},v', + 'Id': '{file|basename},v {node|short} {date|utcdate} {author|user}', + 'Header': '{root}/{file},v {node|short} {date|utcdate} {author|user}', + } + def __init__(self, ui, repo, path='', node=None, expand=True): self.ui = ui self.repo = repo self.path = path self.node = node + self.t = expand or None + templates = dict(ui.configitems('keywordmaps')) if templates: for k in templates.keys(): templates[k] = templater.parsestring(templates[k], quoted=False) - self.templates = templates or deftemplates + self.templates = templates or self.deftemplates escaped = [re.escape(k) for k in self.templates.keys()] - self.re_kw = re.compile(keyword_raw % '|'.join(escaped)) - if expand: + rawkeyword = r'\$(%s)[^$\n\r]*?\$' + self.re_kw = re.compile(rawkeyword % '|'.join(escaped)) + if self.t: templater.common_filters['utcdate'] = utcdate try: - self.t = cmdutil.changeset_templater(ui, repo, + self.t = cmdutil.changeset_templater(self.ui, self.repo, False, '', False) except TypeError: # depending on hg rev changeset_templater has extra "brinfo" arg - self.t = cmdutil.changeset_templater(ui, repo, + self.t = cmdutil.changeset_templater(self.ui, self.repo, False, None, '', False) - else: - self.t = None def ctxnode(self, node): '''Obtains missing node from file context.''' @@ -323,7 +307,7 @@ ui.setconfig('keyword', fn, '') if opts['default']: kwstatus = 'default' - kwmaps = deftemplates + kwmaps = kwtemplater.deftemplates else: if args or opts['rcfile']: kwstatus = 'custom' @@ -332,7 +316,8 @@ ui.setconfig('keywordmaps', k.strip(), v.strip()) if opts['rcfile']: ui.readconfig(opts['rcfile']) - kwmaps = dict(ui.configitems('keywordmaps')) or deftemplates + kwmaps = (dict(ui.configitems('keywordmaps')) or + kwtemplater.deftemplates) if ui.configitems('keywordmaps'): for k, v in kwmaps.items(): ui.setconfig('keywordmaps', k, v) @@ -365,10 +350,22 @@ This is done for local repos only, and only if there are files configured at all for keyword substitution.''' + nokwcommands = ['add', 'addremove', 'bundle', 'clone', 'copy', 'export', + 'grep', 'identify', 'incoming', 'init', 'outgoing', 'push', + 'remove', 'rename', 'rollback'] + # for backwards compatibility ui = repo.ui - if not repo.local() or getcmd(ui) in nokwcommands: + def getcmd(): + # cmdutil.parse(ui, sys.argv[1:])[0] doesn't work for "hg diff -r" + args = fancyopts.fancyopts(sys.argv[1:], commands.globalopts, {}) + if args: + cmd = args[0] + aliases, i = findcmd(ui, cmd) + return aliases[0] + + if not repo.local() or getcmd() in nokwcommands: return kwfmatcher = keywordmatcher(ui, repo) @@ -382,8 +379,7 @@ if kwfmatcher(f): kwt = kwtemplater(ui, self, path=f, expand=kwexp) return kwfilelog(self.sopener, f, kwt, kwcnt) - else: - return filelog.filelog(self.sopener, f) + return filelog.filelog(self.sopener, f) def commit(self, files=None, text='', user=None, date=None, match=util.always, force=False, lock=None, wlock=None,