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

withPatchMods :: FileModMonad a
              -> Set AnchoredPath
              -> (Set AnchoredPath, [PatchMod AnchoredPath])
withPatchMods :: forall a.
FileModMonad a
-> Set AnchoredPath -> (Set AnchoredPath, [PatchMod AnchoredPath])
withPatchMods (FMM State (Set AnchoredPath, [PatchMod AnchoredPath]) a
m) Set AnchoredPath
fps = forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ 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 :: forall x.
FileModMonad x
-> Tree (ApplyMonadBase FileModMonad)
-> FileModMonad (x, Tree (ApplyMonadBase FileModMonad))
nestedApply FileModMonad x
_ Tree (ApplyMonadBase FileModMonad)
_ = forall a. HasCallStack => [Char] -> a
error [Char]
"nestedApply FileModMonad"
    liftApply :: forall x.
(Tree (ApplyMonadBase FileModMonad)
 -> ApplyMonadBase FileModMonad x)
-> Tree (ApplyMonadBase FileModMonad)
-> FileModMonad (x, Tree (ApplyMonadBase FileModMonad))
liftApply Tree (ApplyMonadBase FileModMonad) -> ApplyMonadBase FileModMonad x
_ Tree (ApplyMonadBase FileModMonad)
_ = forall a. HasCallStack => [Char] -> a
error [Char]
"liftApply FileModMonad"
    getApplyState :: FileModMonad (Tree (ApplyMonadBase FileModMonad))
getApplyState = forall a. HasCallStack => [Char] -> a
error [Char]
"getApplyState FileModMonad"

instance ApplyMonadTree FileModMonad where
    mDoesDirectoryExist :: AnchoredPath -> FileModMonad Bool
mDoesDirectoryExist AnchoredPath
d = do
      Set AnchoredPath
fps <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets forall a b. (a, b) -> a
fst
      forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ 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 <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets forall a b. (a, b) -> a
fst
      forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Ord a => a -> Set a -> Bool
S.member AnchoredPath
f Set AnchoredPath
fps
    mReadFilePS :: AnchoredPath -> FileModMonad ByteString
mReadFilePS AnchoredPath
_ = 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 <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets forall a b. (a, b) -> a
fst
      -- we have to account for directory moves
      PatchMod AnchoredPath -> FileModMonad ()
addMod (forall a. a -> a -> PatchMod a
PRename AnchoredPath
a AnchoredPath
b)
      (Set AnchoredPath -> Set AnchoredPath) -> FileModMonad ()
modifyFps (forall a. Ord a => a -> Set a -> Set a
S.delete AnchoredPath
a)
      AnchoredPath -> FileModMonad ()
addFile AnchoredPath
b
      forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (forall a. Set a -> [a]
S.toList Set AnchoredPath
fns) forall a b. (a -> b) -> a -> b
$ \AnchoredPath
fn ->
        forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (AnchoredPath
a AnchoredPath -> AnchoredPath -> Bool
`isPrefix` AnchoredPath
fn Bool -> Bool -> Bool
&& AnchoredPath
a forall a. Eq a => a -> a -> Bool
/= AnchoredPath
fn) forall a b. (a -> b) -> a -> b
$ do
          (Set AnchoredPath -> Set AnchoredPath) -> FileModMonad ()
modifyFps (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 (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 (forall a. a -> PatchMod a
PTouch AnchoredPath
f)

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

addMod :: PatchMod AnchoredPath -> FileModMonad ()
addMod :: PatchMod AnchoredPath -> FileModMonad ()
addMod PatchMod AnchoredPath
pm = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second (PatchMod AnchoredPath
pm forall a. a -> [a] -> [a]
:)

addFile :: AnchoredPath -> FileModMonad ()
addFile :: AnchoredPath -> FileModMonad ()
addFile AnchoredPath
f = (Set AnchoredPath -> Set AnchoredPath) -> FileModMonad ()
modifyFps (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 (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 (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 <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets forall a b. (a, b) -> a
fst
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall a. Ord a => a -> Set a -> Bool
S.member AnchoredPath
fn Set AnchoredPath
fs) forall a b. (a -> b) -> a -> b
$
        forall a. HasCallStack => [Char] -> a
error 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 (forall a. a -> PatchMod a
PRemove AnchoredPath
f) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Set AnchoredPath -> Set AnchoredPath) -> FileModMonad ()
modifyFps (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 = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ 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 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 :: forall (p :: * -> * -> *) wX wY.
(Apply p, ApplyState p ~ Tree) =>
p wX wY
-> Set AnchoredPath -> (Set AnchoredPath, [PatchMod AnchoredPath])
applyToFileMods p wX wY
patch = forall a.
FileModMonad a
-> Set AnchoredPath -> (Set AnchoredPath, [PatchMod AnchoredPath])
withPatchMods (forall (p :: * -> * -> *) (m :: * -> *) wX wY.
(Apply p, ApplyMonad (ApplyState p) m) =>
p wX wY -> m ()
apply p wX wY
patch)