comparison mercurial/dispatch.py @ 14439:80c599eee3f3

dispatch: use the request to store the ui object and check if we got one before creating. note that the contents of the ui object might change after dispatch() returns (by options passed through --config for example), to ensure it doesn't, pass a copy() of it.
author Idan Kamara <idankk86@gmail.com>
date Thu, 26 May 2011 00:53:23 +0300
parents 08bfec2ef031
children eccbb9980ada
comparison
equal deleted inserted replaced
14438:08bfec2ef031 14439:80c599eee3f3
10 import util, commands, hg, fancyopts, extensions, hook, error 10 import util, commands, hg, fancyopts, extensions, hook, error
11 import cmdutil, encoding 11 import cmdutil, encoding
12 import ui as uimod 12 import ui as uimod
13 13
14 class request(object): 14 class request(object):
15 def __init__(self, args): 15 def __init__(self, args, ui=None):
16 self.args = args 16 self.args = args
17 self.ui = ui
17 18
18 def run(): 19 def run():
19 "run the command in sys.argv" 20 "run the command in sys.argv"
20 sys.exit(dispatch(request(sys.argv[1:]))) 21 sys.exit(dispatch(request(sys.argv[1:])))
21 22
22 def dispatch(req): 23 def dispatch(req):
23 "run the command specified in req.args" 24 "run the command specified in req.args"
24 try: 25 try:
25 u = uimod.ui() 26 if not req.ui:
27 req.ui = uimod.ui()
26 if '--traceback' in req.args: 28 if '--traceback' in req.args:
27 u.setconfig('ui', 'traceback', 'on') 29 req.ui.setconfig('ui', 'traceback', 'on')
28 except util.Abort, inst: 30 except util.Abort, inst:
29 sys.stderr.write(_("abort: %s\n") % inst) 31 sys.stderr.write(_("abort: %s\n") % inst)
30 if inst.hint: 32 if inst.hint:
31 sys.stderr.write(_("(%s)\n") % inst.hint) 33 sys.stderr.write(_("(%s)\n") % inst.hint)
32 return -1 34 return -1
35 sys.stderr.write(_("hg: parse error at %s: %s\n") % 37 sys.stderr.write(_("hg: parse error at %s: %s\n") %
36 (inst.args[1], inst.args[0])) 38 (inst.args[1], inst.args[0]))
37 else: 39 else:
38 sys.stderr.write(_("hg: parse error: %s\n") % inst.args[0]) 40 sys.stderr.write(_("hg: parse error: %s\n") % inst.args[0])
39 return -1 41 return -1
40 return _runcatch(u, req) 42 return _runcatch(req)
41 43
42 def _runcatch(ui, req): 44 def _runcatch(req):
43 def catchterm(*args): 45 def catchterm(*args):
44 raise error.SignalInterrupt 46 raise error.SignalInterrupt
45 47
48 ui = req.ui
46 try: 49 try:
47 for name in 'SIGBREAK', 'SIGHUP', 'SIGTERM': 50 for name in 'SIGBREAK', 'SIGHUP', 'SIGTERM':
48 num = getattr(signal, name, None) 51 num = getattr(signal, name, None)
49 if num: 52 if num:
50 signal.signal(num, catchterm) 53 signal.signal(num, catchterm)
57 if '--debugger' in req.args: 60 if '--debugger' in req.args:
58 ui.warn(_("entering debugger - " 61 ui.warn(_("entering debugger - "
59 "type c to continue starting hg or h for help\n")) 62 "type c to continue starting hg or h for help\n"))
60 pdb.set_trace() 63 pdb.set_trace()
61 try: 64 try:
62 return _dispatch(ui, req) 65 return _dispatch(req)
63 finally: 66 finally:
64 ui.flush() 67 ui.flush()
65 except: 68 except:
66 # enter the debugger when we hit an exception 69 # enter the debugger when we hit an exception
67 if '--debugger' in req.args: 70 if '--debugger' in req.args:
488 491
489 commands.norepo = norepo 492 commands.norepo = norepo
490 os.chdir(cwd) 493 os.chdir(cwd)
491 494
492 _loaded = set() 495 _loaded = set()
493 def _dispatch(ui, req): 496 def _dispatch(req):
494 args = req.args 497 args = req.args
498 ui = req.ui
499
495 shellaliasfn = _checkshellalias(ui, args) 500 shellaliasfn = _checkshellalias(ui, args)
496 if shellaliasfn: 501 if shellaliasfn:
497 return shellaliasfn() 502 return shellaliasfn()
498 503
499 # read --config before doing anything else 504 # read --config before doing anything else
600 if args and not path: # try to infer -R from command args 605 if args and not path: # try to infer -R from command args
601 repos = map(cmdutil.findrepo, args) 606 repos = map(cmdutil.findrepo, args)
602 guess = repos[0] 607 guess = repos[0]
603 if guess and repos.count(guess) == len(repos): 608 if guess and repos.count(guess) == len(repos):
604 req.args = ['--repository', guess] + fullargs 609 req.args = ['--repository', guess] + fullargs
605 return _dispatch(ui, req) 610 return _dispatch(req)
606 if not path: 611 if not path:
607 raise error.RepoError(_("no repository found in %r" 612 raise error.RepoError(_("no repository found in %r"
608 " (.hg not found)") % os.getcwd()) 613 " (.hg not found)") % os.getcwd())
609 raise 614 raise
610 args.insert(0, repo) 615 args.insert(0, repo)