All pastes #1965574 Raw Edit

nicdumz

public text v1 · immutable
#1965574 ·published 2010-10-18 07:57 UTC
rendered paste body
# HG changeset patch# User Nicolas Dumazet <nicdumz.commits@gmail.com># Date 1287388636 -32400# Node ID 74375645e3d57839485be4d376dce79769468f53# Parent  d10369fefd01f165f76263d9b92423ae018b84bcblackblox extension to help with debuggingdiff --git a/hgext/blackbox.py b/hgext/blackbox.pynew file mode 100644--- /dev/null+++ b/hgext/blackbox.py@@ -0,0 +1,57 @@+"""log repository events to help debugging++Appends to .hg/blackbox events happening in the repository to help+debugging and diagnosis. So far, this blackbox will track:+ - hg commands and their return codes+ - tracebacks+ - hooks+ - incoming changesets+"""++from mercurial import dispatch, extensions, hook, util+import traceback++def runcommand(orig, lui, repo, cmd, fullargs, *args, **kwargs):+    if repo is None:+        # not much we can log+        return orig(lui, repo, cmd, fullargs, *args, **kwargs)++    repo.blacklog(' '.join(fullargs))+    try:+        ret = orig(lui, repo, cmd, fullargs, *args, **kwargs)+        if ret:+            repo._blackbox.write(' [%d]\n' % ret)+        return ret+    except:+        traceback.print_exc(None, repo._blackbox)+        raise++def exthook(orig, ui, repo, name, cmd, *args, **kw):+    if repo:+        repo.blacklog('exthook: %s: %s' % (name, cmd))+    return orig(ui, repo, name, cmd, *args, **kw)+def pythonhook(orig, ui, repo, name, hname, funcname, *args, **kw):+    if repo and not hname.endswith('.blackbox'):+        repo.blacklog('pythonhook: %s: %s' % (hname, funcname))+    return orig(ui, repo, name, hname, funcname, *args, **kw)++def uisetup(ui):+    extensions.wrapfunction(dispatch, 'runcommand', runcommand)+    extensions.wrapfunction(hook, '_exthook', exthook)+    extensions.wrapfunction(hook, '_pythonhook', pythonhook)++def incoming(ui, repo, hooktype, node, source, url):+    repo.blacklog('incoming: %s: %s from %s' % (source, node[:12], url))+def reposetup(ui, repo):+    if not repo.local():+        return++    repo._blackbox = repo.opener('blackbox', 'a')+    class blackboxrepo(repo.__class__):+        def blacklog(self, msg):+            msg = '%s - %s\n' % (util.datestr(), msg)+            self._blackbox.write(msg)++    repo.__class__ = blackboxrepo+    ui.setconfig('hooks', 'incoming.blackbox', incoming)+diff --git a/tests/test-blackbox.t b/tests/test-blackbox.tnew file mode 100644--- /dev/null+++ b/tests/test-blackbox.t@@ -0,0 +1,80 @@+  $ echo '[extensions]' >> $HGRCPATH+  $ echo 'blackbox =' >> $HGRCPATH++== Non-zero exit codes ==++  $ hg init empty; cd empty+  $ hg add empty+  empty: No such file or directory+  [1]+  $ cat .hg/blackbox+  * - add empty (glob)+   [1]++== Logging hooks ==++  $ rm .hg/blackbox+  $ hg --config "hooks.pre-log.foo=echo 'hook'" log+  hook+  $ cat .hg/blackbox+  * - log (glob)+  * - exthook: pre-log.foo: echo 'hook' (glob)+  $ cd ..++== Incoming changesets ==++Create a repo with a dummy changeset++  $ hg init foo; cd foo+  $ touch a+  $ hg ci -Am'a'+  adding a+  $ hg clone . ../remote+  updating to branch default+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved++And pretend we push to remote a new changeset++  $ echo foo > a+  $ hg ci -Am'modify a'+  $ hg push ../remote+  pushing to ../remote+  searching for changes+  adding changesets+  adding manifests+  adding file changes+  added 1 changesets with 1 changes to 1 files++And check that the incoming operation was recorded++  $ cd ../remote+  $ cat .hg/blackbox+  * - incoming: push: c3cdbe7bbdc4 from file:$TESTTMP/foo (glob)++== Tracebacks ==++  $ rm .hg/blackbox+  $ hg diff -c notexisting+  abort: unknown revision 'notexisting'!+  [255]+  $ cat .hg/blackbox+  * - diff -c notexisting (glob)+  Traceback (most recent call last):+    File "*/hgext/blackbox.py", line *, in runcommand (glob)+      ret = orig(lui, repo, cmd, fullargs, *args, **kwargs)+    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 <lambda> (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'+