sapling/hgext/debugshell.py
Jun Wu 109488ef6b debugshell: assign m to be mercurial
Summary:
Since we have other short variables like `cl`, `mf`. It makes sense to also
assign `m` as a shortcut to `mercurial`. It's handy to test methods like
`m.mdiff.allblocks` etc.

Reviewed By: markbt

Differential Revision: D7627705

fbshipit-source-id: 167bdef9cd6319d233b18aa0ad046a3055e92ffb
2018-04-17 16:35:25 -07:00

63 lines
1.5 KiB
Python

# debugshell extension
"""a python shell with repo, changelog & manifest objects"""
from __future__ import absolute_import
import code
import sys
import mercurial
from mercurial import (
demandimport,
registrar,
)
cmdtable = {}
command = registrar.command(cmdtable)
def _assignobjects(objects, repo):
objects.update({
'm': mercurial,
'mercurial': mercurial,
})
if repo:
objects.update({
'repo': repo,
'cl': repo.changelog,
'mf': repo.manifestlog,
})
def pdb(ui, repo, msg, **opts):
objects = {}
_assignobjects(objects, repo)
code.interact(msg, local=objects)
def ipdb(ui, repo, msg, **opts):
import IPython
_assignobjects(locals(), repo)
IPython.embed()
@command('debugshell|dbsh', [], optionalrepo=True)
def debugshell(ui, repo, **opts):
bannermsg = "loaded repo : %s\n" \
"using source: %s" % (repo and repo.root or '(none)',
mercurial.__path__[0])
pdbmap = {
'pdb' : 'code',
'ipdb' : 'IPython'
}
debugger = ui.config("ui", "debugger")
if not debugger:
debugger = 'pdb'
# if IPython doesn't exist, fallback to code.interact
try:
with demandimport.deactivated():
__import__(pdbmap[debugger])
except ImportError:
ui.warn(("%s debugger specified but %s module was not found\n")
% (debugger, pdbmap[debugger]))
debugger = 'pdb'
getattr(sys.modules[__name__], debugger)(ui, repo, bannermsg, **opts)