{-# LANGUAGE MultiParamTypeClasses #-}
module Darcs.Patch.Index.Monad
( withPatchMods
, applyToFileMods
, FileMod(..)
) where
import Darcs.Prelude
import Darcs.Patch.Apply ( Apply(..) )
import Darcs.Patch.ApplyMonad ( ApplyMonad(..), ApplyMonadTree(..) )
import Control.Monad ( when, forM_ )
import Control.Monad.Catch ( MonadThrow(..), SomeException )
import Control.Monad.State ( MonadState, StateT, execStateT, gets, modify )
import Control.Arrow
import Darcs.Util.Path ( AnchoredPath, anchorPath, movedirfilename, isPrefix )
import qualified Data.Set as S
import Data.Set ( Set )
import Darcs.Util.Tree (Tree)
data FileMod a
= PTouch a
| PCreateFile a
| PCreateDir a
| PRename a a
| PRemove a
| PDuplicateTouch a
deriving (Int -> FileMod a -> ShowS
[FileMod a] -> ShowS
FileMod a -> String
(Int -> FileMod a -> ShowS)
-> (FileMod a -> String)
-> ([FileMod a] -> ShowS)
-> Show (FileMod a)
forall a. Show a => Int -> FileMod a -> ShowS
forall a. Show a => [FileMod a] -> ShowS
forall a. Show a => FileMod a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> FileMod a -> ShowS
showsPrec :: Int -> FileMod a -> ShowS
$cshow :: forall a. Show a => FileMod a -> String
show :: FileMod a -> String
$cshowList :: forall a. Show a => [FileMod a] -> ShowS
showList :: [FileMod a] -> ShowS
Show, FileMod a -> FileMod a -> Bool
(FileMod a -> FileMod a -> Bool)
-> (FileMod a -> FileMod a -> Bool) -> Eq (FileMod a)
forall a. Eq a => FileMod a -> FileMod a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => FileMod a -> FileMod a -> Bool
== :: FileMod a -> FileMod a -> Bool
$c/= :: forall a. Eq a => FileMod a -> FileMod a -> Bool
/= :: FileMod a -> FileMod a -> Bool
Eq, (forall a b. (a -> b) -> FileMod a -> FileMod b)
-> (forall a b. a -> FileMod b -> FileMod a) -> Functor FileMod
forall a b. a -> FileMod b -> FileMod a
forall a b. (a -> b) -> FileMod a -> FileMod b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> FileMod a -> FileMod b
fmap :: forall a b. (a -> b) -> FileMod a -> FileMod b
$c<$ :: forall a b. a -> FileMod b -> FileMod a
<$ :: forall a b. a -> FileMod b -> FileMod a
Functor)
type FileModState = (Set AnchoredPath, [FileMod AnchoredPath])
newtype FileModMonad a =
FMM (StateT FileModState (Either SomeException) a)
deriving ( (forall a b. (a -> b) -> FileModMonad a -> FileModMonad b)
-> (forall a b. a -> FileModMonad b -> FileModMonad a)
-> Functor FileModMonad
forall a b. a -> FileModMonad b -> FileModMonad a
forall a b. (a -> b) -> FileModMonad a -> FileModMonad b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> FileModMonad a -> FileModMonad b
fmap :: forall a b. (a -> b) -> FileModMonad a -> FileModMonad b
$c<$ :: forall a b. a -> FileModMonad b -> FileModMonad a
<$ :: forall a b. a -> FileModMonad b -> FileModMonad a
Functor
, Functor FileModMonad
Functor FileModMonad =>
(forall a. a -> FileModMonad a)
-> (forall a b.
FileModMonad (a -> b) -> FileModMonad a -> FileModMonad b)
-> (forall a b c.
(a -> b -> c)
-> FileModMonad a -> FileModMonad b -> FileModMonad c)
-> (forall a b. FileModMonad a -> FileModMonad b -> FileModMonad b)
-> (forall a b. FileModMonad a -> FileModMonad b -> FileModMonad a)
-> Applicative FileModMonad
forall a. a -> FileModMonad a
forall a b. FileModMonad a -> FileModMonad b -> FileModMonad a
forall a b. FileModMonad a -> FileModMonad b -> FileModMonad b
forall a b.
FileModMonad (a -> b) -> FileModMonad a -> FileModMonad b
forall a b c.
(a -> b -> c) -> FileModMonad a -> FileModMonad b -> FileModMonad c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
$cpure :: forall a. a -> FileModMonad a
pure :: forall a. a -> FileModMonad a
$c<*> :: forall a b.
FileModMonad (a -> b) -> FileModMonad a -> FileModMonad b
<*> :: forall a b.
FileModMonad (a -> b) -> FileModMonad a -> FileModMonad b
$cliftA2 :: forall a b c.
(a -> b -> c) -> FileModMonad a -> FileModMonad b -> FileModMonad c
liftA2 :: forall a b c.
(a -> b -> c) -> FileModMonad a -> FileModMonad b -> FileModMonad c
$c*> :: forall a b. FileModMonad a -> FileModMonad b -> FileModMonad b
*> :: forall a b. FileModMonad a -> FileModMonad b -> FileModMonad b
$c<* :: forall a b. FileModMonad a -> FileModMonad b -> FileModMonad a
<* :: forall a b. FileModMonad a -> FileModMonad b -> FileModMonad a
Applicative
, Applicative FileModMonad
Applicative FileModMonad =>
(forall a b.
FileModMonad a -> (a -> FileModMonad b) -> FileModMonad b)
-> (forall a b. FileModMonad a -> FileModMonad b -> FileModMonad b)
-> (forall a. a -> FileModMonad a)
-> Monad FileModMonad
forall a. a -> FileModMonad a
forall a b. FileModMonad a -> FileModMonad b -> FileModMonad b
forall a b.
FileModMonad a -> (a -> FileModMonad b) -> FileModMonad b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
$c>>= :: forall a b.
FileModMonad a -> (a -> FileModMonad b) -> FileModMonad b
>>= :: forall a b.
FileModMonad a -> (a -> FileModMonad b) -> FileModMonad b
$c>> :: forall a b. FileModMonad a -> FileModMonad b -> FileModMonad b
>> :: forall a b. FileModMonad a -> FileModMonad b -> FileModMonad b
$creturn :: forall a. a -> FileModMonad a
return :: forall a. a -> FileModMonad a
Monad
, Monad FileModMonad
Monad FileModMonad =>
(forall e a. (HasCallStack, Exception e) => e -> FileModMonad a)
-> MonadThrow FileModMonad
forall e a. (HasCallStack, Exception e) => e -> FileModMonad a
forall (m :: * -> *).
Monad m =>
(forall e a. (HasCallStack, Exception e) => e -> m a)
-> MonadThrow m
$cthrowM :: forall e a. (HasCallStack, Exception e) => e -> FileModMonad a
throwM :: forall e a. (HasCallStack, Exception e) => e -> FileModMonad a
MonadThrow
, MonadState FileModState
)
withPatchMods :: FileModMonad a
-> Set AnchoredPath
-> FileModState
withPatchMods :: forall a.
FileModMonad a
-> Set AnchoredPath -> (Set AnchoredPath, [FileMod AnchoredPath])
withPatchMods (FMM StateT
(Set AnchoredPath, [FileMod AnchoredPath]) (Either SomeException) a
m) Set AnchoredPath
fps =
([FileMod AnchoredPath] -> [FileMod AnchoredPath])
-> (Set AnchoredPath, [FileMod AnchoredPath])
-> (Set AnchoredPath, [FileMod AnchoredPath])
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second [FileMod AnchoredPath] -> [FileMod AnchoredPath]
forall a. [a] -> [a]
reverse ((Set AnchoredPath, [FileMod AnchoredPath])
-> (Set AnchoredPath, [FileMod AnchoredPath]))
-> (Set AnchoredPath, [FileMod AnchoredPath])
-> (Set AnchoredPath, [FileMod AnchoredPath])
forall a b. (a -> b) -> a -> b
$
case StateT
(Set AnchoredPath, [FileMod AnchoredPath]) (Either SomeException) a
-> (Set AnchoredPath, [FileMod AnchoredPath])
-> Either SomeException (Set AnchoredPath, [FileMod AnchoredPath])
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m s
execStateT StateT
(Set AnchoredPath, [FileMod AnchoredPath]) (Either SomeException) a
m (Set AnchoredPath
fps,[]) of
Left SomeException
e -> String -> (Set AnchoredPath, [FileMod AnchoredPath])
forall a. HasCallStack => String -> a
error (SomeException -> String
forall a. Show a => a -> String
show SomeException
e)
Right (Set AnchoredPath, [FileMod AnchoredPath])
r -> (Set AnchoredPath, [FileMod AnchoredPath])
r
instance ApplyMonad Tree FileModMonad where
readFilePS :: ObjectIdOf Tree -> FileModMonad ByteString
readFilePS = String -> AnchoredPath -> FileModMonad ByteString
forall a. HasCallStack => String -> a
error String
"readFilePS FileModMonad"
instance ApplyMonadTree FileModMonad where
mDoesDirectoryExist :: AnchoredPath -> FileModMonad Bool
mDoesDirectoryExist AnchoredPath
d = do
Set AnchoredPath
fps <- ((Set AnchoredPath, [FileMod AnchoredPath]) -> Set AnchoredPath)
-> FileModMonad (Set AnchoredPath)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (Set AnchoredPath, [FileMod AnchoredPath]) -> Set AnchoredPath
forall a b. (a, b) -> a
fst
Bool -> FileModMonad Bool
forall a. a -> FileModMonad a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> FileModMonad Bool) -> Bool -> FileModMonad Bool
forall a b. (a -> b) -> a -> b
$ AnchoredPath -> Set AnchoredPath -> Bool
forall a. Ord a => a -> Set a -> Bool
S.member AnchoredPath
d Set AnchoredPath
fps
mDoesFileExist :: AnchoredPath -> FileModMonad Bool
mDoesFileExist AnchoredPath
f = do
Set AnchoredPath
fps <- ((Set AnchoredPath, [FileMod AnchoredPath]) -> Set AnchoredPath)
-> FileModMonad (Set AnchoredPath)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (Set AnchoredPath, [FileMod AnchoredPath]) -> Set AnchoredPath
forall a b. (a, b) -> a
fst
Bool -> FileModMonad Bool
forall a. a -> FileModMonad a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> FileModMonad Bool) -> Bool -> FileModMonad Bool
forall a b. (a -> b) -> a -> b
$ AnchoredPath -> Set AnchoredPath -> Bool
forall a. Ord a => a -> Set a -> Bool
S.member AnchoredPath
f Set AnchoredPath
fps
mReadFilePS :: AnchoredPath -> FileModMonad ByteString
mReadFilePS AnchoredPath
_ = String -> FileModMonad ByteString
forall a. HasCallStack => String -> a
error String
"mReadFilePS FileModMonad"
mCreateFile :: AnchoredPath -> FileModMonad ()
mCreateFile = AnchoredPath -> FileModMonad ()
createFile
mCreateDirectory :: AnchoredPath -> FileModMonad ()
mCreateDirectory = AnchoredPath -> FileModMonad ()
createDir
mRemoveFile :: AnchoredPath -> FileModMonad ()
mRemoveFile = AnchoredPath -> FileModMonad ()
remove
mRemoveDirectory :: AnchoredPath -> FileModMonad ()
mRemoveDirectory = AnchoredPath -> FileModMonad ()
remove
mRename :: AnchoredPath -> AnchoredPath -> FileModMonad ()
mRename AnchoredPath
a AnchoredPath
b = do
Set AnchoredPath
fns <- ((Set AnchoredPath, [FileMod AnchoredPath]) -> Set AnchoredPath)
-> FileModMonad (Set AnchoredPath)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (Set AnchoredPath, [FileMod AnchoredPath]) -> Set AnchoredPath
forall a b. (a, b) -> a
fst
FileMod AnchoredPath -> FileModMonad ()
addMod (AnchoredPath -> AnchoredPath -> FileMod AnchoredPath
forall a. a -> a -> FileMod a
PRename AnchoredPath
a AnchoredPath
b)
(Set AnchoredPath -> Set AnchoredPath) -> FileModMonad ()
modifyFps (AnchoredPath -> Set AnchoredPath -> Set AnchoredPath
forall a. Ord a => a -> Set a -> Set a
S.delete AnchoredPath
a)
AnchoredPath -> FileModMonad ()
addFile AnchoredPath
b
[AnchoredPath]
-> (AnchoredPath -> FileModMonad ()) -> FileModMonad ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Set AnchoredPath -> [AnchoredPath]
forall a. Set a -> [a]
S.toList Set AnchoredPath
fns) ((AnchoredPath -> FileModMonad ()) -> FileModMonad ())
-> (AnchoredPath -> FileModMonad ()) -> FileModMonad ()
forall a b. (a -> b) -> a -> b
$ \AnchoredPath
fn ->
Bool -> FileModMonad () -> FileModMonad ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (AnchoredPath
a AnchoredPath -> AnchoredPath -> Bool
`isPrefix` AnchoredPath
fn Bool -> Bool -> Bool
&& AnchoredPath
a AnchoredPath -> AnchoredPath -> Bool
forall a. Eq a => a -> a -> Bool
/= AnchoredPath
fn) (FileModMonad () -> FileModMonad ())
-> FileModMonad () -> FileModMonad ()
forall a b. (a -> b) -> a -> b
$ do
(Set AnchoredPath -> Set AnchoredPath) -> FileModMonad ()
modifyFps (AnchoredPath -> Set AnchoredPath -> Set AnchoredPath
forall a. Ord a => a -> Set a -> Set a
S.delete AnchoredPath
fn)
let newfn :: AnchoredPath
newfn = AnchoredPath -> AnchoredPath -> AnchoredPath -> AnchoredPath
movedirfilename AnchoredPath
a AnchoredPath
b AnchoredPath
fn
AnchoredPath -> FileModMonad ()
addFile AnchoredPath
newfn
FileMod AnchoredPath -> FileModMonad ()
addMod (AnchoredPath -> AnchoredPath -> FileMod AnchoredPath
forall a. a -> a -> FileMod a
PRename AnchoredPath
fn AnchoredPath
newfn)
mModifyFilePS :: AnchoredPath
-> (ByteString -> FileModMonad ByteString) -> FileModMonad ()
mModifyFilePS AnchoredPath
f ByteString -> FileModMonad ByteString
_ = FileMod AnchoredPath -> FileModMonad ()
addMod (AnchoredPath -> FileMod AnchoredPath
forall a. a -> FileMod a
PTouch AnchoredPath
f)
addMod :: FileMod AnchoredPath -> FileModMonad ()
addMod :: FileMod AnchoredPath -> FileModMonad ()
addMod FileMod AnchoredPath
pm = ((Set AnchoredPath, [FileMod AnchoredPath])
-> (Set AnchoredPath, [FileMod AnchoredPath]))
-> FileModMonad ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (((Set AnchoredPath, [FileMod AnchoredPath])
-> (Set AnchoredPath, [FileMod AnchoredPath]))
-> FileModMonad ())
-> ((Set AnchoredPath, [FileMod AnchoredPath])
-> (Set AnchoredPath, [FileMod AnchoredPath]))
-> FileModMonad ()
forall a b. (a -> b) -> a -> b
$ ([FileMod AnchoredPath] -> [FileMod AnchoredPath])
-> (Set AnchoredPath, [FileMod AnchoredPath])
-> (Set AnchoredPath, [FileMod AnchoredPath])
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second (FileMod AnchoredPath
pm FileMod AnchoredPath
-> [FileMod AnchoredPath] -> [FileMod AnchoredPath]
forall a. a -> [a] -> [a]
:)
addFile :: AnchoredPath -> FileModMonad ()
addFile :: AnchoredPath -> FileModMonad ()
addFile AnchoredPath
f = (Set AnchoredPath -> Set AnchoredPath) -> FileModMonad ()
modifyFps (AnchoredPath -> Set AnchoredPath -> Set AnchoredPath
forall a. Ord a => a -> Set a -> Set a
S.insert AnchoredPath
f)
createFile :: AnchoredPath -> FileModMonad ()
createFile :: AnchoredPath -> FileModMonad ()
createFile AnchoredPath
fn = do
AnchoredPath -> Bool -> FileModMonad ()
errorIfPresent AnchoredPath
fn Bool
True
FileMod AnchoredPath -> FileModMonad ()
addMod (AnchoredPath -> FileMod AnchoredPath
forall a. a -> FileMod a
PCreateFile AnchoredPath
fn)
AnchoredPath -> FileModMonad ()
addFile AnchoredPath
fn
createDir :: AnchoredPath -> FileModMonad ()
createDir :: AnchoredPath -> FileModMonad ()
createDir AnchoredPath
fn = do
AnchoredPath -> Bool -> FileModMonad ()
errorIfPresent AnchoredPath
fn Bool
False
FileMod AnchoredPath -> FileModMonad ()
addMod (AnchoredPath -> FileMod AnchoredPath
forall a. a -> FileMod a
PCreateDir AnchoredPath
fn)
AnchoredPath -> FileModMonad ()
addFile AnchoredPath
fn
errorIfPresent :: AnchoredPath -> Bool -> FileModMonad ()
errorIfPresent :: AnchoredPath -> Bool -> FileModMonad ()
errorIfPresent AnchoredPath
fn Bool
isFile = do
Set AnchoredPath
fs <- ((Set AnchoredPath, [FileMod AnchoredPath]) -> Set AnchoredPath)
-> FileModMonad (Set AnchoredPath)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (Set AnchoredPath, [FileMod AnchoredPath]) -> Set AnchoredPath
forall a b. (a, b) -> a
fst
Bool -> FileModMonad () -> FileModMonad ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (AnchoredPath -> Set AnchoredPath -> Bool
forall a. Ord a => a -> Set a -> Bool
S.member AnchoredPath
fn Set AnchoredPath
fs) (FileModMonad () -> FileModMonad ())
-> FileModMonad () -> FileModMonad ()
forall a b. (a -> b) -> a -> b
$ IOError -> FileModMonad ()
forall e a. (HasCallStack, Exception e) => e -> FileModMonad a
forall (m :: * -> *) e a.
(MonadThrow m, HasCallStack, Exception e) =>
e -> m a
throwM (IOError -> FileModMonad ()) -> IOError -> FileModMonad ()
forall a b. (a -> b) -> a -> b
$ String -> IOError
userError (String -> IOError) -> String -> IOError
forall a b. (a -> b) -> a -> b
$ [String] -> String
unwords
[ String
"error: patch index entry for"
, if Bool
isFile then String
"file" else String
"directory"
, String -> AnchoredPath -> String
anchorPath String
"" AnchoredPath
fn
, String
"created >1 times. Run `darcs repair` and try again."
]
remove :: AnchoredPath -> FileModMonad ()
remove :: AnchoredPath -> FileModMonad ()
remove AnchoredPath
f = FileMod AnchoredPath -> FileModMonad ()
addMod (AnchoredPath -> FileMod AnchoredPath
forall a. a -> FileMod a
PRemove AnchoredPath
f) FileModMonad () -> FileModMonad () -> FileModMonad ()
forall a b. FileModMonad a -> FileModMonad b -> FileModMonad b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Set AnchoredPath -> Set AnchoredPath) -> FileModMonad ()
modifyFps (AnchoredPath -> Set AnchoredPath -> Set AnchoredPath
forall a. Ord a => a -> Set a -> Set a
S.delete AnchoredPath
f)
modifyFps :: (Set AnchoredPath -> Set AnchoredPath) -> FileModMonad ()
modifyFps :: (Set AnchoredPath -> Set AnchoredPath) -> FileModMonad ()
modifyFps Set AnchoredPath -> Set AnchoredPath
f = ((Set AnchoredPath, [FileMod AnchoredPath])
-> (Set AnchoredPath, [FileMod AnchoredPath]))
-> FileModMonad ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (((Set AnchoredPath, [FileMod AnchoredPath])
-> (Set AnchoredPath, [FileMod AnchoredPath]))
-> FileModMonad ())
-> ((Set AnchoredPath, [FileMod AnchoredPath])
-> (Set AnchoredPath, [FileMod AnchoredPath]))
-> FileModMonad ()
forall a b. (a -> b) -> a -> b
$ (Set AnchoredPath -> Set AnchoredPath)
-> (Set AnchoredPath, [FileMod AnchoredPath])
-> (Set AnchoredPath, [FileMod AnchoredPath])
forall b c d. (b -> c) -> (b, d) -> (c, d)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first Set AnchoredPath -> Set AnchoredPath
f
applyToFileMods :: (Apply p, ApplyState p ~ Tree)
=> p wX wY
-> Set AnchoredPath
-> FileModState
applyToFileMods :: forall (p :: * -> * -> *) wX wY.
(Apply p, ApplyState p ~ Tree) =>
p wX wY
-> Set AnchoredPath -> (Set AnchoredPath, [FileMod AnchoredPath])
applyToFileMods p wX wY
patch = FileModMonad ()
-> Set AnchoredPath -> (Set AnchoredPath, [FileMod AnchoredPath])
forall a.
FileModMonad a
-> Set AnchoredPath -> (Set AnchoredPath, [FileMod AnchoredPath])
withPatchMods (p wX wY -> FileModMonad ()
forall (m :: * -> *) wX wY.
ApplyMonad (ApplyState p) m =>
p wX wY -> m ()
forall (p :: * -> * -> *) (m :: * -> *) wX wY.
(Apply p, ApplyMonad (ApplyState p) m) =>
p wX wY -> m ()
apply p wX wY
patch)