{-# OPTIONS_GHC -fno-warn-orphans #-}
module Darcs.Patch.Prim.V1 ( Prim ) where
import Darcs.Prelude
import Data.Maybe ( fromMaybe )
import Darcs.Patch.Prim.V1.Apply ()
import Darcs.Patch.Prim.V1.Coalesce ()
import Darcs.Patch.Prim.V1.Commute ()
import Darcs.Patch.Prim.V1.Core ( Prim )
import Darcs.Patch.Prim.V1.Details ()
import Darcs.Patch.Prim.V1.Mangle ()
import Darcs.Patch.Prim.V1.Read ()
import Darcs.Patch.Prim.V1.Show ()
import Darcs.Patch.Commute ( Commute(..), commuteFL )
import Darcs.Patch.Invert ( Invert(..), dropInverses )
import Darcs.Patch.Prim.Class
( PrimSift(..)
, PrimClassify
( primIsHunk
, primIsBinary
, primIsSetpref
, primIsAddfile
, primIsAdddir
)
, PrimCanonize(tryToShrink)
)
import Darcs.Patch.Witnesses.Eq ( Eq2(..), EqCheck(..) )
import Darcs.Patch.Witnesses.Ordered
( FL(..)
, RL(..)
, (:>)(..)
, allFL
, lengthFL
, reverseFL
, filterOutFLFL
)
import Darcs.Patch.Witnesses.Sealed ( Sealed(..), seal )
import Darcs.Patch.Witnesses.Unsafe ( unsafeCoerceP )
instance PrimSift Prim where
siftForPending = v1siftForPending where
crudeSift :: forall prim wX wY. PrimClassify prim
=> FL prim wX wY -> FL prim wX wY
crudeSift xs =
if isSimple xs
then filterOutFLFL ishunkbinary xs
else xs
where
ishunkbinary :: prim wA wB -> EqCheck wA wB
ishunkbinary x
| primIsHunk x || primIsBinary x = unsafeCoerceP IsEq
| otherwise = NotEq
isSimple = allFL $ \x -> primIsHunk x || primIsBinary x || primIsSetpref x
v1siftForPending
:: forall prim wX wY.
(Commute prim, Invert prim, Eq2 prim, PrimCanonize prim, PrimClassify prim)
=> FL prim wX wY
-> Sealed (FL prim wX)
v1siftForPending simple_ps
| allFL (\p -> primIsAddfile p || primIsAdddir p) oldps = seal oldps
| otherwise =
case sift (reverseFL oldps) NilFL of
Sealed x ->
let ps = tryToShrink x in
if (lengthFL ps < lengthFL oldps)
then v1siftForPending ps
else seal ps
where
oldps = fromMaybe simple_ps $ dropInverses $ crudeSift simple_ps
sift :: RL prim wA wB -> FL prim wB wC -> Sealed (FL prim wA)
sift NilRL sofar = seal sofar
sift (ps :<: p) sofar
| primIsHunk p || primIsBinary p
, Just (sofar' :> _) <- commuteFL (p :> sofar) = sift ps sofar'
| otherwise = sift ps (p :>: sofar)