From f5fed5a270c34e7603a6293c17815295aac726b9 Mon Sep 17 00:00:00 2001 From: Sietse Brouwer Date: Thu, 3 Dec 2015 01:38:21 +0100 Subject: [PATCH] dirstate: don't write repo.currenttransaction to repo.dirstate if repo is None (issue4983) Some hooks, such as post-init and post-clone, do not get a repo parameter in their environment. If there is no repo, there is no repo.currenttransaction(); attempting to retrieve it anyway was causing crashes. Now currenttransaction is only retrieved and written if the repo is not None. --- mercurial/hook.py | 9 +++++---- tests/test-hook.t | 13 +++++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/mercurial/hook.py b/mercurial/hook.py index 0a1fabc9e2..89948f46e3 100644 --- a/mercurial/hook.py +++ b/mercurial/hook.py @@ -120,10 +120,11 @@ def _exthook(ui, repo, name, cmd, args, throw): env = {} # make in-memory changes visible to external process - tr = repo.currenttransaction() - repo.dirstate.write(tr) - if tr and tr.writepending(): - env['HG_PENDING'] = repo.root + if repo is not None: + tr = repo.currenttransaction() + repo.dirstate.write(tr) + if tr and tr.writepending(): + env['HG_PENDING'] = repo.root for k, v in args.iteritems(): if callable(v): diff --git a/tests/test-hook.t b/tests/test-hook.t index 0f53370027..f718235676 100644 --- a/tests/test-hook.t +++ b/tests/test-hook.t @@ -681,10 +681,19 @@ new tags must be visible in pretxncommit (issue3210) $ hg tag -f foo ['a', 'foo', 'tip'] -new commits must be visible in pretxnchangegroup (issue3428) +post-init hooks must not crash (issue4983) +This also creates the `to` repo for the next test block. $ cd .. - $ hg init to + $ cat << EOF >> hgrc-with-post-init-hook + > [hooks] + > post-init = printenv.py post-init + > EOF + $ HGRCPATH=hgrc-with-post-init-hook hg init to + post-init hook: HG_ARGS=init to HG_OPTS={'insecure': None, 'remotecmd': '', 'ssh': ''} HG_PATS=['to'] HG_RESULT=0 + +new commits must be visible in pretxnchangegroup (issue3428) + $ echo '[hooks]' >> to/.hg/hgrc $ echo 'prechangegroup = hg --traceback tip' >> to/.hg/hgrc $ echo 'pretxnchangegroup = hg --traceback tip' >> to/.hg/hgrc