-- Copyright (C) 2009 Benedikt Schmidt
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2, or (at your option)
-- any later version.
--
-- This program is distributed in the hope that it will be useful,

-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; see the file COPYING.  If not, write to
-- the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-- Boston, MA 02110-1301, USA.

{-# LANGUAGE MultiParamTypeClasses #-}

module Darcs.Patch.Index.Monad
    ( withPatchMods
    , applyToFileMods
    , makePatchID
    ) where

import Darcs.Prelude

import Darcs.Patch.Index.Types ( PatchMod(..), PatchId(..) )
import Darcs.Patch.Info ( makePatchname, PatchInfo )
import Darcs.Patch.Apply ( Apply(..) )
import Darcs.Patch.ApplyMonad ( ApplyMonad(..), ApplyMonadTree(..) )
import Control.Monad.State
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)

newtype FileModMonad a = FMM (State (Set AnchoredPath, [PatchMod AnchoredPath]) a)
  deriving ( a -> FileModMonad b -> FileModMonad a
(a -> b) -> FileModMonad a -> FileModMonad b
(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
<$ :: a -> FileModMonad b -> FileModMonad a
$c<$ :: forall a b. a -> FileModMonad b -> FileModMonad a
fmap :: (a -> b) -> FileModMonad a -> FileModMonad b
$cfmap :: forall a b. (a -> b) -> FileModMonad a -> FileModMonad b
Functor
           , Functor FileModMonad
a -> FileModMonad a
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
FileModMonad a -> FileModMonad b -> FileModMonad b
FileModMonad a -> FileModMonad b -> FileModMonad a
FileModMonad (a -> b) -> FileModMonad a -> FileModMonad b
(a -> b -> c) -> FileModMonad a -> FileModMonad b -> FileModMonad c
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
<* :: FileModMonad a -> FileModMonad b -> FileModMonad a
$c<* :: forall a b. FileModMonad a -> FileModMonad b -> FileModMonad a
*> :: FileModMonad a -> FileModMonad b -> FileModMonad b
$c*> :: forall a b. FileModMonad a -> FileModMonad b -> FileModMonad b
liftA2 :: (a -> b -> c) -> FileModMonad a -> FileModMonad b -> FileModMonad c
$cliftA2 :: forall a b c.
(a -> b -> c) -> FileModMonad a -> FileModMonad b -> FileModMonad c
<*> :: FileModMonad (a -> b) -> FileModMonad a -> FileModMonad b
$c<*> :: forall a b.
FileModMonad (a -> b) -> FileModMonad a -> FileModMonad b
pure :: a -> FileModMonad a
$cpure :: forall a. a -> FileModMonad a
$cp1Applicative :: Functor FileModMonad
Applicative
           , Applicative FileModMonad
a -> FileModMonad a
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
FileModMonad a -> (a -> FileModMonad b) -> FileModMonad b
FileModMonad a -> FileModMonad b -> FileModMonad b
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
return :: a -> FileModMonad a
$creturn :: forall a. a -> FileModMonad a
>> :: FileModMonad a -> FileModMonad b -> FileModMonad b
$c>> :: forall a b. FileModMonad a -> FileModMonad b -> FileModMonad b
>>= :: FileModMonad a -> (a -> FileModMonad b) -> FileModMonad b
$c>>= :: forall a b.
FileModMonad a -> (a -> FileModMonad b) -> FileModMonad b
$cp1Monad :: Applicative FileModMonad
Monad
           , MonadState (Set AnchoredPath, [PatchMod AnchoredPath])
           )

withPatchMods :: FileModMonad a
              -> Set AnchoredPath
              -> (Set AnchoredPath, [PatchMod AnchoredPath])
withPatchMods :: FileModMonad a
-> Set AnchoredPath -> (Set AnchoredPath, [PatchMod AnchoredPath])
withPatchMods (FMM State (Set AnchoredPath, [PatchMod AnchoredPath]) a
m) Set AnchoredPath
fps = ([PatchMod AnchoredPath] -> [PatchMod AnchoredPath])
-> (Set AnchoredPath, [PatchMod AnchoredPath])
-> (Set AnchoredPath, [PatchMod AnchoredPath])
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second [PatchMod AnchoredPath] -> [PatchMod AnchoredPath]
forall a. [a] -> [a]
reverse ((Set AnchoredPath, [PatchMod AnchoredPath])
 -> (Set AnchoredPath, [PatchMod AnchoredPath]))
-> (Set AnchoredPath, [PatchMod AnchoredPath])
-> (Set AnchoredPath, [PatchMod AnchoredPath])
forall a b. (a -> b) -> a -> b
$ State (Set AnchoredPath, [PatchMod AnchoredPath]) a
-> (Set AnchoredPath, [PatchMod AnchoredPath])
-> (Set AnchoredPath, [PatchMod AnchoredPath])
forall s a. State s a -> s -> s
execState State (Set AnchoredPath, [PatchMod AnchoredPath]) a
m (Set AnchoredPath
fps,[])

-- These instances are defined to be used only with
-- apply.
instance ApplyMonad Tree FileModMonad where
    type ApplyMonadBase FileModMonad = FileModMonad
    nestedApply :: FileModMonad x
-> Tree (ApplyMonadBase FileModMonad)
-> FileModMonad (x, Tree (ApplyMonadBase FileModMonad))
nestedApply FileModMonad x
_ Tree (ApplyMonadBase FileModMonad)
_ = [Char] -> FileModMonad (x, Tree FileModMonad)
forall a. HasCallStack => [Char] -> a
error [Char]
"nestedApply FileModMonad"
    liftApply :: (Tree (ApplyMonadBase FileModMonad)
 -> ApplyMonadBase FileModMonad x)
-> Tree (ApplyMonadBase FileModMonad)
-> FileModMonad (x, Tree (ApplyMonadBase FileModMonad))
liftApply Tree (ApplyMonadBase FileModMonad) -> ApplyMonadBase FileModMonad x
_ Tree (ApplyMonadBase FileModMonad)
_ = [Char] -> FileModMonad (x, Tree FileModMonad)
forall a. HasCallStack => [Char] -> a
error [Char]
"liftApply FileModMonad"
    getApplyState :: FileModMonad (Tree (ApplyMonadBase FileModMonad))
getApplyState = [Char] -> FileModMonad (Tree FileModMonad)
forall a. HasCallStack => [Char] -> a
error [Char]
"getApplyState FileModMonad"

instance ApplyMonadTree FileModMonad where
    mDoesDirectoryExist :: AnchoredPath -> FileModMonad Bool
mDoesDirectoryExist AnchoredPath
d = do
      Set AnchoredPath
fps <- ((Set AnchoredPath, [PatchMod AnchoredPath]) -> Set AnchoredPath)
-> FileModMonad (Set AnchoredPath)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (Set AnchoredPath, [PatchMod AnchoredPath]) -> Set AnchoredPath
forall a b. (a, b) -> a
fst
      Bool -> FileModMonad Bool
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, [PatchMod AnchoredPath]) -> Set AnchoredPath)
-> FileModMonad (Set AnchoredPath)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (Set AnchoredPath, [PatchMod AnchoredPath]) -> Set AnchoredPath
forall a b. (a, b) -> a
fst
      Bool -> FileModMonad Bool
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
_ = [Char] -> FileModMonad ByteString
forall a. HasCallStack => [Char] -> a
error [Char]
"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, [PatchMod AnchoredPath]) -> Set AnchoredPath)
-> FileModMonad (Set AnchoredPath)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (Set AnchoredPath, [PatchMod AnchoredPath]) -> Set AnchoredPath
forall a b. (a, b) -> a
fst
      -- we have to account for directory moves
      PatchMod AnchoredPath -> FileModMonad ()
