module Darcs.UI.Commands.Util.Tree
(
treeHas
, treeHasDir
, treeHasFile
, treeHasAnycase
) where
import Darcs.Prelude
import Control.Monad ( forM )
import Control.Monad.State.Strict( gets )
import Data.Maybe ( fromMaybe )
import qualified Darcs.Util.Tree.Monad as TM
( TreeMonad, withDirectory, fileExists, directoryExists
, virtualTreeMonad, currentDirectory, exists, tree )
import Darcs.Util.Tree ( Tree, listImmediate, findTree )
import Darcs.Util.Path
( AnchoredPath(..), eqAnycase )
treeHasAnycase :: Monad m
=> Tree m
-> AnchoredPath
-> m Bool
treeHasAnycase tree path =
fst `fmap` TM.virtualTreeMonad (existsAnycase path) tree
existsAnycase :: Monad m
=> AnchoredPath
-> TM.TreeMonad m Bool
existsAnycase (AnchoredPath []) = return True
existsAnycase (AnchoredPath (x:xs)) = do
wd <- TM.currentDirectory
tree <- fromMaybe (error "invalid path passed to existsAnycase") <$>
gets (flip findTree wd . TM.tree)
let subs = [ AnchoredPath [n] | (n, _) <- listImmediate tree,
eqAnycase n x ]
or `fmap` forM subs (\path -> do
file <- TM.fileExists path
if file then return True
else TM.withDirectory path (existsAnycase $ AnchoredPath xs))
treeHas :: Monad m => Tree m -> AnchoredPath -> m Bool
treeHas tree path = fst `fmap` TM.virtualTreeMonad (TM.exists path) tree
treeHasDir :: Monad m => Tree m -> AnchoredPath -> m Bool
treeHasDir tree path = fst `fmap` TM.virtualTreeMonad (TM.directoryExists path) tree
treeHasFile :: Monad m => Tree m -> AnchoredPath -> m Bool
treeHasFile tree path = fst `fmap` TM.virtualTreeMonad (TM.fileExists path) tree