module Hish.VCS
(
VCS (..)
, Git (..)
, Darcs (..)
) where
import Text.Regex.TDFA ((=~))
import qualified Data.String.Utils as S (replace)
import qualified Data.List as DL (lines)
import System.Directory (findExecutable)
class VCS a where
vcsAhead :: a -> String -> Maybe String
vcsBehind :: a -> String -> Maybe String
vcsCleanliness :: a -> String -> Maybe String
vcsCurrentBranch :: a -> String -> Maybe String
branchCmd :: a -> String
branchArgs :: a -> [String]
statusCmd :: a -> String
statusArgs :: a -> [String]
installed :: a -> IO Bool
data Git = Git
instance Show Git where
show Git = "G"
instance VCS Git where
vcsAhead _ s =
case (s =~ "ahead [0-9]+" :: String) of
[] -> Nothing
ahead -> Just $ S.replace "ahead " "" ahead
vcsBehind _ s =
case (s =~ "behind [0-9]+" :: String) of
[] -> Nothing
behind -> Just $ S.replace "behind " "" behind
vcsCleanliness _ s = let body = tail $ DL.lines s in
case body of
[] -> Just ""
_ -> let (index,tree) = unzip $ map ((\ (a:b:_)->(a,b)).take 2) body in case (filter (/=' ') index, filter (/=' ')tree) of
("","") -> Just ""
(_,"") -> Just "#"
("",_) -> Just "*"
(idx,tre) -> if ("" == (filter (/='?') (idx++tre)))
then Just "?"
else Just "#"
vcsCurrentBranch _ = Just
. drop 2
. head
. filter ((=='*').head)
. lines
branchCmd _ = "git"
branchArgs _ = ["branch"]
statusCmd _ = "git"
statusArgs _ = ["status","--porcelain","-sb"]
installed _ = do
ext <- findExecutable "git"
case ext of
Nothing -> return False
Just _ -> return True
data Darcs = Darcs
instance Show Darcs where
show Darcs = "D"
instance VCS Darcs where
vcsCleanliness _ ("No changes!") = Just "#"
vcsCleanliness _ s = let body = map head $ DL.lines s in
if ((filter (/='a') body)=="") then Just "?" else Just "*"
vcsAhead _ s = do
return ""
vcsBehind _ s = do
return ""
vcsCurrentBranch _ s =
return ""
branchCmd Darcs = "darcs"
branchArgs Darcs = ["whatsnew"]
statusCmd Darcs = "darcs"
statusArgs Darcs = ["whatsnew","-l"]
installed _ = do
ext <- findExecutable "darcs"
case ext of
Nothing -> return False
Just _ -> return True