convert: fix issue702 about GIT_DIR= construct unsupported under Windows.
authorPatrick Mezard <pmezard@gmail.com>
Sun, 26 Aug 2007 14:51:27 +0200
changeset 5217 149742a628fd
parent 5216 39e6deaa8b81
child 5218 4fa0f2dff643
convert: fix issue702 about GIT_DIR= construct unsupported under Windows.
hgext/convert/git.py
--- a/hgext/convert/git.py	Sun Jul 01 21:09:08 2007 -0700
+++ b/hgext/convert/git.py	Sun Aug 26 14:51:27 2007 +0200
@@ -14,8 +14,23 @@
             return s.decode("utf-8", "replace").encode("utf-8")
 
 class convert_git(converter_source):
-    def gitcmd(self, s):
-        return os.popen('GIT_DIR=%s %s' % (self.path, s))
+    # Windows does not support GIT_DIR= construct while other systems
+    # cannot remove environment variable. Just assume none have
+    # both issues.
+    if hasattr(os, 'unsetenv'):
+        def gitcmd(self, s):
+            prevgitdir = os.environ.get('GIT_DIR')
+            os.environ['GIT_DIR'] = self.path
+            try:
+                return os.popen(s)
+            finally:
+                if prevgitdir is None:
+                    del os.environ['GIT_DIR']
+                else:
+                    os.environ['GIT_DIR'] = prevgitdir
+    else:
+        def gitcmd(self, s):
+            return os.popen('GIT_DIR=%s %s' % (self.path, s))
     
     def __init__(self, ui, path):
         if os.path.isdir(path + "/.git"):