diff --git a/hgext/blackbox.py b/hgext/blackbox.py new file mode 100644 --- /dev/null +++ b/hgext/blackbox.py @@ -0,0 +1,38 @@ +"""log commands ran in the repository and exceptions encountered + +Appends to .hg/blackbox events happening in the repository to help +debugging and diagnosis. Each command that is run in the repository, +or each exception encountered will be logged. +""" + +from mercurial.i18n import _ +from mercurial import extensions, dispatch +import traceback + +def runcommand(orig, lui, repo, cmd, fullargs, ui, options, d, cmdpats, cmdoptions): + if repo is None: + # not much we can log + return orig(lui, repo, cmd, fullargs, ui, options, d, cmdpats, cmdoptions) + blackboxfile = repo.opener('blackbox', 'a') + try: + blackboxfile.write('%s\n' % ' '.join(fullargs)) + try: + return orig(lui, repo, cmd, fullargs, ui, options, d, cmdpats, cmdoptions) + except: + traceback.print_exc(None, blackboxfile) + raise + finally: + blackboxfile.close() + +def incoming(ui, repo, hooktype, node, source, url): + blackboxfile = repo.opener('blackbox', 'a') + try: + blackboxfile.write('incoming: %s: %s from %s\n' % (source, node[:12], url)) + finally: + blackboxfile.close() + +def uisetup(ui): + extensions.wrapfunction(dispatch, 'runcommand', runcommand) +def reposetup(ui, repo): + ui.setconfig('hooks', 'incoming.blackbox', incoming) + diff --git a/tests/test-blackbox.t b/tests/test-blackbox.t new file mode 100644 --- /dev/null +++ b/tests/test-blackbox.t @@ -0,0 +1,60 @@ + $ echo '[extensions]' >> $HGRCPATH + $ echo 'blackbox =' >> $HGRCPATH + +Create a repo with a dummy changeset + + $ hg init foo; cd foo + $ touch a + $ hg ci -Am'a' + adding a + $ hg clone . ../bar + updating to branch default + 1 files updated, 0 files merged, 0 files removed, 0 files unresolved + +And pretend we push to remote + + $ echo foo > a + $ hg ci -Am'modify a' + $ hg push ../bar + pushing to ../bar + searching for changes + adding changesets + adding manifests + adding file changes + added 1 changesets with 1 changes to 1 files + +Log a few commands + + $ cd ../bar + $ hg foo > /dev/null + hg: unknown command 'foo' + [255] + $ hg st + $ hg diff -c notexisting + abort: unknown revision 'notexisting'! + [255] + +And check that both the incoming hook, exceptions and commands are logged: + + $ cat .hg/blackbox + incoming: push: c3cdbe7bbdc4 from file:$TESTTMP/foo + st + diff -c notexisting + Traceback (most recent call last): + File "*/hgext/blackbox.py", line *, in runcommand (glob) + return orig(lui, repo, cmd, fullargs, ui, options, d, cmdpats, cmdoptions) + File "*/mercurial/dispatch.py", line *, in runcommand (glob) + ret = _runcommand(ui, options, cmd, d) + File "*/mercurial/dispatch.py", line *, in _runcommand (glob) + return checkargs() + File "*/mercurial/dispatch.py", line *, in checkargs (glob) + return cmdfunc() + File "*/mercurial/dispatch.py", line *, in (glob) + d = lambda: util.checksignature(func)(ui, *args, **cmdoptions) + File "*/mercurial/util.py", line *, in check (glob) + return func(*args, **kwargs) + File "*/mercurial/commands.py", line *, in diff (glob) + node2 = repo.lookup(change) + File "*/mercurial/localrepo.py", line *, in lookup (glob) + raise error.RepoLookupError(_("unknown revision '%s'") % key) + RepoLookupError: unknown revision 'notexisting'