module Darcs.UI.Commands.Util.Tree
(
treeHas
, treeHasDir
, treeHasFile
, treeHasAnycase
) where
import Prelude ()
import Darcs.Prelude
import Control.Monad ( forM )
import Control.Monad.State.Strict( gets )
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(..), floatPath, eqAnycase )
treeHasAnycase :: Monad m
=> Tree m
-> FilePath
-> m Bool
treeHasAnycase tree path =
fst `fmap` TM.virtualTreeMonad (existsAnycase $ floatPath path) tree
existsAnycase :: Monad m
=> AnchoredPath
-> TM.TreeMonad m Bool
existsAnycase (AnchoredPath []) = return True
existsAnycase (AnchoredPath (x:xs)) = do
wd <- TM.currentDirectory
Just tree <- 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 -> FilePath -> m Bool
treeHas tree path = fst `fmap` TM.virtualTreeMonad (TM.exists $ floatPath path) tree
treeHasDir :: Monad m => Tree m -> FilePath -> m Bool
treeHasDir tree path = fst `fmap` TM.virtualTreeMonad (TM.directoryExists $ floatPath path) tree
treeHasFile :: Monad m => Tree m -> FilePath -> m Bool
treeHasFile tree path = fst `fmap` TM.virtualTreeMonad (TM.fileExists $ floatPath path) tree