Skip to content

Commit

Permalink
better type for branch/remote information
Browse files Browse the repository at this point in the history
Now: (Branch, Maybe(Branch, Maybe Distance))
  • Loading branch information
olivierverdier committed Feb 4, 2015
1 parent fc116c9 commit d9f5cb8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
35 changes: 19 additions & 16 deletions src/BranchParse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,52 +51,55 @@ isValidBranch b = not . or $ [null,
instance Arbitrary Branch where
arbitrary = MkBranch <$> arbitrary `suchThat` isValidBranch

data Remote = MkRemote Branch (Maybe Distance) deriving (Eq, Show)

data BranchInfo = MkBranchInfo (Maybe Branch) (Maybe Branch) (Maybe Distance) deriving (Eq, Show)
getDistance :: Remote -> Maybe Distance
getDistance (MkRemote _ md) = md

noBranchInfo :: BranchInfo
noBranchInfo = MkBranchInfo Nothing Nothing Nothing
data BranchInfo = MkBranchInfo Branch (Maybe Remote) deriving (Eq, Show)

newRepo :: Parser BranchInfo
type MBranchInfo = Maybe BranchInfo

newRepo :: Parser MBranchInfo
newRepo =
fmap (\ branch -> MkBranchInfo (Just $ MkBranch branch) Nothing Nothing)
fmap (\ branch -> Just $ MkBranchInfo (MkBranch branch) Nothing)
$ string "Initial commit on " *> many anyChar <* eof

noBranch :: Parser BranchInfo
noBranch :: Parser MBranchInfo
noBranch =
noBranchInfo
Nothing
<$ manyTill anyChar (try $ string " (no branch)") <* eof

trackedBranch :: Parser Branch
trackedBranch = MkBranch <$> manyTill anyChar (try $ string "...")

branchRemoteTracking :: Parser BranchInfo
branchRemoteTracking :: Parser MBranchInfo
branchRemoteTracking =
(\ branch tracking behead -> MkBranchInfo (Just branch) (Just $ MkBranch tracking) (Just behead))
(\ branch tracking behead -> Just $ MkBranchInfo branch $ Just $ MkRemote (MkBranch tracking) (Just behead))
<$> trackedBranch
<*> many (noneOf " ") <* char ' '
<*> inBrackets

branchRemote :: Parser BranchInfo
branchRemote :: Parser MBranchInfo
branchRemote =
(\ branch tracking -> MkBranchInfo (Just branch) (Just $ MkBranch tracking) Nothing)
(\ branch tracking -> Just $ MkBranchInfo branch $ Just $ MkRemote (MkBranch tracking) Nothing)
<$> trackedBranch
<*> many (noneOf " ") <* eof

branchOnly :: Parser BranchInfo
branchOnly :: Parser MBranchInfo
branchOnly =
(\ branch -> MkBranchInfo (Just $ MkBranch branch) Nothing Nothing)
(\ branch -> Just $ MkBranchInfo (MkBranch branch) Nothing)
<$> many (noneOf " ") <* eof

branchParser :: Parser BranchInfo
branchParser :: Parser MBranchInfo
branchParser =
try noBranch
<|> try newRepo
<|> try branchRemoteTracking
<|> try branchRemote
<|> branchOnly

branchParser' :: Parser BranchInfo
branchParser' :: Parser MBranchInfo
branchParser' = (string "## ") >> branchParser

inBrackets :: Parser Distance
Expand All @@ -117,7 +120,7 @@ aheadBehind =
<* string ", "
<*> behind

branchInfo :: String -> Either ParseError BranchInfo
branchInfo :: String -> Either ParseError MBranchInfo
branchInfo = parse branchParser' ""

pairFromDistance :: Distance -> (Int, Int)
Expand Down
14 changes: 7 additions & 7 deletions src/TestBranchParse.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import BranchParse (BranchInfo(MkBranchInfo), branchInfo, Distance, Branch, noBranchInfo)
import BranchParse (MBranchInfo, BranchInfo(MkBranchInfo), branchInfo, Distance, getDistance, Branch, Remote(MkRemote))
import Test.QuickCheck (property, stdArgs, maxSuccess, quickCheckWithResult, Result, Property)
import Test.QuickCheck.Test (isSuccess)
import System.Exit (exitFailure)
Expand All @@ -7,7 +7,7 @@ import Control.Monad (forM, unless)

{- Helper to tackle the Either type -}

checkRight :: BranchInfo -> String -> Bool
checkRight :: MBranchInfo -> String -> Bool
checkRight b s = expectRight b $ branchInfo $ "## " ++ s
where
expectRight expected computed = case computed of
Expand All @@ -19,31 +19,31 @@ checkRight b s = expectRight b $ branchInfo $ "## " ++ s
propNoBranch :: Branch -> Bool
propNoBranch b =
checkRight
noBranchInfo
Nothing
$ show b ++ " (no branch)"

propNewRepo :: Branch -> Bool
propNewRepo b =
checkRight
(MkBranchInfo (Just b) Nothing Nothing)
(Just $ MkBranchInfo b Nothing)
$ "Initial commit on " ++ show b

propBranchOnly :: Branch -> Bool
propBranchOnly b =
checkRight
(MkBranchInfo (Just b) Nothing Nothing)
(Just $ MkBranchInfo b Nothing)
$ show b

propBranchRemote :: Branch -> Branch -> Bool
propBranchRemote b t =
checkRight
(MkBranchInfo (Just b) (Just t) Nothing)
(Just $ MkBranchInfo b $ Just $ MkRemote t Nothing)
$ show b ++"..." ++ show t

propBranchRemoteTracking :: Branch -> Branch -> Distance -> Bool
propBranchRemoteTracking b t distance =
checkRight
(MkBranchInfo (Just b) (Just t) (Just distance))
(Just $ MkBranchInfo b $ Just $ MkRemote t $ Just distance)
$ show b ++ "..." ++ show t ++ " " ++ show distance


Expand Down
30 changes: 16 additions & 14 deletions src/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,49 @@ module Utils where

import Data.Maybe (fromMaybe)
import Control.Applicative ((<$>), (<*>))
import BranchParse (Branch, BranchInfo(MkBranchInfo), branchInfo, Distance, pairFromDistance)
import BranchParse (Branch(MkBranch), MBranchInfo, BranchInfo(MkBranchInfo), branchInfo, getDistance, pairFromDistance, Remote)
import StatusParse (Status(MakeStatus), processStatus)

{- Type aliases -}

newtype Hash = MkHash {getHash :: String}
type Numbers = [String]

{- Combining branch and status parsing -}

rightOrNothing :: Either a b -> Maybe b
rightOrNothing = either (const Nothing) Just

processBranch :: String -> Maybe BranchInfo
processBranch :: String -> Maybe MBranchInfo
processBranch = rightOrNothing . branchInfo

processGitStatus :: [String] -> Maybe (BranchInfo, Status Int)
processGitStatus :: [String] -> Maybe (MBranchInfo, Status Int)
processGitStatus [] = Nothing
processGitStatus (branchLine:statusLines) = (,) <$> processBranch branchLine <*> processStatus statusLines

showStatusNumbers :: Status Int -> Numbers
showStatusNumbers :: Status Int -> [String]
showStatusNumbers (MakeStatus s x c t) = show <$> [s, x, c, t]


showBranchNumbers :: Maybe Distance -> Numbers
showBranchNumbers distance = show <$> [ahead, behind]
showRemoteNumbers :: Maybe Remote -> [String]
showRemoteNumbers mremote = show <$> [ahead, behind]
where
(ahead, behind) = fromMaybe (0,0) -- the script needs some value, (0,0) means no display
$ pairFromDistance <$> distance
$ pairFromDistance <$> (getDistance =<< mremote)

showBranchInfo :: BranchInfo -> [String]
showBranchInfo (MkBranchInfo branch mremote) = show branch : showRemoteNumbers mremote

{- Combine status info, branch info and hash -}

branchOrHashWith :: Char -> Maybe Hash -> Maybe Branch -> String
branchOrHashWith _ _ (Just branch) = show branch
branchOrHashWith c (Just hash) Nothing = c : getHash hash
branchOrHashWith _ Nothing _ = ""
branchOrHashWith :: Char -> Maybe Hash -> Maybe BranchInfo -> [String]
branchOrHashWith _ _ (Just bi) = showBranchInfo bi
branchOrHashWith c (Just hash) Nothing = showBranchInfo $ MkBranchInfo (MkBranch $ c : getHash hash) Nothing
branchOrHashWith _ Nothing _ = showBranchInfo $ MkBranchInfo (MkBranch "") Nothing

allStrings :: Maybe Hash
-> (BranchInfo, Status Int)
-> (MBranchInfo, Status Int)
-> [String]
allStrings mhash (MkBranchInfo branch _ behead, stat) = branchOrHashWith ':' mhash branch : (showBranchNumbers behead ++ showStatusNumbers stat)
allStrings mhash (bi, stat) = branchOrHashWith ':' mhash bi ++ showStatusNumbers stat

stringsFromStatus :: Maybe Hash
-> String -- status
Expand Down

0 comments on commit d9f5cb8

Please sign in to comment.