Show More
@@ -1,402 +1,401 b'' | |||||
1 | # dispatch.py - command dispatching for mercurial |
|
1 | # dispatch.py - command dispatching for mercurial | |
2 | # |
|
2 | # | |
3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms |
|
5 | # This software may be used and distributed according to the terms | |
6 | # of the GNU General Public License, incorporated herein by reference. |
|
6 | # of the GNU General Public License, incorporated herein by reference. | |
7 |
|
7 | |||
8 | import demandimport; demandimport.enable() |
|
|||
9 | from node import * |
|
8 | from node import * | |
10 | from i18n import _ |
|
9 | from i18n import _ | |
11 | import os, sys, atexit, signal, pdb, traceback, socket, errno, shlex, time |
|
10 | import os, sys, atexit, signal, pdb, traceback, socket, errno, shlex, time | |
12 | import util, commands, hg, lock, fancyopts, revlog, version, extensions, hook |
|
11 | import util, commands, hg, lock, fancyopts, revlog, version, extensions, hook | |
13 | import cmdutil |
|
12 | import cmdutil | |
14 | import ui as _ui |
|
13 | import ui as _ui | |
15 |
|
14 | |||
16 | class ParseError(Exception): |
|
15 | class ParseError(Exception): | |
17 | """Exception raised on errors in parsing the command line.""" |
|
16 | """Exception raised on errors in parsing the command line.""" | |
18 |
|
17 | |||
19 | def run(): |
|
18 | def run(): | |
20 | "run the command in sys.argv" |
|
19 | "run the command in sys.argv" | |
21 | sys.exit(dispatch(sys.argv[1:])) |
|
20 | sys.exit(dispatch(sys.argv[1:])) | |
22 |
|
21 | |||
23 | def dispatch(args): |
|
22 | def dispatch(args): | |
24 | "run the command specified in args" |
|
23 | "run the command specified in args" | |
25 | try: |
|
24 | try: | |
26 | u = _ui.ui(traceback='--traceback' in args) |
|
25 | u = _ui.ui(traceback='--traceback' in args) | |
27 | except util.Abort, inst: |
|
26 | except util.Abort, inst: | |
28 | sys.stderr.write(_("abort: %s\n") % inst) |
|
27 | sys.stderr.write(_("abort: %s\n") % inst) | |
29 | return -1 |
|
28 | return -1 | |
30 | return _runcatch(u, args) |
|
29 | return _runcatch(u, args) | |
31 |
|
30 | |||
32 | def _runcatch(ui, args): |
|
31 | def _runcatch(ui, args): | |
33 | def catchterm(*args): |
|
32 | def catchterm(*args): | |
34 | raise util.SignalInterrupt |
|
33 | raise util.SignalInterrupt | |
35 |
|
34 | |||
36 | for name in 'SIGBREAK', 'SIGHUP', 'SIGTERM': |
|
35 | for name in 'SIGBREAK', 'SIGHUP', 'SIGTERM': | |
37 | num = getattr(signal, name, None) |
|
36 | num = getattr(signal, name, None) | |
38 | if num: signal.signal(num, catchterm) |
|
37 | if num: signal.signal(num, catchterm) | |
39 |
|
38 | |||
40 | try: |
|
39 | try: | |
41 | try: |
|
40 | try: | |
42 | # enter the debugger before command execution |
|
41 | # enter the debugger before command execution | |
43 | if '--debugger' in args: |
|
42 | if '--debugger' in args: | |
44 | pdb.set_trace() |
|
43 | pdb.set_trace() | |
45 | try: |
|
44 | try: | |
46 | return _dispatch(ui, args) |
|
45 | return _dispatch(ui, args) | |
47 | finally: |
|
46 | finally: | |
48 | ui.flush() |
|
47 | ui.flush() | |
49 | except: |
|
48 | except: | |
50 | # enter the debugger when we hit an exception |
|
49 | # enter the debugger when we hit an exception | |
51 | if '--debugger' in args: |
|
50 | if '--debugger' in args: | |
52 | pdb.post_mortem(sys.exc_info()[2]) |
|
51 | pdb.post_mortem(sys.exc_info()[2]) | |
53 | ui.print_exc() |
|
52 | ui.print_exc() | |
54 | raise |
|
53 | raise | |
55 |
|
54 | |||
56 | except ParseError, inst: |
|
55 | except ParseError, inst: | |
57 | if inst.args[0]: |
|
56 | if inst.args[0]: | |
58 | ui.warn(_("hg %s: %s\n") % (inst.args[0], inst.args[1])) |
|
57 | ui.warn(_("hg %s: %s\n") % (inst.args[0], inst.args[1])) | |
59 | commands.help_(ui, inst.args[0]) |
|
58 | commands.help_(ui, inst.args[0]) | |
60 | else: |
|
59 | else: | |
61 | ui.warn(_("hg: %s\n") % inst.args[1]) |
|
60 | ui.warn(_("hg: %s\n") % inst.args[1]) | |
62 | commands.help_(ui, 'shortlist') |
|
61 | commands.help_(ui, 'shortlist') | |
63 | except cmdutil.AmbiguousCommand, inst: |
|
62 | except cmdutil.AmbiguousCommand, inst: | |
64 | ui.warn(_("hg: command '%s' is ambiguous:\n %s\n") % |
|
63 | ui.warn(_("hg: command '%s' is ambiguous:\n %s\n") % | |
65 | (inst.args[0], " ".join(inst.args[1]))) |
|
64 | (inst.args[0], " ".join(inst.args[1]))) | |
66 | except cmdutil.UnknownCommand, inst: |
|
65 | except cmdutil.UnknownCommand, inst: | |
67 | ui.warn(_("hg: unknown command '%s'\n") % inst.args[0]) |
|
66 | ui.warn(_("hg: unknown command '%s'\n") % inst.args[0]) | |
68 | commands.help_(ui, 'shortlist') |
|
67 | commands.help_(ui, 'shortlist') | |
69 | except hg.RepoError, inst: |
|
68 | except hg.RepoError, inst: | |
70 | ui.warn(_("abort: %s!\n") % inst) |
|
69 | ui.warn(_("abort: %s!\n") % inst) | |
71 | except lock.LockHeld, inst: |
|
70 | except lock.LockHeld, inst: | |
72 | if inst.errno == errno.ETIMEDOUT: |
|
71 | if inst.errno == errno.ETIMEDOUT: | |
73 | reason = _('timed out waiting for lock held by %s') % inst.locker |
|
72 | reason = _('timed out waiting for lock held by %s') % inst.locker | |
74 | else: |
|
73 | else: | |
75 | reason = _('lock held by %s') % inst.locker |
|
74 | reason = _('lock held by %s') % inst.locker | |
76 | ui.warn(_("abort: %s: %s\n") % (inst.desc or inst.filename, reason)) |
|
75 | ui.warn(_("abort: %s: %s\n") % (inst.desc or inst.filename, reason)) | |
77 | except lock.LockUnavailable, inst: |
|
76 | except lock.LockUnavailable, inst: | |
78 | ui.warn(_("abort: could not lock %s: %s\n") % |
|
77 | ui.warn(_("abort: could not lock %s: %s\n") % | |
79 | (inst.desc or inst.filename, inst.strerror)) |
|
78 | (inst.desc or inst.filename, inst.strerror)) | |
80 | except revlog.RevlogError, inst: |
|
79 | except revlog.RevlogError, inst: | |
81 | ui.warn(_("abort: %s!\n") % inst) |
|
80 | ui.warn(_("abort: %s!\n") % inst) | |
82 | except util.SignalInterrupt: |
|
81 | except util.SignalInterrupt: | |
83 | ui.warn(_("killed!\n")) |
|
82 | ui.warn(_("killed!\n")) | |
84 | except KeyboardInterrupt: |
|
83 | except KeyboardInterrupt: | |
85 | try: |
|
84 | try: | |
86 | ui.warn(_("interrupted!\n")) |
|
85 | ui.warn(_("interrupted!\n")) | |
87 | except IOError, inst: |
|
86 | except IOError, inst: | |
88 | if inst.errno == errno.EPIPE: |
|
87 | if inst.errno == errno.EPIPE: | |
89 | if ui.debugflag: |
|
88 | if ui.debugflag: | |
90 | ui.warn(_("\nbroken pipe\n")) |
|
89 | ui.warn(_("\nbroken pipe\n")) | |
91 | else: |
|
90 | else: | |
92 | raise |
|
91 | raise | |
93 | except socket.error, inst: |
|
92 | except socket.error, inst: | |
94 | ui.warn(_("abort: %s\n") % inst[1]) |
|
93 | ui.warn(_("abort: %s\n") % inst[1]) | |
95 | except IOError, inst: |
|
94 | except IOError, inst: | |
96 | if hasattr(inst, "code"): |
|
95 | if hasattr(inst, "code"): | |
97 | ui.warn(_("abort: %s\n") % inst) |
|
96 | ui.warn(_("abort: %s\n") % inst) | |
98 | elif hasattr(inst, "reason"): |
|
97 | elif hasattr(inst, "reason"): | |
99 | try: # usually it is in the form (errno, strerror) |
|
98 | try: # usually it is in the form (errno, strerror) | |
100 | reason = inst.reason.args[1] |
|
99 | reason = inst.reason.args[1] | |
101 | except: # it might be anything, for example a string |
|
100 | except: # it might be anything, for example a string | |
102 | reason = inst.reason |
|
101 | reason = inst.reason | |
103 | ui.warn(_("abort: error: %s\n") % reason) |
|
102 | ui.warn(_("abort: error: %s\n") % reason) | |
104 | elif hasattr(inst, "args") and inst[0] == errno.EPIPE: |
|
103 | elif hasattr(inst, "args") and inst[0] == errno.EPIPE: | |
105 | if ui.debugflag: |
|
104 | if ui.debugflag: | |
106 | ui.warn(_("broken pipe\n")) |
|
105 | ui.warn(_("broken pipe\n")) | |
107 | elif getattr(inst, "strerror", None): |
|
106 | elif getattr(inst, "strerror", None): | |
108 | if getattr(inst, "filename", None): |
|
107 | if getattr(inst, "filename", None): | |
109 | ui.warn(_("abort: %s: %s\n") % (inst.strerror, inst.filename)) |
|
108 | ui.warn(_("abort: %s: %s\n") % (inst.strerror, inst.filename)) | |
110 | else: |
|
109 | else: | |
111 | ui.warn(_("abort: %s\n") % inst.strerror) |
|
110 | ui.warn(_("abort: %s\n") % inst.strerror) | |
112 | else: |
|
111 | else: | |
113 | raise |
|
112 | raise | |
114 | except OSError, inst: |
|
113 | except OSError, inst: | |
115 | if getattr(inst, "filename", None): |
|
114 | if getattr(inst, "filename", None): | |
116 | ui.warn(_("abort: %s: %s\n") % (inst.strerror, inst.filename)) |
|
115 | ui.warn(_("abort: %s: %s\n") % (inst.strerror, inst.filename)) | |
117 | else: |
|
116 | else: | |
118 | ui.warn(_("abort: %s\n") % inst.strerror) |
|
117 | ui.warn(_("abort: %s\n") % inst.strerror) | |
119 | except util.UnexpectedOutput, inst: |
|
118 | except util.UnexpectedOutput, inst: | |
120 | ui.warn(_("abort: %s") % inst[0]) |
|
119 | ui.warn(_("abort: %s") % inst[0]) | |
121 | if not isinstance(inst[1], basestring): |
|
120 | if not isinstance(inst[1], basestring): | |
122 | ui.warn(" %r\n" % (inst[1],)) |
|
121 | ui.warn(" %r\n" % (inst[1],)) | |
123 | elif not inst[1]: |
|
122 | elif not inst[1]: | |
124 | ui.warn(_(" empty string\n")) |
|
123 | ui.warn(_(" empty string\n")) | |
125 | else: |
|
124 | else: | |
126 | ui.warn("\n%r\n" % util.ellipsis(inst[1])) |
|
125 | ui.warn("\n%r\n" % util.ellipsis(inst[1])) | |
127 | except ImportError, inst: |
|
126 | except ImportError, inst: | |
128 | m = str(inst).split()[-1] |
|
127 | m = str(inst).split()[-1] | |
129 | ui.warn(_("abort: could not import module %s!\n" % m)) |
|
128 | ui.warn(_("abort: could not import module %s!\n" % m)) | |
130 | if m in "mpatch bdiff".split(): |
|
129 | if m in "mpatch bdiff".split(): | |
131 | ui.warn(_("(did you forget to compile extensions?)\n")) |
|
130 | ui.warn(_("(did you forget to compile extensions?)\n")) | |
132 | elif m in "zlib".split(): |
|
131 | elif m in "zlib".split(): | |
133 | ui.warn(_("(is your Python install correct?)\n")) |
|
132 | ui.warn(_("(is your Python install correct?)\n")) | |
134 |
|
133 | |||
135 | except util.Abort, inst: |
|
134 | except util.Abort, inst: | |
136 | ui.warn(_("abort: %s\n") % inst) |
|
135 | ui.warn(_("abort: %s\n") % inst) | |
137 | except SystemExit, inst: |
|
136 | except SystemExit, inst: | |
138 | # Commands shouldn't sys.exit directly, but give a return code. |
|
137 | # Commands shouldn't sys.exit directly, but give a return code. | |
139 | # Just in case catch this and and pass exit code to caller. |
|
138 | # Just in case catch this and and pass exit code to caller. | |
140 | return inst.code |
|
139 | return inst.code | |
141 | except: |
|
140 | except: | |
142 | ui.warn(_("** unknown exception encountered, details follow\n")) |
|
141 | ui.warn(_("** unknown exception encountered, details follow\n")) | |
143 | ui.warn(_("** report bug details to " |
|
142 | ui.warn(_("** report bug details to " | |
144 | "http://www.selenic.com/mercurial/bts\n")) |
|
143 | "http://www.selenic.com/mercurial/bts\n")) | |
145 | ui.warn(_("** or mercurial@selenic.com\n")) |
|
144 | ui.warn(_("** or mercurial@selenic.com\n")) | |
146 | ui.warn(_("** Mercurial Distributed SCM (version %s)\n") |
|
145 | ui.warn(_("** Mercurial Distributed SCM (version %s)\n") | |
147 | % version.get_version()) |
|
146 | % version.get_version()) | |
148 | raise |
|
147 | raise | |
149 |
|
148 | |||
150 | return -1 |
|
149 | return -1 | |
151 |
|
150 | |||
152 | def _findrepo(): |
|
151 | def _findrepo(): | |
153 | p = os.getcwd() |
|
152 | p = os.getcwd() | |
154 | while not os.path.isdir(os.path.join(p, ".hg")): |
|
153 | while not os.path.isdir(os.path.join(p, ".hg")): | |
155 | oldp, p = p, os.path.dirname(p) |
|
154 | oldp, p = p, os.path.dirname(p) | |
156 | if p == oldp: |
|
155 | if p == oldp: | |
157 | return None |
|
156 | return None | |
158 |
|
157 | |||
159 | return p |
|
158 | return p | |
160 |
|
159 | |||
161 | def _parse(ui, args): |
|
160 | def _parse(ui, args): | |
162 | options = {} |
|
161 | options = {} | |
163 | cmdoptions = {} |
|
162 | cmdoptions = {} | |
164 |
|
163 | |||
165 | try: |
|
164 | try: | |
166 | args = fancyopts.fancyopts(args, commands.globalopts, options) |
|
165 | args = fancyopts.fancyopts(args, commands.globalopts, options) | |
167 | except fancyopts.getopt.GetoptError, inst: |
|
166 | except fancyopts.getopt.GetoptError, inst: | |
168 | raise ParseError(None, inst) |
|
167 | raise ParseError(None, inst) | |
169 |
|
168 | |||
170 | if args: |
|
169 | if args: | |
171 | cmd, args = args[0], args[1:] |
|
170 | cmd, args = args[0], args[1:] | |
172 | aliases, i = cmdutil.findcmd(ui, cmd, commands.table) |
|
171 | aliases, i = cmdutil.findcmd(ui, cmd, commands.table) | |
173 | cmd = aliases[0] |
|
172 | cmd = aliases[0] | |
174 | defaults = ui.config("defaults", cmd) |
|
173 | defaults = ui.config("defaults", cmd) | |
175 | if defaults: |
|
174 | if defaults: | |
176 | args = shlex.split(defaults) + args |
|
175 | args = shlex.split(defaults) + args | |
177 | c = list(i[1]) |
|
176 | c = list(i[1]) | |
178 | else: |
|
177 | else: | |
179 | cmd = None |
|
178 | cmd = None | |
180 | c = [] |
|
179 | c = [] | |
181 |
|
180 | |||
182 | # combine global options into local |
|
181 | # combine global options into local | |
183 | for o in commands.globalopts: |
|
182 | for o in commands.globalopts: | |
184 | c.append((o[0], o[1], options[o[1]], o[3])) |
|
183 | c.append((o[0], o[1], options[o[1]], o[3])) | |
185 |
|
184 | |||
186 | try: |
|
185 | try: | |
187 | args = fancyopts.fancyopts(args, c, cmdoptions) |
|
186 | args = fancyopts.fancyopts(args, c, cmdoptions) | |
188 | except fancyopts.getopt.GetoptError, inst: |
|
187 | except fancyopts.getopt.GetoptError, inst: | |
189 | raise ParseError(cmd, inst) |
|
188 | raise ParseError(cmd, inst) | |
190 |
|
189 | |||
191 | # separate global options back out |
|
190 | # separate global options back out | |
192 | for o in commands.globalopts: |
|
191 | for o in commands.globalopts: | |
193 | n = o[1] |
|
192 | n = o[1] | |
194 | options[n] = cmdoptions[n] |
|
193 | options[n] = cmdoptions[n] | |
195 | del cmdoptions[n] |
|
194 | del cmdoptions[n] | |
196 |
|
195 | |||
197 | return (cmd, cmd and i[0] or None, args, options, cmdoptions) |
|
196 | return (cmd, cmd and i[0] or None, args, options, cmdoptions) | |
198 |
|
197 | |||
199 | def _parseconfig(config): |
|
198 | def _parseconfig(config): | |
200 | """parse the --config options from the command line""" |
|
199 | """parse the --config options from the command line""" | |
201 | parsed = [] |
|
200 | parsed = [] | |
202 | for cfg in config: |
|
201 | for cfg in config: | |
203 | try: |
|
202 | try: | |
204 | name, value = cfg.split('=', 1) |
|
203 | name, value = cfg.split('=', 1) | |
205 | section, name = name.split('.', 1) |
|
204 | section, name = name.split('.', 1) | |
206 | if not section or not name: |
|
205 | if not section or not name: | |
207 | raise IndexError |
|
206 | raise IndexError | |
208 | parsed.append((section, name, value)) |
|
207 | parsed.append((section, name, value)) | |
209 | except (IndexError, ValueError): |
|
208 | except (IndexError, ValueError): | |
210 | raise util.Abort(_('malformed --config option: %s') % cfg) |
|
209 | raise util.Abort(_('malformed --config option: %s') % cfg) | |
211 | return parsed |
|
210 | return parsed | |
212 |
|
211 | |||
213 | def _earlygetopt(aliases, args): |
|
212 | def _earlygetopt(aliases, args): | |
214 | """Return list of values for an option (or aliases). |
|
213 | """Return list of values for an option (or aliases). | |
215 |
|
214 | |||
216 | The values are listed in the order they appear in args. |
|
215 | The values are listed in the order they appear in args. | |
217 | The options and values are removed from args. |
|
216 | The options and values are removed from args. | |
218 | """ |
|
217 | """ | |
219 | try: |
|
218 | try: | |
220 | argcount = args.index("--") |
|
219 | argcount = args.index("--") | |
221 | except ValueError: |
|
220 | except ValueError: | |
222 | argcount = len(args) |
|
221 | argcount = len(args) | |
223 | shortopts = [opt for opt in aliases if len(opt) == 2] |
|
222 | shortopts = [opt for opt in aliases if len(opt) == 2] | |
224 | values = [] |
|
223 | values = [] | |
225 | pos = 0 |
|
224 | pos = 0 | |
226 | while pos < argcount: |
|
225 | while pos < argcount: | |
227 | if args[pos] in aliases: |
|
226 | if args[pos] in aliases: | |
228 | if pos + 1 >= argcount: |
|
227 | if pos + 1 >= argcount: | |
229 | # ignore and let getopt report an error if there is no value |
|
228 | # ignore and let getopt report an error if there is no value | |
230 | break |
|
229 | break | |
231 | del args[pos] |
|
230 | del args[pos] | |
232 | values.append(args.pop(pos)) |
|
231 | values.append(args.pop(pos)) | |
233 | argcount -= 2 |
|
232 | argcount -= 2 | |
234 | elif args[pos][:2] in shortopts: |
|
233 | elif args[pos][:2] in shortopts: | |
235 | # short option can have no following space, e.g. hg log -Rfoo |
|
234 | # short option can have no following space, e.g. hg log -Rfoo | |
236 | values.append(args.pop(pos)[2:]) |
|
235 | values.append(args.pop(pos)[2:]) | |
237 | argcount -= 1 |
|
236 | argcount -= 1 | |
238 | else: |
|
237 | else: | |
239 | pos += 1 |
|
238 | pos += 1 | |
240 | return values |
|
239 | return values | |
241 |
|
240 | |||
242 | _loaded = {} |
|
241 | _loaded = {} | |
243 | def _dispatch(ui, args): |
|
242 | def _dispatch(ui, args): | |
244 | # read --config before doing anything else |
|
243 | # read --config before doing anything else | |
245 | # (e.g. to change trust settings for reading .hg/hgrc) |
|
244 | # (e.g. to change trust settings for reading .hg/hgrc) | |
246 | config = _earlygetopt(['--config'], args) |
|
245 | config = _earlygetopt(['--config'], args) | |
247 | if config: |
|
246 | if config: | |
248 | ui.updateopts(config=_parseconfig(config)) |
|
247 | ui.updateopts(config=_parseconfig(config)) | |
249 |
|
248 | |||
250 | # check for cwd |
|
249 | # check for cwd | |
251 | cwd = _earlygetopt(['--cwd'], args) |
|
250 | cwd = _earlygetopt(['--cwd'], args) | |
252 | if cwd: |
|
251 | if cwd: | |
253 | os.chdir(cwd[-1]) |
|
252 | os.chdir(cwd[-1]) | |
254 |
|
253 | |||
255 | # read the local repository .hgrc into a local ui object |
|
254 | # read the local repository .hgrc into a local ui object | |
256 | path = _findrepo() or "" |
|
255 | path = _findrepo() or "" | |
257 | if not path: |
|
256 | if not path: | |
258 | lui = ui |
|
257 | lui = ui | |
259 | if path: |
|
258 | if path: | |
260 | try: |
|
259 | try: | |
261 | lui = _ui.ui(parentui=ui) |
|
260 | lui = _ui.ui(parentui=ui) | |
262 | lui.readconfig(os.path.join(path, ".hg", "hgrc")) |
|
261 | lui.readconfig(os.path.join(path, ".hg", "hgrc")) | |
263 | except IOError: |
|
262 | except IOError: | |
264 | pass |
|
263 | pass | |
265 |
|
264 | |||
266 | # now we can expand paths, even ones in .hg/hgrc |
|
265 | # now we can expand paths, even ones in .hg/hgrc | |
267 | rpath = _earlygetopt(["-R", "--repository", "--repo"], args) |
|
266 | rpath = _earlygetopt(["-R", "--repository", "--repo"], args) | |
268 | if rpath: |
|
267 | if rpath: | |
269 | path = lui.expandpath(rpath[-1]) |
|
268 | path = lui.expandpath(rpath[-1]) | |
270 | lui = _ui.ui(parentui=ui) |
|
269 | lui = _ui.ui(parentui=ui) | |
271 | lui.readconfig(os.path.join(path, ".hg", "hgrc")) |
|
270 | lui.readconfig(os.path.join(path, ".hg", "hgrc")) | |
272 |
|
271 | |||
273 | extensions.loadall(lui) |
|
272 | extensions.loadall(lui) | |
274 | for name, module in extensions.extensions(): |
|
273 | for name, module in extensions.extensions(): | |
275 | if name in _loaded: |
|
274 | if name in _loaded: | |
276 | continue |
|
275 | continue | |
277 | cmdtable = getattr(module, 'cmdtable', {}) |
|
276 | cmdtable = getattr(module, 'cmdtable', {}) | |
278 | overrides = [cmd for cmd in cmdtable if cmd in commands.table] |
|
277 | overrides = [cmd for cmd in cmdtable if cmd in commands.table] | |
279 | if overrides: |
|
278 | if overrides: | |
280 | ui.warn(_("extension '%s' overrides commands: %s\n") |
|
279 | ui.warn(_("extension '%s' overrides commands: %s\n") | |
281 | % (name, " ".join(overrides))) |
|
280 | % (name, " ".join(overrides))) | |
282 | commands.table.update(cmdtable) |
|
281 | commands.table.update(cmdtable) | |
283 | _loaded[name] = 1 |
|
282 | _loaded[name] = 1 | |
284 | # check for fallback encoding |
|
283 | # check for fallback encoding | |
285 | fallback = lui.config('ui', 'fallbackencoding') |
|
284 | fallback = lui.config('ui', 'fallbackencoding') | |
286 | if fallback: |
|
285 | if fallback: | |
287 | util._fallbackencoding = fallback |
|
286 | util._fallbackencoding = fallback | |
288 |
|
287 | |||
289 | fullargs = args |
|
288 | fullargs = args | |
290 | cmd, func, args, options, cmdoptions = _parse(lui, args) |
|
289 | cmd, func, args, options, cmdoptions = _parse(lui, args) | |
291 |
|
290 | |||
292 | if options["config"]: |
|
291 | if options["config"]: | |
293 | raise util.Abort(_("Option --config may not be abbreviated!")) |
|
292 | raise util.Abort(_("Option --config may not be abbreviated!")) | |
294 | if options["cwd"]: |
|
293 | if options["cwd"]: | |
295 | raise util.Abort(_("Option --cwd may not be abbreviated!")) |
|
294 | raise util.Abort(_("Option --cwd may not be abbreviated!")) | |
296 | if options["repository"]: |
|
295 | if options["repository"]: | |
297 | raise util.Abort(_( |
|
296 | raise util.Abort(_( | |
298 | "Option -R has to be separated from other options (i.e. not -qR) " |
|
297 | "Option -R has to be separated from other options (i.e. not -qR) " | |
299 | "and --repository may only be abbreviated as --repo!")) |
|
298 | "and --repository may only be abbreviated as --repo!")) | |
300 |
|
299 | |||
301 | if options["encoding"]: |
|
300 | if options["encoding"]: | |
302 | util._encoding = options["encoding"] |
|
301 | util._encoding = options["encoding"] | |
303 | if options["encodingmode"]: |
|
302 | if options["encodingmode"]: | |
304 | util._encodingmode = options["encodingmode"] |
|
303 | util._encodingmode = options["encodingmode"] | |
305 | if options["time"]: |
|
304 | if options["time"]: | |
306 | def get_times(): |
|
305 | def get_times(): | |
307 | t = os.times() |
|
306 | t = os.times() | |
308 | if t[4] == 0.0: # Windows leaves this as zero, so use time.clock() |
|
307 | if t[4] == 0.0: # Windows leaves this as zero, so use time.clock() | |
309 | t = (t[0], t[1], t[2], t[3], time.clock()) |
|
308 | t = (t[0], t[1], t[2], t[3], time.clock()) | |
310 | return t |
|
309 | return t | |
311 | s = get_times() |
|
310 | s = get_times() | |
312 | def print_time(): |
|
311 | def print_time(): | |
313 | t = get_times() |
|
312 | t = get_times() | |
314 | ui.warn(_("Time: real %.3f secs (user %.3f+%.3f sys %.3f+%.3f)\n") % |
|
313 | ui.warn(_("Time: real %.3f secs (user %.3f+%.3f sys %.3f+%.3f)\n") % | |
315 | (t[4]-s[4], t[0]-s[0], t[2]-s[2], t[1]-s[1], t[3]-s[3])) |
|
314 | (t[4]-s[4], t[0]-s[0], t[2]-s[2], t[1]-s[1], t[3]-s[3])) | |
316 | atexit.register(print_time) |
|
315 | atexit.register(print_time) | |
317 |
|
316 | |||
318 | ui.updateopts(options["verbose"], options["debug"], options["quiet"], |
|
317 | ui.updateopts(options["verbose"], options["debug"], options["quiet"], | |
319 | not options["noninteractive"], options["traceback"]) |
|
318 | not options["noninteractive"], options["traceback"]) | |
320 |
|
319 | |||
321 | if options['help']: |
|
320 | if options['help']: | |
322 | return commands.help_(ui, cmd, options['version']) |
|
321 | return commands.help_(ui, cmd, options['version']) | |
323 | elif options['version']: |
|
322 | elif options['version']: | |
324 | return commands.version_(ui) |
|
323 | return commands.version_(ui) | |
325 | elif not cmd: |
|
324 | elif not cmd: | |
326 | return commands.help_(ui, 'shortlist') |
|
325 | return commands.help_(ui, 'shortlist') | |
327 |
|
326 | |||
328 | repo = None |
|
327 | repo = None | |
329 | if cmd not in commands.norepo.split(): |
|
328 | if cmd not in commands.norepo.split(): | |
330 | try: |
|
329 | try: | |
331 | repo = hg.repository(ui, path=path) |
|
330 | repo = hg.repository(ui, path=path) | |
332 | ui = repo.ui |
|
331 | ui = repo.ui | |
333 | if not repo.local(): |
|
332 | if not repo.local(): | |
334 | raise util.Abort(_("repository '%s' is not local") % path) |
|
333 | raise util.Abort(_("repository '%s' is not local") % path) | |
335 | except hg.RepoError: |
|
334 | except hg.RepoError: | |
336 | if cmd not in commands.optionalrepo.split(): |
|
335 | if cmd not in commands.optionalrepo.split(): | |
337 | if not path: |
|
336 | if not path: | |
338 | raise hg.RepoError(_("There is no Mercurial repository here" |
|
337 | raise hg.RepoError(_("There is no Mercurial repository here" | |
339 | " (.hg not found)")) |
|
338 | " (.hg not found)")) | |
340 | raise |
|
339 | raise | |
341 | d = lambda: func(ui, repo, *args, **cmdoptions) |
|
340 | d = lambda: func(ui, repo, *args, **cmdoptions) | |
342 | else: |
|
341 | else: | |
343 | d = lambda: func(ui, *args, **cmdoptions) |
|
342 | d = lambda: func(ui, *args, **cmdoptions) | |
344 |
|
343 | |||
345 | # run pre-hook, and abort if it fails |
|
344 | # run pre-hook, and abort if it fails | |
346 | ret = hook.hook(ui, repo, "pre-%s" % cmd, False, args=" ".join(fullargs)) |
|
345 | ret = hook.hook(ui, repo, "pre-%s" % cmd, False, args=" ".join(fullargs)) | |
347 | if ret: |
|
346 | if ret: | |
348 | return ret |
|
347 | return ret | |
349 | ret = _runcommand(ui, options, cmd, d) |
|
348 | ret = _runcommand(ui, options, cmd, d) | |
350 | # run post-hook, passing command result |
|
349 | # run post-hook, passing command result | |
351 | hook.hook(ui, repo, "post-%s" % cmd, False, args=" ".join(fullargs), |
|
350 | hook.hook(ui, repo, "post-%s" % cmd, False, args=" ".join(fullargs), | |
352 | result = ret) |
|
351 | result = ret) | |
353 | return ret |
|
352 | return ret | |
354 |
|
353 | |||
355 | def _runcommand(ui, options, cmd, cmdfunc): |
|
354 | def _runcommand(ui, options, cmd, cmdfunc): | |
356 | def checkargs(): |
|
355 | def checkargs(): | |
357 | try: |
|
356 | try: | |
358 | return cmdfunc() |
|
357 | return cmdfunc() | |
359 | except TypeError, inst: |
|
358 | except TypeError, inst: | |
360 | # was this an argument error? |
|
359 | # was this an argument error? | |
361 | tb = traceback.extract_tb(sys.exc_info()[2]) |
|
360 | tb = traceback.extract_tb(sys.exc_info()[2]) | |
362 | if len(tb) != 2: # no |
|
361 | if len(tb) != 2: # no | |
363 | raise |
|
362 | raise | |
364 | raise ParseError(cmd, _("invalid arguments")) |
|
363 | raise ParseError(cmd, _("invalid arguments")) | |
365 |
|
364 | |||
366 | if options['profile']: |
|
365 | if options['profile']: | |
367 | import hotshot, hotshot.stats |
|
366 | import hotshot, hotshot.stats | |
368 | prof = hotshot.Profile("hg.prof") |
|
367 | prof = hotshot.Profile("hg.prof") | |
369 | try: |
|
368 | try: | |
370 | try: |
|
369 | try: | |
371 | return prof.runcall(checkargs) |
|
370 | return prof.runcall(checkargs) | |
372 | except: |
|
371 | except: | |
373 | try: |
|
372 | try: | |
374 | ui.warn(_('exception raised - generating ' |
|
373 | ui.warn(_('exception raised - generating ' | |
375 | 'profile anyway\n')) |
|
374 | 'profile anyway\n')) | |
376 | except: |
|
375 | except: | |
377 | pass |
|
376 | pass | |
378 | raise |
|
377 | raise | |
379 | finally: |
|
378 | finally: | |
380 | prof.close() |
|
379 | prof.close() | |
381 | stats = hotshot.stats.load("hg.prof") |
|
380 | stats = hotshot.stats.load("hg.prof") | |
382 | stats.strip_dirs() |
|
381 | stats.strip_dirs() | |
383 | stats.sort_stats('time', 'calls') |
|
382 | stats.sort_stats('time', 'calls') | |
384 | stats.print_stats(40) |
|
383 | stats.print_stats(40) | |
385 | elif options['lsprof']: |
|
384 | elif options['lsprof']: | |
386 | try: |
|
385 | try: | |
387 | from mercurial import lsprof |
|
386 | from mercurial import lsprof | |
388 | except ImportError: |
|
387 | except ImportError: | |
389 | raise util.Abort(_( |
|
388 | raise util.Abort(_( | |
390 | 'lsprof not available - install from ' |
|
389 | 'lsprof not available - install from ' | |
391 | 'http://codespeak.net/svn/user/arigo/hack/misc/lsprof/')) |
|
390 | 'http://codespeak.net/svn/user/arigo/hack/misc/lsprof/')) | |
392 | p = lsprof.Profiler() |
|
391 | p = lsprof.Profiler() | |
393 | p.enable(subcalls=True) |
|
392 | p.enable(subcalls=True) | |
394 | try: |
|
393 | try: | |
395 | return checkargs() |
|
394 | return checkargs() | |
396 | finally: |
|
395 | finally: | |
397 | p.disable() |
|
396 | p.disable() | |
398 | stats = lsprof.Stats(p.getstats()) |
|
397 | stats = lsprof.Stats(p.getstats()) | |
399 | stats.sort() |
|
398 | stats.sort() | |
400 | stats.pprint(top=10, file=sys.stderr, climit=5) |
|
399 | stats.pprint(top=10, file=sys.stderr, climit=5) | |
401 | else: |
|
400 | else: | |
402 | return checkargs() |
|
401 | return checkargs() |
General Comments 0
You need to be logged in to leave comments.
Login now