comparison mercurial/commands.py @ 2057:fef2d653beaf

Never exit directly from commands.dispatch(), but pass return code to caller. Usually the caller is commands.run(). Some extensions still use sys.exit(), this is catched, too. Fixed wrong return statement in commands.recover() yielding a zero exit code.
author Thomas Arendsen Hein <thomas@intevation.de>
date Tue, 11 Apr 2006 08:42:07 +0200
parents 1f6d520557ec
children f71e9656524f
comparison
equal deleted inserted replaced
2056:1f6d520557ec 2057:fef2d653beaf
2256 This command tries to fix the repository status after an interrupted 2256 This command tries to fix the repository status after an interrupted
2257 operation. It should only be necessary when Mercurial suggests it. 2257 operation. It should only be necessary when Mercurial suggests it.
2258 """ 2258 """
2259 if repo.recover(): 2259 if repo.recover():
2260 return repo.verify() 2260 return repo.verify()
2261 return False 2261 return 1
2262 2262
2263 def remove(ui, repo, pat, *pats, **opts): 2263 def remove(ui, repo, pat, *pats, **opts):
2264 """remove the specified files on the next commit 2264 """remove the specified files on the next commit
2265 2265
2266 Schedule the indicated files for removal from the repository. 2266 Schedule the indicated files for removal from the repository.
3256 3256
3257 try: 3257 try:
3258 u = ui.ui() 3258 u = ui.ui()
3259 except util.Abort, inst: 3259 except util.Abort, inst:
3260 sys.stderr.write(_("abort: %s\n") % inst) 3260 sys.stderr.write(_("abort: %s\n") % inst)
3261 sys.exit(1) 3261 return -1
3262 3262
3263 external = [] 3263 external = []
3264 for x in u.extensions(): 3264 for x in u.extensions():
3265 def on_exception(exc, inst): 3265 try:
3266 u.warn(_("*** failed to import extension %s: %s\n") % (x[0], inst)) 3266 if x[1]:
3267 if "--traceback" in sys.argv[1:]:
3268 traceback.print_exc()
3269 sys.exit(0)
3270 if x[1]:
3271 try:
3272 mod = imp.load_source(x[0], x[1]) 3267 mod = imp.load_source(x[0], x[1])
3273 except Exception, inst: 3268 else:
3274 on_exception(Exception, inst) 3269 def importh(name):
3275 continue 3270 mod = __import__(name)
3276 else: 3271 components = name.split('.')
3277 def importh(name): 3272 for comp in components[1:]:
3278 mod = __import__(name) 3273 mod = getattr(mod, comp)
3279 components = name.split('.') 3274 return mod
3280 for comp in components[1:]:
3281 mod = getattr(mod, comp)
3282 return mod
3283 try:
3284 try: 3275 try:
3285 mod = importh("hgext." + x[0]) 3276 mod = importh("hgext." + x[0])
3286 except ImportError: 3277 except ImportError:
3287 mod = importh(x[0]) 3278 mod = importh(x[0])
3288 except Exception, inst: 3279 external.append(mod)
3289 on_exception(Exception, inst) 3280 except Exception, inst:
3290 continue 3281 u.warn(_("*** failed to import extension %s: %s\n") % (x[0], inst))
3291 3282 if "--traceback" in sys.argv[1:]:
3292 external.append(mod) 3283 traceback.print_exc()
3284 return 1
3285 continue
3286
3293 for x in external: 3287 for x in external:
3294 cmdtable = getattr(x, 'cmdtable', {}) 3288 cmdtable = getattr(x, 'cmdtable', {})
3295 for t in cmdtable: 3289 for t in cmdtable:
3296 if t in table: 3290 if t in table:
3297 u.warn(_("module %s overrides %s\n") % (x.__name__, t)) 3291 u.warn(_("module %s overrides %s\n") % (x.__name__, t))
3329 3323
3330 path = u.expandpath(options["repository"]) or "" 3324 path = u.expandpath(options["repository"]) or ""
3331 repo = path and hg.repository(u, path=path) or None 3325 repo = path and hg.repository(u, path=path) or None
3332 3326
3333 if options['help']: 3327 if options['help']:
3334 help_(u, cmd, options['version']) 3328 return help_(u, cmd, options['version'])
3335 sys.exit(0)
3336 elif options['version']: 3329 elif options['version']:
3337 show_version(u) 3330 return show_version(u)
3338 sys.exit(0)
3339 elif not cmd: 3331 elif not cmd:
3340 help_(u, 'shortlist') 3332 return help_(u, 'shortlist')
3341 sys.exit(0)
3342 3333
3343 if cmd not in norepo.split(): 3334 if cmd not in norepo.split():
3344 try: 3335 try:
3345 if not repo: 3336 if not repo:
3346 repo = hg.repository(u, path=path) 3337 repo = hg.repository(u, path=path)
3391 u.warn(_("hg %s: %s\n") % (inst.args[0], inst.args[1])) 3382 u.warn(_("hg %s: %s\n") % (inst.args[0], inst.args[1]))
3392 help_(u, inst.args[0]) 3383 help_(u, inst.args[0])
3393 else: 3384 else:
3394 u.warn(_("hg: %s\n") % inst.args[1]) 3385 u.warn(_("hg: %s\n") % inst.args[1])
3395 help_(u, 'shortlist') 3386 help_(u, 'shortlist')
3396 sys.exit(-1)
3397 except AmbiguousCommand, inst: 3387 except AmbiguousCommand, inst:
3398 u.warn(_("hg: command '%s' is ambiguous:\n %s\n") % 3388 u.warn(_("hg: command '%s' is ambiguous:\n %s\n") %
3399 (inst.args[0], " ".join(inst.args[1]))) 3389 (inst.args[0], " ".join(inst.args[1])))
3400 sys.exit(1)
3401 except UnknownCommand, inst: 3390 except UnknownCommand, inst:
3402 u.warn(_("hg: unknown command '%s'\n") % inst.args[0]) 3391 u.warn(_("hg: unknown command '%s'\n") % inst.args[0])
3403 help_(u, 'shortlist') 3392 help_(u, 'shortlist')
3404 sys.exit(1)
3405 except hg.RepoError, inst: 3393 except hg.RepoError, inst:
3406 u.warn(_("abort: "), inst, "!\n") 3394 u.warn(_("abort: "), inst, "!\n")
3407 except lock.LockHeld, inst: 3395 except lock.LockHeld, inst:
3408 if inst.errno == errno.ETIMEDOUT: 3396 if inst.errno == errno.ETIMEDOUT:
3409 reason = _('timed out waiting for lock held by %s') % inst.locker 3397 reason = _('timed out waiting for lock held by %s') % inst.locker
3446 u.warn(_("abort: %s: %s\n") % (inst.strerror, inst.filename)) 3434 u.warn(_("abort: %s: %s\n") % (inst.strerror, inst.filename))
3447 else: 3435 else:
3448 u.warn(_("abort: %s\n") % inst.strerror) 3436 u.warn(_("abort: %s\n") % inst.strerror)
3449 except util.Abort, inst: 3437 except util.Abort, inst:
3450 u.warn(_('abort: '), inst.args[0] % inst.args[1:], '\n') 3438 u.warn(_('abort: '), inst.args[0] % inst.args[1:], '\n')
3451 sys.exit(1)
3452 except TypeError, inst: 3439 except TypeError, inst:
3453 # was this an argument error? 3440 # was this an argument error?
3454 tb = traceback.extract_tb(sys.exc_info()[2]) 3441 tb = traceback.extract_tb(sys.exc_info()[2])
3455 if len(tb) > 2: # no 3442 if len(tb) > 2: # no
3456 raise 3443 raise
3457 u.debug(inst, "\n") 3444 u.debug(inst, "\n")
3458 u.warn(_("%s: invalid arguments\n") % cmd) 3445 u.warn(_("%s: invalid arguments\n") % cmd)
3459 help_(u, cmd) 3446 help_(u, cmd)
3460 except SystemExit: 3447 except SystemExit, inst:
3461 # don't catch this in the catch-all below 3448 # Commands shouldn't sys.exit directly, but give a return code.
3462 raise 3449 # Just in case catch this and and pass exit code to caller.
3450 return inst.code
3463 except: 3451 except:
3464 u.warn(_("** unknown exception encountered, details follow\n")) 3452 u.warn(_("** unknown exception encountered, details follow\n"))
3465 u.warn(_("** report bug details to mercurial@selenic.com\n")) 3453 u.warn(_("** report bug details to mercurial@selenic.com\n"))
3466 u.warn(_("** Mercurial Distributed SCM (version %s)\n") 3454 u.warn(_("** Mercurial Distributed SCM (version %s)\n")
3467 % version.get_version()) 3455 % version.get_version())
3468 raise 3456 raise
3469 3457
3470 sys.exit(-1) 3458 return -1