# HG changeset patch # User Vadim Gelfer # Date 2006-03-11 06:24:19 # Node ID 05c7d75be9256c1575d20b9a7f886422877fdb9a # Parent 9ef0850b9b68107a618f53ee2954f152e50305c4 fix broken environment save/restore when a hook runs. move "run commend with different env/cwd" code out to function in util. new function is called esystem. diff --git a/hgmerge b/hgmerge --- a/hgmerge +++ b/hgmerge @@ -7,6 +7,8 @@ set -e # bail out quickly on failure +echo $1 $2 $3 + LOCAL="$1" BASE="$2" OTHER="$3" diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py --- a/mercurial/localrepo.py +++ b/mercurial/localrepo.py @@ -54,28 +54,8 @@ class localrepository(object): def hook(self, name, throw=False, **args): def runhook(name, cmd): self.ui.note(_("running hook %s: %s\n") % (name, cmd)) - old = {} - for k, v in args.items(): - k = k.upper() - old['HG_' + k] = os.environ.get(k, None) - old[k] = os.environ.get(k, None) - os.environ['HG_' + k] = str(v) - os.environ[k] = str(v) - - try: - # Hooks run in the repository root - olddir = os.getcwd() - os.chdir(self.root) - r = os.system(cmd) - finally: - for k, v in old.items(): - if v is not None: - os.environ[k] = v - else: - del os.environ[k] - - os.chdir(olddir) - + env = dict([('HG_' + k.upper(), v) for k, v in args.iteritems()]) + r = util.esystem(cmd, environ=env, cwd=self.root) if r: desc, r = util.explain_exit(r) if throw: @@ -231,7 +211,7 @@ class localrepository(object): self.opener("journal.dirstate", "w").write(ds) tr = transaction.transaction(self.ui.warn, self.opener, - self.join("journal"), + self.join("journal"), aftertrans(self.path)) self.transhandle = tr return tr diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -325,6 +325,29 @@ def system(cmd, errprefix=None): errmsg = "%s: %s" % (errprefix, errmsg) raise Abort(errmsg) +def esystem(cmd, environ={}, cwd=None): + '''enhanced shell command execution. + run with environment maybe modified, maybe in different dir.''' + oldenv = {} + for k in environ: + oldenv[k] = os.environ.get(k) + if cwd is not None: + oldcwd = os.getcwd() + try: + for k, v in environ.iteritems(): + os.environ[k] = str(v) + if cwd is not None and oldcwd != cwd: + os.chdir(cwd) + return os.system(cmd) + finally: + for k, v in oldenv.iteritems(): + if v is None: + del os.environ[k] + else: + os.environ[k] = v + if cwd is not None and oldcwd != cwd: + os.chdir(oldcwd) + def rename(src, dst): """forcibly rename a file""" try: