{-# LANGUAGE UndecidableInstances #-}
module Darcs.Patch.Rebase.Suspended
( Suspended(..)
, countToEdit, simplifyPush, simplifyPushes
, addFixupsToSuspended, removeFixupsFromSuspended
, addToEditsToSuspended
) where
import Darcs.Prelude
import Darcs.Patch.Effect ( Effect(..) )
import Darcs.Patch.Format ( PatchListFormat(..) )
import Darcs.Patch.Invert ( invert )
import Darcs.Patch.Named ( Named(..) )
import Darcs.Patch.Commute ( Commute(..) )
import Darcs.Patch.Info ( replaceJunk )
import Darcs.Patch.Inspect ( PatchInspect(..) )
import Darcs.Patch.Read ( ReadPatch(..) )
import Darcs.Patch.FromPrim ( PrimPatchBase(..), FromPrim(..), FromPrim(..) )
import Darcs.Patch.Read ( bracketedFL )
import Darcs.Patch.Rebase.Fixup ( RebaseFixup(..), namedToFixups )
import Darcs.Patch.Rebase.Name ( RebaseName(..) )
import Darcs.Patch.RepoPatch ( RepoPatch )
import qualified Darcs.Patch.Rebase.Change as Change ( simplifyPush, simplifyPushes )
import Darcs.Patch.Rebase.Change ( RebaseChange(..), addNamedToRebase )
import qualified Darcs.Patch.Rebase.Legacy.Item as Item ( toRebaseChanges, RebaseItem )
import Darcs.Patch.Show ( ShowPatchBasic(..) )
import Darcs.Util.Parser ( lexString, lexWord )
import Darcs.Patch.Witnesses.Ordered
import Darcs.Patch.Witnesses.Sealed
import Darcs.Patch.Witnesses.Show ( Show1, Show2 )
import Darcs.Util.Printer ( vcat, text, blueText, ($$), (<+>) )
import qualified Darcs.Util.Diff as D ( DiffAlgorithm(MyersDiff) )
import Control.Applicative ( (<|>) )
import qualified Data.ByteString.Char8 as BC ( pack )
data Suspended p wX wY where
Items :: FL (RebaseChange (PrimOf p)) wX wY -> Suspended p wX wX
deriving instance (Show2 p, Show2 (PrimOf p)) => Show (Suspended p wX wY)
instance (Show2 p, Show2 (PrimOf p)) => Show1 (Suspended p wX)
instance (Show2 p, Show2 (PrimOf p)) => Show2 (Suspended p)
instance (PrimPatchBase p, PatchInspect p) => PatchInspect (Suspended p) where
listTouchedFiles :: Suspended p wX wY -> [AnchoredPath]
listTouchedFiles (Items FL (RebaseChange (PrimOf p)) wX wY
ps) = FL (RebaseChange (PrimOf p)) wX wY -> [AnchoredPath]
forall (p :: * -> * -> *) wX wY.
PatchInspect p =>
p wX wY -> [AnchoredPath]
listTouchedFiles FL (RebaseChange (PrimOf p)) wX wY
ps
hunkMatches :: (ByteString -> Bool) -> Suspended p wX wY -> Bool
hunkMatches ByteString -> Bool
f (Items FL (RebaseChange (PrimOf p)) wX wY
ps) = (ByteString -> Bool) -> FL (RebaseChange (PrimOf p)) wX wY -> Bool
forall (p :: * -> * -> *) wX wY.
PatchInspect p =>
(ByteString -> Bool) -> p wX wY -> Bool
hunkMatches ByteString -> Bool
f FL (RebaseChange (PrimOf p)) wX wY
ps
instance (PrimPatchBase p, PatchListFormat p, ShowPatchBasic p) => ShowPatchBasic (Suspended p) where
showPatch :: ShowPatchFor -> Suspended p wX wY -> Doc
showPatch ShowPatchFor
f (Items FL (RebaseChange (PrimOf p)) wX wY
ps)
= String -> Doc
blueText String
"rebase" Doc -> Doc -> Doc
<+> String -> Doc
text String
"0.2" Doc -> Doc -> Doc
<+> String -> Doc
blueText String
"{"
Doc -> Doc -> Doc
$$ [Doc] -> Doc
vcat ((forall wW wZ. RebaseChange (PrimOf p) wW wZ -> Doc)
-> FL (RebaseChange (PrimOf p)) wX wY -> [Doc]
forall (a :: * -> * -> *) b wX wY.
(forall wW wZ. a wW wZ -> b) -> FL a wX wY -> [b]
mapFL (ShowPatchFor -> RebaseChange (PrimOf p) wW wZ -> Doc
forall (p :: * -> * -> *) wX wY.
ShowPatchBasic p =>
ShowPatchFor -> p wX wY -> Doc
showPatch ShowPatchFor
f) FL (RebaseChange (PrimOf p)) wX wY
ps)
Doc -> Doc -> Doc
$$ String -> Doc
blueText String
"}"
instance (PrimPatchBase p, PatchListFormat p, ReadPatch p, RepoPatch p) => ReadPatch (Suspended p) where
readPatch' :: Parser (Sealed (Suspended p wX))
readPatch' =
do ByteString -> Parser ()
lexString (String -> ByteString
BC.pack String
"rebase")
ByteString
version <- Parser ByteString
lexWord
case () of
()
_ | ByteString
version ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== String -> ByteString
BC.pack String
"0.2" ->
(ByteString -> Parser ()
lexString (String -> ByteString
BC.pack String
"{}") Parser ()
-> Parser (Sealed (Suspended p wX))
-> Parser (Sealed (Suspended p wX))
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Sealed (Suspended p wX) -> Parser (Sealed (Suspended p wX))
forall (m :: * -> *) a. Monad m => a -> m a
return (Suspended p wX wX -> Sealed (Suspended p wX)
forall (a :: * -> *) wX. a wX -> Sealed a
seal (FL (RebaseChange (PrimOf p)) wX wX -> Suspended p wX wX
forall (p :: * -> * -> *) wX wY.
FL (RebaseChange (PrimOf p)) wX wY -> Suspended p wX wX
Items FL (RebaseChange (PrimOf p)) wX wX
forall (a :: * -> * -> *) wX. FL a wX wX
NilFL)))
Parser (Sealed (Suspended p wX))
-> Parser (Sealed (Suspended p wX))
-> Parser (Sealed (Suspended p wX))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
((forall wX.
FL (RebaseChange (PrimOf p)) wX wX -> Sealed (Suspended p wX))
-> Sealed (FL (RebaseChange (PrimOf p)) wX)
-> Sealed (Suspended p wX)
forall (a :: * -> *) b. (forall wX. a wX -> b) -> Sealed a -> b
unseal (Suspended p wX wX -> Sealed (Suspended p wX)
forall (a :: * -> *) wX. a wX -> Sealed a
Sealed (Suspended p wX wX -> Sealed (Suspended p wX))
-> (FL (RebaseChange (PrimOf p)) wX wX -> Suspended p wX wX)
-> FL (RebaseChange (PrimOf p)) wX wX
-> Sealed (Suspended p wX)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FL (RebaseChange (PrimOf p)) wX wX -> Suspended p wX wX
forall (p :: * -> * -> *) wX wY.
FL (RebaseChange (PrimOf p)) wX wY -> Suspended p wX wX
Items) (Sealed (FL (RebaseChange (PrimOf p)) wX)
-> Sealed (Suspended p wX))
-> Parser ByteString (Sealed (FL (RebaseChange (PrimOf p)) wX))
-> Parser (Sealed (Suspended p wX))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall wY. Parser (Sealed (RebaseChange (PrimOf p) wY)))
-> Char
-> Char
-> Parser ByteString (Sealed (FL (RebaseChange (PrimOf p)) wX))
forall (p :: * -> * -> *) wX.
(forall wY. Parser (Sealed (p wY)))
-> Char -> Char -> Parser (Sealed (FL p wX))
bracketedFL forall wY. Parser (Sealed (RebaseChange (PrimOf p) wY))
forall (p :: * -> * -> *) wX. ReadPatch p => Parser (Sealed (p wX))
readPatch' Char
'{' Char
'}')
| ByteString
version ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== String -> ByteString
BC.pack String
"0.0" ->
let
itemsToSuspended :: Sealed (FL (Item.RebaseItem p) wX) -> Sealed (Suspended p wX)
itemsToSuspended :: Sealed (FL (RebaseItem p) wX) -> Sealed (Suspended p wX)
itemsToSuspended (Sealed FL (RebaseItem p) wX wX
ps) =
case FL (RebaseItem p) wX wX -> Sealed (FL (RebaseChange (PrimOf p)) wX)
forall (p :: * -> * -> *) wX wY.
RepoPatch p =>
FL (RebaseItem p) wX wY -> Sealed (FL (RebaseChange (PrimOf p)) wX)
Item.toRebaseChanges FL (RebaseItem p) wX wX
ps of
Sealed FL (RebaseChange (PrimOf p)) wX wX
ps' -> Suspended p wX wX -> Sealed (Suspended p wX)
forall (a :: * -> *) wX. a wX -> Sealed a
Sealed (FL (RebaseChange (PrimOf p)) wX wX -> Suspended p wX wX
forall (p :: * -> * -> *) wX wY.
FL (RebaseChange (PrimOf p)) wX wY -> Suspended p wX wX
Items FL (RebaseChange (PrimOf p)) wX wX
ps')
in
(ByteString -> Parser ()
lexString (String -> ByteString
BC.pack String
"{}") Parser ()
-> Parser (Sealed (Suspended p wX))
-> Parser (Sealed (Suspended p wX))
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Sealed (Suspended p wX) -> Parser (Sealed (Suspended p wX))
forall (m :: * -> *) a. Monad m => a -> m a
return (Suspended p wX wX -> Sealed (Suspended p wX)
forall (a :: * -> *) wX. a wX -> Sealed a
seal (FL (RebaseChange (PrimOf p)) wX wX -> Suspended p wX wX
forall (p :: * -> * -> *) wX wY.
FL (RebaseChange (PrimOf p)) wX wY -> Suspended p wX wX
Items FL (RebaseChange (PrimOf p)) wX wX
forall (a :: * -> * -> *) wX. FL a wX wX
NilFL)))
Parser (Sealed (Suspended p wX))
-> Parser (Sealed (Suspended p wX))
-> Parser (Sealed (Suspended p wX))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
Sealed (FL (RebaseItem p) wX) -> Sealed (Suspended p wX)
forall wX. Sealed (FL (RebaseItem p) wX) -> Sealed (Suspended p wX)
itemsToSuspended (Sealed (FL (RebaseItem p) wX) -> Sealed (Suspended p wX))
-> Parser ByteString (Sealed (FL (RebaseItem p) wX))
-> Parser (Sealed (Suspended p wX))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall wY. Parser (Sealed (RebaseItem p wY)))
-> Char
-> Char
-> Parser ByteString (Sealed (FL (RebaseItem p) wX))
forall (p :: * -> * -> *) wX.
(forall wY. Parser (Sealed (p wY)))
-> Char -> Char -> Parser (Sealed (FL p wX))
bracketedFL forall wY. Parser (Sealed (RebaseItem p wY))
forall (p :: * -> * -> *) wX. ReadPatch p => Parser (Sealed (p wX))
readPatch' Char
'{' Char
'}'
| Bool
otherwise -> String -> Parser (Sealed (Suspended p wX))
forall a. HasCallStack => String -> a
error (String -> Parser (Sealed (Suspended p wX)))
-> String -> Parser (Sealed (Suspended p wX))
forall a b. (a -> b) -> a -> b
$ String
"can't handle rebase version " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ByteString -> String
forall a. Show a => a -> String
show ByteString
version
countToEdit :: Suspended p wX wY -> Int
countToEdit :: Suspended p wX wY -> Int
countToEdit (Items FL (RebaseChange (PrimOf p)) wX wY
ps) = FL (RebaseChange (PrimOf p)) wX wY -> Int
forall (a :: * -> * -> *) wX wZ. FL a wX wZ -> Int
lengthFL FL (RebaseChange (PrimOf p)) wX wY
ps
onSuspended
:: (forall wZ . FL (RebaseChange (PrimOf p)) wY wZ -> Sealed (FL (RebaseChange (PrimOf p)) wX))
-> Suspended p wY wY
-> Suspended p wX wX
onSuspended :: (forall wZ.
FL (RebaseChange (PrimOf p)) wY wZ
-> Sealed (FL (RebaseChange (PrimOf p)) wX))
-> Suspended p wY wY -> Suspended p wX wX
onSuspended forall wZ.
FL (RebaseChange (PrimOf p)) wY wZ
-> Sealed (FL (RebaseChange (PrimOf p)) wX)
f (Items FL (RebaseChange (PrimOf p)) wY wY
ps) = (forall wX.
FL (RebaseChange (PrimOf p)) wX wX -> Suspended p wX wX)
-> Sealed (FL (RebaseChange (PrimOf p)) wX) -> Suspended p wX wX
forall (a :: * -> *) b. (forall wX. a wX -> b) -> Sealed a -> b
unseal forall wX. FL (RebaseChange (PrimOf p)) wX wX -> Suspended p wX wX
forall (p :: * -> * -> *) wX wY.
FL (RebaseChange (PrimOf p)) wX wY -> Suspended p wX wX
Items (FL (RebaseChange (PrimOf p)) wY wY
-> Sealed (FL (RebaseChange (PrimOf p)) wX)
forall wZ.
FL (RebaseChange (PrimOf p)) wY wZ
-> Sealed (FL (RebaseChange (PrimOf p)) wX)
f FL (RebaseChange (PrimOf p)) wY wY
ps)
addFixupsToSuspended
:: (PrimPatchBase p, Commute p, FromPrim p, Effect p)
=> Named p wX wY
-> Suspended p wY wY
-> Suspended p wX wX
addFixupsToSuspended :: Named p wX wY -> Suspended p wY wY -> Suspended p wX wX
addFixupsToSuspended Named p wX wY
p = DiffAlgorithm
-> FL (RebaseFixup (PrimOf p)) wX wY
-> Suspended p wY wY
-> Suspended p wX wX
forall (p :: * -> * -> *) wX wY.
(PrimPatchBase p, Commute p, FromPrim p, Effect p) =>
DiffAlgorithm
-> FL (RebaseFixup (PrimOf p)) wX wY
-> Suspended p wY wY
-> Suspended p wX wX
simplifyPushes DiffAlgorithm
D.MyersDiff (Named p wX wY -> FL (RebaseFixup (PrimOf p)) wX wY
forall (p :: * -> * -> *) wX wY.
(PrimPatch (PrimOf p), Effect p) =>
Named p wX wY -> FL (RebaseFixup (PrimOf p)) wX wY
namedToFixups Named p wX wY
p)
removeFixupsFromSuspended
:: (PrimPatchBase p, Commute p, FromPrim p, Effect p)
=> Named p wX wY
-> Suspended p wX wX
-> Suspended p wY wY
removeFixupsFromSuspended :: Named p wX wY -> Suspended p wX wX -> Suspended p wY wY
removeFixupsFromSuspended Named p wX wY
p = DiffAlgorithm
-> FL (RebaseFixup (PrimOf p)) wY wX
-> Suspended p wX wX
-> Suspended p wY wY
forall (p :: * -> * -> *) wX wY.
(PrimPatchBase p, Commute p, FromPrim p, Effect p) =>
DiffAlgorithm
-> FL (RebaseFixup (PrimOf p)) wX wY
-> Suspended p wY wY
-> Suspended p wX wX
simplifyPushes DiffAlgorithm
D.MyersDiff (FL (RebaseFixup (PrimOf p)) wX wY
-> FL (RebaseFixup (PrimOf p)) wY wX
forall (p :: * -> * -> *) wX wY. Invert p => p wX wY -> p wY wX
invert (Named p wX wY -> FL (RebaseFixup (PrimOf p)) wX wY
forall (p :: * -> * -> *) wX wY.
(PrimPatch (PrimOf p), Effect p) =>
Named p wX wY -> FL (RebaseFixup (PrimOf p)) wX wY
namedToFixups Named p wX wY
p))
addToEditsToSuspended
:: RepoPatch p
=> D.DiffAlgorithm
-> FL (Named p) wX wY
-> Suspended p wY wY
-> IO (Suspended p wX wX)
addToEditsToSuspended :: DiffAlgorithm
-> FL (Named p) wX wY
-> Suspended p wY wY
-> IO (Suspended p wX wX)
addToEditsToSuspended DiffAlgorithm
_ FL (Named p) wX wY
NilFL Suspended p wY wY
items = Suspended p wY wY -> IO (Suspended p wY wY)
forall (m :: * -> *) a. Monad m => a -> m a
return Suspended p wY wY
items
addToEditsToSuspended DiffAlgorithm
da (NamedP PatchInfo
old [PatchInfo]
ds FL p wX wY
ps :>: FL (Named p) wY wY
qs) Suspended p wY wY
items = do
Suspended p wY wY
items' <- DiffAlgorithm
-> FL (Named p) wY wY
-> Suspended p wY wY
-> IO (Suspended p wY wY)
forall (p :: * -> * -> *) wX wY.
RepoPatch p =>
DiffAlgorithm
-> FL (Named p) wX wY
-> Suspended p wY wY
-> IO (Suspended p wX wX)
addToEditsToSuspended DiffAlgorithm
da FL (Named p) wY wY
qs Suspended p wY wY
items
PatchInfo
new <- PatchInfo -> IO PatchInfo
replaceJunk PatchInfo
old
case DiffAlgorithm
-> RebaseFixup (PrimOf p) wY wY
-> Suspended p wY wY
-> Suspended p wY wY
forall (p :: * -> * -> *) wX wY.
(PrimPatchBase p, Commute p, FromPrim p, Effect p) =>
DiffAlgorithm
-> RebaseFixup (PrimOf p) wX wY
-> Suspended p wY wY
-> Suspended p wX wX
simplifyPush DiffAlgorithm
da (RebaseName wY wY -> RebaseFixup (PrimOf p) wY wY
forall wX wY (prim :: * -> * -> *).
RebaseName wX wY -> RebaseFixup prim wX wY
NameFixup (PatchInfo -> PatchInfo -> RebaseName wY wY
forall wX wY. PatchInfo -> PatchInfo -> RebaseName wX wY
Rename PatchInfo
new PatchInfo
old)) Suspended p wY wY
items' of
Items FL (RebaseChange (PrimOf p)) wY wY
items'' ->
case DiffAlgorithm
-> Named p wX wY
-> FL (RebaseChange (PrimOf p)) wY wY
-> Sealed (FL (RebaseChange (PrimOf p)) wX)
forall (p :: * -> * -> *) wX wY wZ.
RepoPatch p =>
DiffAlgorithm
-> Named p wX wY
-> FL (RebaseChange (PrimOf p)) wY wZ
-> Sealed (FL (RebaseChange (PrimOf p)) wX)
addNamedToRebase DiffAlgorithm
da (PatchInfo -> [PatchInfo] -> FL p wX wY -> Named p wX wY
forall (p :: * -> * -> *) wX wY.
PatchInfo -> [PatchInfo] -> FL p wX wY -> Named p wX wY
NamedP PatchInfo
new [PatchInfo]
ds FL p wX wY
ps) FL (RebaseChange (PrimOf p)) wY wY
items'' of
Sealed FL (RebaseChange (PrimOf p)) wX wX
items''' -> Suspended p wX wX -> IO (Suspended p wX wX)
forall (m :: * -> *) a. Monad m => a -> m a
return (Suspended p wX wX -> IO (Suspended p wX wX))
-> Suspended p wX wX -> IO (Suspended p wX wX)
forall a b. (a -> b) -> a -> b
$ FL (RebaseChange (PrimOf p)) wX wX -> Suspended p wX wX
forall (p :: * -> * -> *) wX wY.
FL (RebaseChange (PrimOf p)) wX wY -> Suspended p wX wX
Items FL (RebaseChange (PrimOf p)) wX wX
items'''
simplifyPush
:: (PrimPatchBase p, Commute p, FromPrim p, Effect p)
=> D.DiffAlgorithm
-> RebaseFixup (PrimOf p) wX wY
-> Suspended p wY wY
-> Suspended p wX wX
simplifyPush :: DiffAlgorithm
-> RebaseFixup (PrimOf p) wX wY
-> Suspended p wY wY
-> Suspended p wX wX
simplifyPush DiffAlgorithm
da RebaseFixup (PrimOf p) wX wY
fixups = (forall wZ.
FL (RebaseChange (PrimOf p)) wY wZ
-> Sealed (FL (RebaseChange (PrimOf p)) wX))
-> Suspended p wY wY -> Suspended p wX wX
forall (p :: * -> * -> *) wY wX.
(forall wZ.
FL (RebaseChange (PrimOf p)) wY wZ
-> Sealed (FL (RebaseChange (PrimOf p)) wX))
-> Suspended p wY wY -> Suspended p wX wX
onSuspended (DiffAlgorithm
-> RebaseFixup (PrimOf p) wX wY
-> FL (RebaseChange (PrimOf p)) wY wZ
-> Sealed (FL (RebaseChange (PrimOf p)) wX)
forall (prim :: * -> * -> *) wX wY wZ.
PrimPatch prim =>
DiffAlgorithm
-> RebaseFixup prim wX wY
-> FL (RebaseChange prim) wY wZ
-> Sealed (FL (RebaseChange prim) wX)
Change.simplifyPush DiffAlgorithm
da RebaseFixup (PrimOf p) wX wY
fixups)
simplifyPushes
:: (PrimPatchBase p, Commute p, FromPrim p, Effect p)
=> D.DiffAlgorithm
-> FL (RebaseFixup (PrimOf p)) wX wY
-> Suspended p wY wY
-> Suspended p wX wX
simplifyPushes :: DiffAlgorithm
-> FL (RebaseFixup (PrimOf p)) wX wY
-> Suspended p wY wY
-> Suspended p wX wX
simplifyPushes DiffAlgorithm
da FL (RebaseFixup (PrimOf p)) wX wY
fixups = (forall wZ.
FL (RebaseChange (PrimOf p)) wY wZ
-> Sealed (FL (RebaseChange (PrimOf p)) wX))
-> Suspended p wY wY -> Suspended p wX wX
forall (p :: * -> * -> *) wY wX.
(forall wZ.
FL (RebaseChange (PrimOf p)) wY wZ
-> Sealed (FL (RebaseChange (PrimOf p)) wX))
-> Suspended p wY wY -> Suspended p wX wX
onSuspended (DiffAlgorithm
-> FL (RebaseFixup (PrimOf p)) wX wY
-> FL (RebaseChange (PrimOf p)) wY wZ
-> Sealed (FL (RebaseChange (PrimOf p)) wX)
forall (prim :: * -> * -> *) wX wY wZ.
PrimPatch prim =>
DiffAlgorithm
-> FL (RebaseFixup prim) wX wY
-> FL (RebaseChange prim) wY wZ
-> Sealed (FL (RebaseChange prim) wX)
Change.simplifyPushes DiffAlgorithm
da FL (RebaseFixup (PrimOf p)) wX wY
fixups)