2016-02-25 17:46:31 +00:00
|
|
|
{- Adjusting the environment while running git commands.
|
|
|
|
-
|
|
|
|
- Copyright 2014-2016 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
|
|
|
module Git.Env where
|
|
|
|
|
2016-05-06 16:26:37 +00:00
|
|
|
import Common
|
2016-02-25 17:46:31 +00:00
|
|
|
import Git
|
2016-02-25 18:59:35 +00:00
|
|
|
import Git.Types
|
2016-02-25 17:46:31 +00:00
|
|
|
import Utility.Env
|
|
|
|
|
|
|
|
{- Adjusts the gitEnv of a Repo. Copies the system environment if the repo
|
|
|
|
- does not have any gitEnv yet. -}
|
|
|
|
adjustGitEnv :: Repo -> ([(String, String)] -> [(String, String)]) -> IO Repo
|
|
|
|
adjustGitEnv g adj = do
|
2016-09-29 17:36:48 +00:00
|
|
|
e <- maybe copyGitEnv return (gitEnv g)
|
2016-02-25 17:46:31 +00:00
|
|
|
let e' = adj e
|
|
|
|
return $ g { gitEnv = Just e' }
|
|
|
|
where
|
2016-09-29 17:36:48 +00:00
|
|
|
|
|
|
|
{- Copies the current environment, so it can be adjusted when running a git
|
|
|
|
- command. -}
|
|
|
|
copyGitEnv :: IO [(String, String)]
|
|
|
|
copyGitEnv = do
|
2016-02-25 17:46:31 +00:00
|
|
|
#ifdef __ANDROID__
|
2016-09-29 17:36:48 +00:00
|
|
|
{- This should not be necessary on Android, but there is some
|
|
|
|
- weird getEnvironment breakage. See
|
|
|
|
- https://github.com/neurocyte/ghc-android/issues/7
|
|
|
|
- Use getEnv to get some key environment variables that
|
|
|
|
- git expects to have. -}
|
|
|
|
let keyenv = words "USER PATH GIT_EXEC_PATH HOSTNAME HOME"
|
|
|
|
let getEnvPair k = maybe Nothing (\v -> Just (k, v)) <$> getEnv k
|
|
|
|
catMaybes <$> forM keyenv getEnvPair
|
2016-02-25 17:46:31 +00:00
|
|
|
#else
|
2016-09-29 17:36:48 +00:00
|
|
|
getEnvironment
|
2016-02-25 17:46:31 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
addGitEnv :: Repo -> String -> String -> IO Repo
|
|
|
|
addGitEnv g var val = adjustGitEnv g (addEntry var val)
|
2016-02-25 18:59:35 +00:00
|
|
|
|
2016-05-06 16:26:37 +00:00
|
|
|
{- Environment variables to use when running a command.
|
|
|
|
- Includes GIT_DIR pointing at the repo, and GIT_WORK_TREE when the repo
|
|
|
|
- is not bare. Also includes anything added to the Repo's gitEnv,
|
|
|
|
- and a copy of the rest of the system environment. -}
|
|
|
|
propGitEnv :: Repo -> IO [(String, String)]
|
|
|
|
propGitEnv g = do
|
|
|
|
g' <- addGitEnv g "GIT_DIR" (localGitDir g)
|
|
|
|
g'' <- maybe (pure g') (addGitEnv g' "GIT_WORK_TREE") (repoWorkTree g)
|
|
|
|
return $ fromMaybe [] (gitEnv g'')
|
|
|
|
|
2016-02-25 18:59:35 +00:00
|
|
|
{- Use with any action that makes a commit to set metadata. -}
|
|
|
|
commitWithMetaData :: CommitMetaData -> CommitMetaData -> (Repo -> IO a) -> Repo -> IO a
|
|
|
|
commitWithMetaData authormetadata committermetadata a g =
|
|
|
|
a =<< adjustGitEnv g adj
|
|
|
|
where
|
|
|
|
adj = mkadj "AUTHOR" authormetadata
|
|
|
|
. mkadj "COMMITTER" committermetadata
|
|
|
|
mkadj p md = go "NAME" commitName
|
|
|
|
. go "EMAIL" commitEmail
|
|
|
|
. go "DATE" commitDate
|
|
|
|
where
|
|
|
|
go s getv = case getv md of
|
|
|
|
Nothing -> id
|
|
|
|
Just v -> addEntry ("GIT_" ++ p ++ "_" ++ s) v
|