module Propellor.Git.Config where

import Propellor.Git
import Utility.Process
import Utility.Exception
import Utility.SafeCommand
import Utility.Monad

import Control.Monad
import Control.Applicative
import Prelude

getGitConfigValue :: String -> IO (Maybe String)
getGitConfigValue :: String -> IO (Maybe String)
getGitConfigValue String
key = do
	Maybe String
value <- IO String -> IO (Maybe String)
forall (m :: * -> *) a. MonadCatch m => m a -> m (Maybe a)
catchMaybeIO (IO String -> IO (Maybe String)) -> IO String -> IO (Maybe String)
forall a b. (a -> b) -> a -> b
$
		(Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\n')
			(String -> String) -> IO String -> IO String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> [String] -> IO String
readProcessString
"git" [String
"config", String
key]
	Maybe String -> IO (Maybe String)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe String -> IO (Maybe String))
-> Maybe String -> IO (Maybe String)
forall a b. (a -> b) -> a -> b
$ case Maybe String
value of
		Just String
v | Bool -> Bool
not (String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
v) -> String -> Maybe String
forall a. a -> Maybe a
Just String
v
		Maybe String
_ -> Maybe String
forall a. Maybe a
Nothing

-- `git config --bool propellor.blah` outputs "false" if propellor.blah is unset
-- i.e. the git convention is that the default value of any git-config setting
-- is "false".  So we don't need a Maybe Bool here.
getGitConfigBool :: String -> IO Bool
getGitConfigBool :: String -> IO Bool
getGitConfigBool String
key = do
	Maybe String
value <- IO String -> IO (Maybe String)
forall (m :: * -> *) a. MonadCatch m => m a -> m (Maybe a)
catchMaybeIO (IO String -> IO (Maybe String)) -> IO String -> IO (Maybe String)
forall a b. (a -> b) -> a -> b
$
		(Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\n')
			(String -> String) -> IO String -> IO String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> [String] -> IO String
readProcess String
"git" [String
"config", String
"--bool", String
key]
	Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> IO Bool) -> Bool -> IO Bool
forall a b. (a -> b) -> a -> b
$ case Maybe String
value of
		Just String
"true" -> Bool
True
		Maybe String
_ -> Bool
False

setRepoUrl :: String -> IO ()
setRepoUrl :: String -> IO ()
setRepoUrl String
"" = () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
setRepoUrl String
url = do
	String
subcmd <- IO Bool -> (IO String, IO String) -> IO String
forall (m :: * -> *) a. Monad m => m Bool -> (m a, m a) -> m a
ifM IO Bool
hasOrigin (String -> IO String
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure String
"set-url", String -> IO String
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure String
"add")
	IO Bool -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO Bool -> IO ()) -> IO Bool -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> [CommandParam] -> IO Bool
boolSystem String
"git" [String -> CommandParam
Param String
"remote", String -> CommandParam
Param String
subcmd, String -> CommandParam
Param String
"origin", String -> CommandParam
Param String
url]
	-- same as --set-upstream-to, except origin branch
	-- may not have been pulled yet
	String
branch <- IO String
getCurrentBranch
	let branchval :: String -> String
branchval String
s = String
"branch." String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
branch String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"." String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s
	IO Bool -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO Bool -> IO ()) -> IO Bool -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> [CommandParam] -> IO Bool
boolSystem String
"git" [String -> CommandParam
Param String
"config", String -> CommandParam
Param (String -> String
branchval String
"remote"), String -> CommandParam
Param String
"origin"]
	IO Bool -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO Bool -> IO ()) -> IO Bool -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> [CommandParam] -> IO Bool
boolSystem String
"git" [String -> CommandParam
Param String
"config", String -> CommandParam
Param (String -> String
branchval String
"merge"), String -> CommandParam
Param (String -> CommandParam) -> String -> CommandParam
forall a b. (a -> b) -> a -> b
$ String
"refs/heads/"String -> String -> String
forall a. [a] -> [a] -> [a]
++String
branch]

getRepoUrl :: IO (Maybe String)
getRepoUrl :: IO (Maybe String)
getRepoUrl = (String -> IO (Maybe String)) -> [String] -> IO (Maybe String)
forall (m :: * -> *) a b.
Monad m =>
(a -> m (Maybe b)) -> [a] -> m (Maybe b)
getM String -> IO (Maybe String)
getGitConfigValue [String]
urls
  where
	urls :: [String]
urls = [String
"remote.deploy.url", String
"remote.origin.url"]