Show More
@@ -1,569 +1,582 | |||
|
1 | 1 | # dispatch.py - command dispatching for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from i18n import _ |
|
9 | import os, sys, atexit, signal, pdb, socket, errno, shlex, time, traceback | |
|
9 | import os, sys, atexit, signal, pdb, socket, errno, shlex, time, traceback, re | |
|
10 | 10 | import util, commands, hg, fancyopts, extensions, hook, error |
|
11 | 11 | import cmdutil, encoding |
|
12 | 12 | import ui as uimod |
|
13 | 13 | |
|
14 | 14 | def run(): |
|
15 | 15 | "run the command in sys.argv" |
|
16 | 16 | sys.exit(dispatch(sys.argv[1:])) |
|
17 | 17 | |
|
18 | 18 | def dispatch(args): |
|
19 | 19 | "run the command specified in args" |
|
20 | 20 | try: |
|
21 | 21 | u = uimod.ui() |
|
22 | 22 | if '--traceback' in args: |
|
23 | 23 | u.setconfig('ui', 'traceback', 'on') |
|
24 | 24 | except util.Abort, inst: |
|
25 | 25 | sys.stderr.write(_("abort: %s\n") % inst) |
|
26 | 26 | if inst.hint: |
|
27 | 27 | sys.stderr.write(_("(%s)\n") % inst.hint) |
|
28 | 28 | return -1 |
|
29 | 29 | except error.ParseError, inst: |
|
30 | 30 | if len(inst.args) > 1: |
|
31 | 31 | sys.stderr.write(_("hg: parse error at %s: %s\n") % |
|
32 | 32 | (inst.args[1], inst.args[0])) |
|
33 | 33 | else: |
|
34 | 34 | sys.stderr.write(_("hg: parse error: %s\n") % inst.args[0]) |
|
35 | 35 | return -1 |
|
36 | 36 | return _runcatch(u, args) |
|
37 | 37 | |
|
38 | 38 | def _runcatch(ui, args): |
|
39 | 39 | def catchterm(*args): |
|
40 | 40 | raise error.SignalInterrupt |
|
41 | 41 | |
|
42 | 42 | try: |
|
43 | 43 | for name in 'SIGBREAK', 'SIGHUP', 'SIGTERM': |
|
44 | 44 | num = getattr(signal, name, None) |
|
45 | 45 | if num: |
|
46 | 46 | signal.signal(num, catchterm) |
|
47 | 47 | except ValueError: |
|
48 | 48 | pass # happens if called in a thread |
|
49 | 49 | |
|
50 | 50 | try: |
|
51 | 51 | try: |
|
52 | 52 | # enter the debugger before command execution |
|
53 | 53 | if '--debugger' in args: |
|
54 | 54 | ui.warn(_("entering debugger - " |
|
55 | 55 | "type c to continue starting hg or h for help\n")) |
|
56 | 56 | pdb.set_trace() |
|
57 | 57 | try: |
|
58 | 58 | return _dispatch(ui, args) |
|
59 | 59 | finally: |
|
60 | 60 | ui.flush() |
|
61 | 61 | except: |
|
62 | 62 | # enter the debugger when we hit an exception |
|
63 | 63 | if '--debugger' in args: |
|
64 | 64 | traceback.print_exc() |
|
65 | 65 | pdb.post_mortem(sys.exc_info()[2]) |
|
66 | 66 | ui.traceback() |
|
67 | 67 | raise |
|
68 | 68 | |
|
69 | 69 | # Global exception handling, alphabetically |
|
70 | 70 | # Mercurial-specific first, followed by built-in and library exceptions |
|
71 | 71 | except error.AmbiguousCommand, inst: |
|
72 | 72 | ui.warn(_("hg: command '%s' is ambiguous:\n %s\n") % |
|
73 | 73 | (inst.args[0], " ".join(inst.args[1]))) |
|
74 | 74 | except error.ParseError, inst: |
|
75 | 75 | if len(inst.args) > 1: |
|
76 | 76 | ui.warn(_("hg: parse error at %s: %s\n") % |
|
77 | 77 | (inst.args[1], inst.args[0])) |
|
78 | 78 | else: |
|
79 | 79 | ui.warn(_("hg: parse error: %s\n") % inst.args[0]) |
|
80 | 80 | return -1 |
|
81 | 81 | except error.LockHeld, inst: |
|
82 | 82 | if inst.errno == errno.ETIMEDOUT: |
|
83 | 83 | reason = _('timed out waiting for lock held by %s') % inst.locker |
|
84 | 84 | else: |
|
85 | 85 | reason = _('lock held by %s') % inst.locker |
|
86 | 86 | ui.warn(_("abort: %s: %s\n") % (inst.desc or inst.filename, reason)) |
|
87 | 87 | except error.LockUnavailable, inst: |
|
88 | 88 | ui.warn(_("abort: could not lock %s: %s\n") % |
|
89 | 89 | (inst.desc or inst.filename, inst.strerror)) |
|
90 | 90 | except error.CommandError, inst: |
|
91 | 91 | if inst.args[0]: |
|
92 | 92 | ui.warn(_("hg %s: %s\n") % (inst.args[0], inst.args[1])) |
|
93 | 93 | commands.help_(ui, inst.args[0]) |
|
94 | 94 | else: |
|
95 | 95 | ui.warn(_("hg: %s\n") % inst.args[1]) |
|
96 | 96 | commands.help_(ui, 'shortlist') |
|
97 | 97 | except error.RepoError, inst: |
|
98 | 98 | ui.warn(_("abort: %s!\n") % inst) |
|
99 | 99 | except error.ResponseError, inst: |
|
100 | 100 | ui.warn(_("abort: %s") % inst.args[0]) |
|
101 | 101 | if not isinstance(inst.args[1], basestring): |
|
102 | 102 | ui.warn(" %r\n" % (inst.args[1],)) |
|
103 | 103 | elif not inst.args[1]: |
|
104 | 104 | ui.warn(_(" empty string\n")) |
|
105 | 105 | else: |
|
106 | 106 | ui.warn("\n%r\n" % util.ellipsis(inst.args[1])) |
|
107 | 107 | except error.RevlogError, inst: |
|
108 | 108 | ui.warn(_("abort: %s!\n") % inst) |
|
109 | 109 | except error.SignalInterrupt: |
|
110 | 110 | ui.warn(_("killed!\n")) |
|
111 | 111 | except error.UnknownCommand, inst: |
|
112 | 112 | ui.warn(_("hg: unknown command '%s'\n") % inst.args[0]) |
|
113 | 113 | try: |
|
114 | 114 | # check if the command is in a disabled extension |
|
115 | 115 | # (but don't check for extensions themselves) |
|
116 | 116 | commands.help_(ui, inst.args[0], unknowncmd=True) |
|
117 | 117 | except error.UnknownCommand: |
|
118 | 118 | commands.help_(ui, 'shortlist') |
|
119 | 119 | except util.Abort, inst: |
|
120 | 120 | ui.warn(_("abort: %s\n") % inst) |
|
121 | 121 | if inst.hint: |
|
122 | 122 | ui.warn(_("(%s)\n") % inst.hint) |
|
123 | 123 | except ImportError, inst: |
|
124 | 124 | ui.warn(_("abort: %s!\n") % inst) |
|
125 | 125 | m = str(inst).split()[-1] |
|
126 | 126 | if m in "mpatch bdiff".split(): |
|
127 | 127 | ui.warn(_("(did you forget to compile extensions?)\n")) |
|
128 | 128 | elif m in "zlib".split(): |
|
129 | 129 | ui.warn(_("(is your Python install correct?)\n")) |
|
130 | 130 | except IOError, inst: |
|
131 | 131 | if hasattr(inst, "code"): |
|
132 | 132 | ui.warn(_("abort: %s\n") % inst) |
|
133 | 133 | elif hasattr(inst, "reason"): |
|
134 | 134 | try: # usually it is in the form (errno, strerror) |
|
135 | 135 | reason = inst.reason.args[1] |
|
136 | 136 | except: # it might be anything, for example a string |
|
137 | 137 | reason = inst.reason |
|
138 | 138 | ui.warn(_("abort: error: %s\n") % reason) |
|
139 | 139 | elif hasattr(inst, "args") and inst.args[0] == errno.EPIPE: |
|
140 | 140 | if ui.debugflag: |
|
141 | 141 | ui.warn(_("broken pipe\n")) |
|
142 | 142 | elif getattr(inst, "strerror", None): |
|
143 | 143 | if getattr(inst, "filename", None): |
|
144 | 144 | ui.warn(_("abort: %s: %s\n") % (inst.strerror, inst.filename)) |
|
145 | 145 | else: |
|
146 | 146 | ui.warn(_("abort: %s\n") % inst.strerror) |
|
147 | 147 | else: |
|
148 | 148 | raise |
|
149 | 149 | except OSError, inst: |
|
150 | 150 | if getattr(inst, "filename", None): |
|
151 | 151 | ui.warn(_("abort: %s: %s\n") % (inst.strerror, inst.filename)) |
|
152 | 152 | else: |
|
153 | 153 | ui.warn(_("abort: %s\n") % inst.strerror) |
|
154 | 154 | except KeyboardInterrupt: |
|
155 | 155 | try: |
|
156 | 156 | ui.warn(_("interrupted!\n")) |
|
157 | 157 | except IOError, inst: |
|
158 | 158 | if inst.errno == errno.EPIPE: |
|
159 | 159 | if ui.debugflag: |
|
160 | 160 | ui.warn(_("\nbroken pipe\n")) |
|
161 | 161 | else: |
|
162 | 162 | raise |
|
163 | 163 | except MemoryError: |
|
164 | 164 | ui.warn(_("abort: out of memory\n")) |
|
165 | 165 | except SystemExit, inst: |
|
166 | 166 | # Commands shouldn't sys.exit directly, but give a return code. |
|
167 | 167 | # Just in case catch this and and pass exit code to caller. |
|
168 | 168 | return inst.code |
|
169 | 169 | except socket.error, inst: |
|
170 | 170 | ui.warn(_("abort: %s\n") % inst.args[-1]) |
|
171 | 171 | except: |
|
172 | 172 | ui.warn(_("** unknown exception encountered, details follow\n")) |
|
173 | 173 | ui.warn(_("** report bug details to " |
|
174 | 174 | "http://mercurial.selenic.com/bts/\n")) |
|
175 | 175 | ui.warn(_("** or mercurial@selenic.com\n")) |
|
176 | 176 | ui.warn(_("** Python %s\n") % sys.version.replace('\n', '')) |
|
177 | 177 | ui.warn(_("** Mercurial Distributed SCM (version %s)\n") |
|
178 | 178 | % util.version()) |
|
179 | 179 | ui.warn(_("** Extensions loaded: %s\n") |
|
180 | 180 | % ", ".join([x[0] for x in extensions.extensions()])) |
|
181 | 181 | raise |
|
182 | 182 | |
|
183 | 183 | return -1 |
|
184 | 184 | |
|
185 | 185 | def aliasargs(fn): |
|
186 | 186 | if hasattr(fn, 'args'): |
|
187 | 187 | return fn.args |
|
188 | 188 | return [] |
|
189 | 189 | |
|
190 | 190 | class cmdalias(object): |
|
191 | 191 | def __init__(self, name, definition, cmdtable): |
|
192 | 192 | self.name = name |
|
193 | 193 | self.definition = definition |
|
194 | 194 | self.args = [] |
|
195 | 195 | self.opts = [] |
|
196 | 196 | self.help = '' |
|
197 | 197 | self.norepo = True |
|
198 | 198 | self.badalias = False |
|
199 | 199 | |
|
200 | 200 | try: |
|
201 | 201 | cmdutil.findcmd(self.name, cmdtable, True) |
|
202 | 202 | self.shadows = True |
|
203 | 203 | except error.UnknownCommand: |
|
204 | 204 | self.shadows = False |
|
205 | 205 | |
|
206 | 206 | if not self.definition: |
|
207 | 207 | def fn(ui, *args): |
|
208 | 208 | ui.warn(_("no definition for alias '%s'\n") % self.name) |
|
209 | 209 | return 1 |
|
210 | 210 | self.fn = fn |
|
211 | 211 | self.badalias = True |
|
212 | 212 | |
|
213 | 213 | return |
|
214 | 214 | |
|
215 | 215 | if self.definition.startswith('!'): |
|
216 | 216 | def fn(ui, *args): |
|
217 | cmd = '%s %s' % (self.definition[1:], ' '.join(args)) | |
|
218 | return util.system(cmd) | |
|
217 | env = {'HG_ARGS': ' '.join((self.name,) + args)} | |
|
218 | def _checkvar(m): | |
|
219 | if int(m.groups()[0]) <= len(args): | |
|
220 | return m.group() | |
|
221 | else: | |
|
222 | return '' | |
|
223 | cmd = re.sub(r'\$(\d+)', _checkvar, self.definition[1:]) | |
|
224 | replace = dict((str(i + 1), arg) for i, arg in enumerate(args)) | |
|
225 | replace['0'] = self.name | |
|
226 | replace['@'] = ' '.join(args) | |
|
227 | cmd = util.interpolate(r'\$', replace, cmd) | |
|
228 | return util.system(cmd, environ=env) | |
|
219 | 229 | self.fn = fn |
|
220 | 230 | return |
|
221 | 231 | |
|
222 | 232 | args = shlex.split(self.definition) |
|
223 | 233 | cmd = args.pop(0) |
|
224 | 234 | args = map(util.expandpath, args) |
|
225 | 235 | |
|
226 | 236 | for invalidarg in ("--cwd", "-R", "--repository", "--repo"): |
|
227 | 237 | if _earlygetopt([invalidarg], args): |
|
228 | 238 | def fn(ui, *args): |
|
229 | 239 | ui.warn(_("error in definition for alias '%s': %s may only " |
|
230 | 240 | "be given on the command line\n") |
|
231 | 241 | % (self.name, invalidarg)) |
|
232 | 242 | return 1 |
|
233 | 243 | |
|
234 | 244 | self.fn = fn |
|
235 | 245 | self.badalias = True |
|
236 | 246 | return |
|
237 | 247 | |
|
238 | 248 | try: |
|
239 | 249 | tableentry = cmdutil.findcmd(cmd, cmdtable, False)[1] |
|
240 | 250 | if len(tableentry) > 2: |
|
241 | 251 | self.fn, self.opts, self.help = tableentry |
|
242 | 252 | else: |
|
243 | 253 | self.fn, self.opts = tableentry |
|
244 | 254 | |
|
245 | 255 | self.args = aliasargs(self.fn) + args |
|
246 | 256 | if cmd not in commands.norepo.split(' '): |
|
247 | 257 | self.norepo = False |
|
248 | 258 | if self.help.startswith("hg " + cmd): |
|
249 | 259 | # drop prefix in old-style help lines so hg shows the alias |
|
250 | 260 | self.help = self.help[4 + len(cmd):] |
|
251 | 261 | self.__doc__ = self.fn.__doc__ |
|
252 | 262 | |
|
253 | 263 | except error.UnknownCommand: |
|
254 | 264 | def fn(ui, *args): |
|
255 | 265 | ui.warn(_("alias '%s' resolves to unknown command '%s'\n") \ |
|
256 | 266 | % (self.name, cmd)) |
|
257 | 267 | try: |
|
258 | 268 | # check if the command is in a disabled extension |
|
259 | 269 | commands.help_(ui, cmd, unknowncmd=True) |
|
260 | 270 | except error.UnknownCommand: |
|
261 | 271 | pass |
|
262 | 272 | return 1 |
|
263 | 273 | self.fn = fn |
|
264 | 274 | self.badalias = True |
|
265 | 275 | except error.AmbiguousCommand: |
|
266 | 276 | def fn(ui, *args): |
|
267 | 277 | ui.warn(_("alias '%s' resolves to ambiguous command '%s'\n") \ |
|
268 | 278 | % (self.name, cmd)) |
|
269 | 279 | return 1 |
|
270 | 280 | self.fn = fn |
|
271 | 281 | self.badalias = True |
|
272 | 282 | |
|
273 | 283 | def __call__(self, ui, *args, **opts): |
|
274 | 284 | if self.shadows: |
|
275 | 285 | ui.debug("alias '%s' shadows command\n" % self.name) |
|
276 | 286 | |
|
277 | return util.checksignature(self.fn)(ui, *args, **opts) | |
|
287 | if self.definition.startswith('!'): | |
|
288 | return self.fn(ui, *args, **opts) | |
|
289 | else: | |
|
290 | return util.checksignature(self.fn)(ui, *args, **opts) | |
|
278 | 291 | |
|
279 | 292 | def addaliases(ui, cmdtable): |
|
280 | 293 | # aliases are processed after extensions have been loaded, so they |
|
281 | 294 | # may use extension commands. Aliases can also use other alias definitions, |
|
282 | 295 | # but only if they have been defined prior to the current definition. |
|
283 | 296 | for alias, definition in ui.configitems('alias'): |
|
284 | 297 | aliasdef = cmdalias(alias, definition, cmdtable) |
|
285 | 298 | cmdtable[alias] = (aliasdef, aliasdef.opts, aliasdef.help) |
|
286 | 299 | if aliasdef.norepo: |
|
287 | 300 | commands.norepo += ' %s' % alias |
|
288 | 301 | |
|
289 | 302 | def _parse(ui, args): |
|
290 | 303 | options = {} |
|
291 | 304 | cmdoptions = {} |
|
292 | 305 | |
|
293 | 306 | try: |
|
294 | 307 | args = fancyopts.fancyopts(args, commands.globalopts, options) |
|
295 | 308 | except fancyopts.getopt.GetoptError, inst: |
|
296 | 309 | raise error.CommandError(None, inst) |
|
297 | 310 | |
|
298 | 311 | if args: |
|
299 | 312 | cmd, args = args[0], args[1:] |
|
300 | 313 | aliases, entry = cmdutil.findcmd(cmd, commands.table, |
|
301 | 314 | ui.config("ui", "strict")) |
|
302 | 315 | cmd = aliases[0] |
|
303 | 316 | args = aliasargs(entry[0]) + args |
|
304 | 317 | defaults = ui.config("defaults", cmd) |
|
305 | 318 | if defaults: |
|
306 | 319 | args = map(util.expandpath, shlex.split(defaults)) + args |
|
307 | 320 | c = list(entry[1]) |
|
308 | 321 | else: |
|
309 | 322 | cmd = None |
|
310 | 323 | c = [] |
|
311 | 324 | |
|
312 | 325 | # combine global options into local |
|
313 | 326 | for o in commands.globalopts: |
|
314 | 327 | c.append((o[0], o[1], options[o[1]], o[3])) |
|
315 | 328 | |
|
316 | 329 | try: |
|
317 | 330 | args = fancyopts.fancyopts(args, c, cmdoptions, True) |
|
318 | 331 | except fancyopts.getopt.GetoptError, inst: |
|
319 | 332 | raise error.CommandError(cmd, inst) |
|
320 | 333 | |
|
321 | 334 | # separate global options back out |
|
322 | 335 | for o in commands.globalopts: |
|
323 | 336 | n = o[1] |
|
324 | 337 | options[n] = cmdoptions[n] |
|
325 | 338 | del cmdoptions[n] |
|
326 | 339 | |
|
327 | 340 | return (cmd, cmd and entry[0] or None, args, options, cmdoptions) |
|
328 | 341 | |
|
329 | 342 | def _parseconfig(ui, config): |
|
330 | 343 | """parse the --config options from the command line""" |
|
331 | 344 | for cfg in config: |
|
332 | 345 | try: |
|
333 | 346 | name, value = cfg.split('=', 1) |
|
334 | 347 | section, name = name.split('.', 1) |
|
335 | 348 | if not section or not name: |
|
336 | 349 | raise IndexError |
|
337 | 350 | ui.setconfig(section, name, value) |
|
338 | 351 | except (IndexError, ValueError): |
|
339 | 352 | raise util.Abort(_('malformed --config option: %r ' |
|
340 | 353 | '(use --config section.name=value)') % cfg) |
|
341 | 354 | |
|
342 | 355 | def _earlygetopt(aliases, args): |
|
343 | 356 | """Return list of values for an option (or aliases). |
|
344 | 357 | |
|
345 | 358 | The values are listed in the order they appear in args. |
|
346 | 359 | The options and values are removed from args. |
|
347 | 360 | """ |
|
348 | 361 | try: |
|
349 | 362 | argcount = args.index("--") |
|
350 | 363 | except ValueError: |
|
351 | 364 | argcount = len(args) |
|
352 | 365 | shortopts = [opt for opt in aliases if len(opt) == 2] |
|
353 | 366 | values = [] |
|
354 | 367 | pos = 0 |
|
355 | 368 | while pos < argcount: |
|
356 | 369 | if args[pos] in aliases: |
|
357 | 370 | if pos + 1 >= argcount: |
|
358 | 371 | # ignore and let getopt report an error if there is no value |
|
359 | 372 | break |
|
360 | 373 | del args[pos] |
|
361 | 374 | values.append(args.pop(pos)) |
|
362 | 375 | argcount -= 2 |
|
363 | 376 | elif args[pos][:2] in shortopts: |
|
364 | 377 | # short option can have no following space, e.g. hg log -Rfoo |
|
365 | 378 | values.append(args.pop(pos)[2:]) |
|
366 | 379 | argcount -= 1 |
|
367 | 380 | else: |
|
368 | 381 | pos += 1 |
|
369 | 382 | return values |
|
370 | 383 | |
|
371 | 384 | def runcommand(lui, repo, cmd, fullargs, ui, options, d, cmdpats, cmdoptions): |
|
372 | 385 | # run pre-hook, and abort if it fails |
|
373 | 386 | ret = hook.hook(lui, repo, "pre-%s" % cmd, False, args=" ".join(fullargs), |
|
374 | 387 | pats=cmdpats, opts=cmdoptions) |
|
375 | 388 | if ret: |
|
376 | 389 | return ret |
|
377 | 390 | ret = _runcommand(ui, options, cmd, d) |
|
378 | 391 | # run post-hook, passing command result |
|
379 | 392 | hook.hook(lui, repo, "post-%s" % cmd, False, args=" ".join(fullargs), |
|
380 | 393 | result=ret, pats=cmdpats, opts=cmdoptions) |
|
381 | 394 | return ret |
|
382 | 395 | |
|
383 | 396 | _loaded = set() |
|
384 | 397 | def _dispatch(ui, args): |
|
385 | 398 | # read --config before doing anything else |
|
386 | 399 | # (e.g. to change trust settings for reading .hg/hgrc) |
|
387 | 400 | _parseconfig(ui, _earlygetopt(['--config'], args)) |
|
388 | 401 | |
|
389 | 402 | # check for cwd |
|
390 | 403 | cwd = _earlygetopt(['--cwd'], args) |
|
391 | 404 | if cwd: |
|
392 | 405 | os.chdir(cwd[-1]) |
|
393 | 406 | |
|
394 | 407 | # read the local repository .hgrc into a local ui object |
|
395 | 408 | try: |
|
396 | 409 | wd = os.getcwd() |
|
397 | 410 | except OSError, e: |
|
398 | 411 | raise util.Abort(_("error getting current working directory: %s") % |
|
399 | 412 | e.strerror) |
|
400 | 413 | path = cmdutil.findrepo(wd) or "" |
|
401 | 414 | if not path: |
|
402 | 415 | lui = ui |
|
403 | 416 | else: |
|
404 | 417 | try: |
|
405 | 418 | lui = ui.copy() |
|
406 | 419 | lui.readconfig(os.path.join(path, ".hg", "hgrc")) |
|
407 | 420 | except IOError: |
|
408 | 421 | pass |
|
409 | 422 | |
|
410 | 423 | # now we can expand paths, even ones in .hg/hgrc |
|
411 | 424 | rpath = _earlygetopt(["-R", "--repository", "--repo"], args) |
|
412 | 425 | if rpath: |
|
413 | 426 | path = lui.expandpath(rpath[-1]) |
|
414 | 427 | lui = ui.copy() |
|
415 | 428 | lui.readconfig(os.path.join(path, ".hg", "hgrc")) |
|
416 | 429 | |
|
417 | 430 | # Configure extensions in phases: uisetup, extsetup, cmdtable, and |
|
418 | 431 | # reposetup. Programs like TortoiseHg will call _dispatch several |
|
419 | 432 | # times so we keep track of configured extensions in _loaded. |
|
420 | 433 | extensions.loadall(lui) |
|
421 | 434 | exts = [ext for ext in extensions.extensions() if ext[0] not in _loaded] |
|
422 | 435 | # Propagate any changes to lui.__class__ by extensions |
|
423 | 436 | ui.__class__ = lui.__class__ |
|
424 | 437 | |
|
425 | 438 | # (uisetup and extsetup are handled in extensions.loadall) |
|
426 | 439 | |
|
427 | 440 | for name, module in exts: |
|
428 | 441 | cmdtable = getattr(module, 'cmdtable', {}) |
|
429 | 442 | overrides = [cmd for cmd in cmdtable if cmd in commands.table] |
|
430 | 443 | if overrides: |
|
431 | 444 | ui.warn(_("extension '%s' overrides commands: %s\n") |
|
432 | 445 | % (name, " ".join(overrides))) |
|
433 | 446 | commands.table.update(cmdtable) |
|
434 | 447 | _loaded.add(name) |
|
435 | 448 | |
|
436 | 449 | # (reposetup is handled in hg.repository) |
|
437 | 450 | |
|
438 | 451 | addaliases(lui, commands.table) |
|
439 | 452 | |
|
440 | 453 | # check for fallback encoding |
|
441 | 454 | fallback = lui.config('ui', 'fallbackencoding') |
|
442 | 455 | if fallback: |
|
443 | 456 | encoding.fallbackencoding = fallback |
|
444 | 457 | |
|
445 | 458 | fullargs = args |
|
446 | 459 | cmd, func, args, options, cmdoptions = _parse(lui, args) |
|
447 | 460 | |
|
448 | 461 | if options["config"]: |
|
449 | 462 | raise util.Abort(_("Option --config may not be abbreviated!")) |
|
450 | 463 | if options["cwd"]: |
|
451 | 464 | raise util.Abort(_("Option --cwd may not be abbreviated!")) |
|
452 | 465 | if options["repository"]: |
|
453 | 466 | raise util.Abort(_( |
|
454 | 467 | "Option -R has to be separated from other options (e.g. not -qR) " |
|
455 | 468 | "and --repository may only be abbreviated as --repo!")) |
|
456 | 469 | |
|
457 | 470 | if options["encoding"]: |
|
458 | 471 | encoding.encoding = options["encoding"] |
|
459 | 472 | if options["encodingmode"]: |
|
460 | 473 | encoding.encodingmode = options["encodingmode"] |
|
461 | 474 | if options["time"]: |
|
462 | 475 | def get_times(): |
|
463 | 476 | t = os.times() |
|
464 | 477 | if t[4] == 0.0: # Windows leaves this as zero, so use time.clock() |
|
465 | 478 | t = (t[0], t[1], t[2], t[3], time.clock()) |
|
466 | 479 | return t |
|
467 | 480 | s = get_times() |
|
468 | 481 | def print_time(): |
|
469 | 482 | t = get_times() |
|
470 | 483 | ui.warn(_("Time: real %.3f secs (user %.3f+%.3f sys %.3f+%.3f)\n") % |
|
471 | 484 | (t[4]-s[4], t[0]-s[0], t[2]-s[2], t[1]-s[1], t[3]-s[3])) |
|
472 | 485 | atexit.register(print_time) |
|
473 | 486 | |
|
474 | 487 | if options['verbose'] or options['debug'] or options['quiet']: |
|
475 | 488 | ui.setconfig('ui', 'verbose', str(bool(options['verbose']))) |
|
476 | 489 | ui.setconfig('ui', 'debug', str(bool(options['debug']))) |
|
477 | 490 | ui.setconfig('ui', 'quiet', str(bool(options['quiet']))) |
|
478 | 491 | if options['traceback']: |
|
479 | 492 | ui.setconfig('ui', 'traceback', 'on') |
|
480 | 493 | if options['noninteractive']: |
|
481 | 494 | ui.setconfig('ui', 'interactive', 'off') |
|
482 | 495 | |
|
483 | 496 | if options['help']: |
|
484 | 497 | return commands.help_(ui, cmd, options['version']) |
|
485 | 498 | elif options['version']: |
|
486 | 499 | return commands.version_(ui) |
|
487 | 500 | elif not cmd: |
|
488 | 501 | return commands.help_(ui, 'shortlist') |
|
489 | 502 | |
|
490 | 503 | repo = None |
|
491 | 504 | cmdpats = args[:] |
|
492 | 505 | if cmd not in commands.norepo.split(): |
|
493 | 506 | try: |
|
494 | 507 | repo = hg.repository(ui, path=path) |
|
495 | 508 | ui = repo.ui |
|
496 | 509 | if not repo.local(): |
|
497 | 510 | raise util.Abort(_("repository '%s' is not local") % path) |
|
498 | 511 | ui.setconfig("bundle", "mainreporoot", repo.root) |
|
499 | 512 | except error.RepoError: |
|
500 | 513 | if cmd not in commands.optionalrepo.split(): |
|
501 | 514 | if args and not path: # try to infer -R from command args |
|
502 | 515 | repos = map(cmdutil.findrepo, args) |
|
503 | 516 | guess = repos[0] |
|
504 | 517 | if guess and repos.count(guess) == len(repos): |
|
505 | 518 | return _dispatch(ui, ['--repository', guess] + fullargs) |
|
506 | 519 | if not path: |
|
507 | 520 | raise error.RepoError(_("There is no Mercurial repository" |
|
508 | 521 | " here (.hg not found)")) |
|
509 | 522 | raise |
|
510 | 523 | args.insert(0, repo) |
|
511 | 524 | elif rpath: |
|
512 | 525 | ui.warn(_("warning: --repository ignored\n")) |
|
513 | 526 | |
|
514 | 527 | msg = ' '.join(' ' in a and repr(a) or a for a in fullargs) |
|
515 | 528 | ui.log("command", msg + "\n") |
|
516 | 529 | d = lambda: util.checksignature(func)(ui, *args, **cmdoptions) |
|
517 | 530 | return runcommand(lui, repo, cmd, fullargs, ui, options, d, |
|
518 | 531 | cmdpats, cmdoptions) |
|
519 | 532 | |
|
520 | 533 | def _runcommand(ui, options, cmd, cmdfunc): |
|
521 | 534 | def checkargs(): |
|
522 | 535 | try: |
|
523 | 536 | return cmdfunc() |
|
524 | 537 | except error.SignatureError: |
|
525 | 538 | raise error.CommandError(cmd, _("invalid arguments")) |
|
526 | 539 | |
|
527 | 540 | if options['profile']: |
|
528 | 541 | format = ui.config('profiling', 'format', default='text') |
|
529 | 542 | |
|
530 | 543 | if not format in ['text', 'kcachegrind']: |
|
531 | 544 | ui.warn(_("unrecognized profiling format '%s'" |
|
532 | 545 | " - Ignored\n") % format) |
|
533 | 546 | format = 'text' |
|
534 | 547 | |
|
535 | 548 | output = ui.config('profiling', 'output') |
|
536 | 549 | |
|
537 | 550 | if output: |
|
538 | 551 | path = ui.expandpath(output) |
|
539 | 552 | ostream = open(path, 'wb') |
|
540 | 553 | else: |
|
541 | 554 | ostream = sys.stderr |
|
542 | 555 | |
|
543 | 556 | try: |
|
544 | 557 | from mercurial import lsprof |
|
545 | 558 | except ImportError: |
|
546 | 559 | raise util.Abort(_( |
|
547 | 560 | 'lsprof not available - install from ' |
|
548 | 561 | 'http://codespeak.net/svn/user/arigo/hack/misc/lsprof/')) |
|
549 | 562 | p = lsprof.Profiler() |
|
550 | 563 | p.enable(subcalls=True) |
|
551 | 564 | try: |
|
552 | 565 | return checkargs() |
|
553 | 566 | finally: |
|
554 | 567 | p.disable() |
|
555 | 568 | |
|
556 | 569 | if format == 'kcachegrind': |
|
557 | 570 | import lsprofcalltree |
|
558 | 571 | calltree = lsprofcalltree.KCacheGrind(p) |
|
559 | 572 | calltree.output(ostream) |
|
560 | 573 | else: |
|
561 | 574 | # format == 'text' |
|
562 | 575 | stats = lsprof.Stats(p.getstats()) |
|
563 | 576 | stats.sort() |
|
564 | 577 | stats.pprint(top=10, file=ostream, climit=5) |
|
565 | 578 | |
|
566 | 579 | if output: |
|
567 | 580 | ostream.close() |
|
568 | 581 | else: |
|
569 | 582 | return checkargs() |
@@ -1,170 +1,201 | |||
|
1 | 1 | $ cat >> $HGRCPATH <<EOF |
|
2 | 2 | > [alias] |
|
3 | 3 | > myinit = init |
|
4 | 4 | > cleanstatus = status -c |
|
5 | 5 | > unknown = bargle |
|
6 | 6 | > ambiguous = s |
|
7 | 7 | > recursive = recursive |
|
8 | 8 | > nodefinition = |
|
9 | 9 | > no--cwd = status --cwd elsewhere |
|
10 | 10 | > no-R = status -R elsewhere |
|
11 | 11 | > no--repo = status --repo elsewhere |
|
12 | 12 | > no--repository = status --repository elsewhere |
|
13 | 13 | > mylog = log |
|
14 | 14 | > lognull = log -r null |
|
15 | 15 | > shortlog = log --template '{rev} {node|short} | {date|isodate}\n' |
|
16 | 16 | > dln = lognull --debug |
|
17 | 17 | > nousage = rollback |
|
18 | 18 | > put = export -r 0 -o "\$FOO/%R.diff" |
|
19 |
> |
|
|
19 | > blank = !echo | |
|
20 | > self = !echo '\$0' | |
|
21 | > echo = !echo '\$@' | |
|
22 | > echo1 = !echo '\$1' | |
|
23 | > echo2 = !echo '\$2' | |
|
24 | > echo13 = !echo '\$1' '\$3' | |
|
25 | > count = !hg log -r '\$@' --template='.' | wc -c | sed -e 's/ //g' | |
|
20 | 26 | > rt = root |
|
21 | 27 | > |
|
22 | 28 | > [defaults] |
|
23 | 29 | > mylog = -q |
|
24 | 30 | > lognull = -q |
|
25 | 31 | > log = -v |
|
26 | 32 | > EOF |
|
27 | 33 | |
|
28 | 34 | |
|
29 | 35 | basic |
|
30 | 36 | |
|
31 | 37 | $ hg myinit alias |
|
32 | 38 | |
|
33 | 39 | |
|
34 | 40 | unknown |
|
35 | 41 | |
|
36 | 42 | $ hg unknown |
|
37 | 43 | alias 'unknown' resolves to unknown command 'bargle' |
|
38 | 44 | $ hg help unknown |
|
39 | 45 | alias 'unknown' resolves to unknown command 'bargle' |
|
40 | 46 | |
|
41 | 47 | |
|
42 | 48 | ambiguous |
|
43 | 49 | |
|
44 | 50 | $ hg ambiguous |
|
45 | 51 | alias 'ambiguous' resolves to ambiguous command 's' |
|
46 | 52 | $ hg help ambiguous |
|
47 | 53 | alias 'ambiguous' resolves to ambiguous command 's' |
|
48 | 54 | |
|
49 | 55 | |
|
50 | 56 | recursive |
|
51 | 57 | |
|
52 | 58 | $ hg recursive |
|
53 | 59 | alias 'recursive' resolves to unknown command 'recursive' |
|
54 | 60 | $ hg help recursive |
|
55 | 61 | alias 'recursive' resolves to unknown command 'recursive' |
|
56 | 62 | |
|
57 | 63 | |
|
58 | 64 | no definition |
|
59 | 65 | |
|
60 | 66 | $ hg nodef |
|
61 | 67 | no definition for alias 'nodefinition' |
|
62 | 68 | $ hg help nodef |
|
63 | 69 | no definition for alias 'nodefinition' |
|
64 | 70 | |
|
65 | 71 | |
|
66 | 72 | invalid options |
|
67 | 73 | |
|
68 | 74 | $ hg no--cwd |
|
69 | 75 | error in definition for alias 'no--cwd': --cwd may only be given on the command line |
|
70 | 76 | $ hg help no--cwd |
|
71 | 77 | error in definition for alias 'no--cwd': --cwd may only be given on the command line |
|
72 | 78 | $ hg no-R |
|
73 | 79 | error in definition for alias 'no-R': -R may only be given on the command line |
|
74 | 80 | $ hg help no-R |
|
75 | 81 | error in definition for alias 'no-R': -R may only be given on the command line |
|
76 | 82 | $ hg no--repo |
|
77 | 83 | error in definition for alias 'no--repo': --repo may only be given on the command line |
|
78 | 84 | $ hg help no--repo |
|
79 | 85 | error in definition for alias 'no--repo': --repo may only be given on the command line |
|
80 | 86 | $ hg no--repository |
|
81 | 87 | error in definition for alias 'no--repository': --repository may only be given on the command line |
|
82 | 88 | $ hg help no--repository |
|
83 | 89 | error in definition for alias 'no--repository': --repository may only be given on the command line |
|
84 | 90 | |
|
85 | 91 | $ cd alias |
|
86 | 92 | |
|
87 | 93 | |
|
88 | 94 | no usage |
|
89 | 95 | |
|
90 | 96 | $ hg nousage |
|
91 | 97 | no rollback information available |
|
92 | 98 | |
|
93 | 99 | $ echo foo > foo |
|
94 | 100 | $ hg ci -Amfoo |
|
95 | 101 | adding foo |
|
96 | 102 | |
|
97 | 103 | |
|
98 | 104 | with opts |
|
99 | 105 | |
|
100 | 106 | $ hg cleanst |
|
101 | 107 | C foo |
|
102 | 108 | |
|
103 | 109 | |
|
104 | 110 | with opts and whitespace |
|
105 | 111 | |
|
106 | 112 | $ hg shortlog |
|
107 | 113 | 0 e63c23eaa88a | 1970-01-01 00:00 +0000 |
|
108 | 114 | |
|
109 | 115 | |
|
110 | 116 | interaction with defaults |
|
111 | 117 | |
|
112 | 118 | $ hg mylog |
|
113 | 119 | 0:e63c23eaa88a |
|
114 | 120 | $ hg lognull |
|
115 | 121 | -1:000000000000 |
|
116 | 122 | |
|
117 | 123 | |
|
118 | 124 | properly recursive |
|
119 | 125 | |
|
120 | 126 | $ hg dln |
|
121 | 127 | changeset: -1:0000000000000000000000000000000000000000 |
|
122 | 128 | parent: -1:0000000000000000000000000000000000000000 |
|
123 | 129 | parent: -1:0000000000000000000000000000000000000000 |
|
124 | 130 | manifest: -1:0000000000000000000000000000000000000000 |
|
125 | 131 | user: |
|
126 | 132 | date: Thu Jan 01 00:00:00 1970 +0000 |
|
127 | 133 | extra: branch=default |
|
128 | 134 | |
|
129 | 135 | |
|
130 | 136 | |
|
131 | 137 | path expanding |
|
132 | 138 | |
|
133 | 139 | $ FOO=`pwd` hg put |
|
134 | 140 | $ cat 0.diff |
|
135 | 141 | # HG changeset patch |
|
136 | 142 | # User test |
|
137 | 143 | # Date 0 0 |
|
138 | 144 | # Node ID e63c23eaa88ae77967edcf4ea194d31167c478b0 |
|
139 | 145 | # Parent 0000000000000000000000000000000000000000 |
|
140 | 146 | foo |
|
141 | 147 | |
|
142 | 148 | diff -r 000000000000 -r e63c23eaa88a foo |
|
143 | 149 | --- /dev/null Thu Jan 01 00:00:00 1970 +0000 |
|
144 | 150 | +++ b/foo Thu Jan 01 00:00:00 1970 +0000 |
|
145 | 151 | @@ -0,0 +1,1 @@ |
|
146 | 152 | +foo |
|
147 | 153 | |
|
148 | 154 | |
|
149 | shell aliases | |
|
155 | simple shell aliases | |
|
150 | 156 | |
|
157 | $ hg blank | |
|
158 | ||
|
159 | $ hg blank foo | |
|
160 | ||
|
161 | $ hg echo | |
|
162 | ||
|
163 | $ hg self | |
|
164 | self | |
|
151 | 165 | $ hg echo foo |
|
152 | 166 | foo |
|
167 | $ hg echo 'test $2' foo | |
|
168 | test $2 foo | |
|
169 | $ hg echo1 foo bar baz | |
|
170 | foo | |
|
171 | $ hg echo2 foo bar baz | |
|
172 | bar | |
|
173 | $ hg echo13 foo bar baz test | |
|
174 | foo baz | |
|
175 | $ hg echo2 foo | |
|
176 | ||
|
177 | $ echo bar > bar | |
|
178 | $ hg ci -qA -m bar | |
|
179 | $ hg count . | |
|
180 | 1 | |
|
181 | $ hg count 'branch(default)' | |
|
182 | 2 | |
|
183 | ||
|
153 | 184 | |
|
154 | 185 | invalid arguments |
|
155 | 186 | |
|
156 | 187 | $ hg rt foo |
|
157 | 188 | hg rt: invalid arguments |
|
158 | 189 | hg rt |
|
159 | 190 | |
|
160 | 191 | alias for: hg root |
|
161 | 192 | |
|
162 | 193 | print the root (top) of the current working directory |
|
163 | 194 | |
|
164 | 195 | Print the root directory of the current repository. |
|
165 | 196 | |
|
166 | 197 | Returns 0 on success. |
|
167 | 198 | |
|
168 | 199 | use "hg -v help rt" to show global options |
|
169 | 200 | |
|
170 | 201 | $ exit 0 |
General Comments 0
You need to be logged in to leave comments.
Login now