module Darcs.PopulationData ( Population(..), PopTree(..), Info(..),
setPopState, notModified, setState,
DirMark(..), getPopFrom
) where
import Darcs.Patch.Info
import Darcs.Utils ( withCurrentDirectory )
import System.Directory ( doesDirectoryExist, getDirectoryContents )
import qualified Data.ByteString.Char8 as BC
import qualified Data.ByteString as B (ByteString)
data Population = Pop { popState :: PatchInfo
, popTree :: PopTree
}
deriving (Show, Eq)
setPopState :: PatchInfo -> Population -> Population
setPopState i (Pop _ tr) = Pop i tr
data PopTree = PopDir !Info ![PopTree]
| PopFile !Info
deriving ( Ord, Eq )
data DirMark = AddedFile | RemovedFile | MovedFile String
| ModifiedFile | DullFile
| AddedDir | RemovedDir | MovedDir !String | DullDir
deriving ( Ord, Eq )
data Info = Info {nameI :: !B.ByteString,
modifiedByI :: !PatchInfo,
modifiedHowI :: !DirMark,
createdByI :: !(Maybe PatchInfo),
creationNameI :: !(Maybe B.ByteString)
}
deriving ( Ord, Eq )
notModified :: Info -> Bool
notModified i = (modifiedHowI i == DullFile) || (modifiedHowI i == DullDir)
setState :: Info -> PatchInfo -> Info
setState i pinfo = i { modifiedByI = pinfo }
instance Show PopTree where
show s = showPop "" s
showPop :: String -> PopTree -> String
showPop indent (PopDir i fs)
= indent ++ show i ++ "\n" ++
unlines (map (showPop (' ':indent)) fs)
showPop indent (PopFile i)
= indent ++ show i
instance Show Info where
show i = show (nameI i) ++ " " ++ show (modifiedHowI i) ++
" at state " ++ show (modifiedByI i)
instance Show DirMark where
show AddedFile = "File added"
show RemovedFile = "File removed"
show (MovedFile s) = "File moved to " ++ s
show ModifiedFile = "File modified"
show DullFile = "File old"
show AddedDir = "Dir added"
show RemovedDir = "Dir removed"
show (MovedDir s) = "Dir moved from " ++ s
show DullDir = "Dir old"
getPopFrom :: FilePath -> PatchInfo -> IO Population
getPopFrom the_directory pinfo =
withCurrentDirectory the_directory $
do popT <- getPopFrom_helper "."
return (Pop pinfo popT)
where getPopFrom_helper :: FilePath -> IO PopTree
getPopFrom_helper dirname = do
isdir <- doesDirectoryExist dirname
let n = BC.pack dirname
if isdir
then do
fnames <- getDirectoryContents dirname
sl <- withCurrentDirectory dirname
(mapM getPopFrom_helper $ filter notHidden fnames)
let i = Info {nameI = n,
modifiedByI = pinfo,
modifiedHowI = DullDir,
createdByI = Just pinfo,
creationNameI = Just n}
return $ PopDir i sl
else do let i = Info {nameI = n,
modifiedByI = pinfo,
modifiedHowI = DullFile,
createdByI = Just pinfo,
creationNameI = Just n}
return $ PopFile i
notHidden :: FilePath -> Bool
notHidden ('.':_) = False
notHidden ('_':_) = False
notHidden _ = True