diff --git a/mercurial/hook.py b/mercurial/hook.py --- a/mercurial/hook.py +++ b/mercurial/hook.py @@ -65,6 +65,12 @@ def _pythonhook(ui, repo, name, hname, f '("%s" is not callable)') % (hname, funcname)) try: + # redirect IO descriptors the the ui descriptors so hooks that write + # directly to these don't mess the command protocol when running through + # the command server + old = sys.stdout, sys.stderr, sys.stdin + sys.stdout, sys.stderr, sys.stdin = ui.fout, ui.ferr, ui.fin + r = obj(ui=ui, repo=repo, hooktype=name, **args) except KeyboardInterrupt: raise @@ -79,6 +85,8 @@ def _pythonhook(ui, repo, name, hname, f raise ui.traceback() return True + finally: + sys.stdout, sys.stderr, sys.stdin = old if r: if throw: raise util.Abort(_('%s hook failed') % hname) diff --git a/tests/test-commandserver.py b/tests/test-commandserver.py --- a/tests/test-commandserver.py +++ b/tests/test-commandserver.py @@ -144,6 +144,16 @@ def localhgrc(server): runcommand(server, ['-R', 'foo', 'showconfig']) shutil.rmtree('foo') +def hook(**args): + print 'hook talking' + print 'now try to read something: %r' % sys.stdin.read() + +def hookoutput(server): + readchannel(server) + runcommand(server, ['--config', + 'hooks.pre-identify=python:test-commandserver.hook', 'id'], + input=cStringIO.StringIO('some input')) + if __name__ == '__main__': os.system('hg init') @@ -158,3 +168,4 @@ if __name__ == '__main__': hgrc.write('[ui]\nfoo=bar\n') hgrc.close() check(localhgrc) + check(hookoutput) diff --git a/tests/test-commandserver.py.out b/tests/test-commandserver.py.out --- a/tests/test-commandserver.py.out +++ b/tests/test-commandserver.py.out @@ -49,3 +49,6 @@ defaults.backout=-d "0 0" defaults.commit=-d "0 0" defaults.tag=-d "0 0" ui.slash=True +hook talking +now try to read something: 'some input' +eff892de26ec tip