{-# 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 (Items ps) = listTouchedFiles ps
hunkMatches f (Items ps) = hunkMatches f ps
instance (PrimPatchBase p, PatchListFormat p, ShowPatchBasic p) => ShowPatchBasic (Suspended p) where
showPatch f (Items ps)
= blueText "rebase" <+> text "0.2" <+> blueText "{"
$$ vcat (mapFL (showPatch f) ps)
$$ blueText "}"
instance (PrimPatchBase p, PatchListFormat p, ReadPatch p, RepoPatch p) => ReadPatch (Suspended p) where
readPatch' =
do lexString (BC.pack "rebase")
version <- lexWord
case () of
_ | version == BC.pack "0.2" ->
(lexString (BC.pack "{}") >> return (seal (Items NilFL)))
<|>
(unseal (Sealed . Items) <$> bracketedFL readPatch' '{' '}')
| version == BC.pack "0.0" ->
let
itemsToSuspended :: Sealed (FL (Item.RebaseItem p) wX) -> Sealed (Suspended p wX)
itemsToSuspended (Sealed ps) =
case Item.toRebaseChanges ps of
Sealed ps' -> Sealed (Items ps')
in
(lexString (BC.pack "{}") >> return (seal (Items NilFL)))
<|>
itemsToSuspended <$> bracketedFL readPatch' '{' '}'
| otherwise -> error $ "can't handle rebase version " ++ show version
countToEdit :: Suspended p wX wY -> Int
countToEdit (Items ps) = lengthFL 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 f (Items ps) = unseal Items (f ps)
addFixupsToSuspended
:: (PrimPatchBase p, Commute p, FromPrim p, Effect p)
=> Named p wX wY
-> Suspended p wY wY
-> Suspended p wX wX
addFixupsToSuspended p = simplifyPushes D.MyersDiff (namedToFixups p)
removeFixupsFromSuspended
:: (PrimPatchBase p, Commute p, FromPrim p, Effect p)
=> Named p wX wY
-> Suspended p wX wX
-> Suspended p wY wY
removeFixupsFromSuspended p = simplifyPushes D.MyersDiff (invert (namedToFixups p))
addToEditsToSuspended
:: RepoPatch p
=> D.DiffAlgorithm
-> FL (Named p) wX wY
-> Suspended p wY wY
-> IO (Suspended p wX wX)
addToEditsToSuspended _ NilFL items = return items
addToEditsToSuspended da (NamedP old ds ps :>: qs) items = do
items' <- addToEditsToSuspended da qs items
new <- replaceJunk old
case simplifyPush da (NameFixup (Rename new old)) items' of
Items items'' ->
case addNamedToRebase da (NamedP new ds ps) items'' of
Sealed items''' -> return $ Items 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 da fixups = onSuspended (Change.simplifyPush da 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 da fixups = onSuspended (Change.simplifyPushes da fixups)