{-# LANGUAGE UndecidableInstances #-}
module Darcs.Patch.Rebase.Legacy.Item
( RebaseItem(..)
, toRebaseChanges
) where
import Darcs.Prelude
import Darcs.Patch.Format ( PatchListFormat(..) )
import Darcs.Patch.Named ( Named(..) )
import Darcs.Patch.Read ( ReadPatch(..) )
import Darcs.Patch.FromPrim ( PrimPatchBase, PrimOf )
import Darcs.Patch.Rebase.Change ( RebaseChange(..), addNamedToRebase )
import Darcs.Patch.Rebase.Fixup ( RebaseFixup(..) )
import Darcs.Patch.RepoPatch ( RepoPatch )
import Darcs.Util.Parser ( Parser, lexString )
import Darcs.Patch.Witnesses.Ordered
import Darcs.Patch.Witnesses.Sealed
import Darcs.Patch.Witnesses.Show ( Show1, Show2 )
import qualified Darcs.Util.Diff as D
import Control.Applicative ( (<|>) )
import qualified Data.ByteString as B ( ByteString )
import qualified Data.ByteString.Char8 as BC ( pack )
data RebaseItem p wX wY where
ToEdit :: Named p wX wY -> RebaseItem p wX wY
Fixup :: RebaseFixup (PrimOf p) wX wY -> RebaseItem p wX wY
deriving instance (Show2 p, Show2 (PrimOf p)) => Show (RebaseItem p wX wY)
instance (Show2 p, Show2 (PrimOf p)) => Show1 (RebaseItem p wX)
instance (Show2 p, Show2 (PrimOf p)) => Show2 (RebaseItem p)
toRebaseChanges
:: forall p wX wY
. RepoPatch p
=> FL (RebaseItem p) wX wY
-> Sealed (FL (RebaseChange (PrimOf p)) wX)
toRebaseChanges NilFL = Sealed NilFL
toRebaseChanges (Fixup f :>: ps) =
case toRebaseChanges ps of
Sealed (RC fixups toedit :>: rest) -> Sealed (RC (f :>: fixups) toedit :>: rest)
Sealed NilFL -> error "rebase chain with Fixup at end"
toRebaseChanges (ToEdit te :>: ps) =
unseal (addNamedToRebase @p D.MyersDiff te) (toRebaseChanges ps)
instance (PrimPatchBase p, PatchListFormat p, ReadPatch p) => ReadPatch (RebaseItem p) where
readPatch' = mapSeal ToEdit <$> readWith (BC.pack "rebase-toedit") <|>
mapSeal (Fixup . PrimFixup) <$> readWith (BC.pack "rebase-fixup" ) <|>
mapSeal (Fixup . NameFixup) <$> readWith (BC.pack "rebase-name" )
where readWith :: forall q wX . ReadPatch q => B.ByteString -> Parser (Sealed (q wX))
readWith str = do lexString str
lexString (BC.pack "(")
res <- readPatch'
lexString (BC.pack ")")
return res