From 10fa4567bd1761a1e26b841128aff9781e7e0df3 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Sun, 26 Oct 2014 13:39:44 -0400 Subject: setenv for windows --- Git/CurrentRepo.hs | 10 +--------- Git/Index.hs | 4 ++-- Utility/Env.hs | 29 +++++++++++++++-------------- git-repair.cabal | 4 +++- 4 files changed, 21 insertions(+), 26 deletions(-) diff --git a/Git/CurrentRepo.hs b/Git/CurrentRepo.hs index 23ebbbc..f611f7a 100644 --- a/Git/CurrentRepo.hs +++ b/Git/CurrentRepo.hs @@ -5,17 +5,13 @@ - Licensed under the GNU GPL version 3 or higher. -} -{-# LANGUAGE CPP #-} - module Git.CurrentRepo where import Common import Git.Types import Git.Construct import qualified Git.Config -#ifndef mingw32_HOST_OS import Utility.Env -#endif {- Gets the current git repository. - @@ -42,17 +38,13 @@ get = do setCurrentDirectory d return $ addworktree wt r where -#ifndef mingw32_HOST_OS pathenv s = do v <- getEnv s case v of Just d -> do - void $ unsetEnv s + unsetEnv s Just <$> absPath d Nothing -> return Nothing -#else - pathenv _ = return Nothing -#endif configure Nothing (Just r) = Git.Config.read r configure (Just d) _ = do diff --git a/Git/Index.hs b/Git/Index.hs index d712245..c42ac42 100644 --- a/Git/Index.hs +++ b/Git/Index.hs @@ -21,8 +21,8 @@ import Utility.Env override :: FilePath -> IO (IO ()) override index = do res <- getEnv var - void $ setEnv var index True - return $ void $ reset res + setEnv var index True + return $ reset res where var = "GIT_INDEX_FILE" reset (Just v) = setEnv var v True diff --git a/Utility/Env.hs b/Utility/Env.hs index 6763c24..ff6644f 100644 --- a/Utility/Env.hs +++ b/Utility/Env.hs @@ -14,6 +14,7 @@ import Utility.Exception import Control.Applicative import Data.Maybe import qualified System.Environment as E +import qualified System.SetEnv #else import qualified System.Posix.Env as PE #endif @@ -39,27 +40,27 @@ getEnvironment = PE.getEnvironment getEnvironment = E.getEnvironment #endif -{- Returns True if it could successfully set the environment variable. +{- Sets an environment variable. To overwrite an existing variable, + - overwrite must be True. - - - There is, apparently, no way to do this in Windows. Instead, - - environment varuables must be provided when running a new process. -} -setEnv :: String -> String -> Bool -> IO Bool + - On Windows, setting a variable to "" unsets it. -} +setEnv :: String -> String -> Bool -> IO () #ifndef mingw32_HOST_OS -setEnv var val overwrite = do - PE.setEnv var val overwrite - return True +setEnv var val overwrite = PE.setEnv var val overwrite #else -setEnv _ _ _ = return False +setEnv var val True = System.SetEnv.setEnv var val +setEnv var val False = do + r <- getEnv var + case r of + Nothing -> setEnv var val True + Just _ -> return () #endif -{- Returns True if it could successfully unset the environment variable. -} -unsetEnv :: String -> IO Bool +unsetEnv :: String -> IO () #ifndef mingw32_HOST_OS -unsetEnv var = do - PE.unsetEnv var - return True +unsetEnv = PE.unsetEnv #else -unsetEnv _ = return False +unsetEnv = System.SetEnv.unsetEnv #endif {- Adds the environment variable to the input environment. If already diff --git a/git-repair.cabal b/git-repair.cabal index e5d6cb9..67573f4 100644 --- a/git-repair.cabal +++ b/git-repair.cabal @@ -30,7 +30,9 @@ Executable git-repair base >= 4.5, base < 5, IfElse, text, process, time, QuickCheck, utf8-string, async, optparse-applicative (>= 0.10.0) - if (! os(windows)) + if (os(windows)) + Build-Depends: setenv + else Build-Depends: unix source-repository head -- cgit v1.2.3