module Darcs.Repository.Pending
( readPending
, readTentativePending
, writeTentativePending
, readNewPending
, writeNewPending
, pendingName
) where
import Prelude ()
import Darcs.Prelude
import Control.Applicative
import qualified Data.ByteString as BS ( empty )
import Darcs.Util.Global ( darcsdir )
import Darcs.Util.Lock ( writeDocBinFile )
import Darcs.Repository.InternalTypes ( Repository(..) )
import Darcs.Patch ( readPatch, RepoPatch, PrimOf )
import Darcs.Patch.Read ( ReadPatch(..), bracketedFL )
import Darcs.Patch.ReadMonads ( ParserM )
import Darcs.Patch.Show ( ShowPatchBasic(..) )
import Darcs.Util.Exception ( catchall )
import Darcs.Patch.Witnesses.Sealed ( Sealed(Sealed), mapSeal )
import Darcs.Patch.Witnesses.Ordered ( FL(..), mapFL )
import Darcs.Util.ByteString ( gzReadFilePS )
import Darcs.Util.Printer ( Doc, ($$), (<>), text, vcat )
pendingName :: String
pendingName = darcsdir ++ "/patches/pending"
newSuffix, tentativeSuffix :: String
newSuffix = ".new"
tentativeSuffix = ".tentative"
readPending :: RepoPatch p => Repository rt p wR wU wT
-> IO (Sealed (FL (PrimOf p) wT))
readPending = readPendingFile ""
readTentativePending :: RepoPatch p => Repository rt p wR wU wT
-> IO (Sealed (FL (PrimOf p) wT))
readTentativePending = readPendingFile tentativeSuffix
readNewPending :: RepoPatch p => Repository rt p wR wU wT
-> IO (Sealed (FL (PrimOf p) wT))
readNewPending = readPendingFile newSuffix
readPendingFile :: ReadPatch prim => String -> Repository rt p wR wU wT
-> IO (Sealed (FL prim wX))
readPendingFile suffix _ = do
pend <- gzReadFilePS (pendingName ++ suffix) `catchall` return BS.empty
return . maybe (Sealed NilFL) (mapSeal unFLM) . readPatch $ pend
newtype FLM p wX wY = FLM { unFLM :: FL p wX wY }
instance ReadPatch p => ReadPatch (FLM p) where
readPatch' = mapSeal FLM <$> readMaybeBracketedFL readPatch' '{' '}'
instance ShowPatchBasic p => ShowPatchBasic (FLM p) where
showPatch = showMaybeBracketedFL showPatch '{' '}' . unFLM
readMaybeBracketedFL :: forall m p wX . ParserM m
=> (forall wY . m (Sealed (p wY))) -> Char -> Char
-> m (Sealed (FL p wX))
readMaybeBracketedFL parser pre post =
bracketedFL parser pre post <|> (mapSeal (:>:NilFL) <$> parser)
showMaybeBracketedFL :: (forall wX wY . p wX wY -> Doc) -> Char -> Char
-> FL p wA wB -> Doc
showMaybeBracketedFL _ pre post NilFL = text [pre] $$ text [post]
showMaybeBracketedFL printer _ _ (p :>: NilFL) = printer p
showMaybeBracketedFL printer pre post ps = text [pre] $$
vcat (mapFL printer ps) $$
text [post]
writeTentativePending :: RepoPatch p => Repository rt p wR wU wT
-> FL (PrimOf p) wT wY -> IO ()
writeTentativePending = writePendingFile tentativeSuffix
writeNewPending :: RepoPatch p => Repository rt p wR wU wT
-> FL (PrimOf p) wT wY -> IO ()
writeNewPending = writePendingFile newSuffix
writePendingFile :: ShowPatchBasic prim => String -> Repository rt p wR wU wT
-> FL prim wX wY -> IO ()
writePendingFile suffix _ = writePatch name . FLM
where
name = pendingName ++ suffix
writePatch :: ShowPatchBasic p => FilePath -> p wX wY -> IO ()
writePatch f p = writeDocBinFile f $ showPatch p <> text "\n"