##// END OF EJS Templates
extensions: use new wrapper functions
Matt Mackall -
r7216:292fb2ad default
parent child Browse files
Show More
@@ -51,7 +51,7 b' qseries.missing = red bold'
51 51
52 52 import re, sys
53 53
54 from mercurial import commands, cmdutil
54 from mercurial import commands, cmdutil, extensions
55 55 from mercurial.i18n import _
56 56
57 57 # start and stop parameters for effects
@@ -89,14 +89,14 b' def render_effects(text, *effects):'
89 89 stop = '\033[' + ';'.join(stop) + 'm'
90 90 return start + text + stop
91 91
92 def colorstatus(statusfunc, ui, repo, *pats, **opts):
92 def colorstatus(orig, ui, repo, *pats, **opts):
93 93 '''run the status command with colored output'''
94 94
95 95 delimiter = opts['print0'] and '\0' or '\n'
96 96
97 97 # run status and capture it's output
98 98 ui.pushbuffer()
99 retval = statusfunc(ui, repo, *pats, **opts)
99 retval = orig(ui, repo, *pats, **opts)
100 100 # filter out empty strings
101 101 lines = [ line for line in ui.popbuffer().split(delimiter) if line ]
102 102
@@ -139,10 +139,10 b' def colorstatus(statusfunc, ui, repo, *p'
139 139 'clean': ('none', ),
140 140 'copied': ('none', ), }
141 141
142 def colorqseries(qseriesfunc, ui, repo, *dummy, **opts):
142 def colorqseries(orig, ui, repo, *dummy, **opts):
143 143 '''run the qseries command with colored output'''
144 144 ui.pushbuffer()
145 retval = qseriesfunc(ui, repo, **opts)
145 retval = orig(ui, repo, **opts)
146 146 patches = ui.popbuffer().splitlines()
147 147 for patch in patches:
148 148 patchname = patch
@@ -168,58 +168,23 b' def colorqseries(qseriesfunc, ui, repo, '
168 168
169 169 def uisetup(ui):
170 170 '''Initialize the extension.'''
171 nocoloropt = ('', 'no-color', None, _("don't colorize output"))
172 _decoratecmd(ui, 'status', commands.table, colorstatus, nocoloropt)
173 _configcmdeffects(ui, 'status', _status_effects);
171 _setupcmd(ui, 'status', commands.table, colorstatus, _status_effects)
174 172 if ui.config('extensions', 'hgext.mq') is not None or \
175 173 ui.config('extensions', 'mq') is not None:
176 174 from hgext import mq
177 _decoratecmd(ui, 'qseries', mq.cmdtable, colorqseries, nocoloropt)
178 _configcmdeffects(ui, 'qseries', _patch_effects);
179
180 def _decoratecmd(ui, cmd, table, delegate, *delegateoptions):
181 '''Replace the function that implements cmd in table with a decorator.
182
183 The decorator that becomes the new implementation of cmd calls
184 delegate. The delegate's first argument is the replaced function,
185 followed by the normal Mercurial command arguments (ui, repo, ...). If
186 the delegate adds command options, supply them as delegateoptions.
187 '''
188 cmdkey, cmdentry = _cmdtableitem(ui, cmd, table)
189 decorator = lambda ui, repo, *args, **opts: \
190 _colordecorator(delegate, cmdentry[0],
191 ui, repo, *args, **opts)
192 # make sure 'hg help cmd' still works
193 decorator.__doc__ = cmdentry[0].__doc__
194 decoratorentry = (decorator,) + cmdentry[1:]
195 for option in delegateoptions:
196 decoratorentry[1].append(option)
197 table[cmdkey] = decoratorentry
175 _setupcmd(ui, 'qseries', mq.cmdtable, colorqseries, _patch_effects)
198 176
199 def _cmdtableitem(ui, cmd, table):
200 '''Return key, value from table for cmd, or None if not found.'''
201 aliases, entry = cmdutil.findcmd(cmd, table)
202 for candidatekey, candidateentry in table.iteritems():
203 if candidateentry is entry:
204 return candidatekey, entry
205
206 def _colordecorator(colorfunc, nocolorfunc, ui, repo, *args, **opts):
207 '''Delegate to colorfunc or nocolorfunc, depending on conditions.
177 def _setupcmd(ui, cmd, table, func, effectsmap):
178 '''patch in command to command table and load effect map'''
179 def nocolor(orig, *args, **kwargs):
180 if kwargs['no_color']:
181 return orig(*args, **kwargs)
182 return func(orig, *args, **kwargs)
208 183
209 Delegate to colorfunc unless --no-color option is set or output is not
210 to a tty.
211 '''
212 if opts['no_color'] or not sys.stdout.isatty():
213 return nocolorfunc(ui, repo, *args, **opts)
214 return colorfunc(nocolorfunc, ui, repo, *args, **opts)
184 entry = extensions.wrapcommand(table, cmd, nocolor)
185 entry[1].append(('', 'no-color', None, _("don't colorize output")))
215 186
216 def _configcmdeffects(ui, cmdname, effectsmap):
217 '''Override default effects for cmdname with those from .hgrc file.
218
219 Entries in the .hgrc file are in the [color] section, and look like
220 'cmdname'.'status' (for instance, 'status.modified = blue bold inverse').
221 '''
222 187 for status in effectsmap:
223 effects = ui.config('color', cmdname + '.' + status)
188 effects = ui.config('color', cmd + '.' + status)
224 189 if effects:
225 190 effectsmap[status] = re.split('\W+', effects)
@@ -20,11 +20,9 b" The default is 'colorful'."
20 20
21 21 import highlight
22 22 from mercurial.hgweb import webcommands, webutil, common
23 from mercurial import extensions
23 24
24 web_filerevision = webcommands._filerevision
25 web_annotate = webcommands.annotate
26
27 def filerevision_highlight(web, tmpl, fctx):
25 def filerevision_highlight(orig, web, tmpl, fctx):
28 26 mt = ''.join(tmpl('mimetype', encoding=web.encoding))
29 27 # only pygmentize for mimetype containing 'html' so we both match
30 28 # 'text/html' and possibly 'application/xhtml+xml' in the future
@@ -36,15 +34,15 b' def filerevision_highlight(web, tmpl, fc'
36 34 if 'html' in mt:
37 35 style = web.config('web', 'pygments_style', 'colorful')
38 36 highlight.pygmentize('fileline', fctx, style, tmpl)
39 return web_filerevision(web, tmpl, fctx)
37 return orig(web, tmpl, fctx)
40 38
41 def annotate_highlight(web, req, tmpl):
39 def annotate_highlight(orig, web, req, tmpl):
42 40 mt = ''.join(tmpl('mimetype', encoding=web.encoding))
43 41 if 'html' in mt:
44 42 fctx = webutil.filectx(web.repo, req)
45 43 style = web.config('web', 'pygments_style', 'colorful')
46 44 highlight.pygmentize('annotateline', fctx, style, tmpl)
47 return web_annotate(web, req, tmpl)
45 return orig(web, req, tmpl)
48 46
49 47 def generate_css(web, req, tmpl):
50 48 pg_style = web.config('web', 'pygments_style', 'colorful')
@@ -52,10 +50,8 b' def generate_css(web, req, tmpl):'
52 50 req.respond(common.HTTP_OK, 'text/css')
53 51 return ['/* pygments_style = %s */\n\n' % pg_style, fmter.get_style_defs('')]
54 52
55
56 53 # monkeypatch in the new version
57
58 webcommands._filerevision = filerevision_highlight
59 webcommands.annotate = annotate_highlight
54 extensions.wrapfunction(webcommands, '_filerevision', filerevision_highlight)
55 extensions.wrapfunction(webcommands, 'annotate', annotate_highlight)
60 56 webcommands.highlightcss = generate_css
61 57 webcommands.__all__.append('highlightcss')
@@ -27,7 +27,7 b''
27 27
28 28 import re
29 29 from mercurial.hgweb import hgweb_mod
30 from mercurial import templatefilters
30 from mercurial import templatefilters, extensions
31 31 from mercurial.i18n import _
32 32
33 33 orig_escape = templatefilters.filters["escape"]
@@ -42,9 +42,7 b' def interhg_escape(x):'
42 42
43 43 templatefilters.filters["escape"] = interhg_escape
44 44
45 orig_refresh = hgweb_mod.hgweb.refresh
46
47 def interhg_refresh(self):
45 def interhg_refresh(orig, self):
48 46 interhg_table[:] = []
49 47 for key, pattern in self.repo.ui.configitems('interhg'):
50 48 # grab the delimiter from the character after the "s"
@@ -79,6 +77,6 b' def interhg_refresh(self):'
79 77 except re.error:
80 78 self.repo.ui.warn(_("interhg: invalid regexp for %s: %s\n")
81 79 % (key, regexp))
82 return orig_refresh(self)
80 return orig(self)
83 81
84 hgweb_mod.hgweb.refresh = interhg_refresh
82 extensions.wrapfunction(hgweb_mod.hgweb, 'refresh', interhg_refresh)
@@ -78,7 +78,7 b" like CVS' $Log$, are not supported. A ke"
78 78 "Log = {desc}" expands to the first line of the changeset description.
79 79 '''
80 80
81 from mercurial import commands, cmdutil, dispatch, filelog, revlog
81 from mercurial import commands, cmdutil, dispatch, filelog, revlog, extensions
82 82 from mercurial import patch, localrepo, templater, templatefilters, util
83 83 from mercurial.hgweb import webcommands
84 84 from mercurial.node import nullid, hex
@@ -416,14 +416,13 b' def uisetup(ui):'
416 416 kwtools['exc'].append(pat)
417 417
418 418 if kwtools['inc']:
419 def kwdispatch_parse(ui, args):
419 def kwdispatch_parse(orig, ui, args):
420 420 '''Monkeypatch dispatch._parse to obtain running hg command.'''
421 cmd, func, args, options, cmdoptions = dispatch_parse(ui, args)
421 cmd, func, args, options, cmdoptions = orig(ui, args)
422 422 kwtools['hgcmd'] = cmd
423 423 return cmd, func, args, options, cmdoptions
424 424
425 dispatch_parse = dispatch._parse
426 dispatch._parse = kwdispatch_parse
425 extensions.wrapfunction(dispatch, '_parse', kwdispatch_parse)
427 426
428 427 def reposetup(ui, repo):
429 428 '''Sets up repo as kwrepo for keyword substitution.
@@ -495,14 +494,14 b' def reposetup(ui, repo):'
495 494 del wlock, lock
496 495
497 496 # monkeypatches
498 def kwpatchfile_init(self, ui, fname, missing=False):
497 def kwpatchfile_init(orig, self, ui, fname, missing=False):
499 498 '''Monkeypatch/wrap patch.patchfile.__init__ to avoid
500 499 rejects or conflicts due to expanded keywords in working dir.'''
501 patchfile_init(self, ui, fname, missing)
500 orig(self, ui, fname, missing)
502 501 # shrink keywords read from working dir
503 502 self.lines = kwt.shrinklines(self.fname, self.lines)
504 503
505 def kw_diff(repo, node1=None, node2=None, match=None,
504 def kw_diff(orig, repo, node1=None, node2=None, match=None,
506 505 fp=None, changes=None, opts=None):
507 506 '''Monkeypatch patch.diff to avoid expansion except when
508 507 comparing against working dir.'''
@@ -510,37 +509,19 b' def reposetup(ui, repo):'
510 509 kwt.matcher = util.never
511 510 elif node1 is not None and node1 != repo['.'].node():
512 511 kwt.restrict = True
513 patch_diff(repo, node1, node2, match, fp, changes, opts)
514
515 def kwweb_annotate(web, req, tmpl):
516 '''Wraps webcommands.annotate turning off keyword expansion.'''
517 kwt.matcher = util.never
518 return webcommands_annotate(web, req, tmpl)
512 orig(repo, node1, node2, match, fp, changes, opts)
519 513
520 def kwweb_changeset(web, req, tmpl):
521 '''Wraps webcommands.changeset turning off keyword expansion.'''
514 def kwweb_skip(orig, web, req, tmpl):
515 '''Wraps webcommands.x turning off keyword expansion.'''
522 516 kwt.matcher = util.never
523 return webcommands_changeset(web, req, tmpl)
524
525 def kwweb_filediff(web, req, tmpl):
526 '''Wraps webcommands.filediff turning off keyword expansion.'''
527 kwt.matcher = util.never
528 return webcommands_filediff(web, req, tmpl)
517 return orig(web, req, tmpl)
529 518
530 519 repo.__class__ = kwrepo
531 520
532 patchfile_init = patch.patchfile.__init__
533 patch_diff = patch.diff
534 webcommands_annotate = webcommands.annotate
535 webcommands_changeset = webcommands.changeset
536 webcommands_filediff = webcommands.filediff
537
538 patch.patchfile.__init__ = kwpatchfile_init
539 patch.diff = kw_diff
540 webcommands.annotate = kwweb_annotate
541 webcommands.changeset = webcommands.rev = kwweb_changeset
542 webcommands.filediff = webcommands.diff = kwweb_filediff
543
521 extensions.wrapfunction(patch.patchfile, '__init__', kwpatchfile_init)
522 extensions.wrapfunction(patch, 'diff', kw_diff)
523 for c in 'annotate changeset rev filediff diff'.split():
524 extensions.wrapfunction(webcommands, c, kwweb_skip)
544 525
545 526 cmdtable = {
546 527 'kwdemo':
@@ -33,7 +33,7 b' from mercurial.i18n import _'
33 33 from mercurial.node import bin, hex, short
34 34 from mercurial.repo import RepoError
35 35 from mercurial import commands, cmdutil, hg, patch, revlog, util
36 from mercurial import repair
36 from mercurial import repair, extensions
37 37 import os, sys, re, errno, urllib
38 38
39 39 commands.norepo += " qclone"
@@ -2366,22 +2366,14 b' def reposetup(ui, repo):'
2366 2366 repo.__class__ = mqrepo
2367 2367 repo.mq = queue(ui, repo.join(""))
2368 2368
2369 def uisetup(ui):
2370 # override import to disallow importing over patch
2371 importalias, importcmd = cmdutil.findcmd('import', commands.table)
2372 for alias, cmd in commands.table.iteritems():
2373 if cmd is importcmd:
2374 importkey = alias
2375 break
2376 orig_import = importcmd[0]
2377 def mqimport(ui, repo, patch1, *patches, **opts):
2369 def mqimport(orig, ui, repo, *args, **kwargs):
2378 2370 if hasattr(repo, 'abort_if_wdir_patched'):
2379 2371 repo.abort_if_wdir_patched(_('cannot import over an applied patch'),
2380 opts.get('force'))
2381 orig_import(ui, repo, patch1, *patches, **opts)
2382 importcmd = list(importcmd)
2383 importcmd[0] = mqimport
2384 commands.table[importkey] = tuple(importcmd)
2372 kwargs.get('force'))
2373 return orig(ui, repo, *args, **kwargs)
2374
2375 def uisetup(ui):
2376 extensions.wrapcommand(commands.table, 'import', mqimport)
2385 2377
2386 2378 seriesopts = [('s', 'summary', None, _('print first line of patch header'))]
2387 2379
@@ -47,10 +47,10 b' them in the global .hgrc'
47 47 '''
48 48
49 49 import sys, os, signal
50 from mercurial import dispatch, util
50 from mercurial import dispatch, util, extensions
51 51
52 52 def uisetup(ui):
53 def pagecmd(ui, options, cmd, cmdfunc):
53 def pagecmd(orig, ui, options, cmd, cmdfunc):
54 54 p = ui.config("pager", "pager", os.environ.get("PAGER"))
55 55 if p and sys.stdout.isatty() and '--debugger' not in sys.argv:
56 56 attend = ui.configlist('pager', 'attend')
@@ -59,7 +59,6 b' def uisetup(ui):'
59 59 sys.stderr = sys.stdout = util.popen(p, "wb")
60 60 if ui.configbool('pager', 'quiet'):
61 61 signal.signal(signal.SIGPIPE, signal.SIG_DFL)
62 return oldrun(ui, options, cmd, cmdfunc)
62 return orig(ui, options, cmd, cmdfunc)
63 63
64 oldrun = dispatch._runcommand
65 dispatch._runcommand = pagecmd
64 extensions.wrapfunction(dispatch, '_runcommand', pagecmd)
@@ -13,7 +13,7 b' For more information:'
13 13 http://www.selenic.com/mercurial/wiki/index.cgi/RebaseProject
14 14 '''
15 15
16 from mercurial import util, repair, merge, cmdutil, dispatch, commands
16 from mercurial import util, repair, merge, cmdutil, dispatch, commands, extensions
17 17 from mercurial.commands import templateopts
18 18 from mercurial.node import nullrev
19 19 from mercurial.i18n import _
@@ -352,7 +352,7 b' def buildstate(repo, dest, src, base, co'
352 352 state[source] = nullrev
353 353 return repo['.'].rev(), repo[dest].rev(), state, external
354 354
355 def pulldelegate(pullfunction, repo, *args, **opts):
355 def pullrebase(orig, ui, repo, *args, **opts):
356 356 'Call rebase after pull if the latter has been invoked with --rebase'
357 357 if opts.get('rebase'):
358 358 if opts.get('update'):
@@ -360,31 +360,19 b' def pulldelegate(pullfunction, repo, *ar'
360 360
361 361 cmdutil.bail_if_changed(repo)
362 362 revsprepull = len(repo)
363 pullfunction(repo.ui, repo, *args, **opts)
363 orig(ui, repo, *args, **opts)
364 364 revspostpull = len(repo)
365 365 if revspostpull > revsprepull:
366 rebase(repo.ui, repo, **opts)
366 rebase(ui, repo, **opts)
367 367 else:
368 pullfunction(repo.ui, repo, *args, **opts)
368 orig(ui, repo, *args, **opts)
369 369
370 370 def uisetup(ui):
371 371 'Replace pull with a decorator to provide --rebase option'
372 # cribbed from color.py
373 aliases, entry = cmdutil.findcmd('pull', commands.table)
374 for candidatekey, candidateentry in commands.table.iteritems():
375 if candidateentry is entry:
376 cmdkey, cmdentry = candidatekey, entry
377 break
378
379 decorator = lambda ui, repo, *args, **opts: \
380 pulldelegate(cmdentry[0], repo, *args, **opts)
381 # make sure 'hg help cmd' still works
382 decorator.__doc__ = cmdentry[0].__doc__
383 decoratorentry = (decorator,) + cmdentry[1:]
384 rebaseopt = ('', 'rebase', None,
372 entry = extensions.wrapcommand(commands.table, 'pull', pullrebase)
373 entry[1].append(('', 'rebase', None,
385 374 _("rebase working directory to branch head"))
386 decoratorentry[1].append(rebaseopt)
387 commands.table[cmdkey] = decoratorentry
375 )
388 376
389 377 cmdtable = {
390 378 "rebase":
@@ -8,6 +8,7 b''
8 8
9 9 import Zeroconf, socket, time, os
10 10 from mercurial import ui
11 from mercurial import extensions
11 12 from mercurial.hgweb import hgweb_mod
12 13 from mercurial.hgweb import hgwebdir_mod
13 14
@@ -114,22 +115,20 b' def getzcpaths():'
114 115 v.properties.get("path", "/"))
115 116 yield "zc-" + n, u
116 117
117 def config(self, section, key, default=None, untrusted=False):
118 def config(orig, self, section, key, default=None, untrusted=False):
118 119 if section == "paths" and key.startswith("zc-"):
119 120 for n, p in getzcpaths():
120 121 if n == key:
121 122 return p
122 return oldconfig(self, section, key, default, untrusted)
123 return orig(self, section, key, default, untrusted)
123 124
124 def configitems(self, section):
125 r = oldconfigitems(self, section, untrusted=False)
125 def configitems(orig, self, section):
126 r = orig(self, section, untrusted=False)
126 127 if section == "paths":
127 128 r += getzcpaths()
128 129 return r
129 130
130 oldconfig = ui.ui.config
131 oldconfigitems = ui.ui.configitems
132 ui.ui.config = config
133 ui.ui.configitems = configitems
131 extensions.wrapfunction(ui.ui, 'config', config)
132 extensions.wrapfunction(ui.ui, 'configitems', configitems)
134 133 hgweb_mod.hgweb = hgwebzc
135 134 hgwebdir_mod.hgwebdir = hgwebdirzc
General Comments 0
You need to be logged in to leave comments. Login now