fix broken environment save/restore when a hook runs.
authorVadim Gelfer <vadim.gelfer@gmail.com>
Fri, 10 Mar 2006 22:24:19 -0800
changeset 1880 05c7d75be925
parent 1879 9ef0850b9b68
child 1881 4392ccefe432
child 1882 c0320567931f
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.
hgmerge
mercurial/localrepo.py
mercurial/util.py
--- a/hgmerge	Fri Mar 10 20:06:41 2006 +0100
+++ b/hgmerge	Fri Mar 10 22:24:19 2006 -0800
@@ -7,6 +7,8 @@
 
 set -e # bail out quickly on failure
 
+echo $1 $2 $3
+
 LOCAL="$1"
 BASE="$2"
 OTHER="$3"
--- a/mercurial/localrepo.py	Fri Mar 10 20:06:41 2006 +0100
+++ b/mercurial/localrepo.py	Fri Mar 10 22:24:19 2006 -0800
@@ -54,28 +54,8 @@
     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 @@
         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
--- a/mercurial/util.py	Fri Mar 10 20:06:41 2006 +0100
+++ b/mercurial/util.py	Fri Mar 10 22:24:19 2006 -0800
@@ -325,6 +325,29 @@
             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: