# HG changeset patch # User Matt Mackall # Date 1182206996 18000 # Node ID e6d105a51ec755ca6b75798c9bcd4457f02e3804 # Parent a04b5f37eda70c2ca65f15021606867e1781ff11 dispatch: add generic pre- and post-command hooks diff --git a/doc/hgrc.5.txt b/doc/hgrc.5.txt --- a/doc/hgrc.5.txt +++ b/doc/hgrc.5.txt @@ -312,10 +312,20 @@ hooks:: new parent is in $HG_PARENT1. If merge, ID of second new parent is in $HG_PARENT2. If update succeeded, $HG_ERROR=0. If update failed (e.g. because conflicts not resolved), $HG_ERROR=1. + pre-;; + Run before executing the associated command. The contents of the + command line are passed as $HG_ARGS. If the hook returns failure, + the command doesn't execute and Mercurial returns the failure code. + post-;; + Run after successful invocations of the associated command. The + contents of the command line are passed as $HG_ARGS and the result + code in $HG_RESULT. Hook failure is ignored. - Note: In earlier releases, the names of hook environment variables - did not have a "HG_" prefix. The old unprefixed names are no longer - provided in the environment. + Note: it is generally better to use standard hooks rather than the + generic pre- and post- command hooks as they are guaranteed to be + called in the appropriate contexts for influencing transactions. + Also, hooks like "commit" will be called in all contexts that + generate a commit (eg. tag) and not just the commit command. The syntax for Python hooks is as follows: diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py --- a/mercurial/cmdutil.py +++ b/mercurial/cmdutil.py @@ -9,7 +9,7 @@ from node import * from i18n import _ import os, sys, atexit, signal, pdb, traceback, socket, errno, shlex import mdiff, bdiff, util, templater, patch, commands, hg, lock, time -import fancyopts, revlog, version, extensions +import fancyopts, revlog, version, extensions, hook revrangesep = ':' @@ -272,6 +272,7 @@ def dispatch(ui, args): if fallback: util._fallbackencoding = fallback + fullargs = args cmd, func, args, options, cmdoptions = parse(ui, args) if options["encoding"]: @@ -302,8 +303,8 @@ def dispatch(ui, args): elif not cmd: return commands.help_(ui, 'shortlist') + repo = None if cmd not in commands.norepo.split(): - repo = None try: repo = hg.repository(ui, path=path) ui = repo.ui @@ -316,7 +317,15 @@ def dispatch(ui, args): else: d = lambda: func(ui, *args, **cmdoptions) - return runcommand(ui, options, cmd, d) + # run pre-hook, and abort if it fails + ret = hook.hook(ui, repo, "pre-%s" % cmd, False, args=" ".join(fullargs)) + if ret: + return ret + ret = runcommand(ui, options, cmd, d) + # run post-hook, passing command result + hook.hook(ui, repo, "post-%s" % cmd, False, args=" ".join(fullargs), + result = ret) + return ret def runcommand(ui, options, cmd, cmdfunc): def checkargs(): diff --git a/tests/test-hook b/tests/test-hook --- a/tests/test-hook +++ b/tests/test-hook @@ -11,6 +11,9 @@ echo 'commit.b = unset HG_LOCAL HG_TAG; echo 'precommit = unset HG_LOCAL HG_NODE HG_TAG; python ../printenv.py precommit' >> .hg/hgrc echo 'pretxncommit = unset HG_LOCAL HG_TAG; python ../printenv.py pretxncommit' >> .hg/hgrc echo 'pretxncommit.tip = hg -q tip' >> .hg/hgrc +echo 'pre-identify = false' >> .hg/hgrc +echo 'pre-cat = echo "meow $HG_ARGS"' >> .hg/hgrc +echo 'post-cat = echo "purr $HG_RESULT"' >> .hg/hgrc echo a > a hg add a hg commit -m a -d "1000000 0" @@ -35,6 +38,10 @@ hg commit -m b -d '1 0' hg merge 1 hg commit -m merge -d '2 0' +# test generic hooks +hg id +hg cat b + cd ../b hg pull ../a diff --git a/tests/test-hook.out b/tests/test-hook.out --- a/tests/test-hook.out +++ b/tests/test-hook.out @@ -22,6 +22,10 @@ pretxncommit hook: HG_NODE=4c52fb2e40228 3:4c52fb2e4022 commit hook: HG_NODE=4c52fb2e402287dd5dc052090682536c8406c321 HG_PARENT1=1324a5531bac09b329c3845d35ae6a7526874edb HG_PARENT2=b702efe9688826e3a91283852b328b84dbf37bc2 commit.b hook: HG_NODE=4c52fb2e402287dd5dc052090682536c8406c321 HG_PARENT1=1324a5531bac09b329c3845d35ae6a7526874edb HG_PARENT2=b702efe9688826e3a91283852b328b84dbf37bc2 +warning: pre-identify hook exited with status 1 +meow cat b +purr 0 +b prechangegroup hook: HG_SOURCE=pull HG_URL=file: changegroup hook: HG_NODE=b702efe9688826e3a91283852b328b84dbf37bc2 HG_SOURCE=pull HG_URL=file: incoming hook: HG_NODE=b702efe9688826e3a91283852b328b84dbf37bc2 HG_SOURCE=pull HG_URL=file: