contrib/debugshell.py
author Yuya Nishihara <yuya@tcha.org>
Sun, 24 May 2015 14:49:41 +0900
changeset 25341 9d6cc87bd507
parent 21243 8b5c039f2b4f
child 27721 e4b512bb6386
permissions -rw-r--r--
revset: make internal _list() expression remove duplicated revisions This allows us to optimize chained 'or' operations to _list() expression. Unlike _intlist() or _hexlist(), it's difficult to remove duplicates by the caller of _list() because different symbols can point to the same revision. If the caller knows all symbols are unique, that probably means revisions or nodes are known, therefore, _intlist() or _hexlist() should be used instead. So, it makes sense to check duplicates by _list() function. '%ls' is no longer used in core, this won't cause performance regression.

# debugshell extension
"""a python shell with repo, changelog & manifest objects"""

import sys
import mercurial
import code
from mercurial import cmdutil

cmdtable = {}
command = cmdutil.command(cmdtable)

def pdb(ui, repo, msg, **opts):
    objects = {
        'mercurial': mercurial,
        'repo': repo,
        'cl': repo.changelog,
        'mf': repo.manifest,
    }

    code.interact(msg, local=objects)

def ipdb(ui, repo, msg, **opts):
    import IPython

    cl = repo.changelog
    mf = repo.manifest
    cl, mf # use variables to appease pyflakes

    IPython.embed()

@command('debugshell|dbsh', [])
def debugshell(ui, repo, **opts):
    bannermsg = "loaded repo : %s\n" \
                "using source: %s" % (repo.root,
                                      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:
        __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)