addMod (AnchoredPath -> AnchoredPath -> PatchMod AnchoredPath
forall a. a -> a -> PatchMod 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
          PatchMod AnchoredPath -> FileModMonad ()
addMod (AnchoredPath -> AnchoredPath -> PatchMod AnchoredPath
forall a. a -> a -> PatchMod a
PRename AnchoredPath
fn AnchoredPath
newfn)
    mModifyFilePS :: AnchoredPath
-> (ByteString -> FileModMonad ByteString) -> FileModMonad ()
mModifyFilePS AnchoredPath
f ByteString -> FileModMonad ByteString
_ = PatchMod AnchoredPath -> FileModMonad ()
addMod (AnchoredPath -> PatchMod AnchoredPath
forall a. a -> PatchMod a
PTouch AnchoredPath
f)

-- ---------------------------------------------------------------------
-- State Handling Functions

addMod :: PatchMod AnchoredPath -> FileModMonad ()
addMod :: PatchMod AnchoredPath -> FileModMonad ()
addMod PatchMod AnchoredPath
pm = ((Set AnchoredPath, [PatchMod AnchoredPath])
 -> (Set AnchoredPath, [PatchMod AnchoredPath]))
-> FileModMonad ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (((Set AnchoredPath, [PatchMod AnchoredPath])
  -> (Set AnchoredPath, [PatchMod AnchoredPath]))
 -> FileModMonad ())
-> ((Set AnchoredPath, [PatchMod AnchoredPath])
    -> (Set AnchoredPath, [PatchMod AnchoredPath]))
-> FileModMonad ()
forall a b. (a -> b) -> a -> b
$ ([PatchMod AnchoredPath] -> [PatchMod AnchoredPath])
-> (Set AnchoredPath, [PatchMod AnchoredPath])
-> (Set AnchoredPath, [PatchMod AnchoredPath])
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second (PatchMod AnchoredPath
pm PatchMod AnchoredPath
-> [PatchMod AnchoredPath] -> [PatchMod 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
  PatchMod AnchoredPath -> FileModMonad ()
addMod (AnchoredPath -> PatchMod AnchoredPath
forall a. a -> PatchMod 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
  PatchMod AnchoredPath -> FileModMonad ()
addMod (AnchoredPath -> PatchMod AnchoredPath
forall a. a -> PatchMod 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, [PatchMod AnchoredPath]) -> Set AnchoredPath)
-> FileModMonad (Set AnchoredPath)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (Set AnchoredPath, [PatchMod 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
$
        [Char] -> FileModMonad ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> FileModMonad ()) -> [Char] -> FileModMonad ()
forall a b. (a -> b) -> a -> b
$ [[Char]] -> [Char]
unwords [ [Char]
"error: patch index entry for"
                        , if Bool
isFile then [Char]
"file" else [Char]
"directory"
                        , [Char] -> AnchoredPath -> [Char]
anchorPath [Char]
"" AnchoredPath
fn
                        , [Char]
"created >1 times. Run `darcs repair` and try again."
                        ]

remove :: AnchoredPath -> FileModMonad ()
remove :: AnchoredPath -> FileModMonad ()
remove AnchoredPath
f = PatchMod AnchoredPath -> FileModMonad ()
addMod (AnchoredPath -> PatchMod AnchoredPath
forall a. a -> PatchMod a
PRemove AnchoredPath
f) FileModMonad () -> FileModMonad () -> FileModMonad ()
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, [PatchMod AnchoredPath])
 -> (Set AnchoredPath, [PatchMod AnchoredPath]))
-> FileModMonad ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (((Set AnchoredPath, [PatchMod AnchoredPath])
  -> (Set AnchoredPath, [PatchMod AnchoredPath]))
 -> FileModMonad ())
-> ((Set AnchoredPath, [PatchMod AnchoredPath])
    -> (Set AnchoredPath, [PatchMod AnchoredPath]))
-> FileModMonad ()
forall a b. (a -> b) -> a -> b
$ (Set AnchoredPath -> Set AnchoredPath)
-> (Set AnchoredPath, [PatchMod AnchoredPath])
-> (Set AnchoredPath, [PatchMod AnchoredPath])
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first Set AnchoredPath -> Set AnchoredPath
f

makePatchID :: PatchInfo -> PatchId
makePatchID :: PatchInfo -> PatchId
makePatchID = SHA1 -> PatchId
PID (SHA1 -> PatchId) -> (PatchInfo -> SHA1) -> PatchInfo -> PatchId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PatchInfo -> SHA1
makePatchname

--------------------------------------------------------------------------------
-- | Apply a patch to set of 'AnchoredPath's, yielding the new set of
-- 'AnchoredPath's and 'PatchMod's
applyToFileMods :: (Apply p, ApplyState p ~ Tree)
                => p wX wY
                -> Set AnchoredPath
                -> (Set AnchoredPath, [PatchMod AnchoredPath])
applyToFileMods :: p wX wY
-> Set AnchoredPath -> (Set AnchoredPath, [PatchMod AnchoredPath])
applyToFileMods p wX wY
patch = FileModMonad ()
-> Set AnchoredPath -> (Set AnchoredPath, [PatchMod AnchoredPath])
forall a.
FileModMonad a
-> Set AnchoredPath -> (Set AnchoredPath, [PatchMod AnchoredPath])
withPatchMods (p wX wY -> FileModMonad ()
forall (p :: * -> * -> *) (m :: * -> *) wX wY.
(Apply p, ApplyMonad (ApplyState p) m) =>
p wX wY -> m ()
apply p wX wY
patch)