module VCSWrapper.Git.Parsers (
parseStatus
, parseBranches
, parseRemotes
, parseSimpleLog
, parsePullMergeConflict
) where
import Data.List
import Data.List.Split (splitOn)
import Text.ParserCombinators.Parsec
import VCSWrapper.Common.Types
import Data.Text (Text)
import qualified Data.Text as T
(isPrefixOf, pack, lines, unpack, splitOn)
import Control.Applicative ((<$>))
parseStatus :: Text
-> [Status]
parseStatus status = [ GITStatus filepath Modified | (_:x:_:filepath) <- lines, x == 'M']
++ [ GITStatus filepath Untracked | (x:_:_:filepath) <- lines, x == '?']
++ [ GITStatus filepath Added | (x:_:_:filepath) <- lines, x == 'A']
++ [ GITStatus filepath Deleted | (x:y:_:filepath) <- lines, x == 'D' || y == 'D']
where
lines = map T.unpack $ T.splitOn "\n" status
parseBranches :: Text -> (Text, [Text])
parseBranches string = (head [T.pack branchname | ('*':_:branchname) <- lined],
[T.pack branchname | (' ':' ':branchname) <- lined])
where
lined = map T.unpack $ T.lines string
parseRemotes :: Text -> [Text]
parseRemotes = T.splitOn "\n"
parsePullMergeConflict :: Text -> Bool
parsePullMergeConflict s = T.isPrefixOf "CONFLICT" s
parseSimpleLog :: Text -> [LogEntry]
parseSimpleLog log =
case parsed of
Right entries -> entries
Left _ -> []
where parsed = parse logEntries "" (T.unpack log)
logEntry :: Parser LogEntry
logEntry = do
string "commit:"
commitID <- T.pack <$> wholeLine
author <- T.pack <$> wholeLine
email <- T.pack <$> wholeLine
date <- T.pack <$> wholeLine
subject <- T.pack <$> wholeLine
body <- T.pack <$> bodyLines
char '\n'
return $ LogEntry Nothing commitID author email date subject body
bodyLines = manyTill anyChar (nullChar)
logEntries :: Parser [LogEntry]
logEntries = manyTill logEntry eof
wholeLine = manyTill anyChar newline
nullChar = satisfy (=='\0') >> return ()