darcs-2.12.5: a distributed, interactive, smart revision control system

Safe Haskell | None
Language | Haskell2010

Darcs.Util.Tree

Contents

- Unfolding stubbed (lazy) Trees.
- Tree access and lookup.
- Files (Blobs).
- Filtering trees.
- Manipulating trees.

Description

The abstract representation of a Tree and useful abstract utilities to handle those.

Synopsis

- data Tree m
- data Blob m = Blob !(m ByteString) !Hash
- data TreeItem m
  - = File !(Blob m)
  - | SubTree !(Tree m)
  - | Stub !(m (Tree m)) !Hash
- data ItemType
  - = TreeType
  - | BlobType
- data Hash
  - = SHA256 !ByteString
  - | SHA1 !ByteString
  - | NoHash
- makeTree :: Monad m => [(Name, TreeItem m)] -> Tree m
- makeTreeWithHash :: Monad m => [(Name, TreeItem m)] -> Hash -> Tree m
- emptyTree :: Monad m => Tree m
- emptyBlob :: Monad m => Blob m
- makeBlob :: Monad m => ByteString -> Blob m
- makeBlobBS :: Monad m => ByteString -> Blob m
- expandUpdate :: Monad m => (AnchoredPath -> Tree m -> m (Tree m)) -> Tree m -> m (Tree m)
- expand :: Monad m => Tree m -> m (Tree m)
- expandPath :: Monad m => Tree m -> AnchoredPath -> m (Tree m)
- checkExpand :: (TreeItem IO -> IO Hash) -> Tree IO -> IO (Either [(AnchoredPath, Hash, Maybe Hash)] (Tree IO))
- items :: Tree m -> Map Name (TreeItem m)
- list :: Tree m -> [(AnchoredPath, TreeItem m)]
- listImmediate :: Tree m -> [(Name, TreeItem m)]
- treeHash :: Tree m -> Hash
- lookup :: Tree m -> Name -> Maybe (TreeItem m)
- find :: Tree m -> AnchoredPath -> Maybe (TreeItem m)
- findFile :: Tree m -> AnchoredPath -> Maybe (Blob m)
- findTree :: Tree m -> AnchoredPath -> Maybe (Tree m)
- itemHash :: TreeItem m -> Hash
- itemType :: TreeItem m -> ItemType
- zipCommonFiles :: (AnchoredPath -> Blob m -> Blob m -> a) -> Tree m -> Tree m -> [a]
- zipFiles :: (AnchoredPath -> Maybe (Blob m) -> Maybe (Blob m) -> a) -> Tree m -> Tree m -> [a]
- zipTrees :: (AnchoredPath -> Maybe (TreeItem m) -> Maybe (TreeItem m) -> a) -> Tree m -> Tree m -> [a]
- diffTrees :: forall m. (Functor m, Monad m) => Tree m -> Tree m -> m (Tree m, Tree m)
- readBlob :: Blob m -> m ByteString
- class Monad m => FilterTree a m where
- restrict :: (FilterTree t m, Monad n) => Tree n -> t m -> t m
- modifyTree :: Monad m => Tree m -> AnchoredPath -> Maybe (TreeItem m) -> Tree m
- updateTree :: (Functor m, Monad m) => (TreeItem m -> m (TreeItem m)) -> Tree m -> m (Tree m)
- partiallyUpdateTree :: (Functor m, Monad m) => (TreeItem m -> m (TreeItem m)) -> (AnchoredPath -> TreeItem m -> Bool) -> Tree m -> m (Tree m)
- updateSubtrees :: (Tree m -> Tree m) -> Tree m -> Tree m
- overlay :: (Functor m, Monad m) => Tree m -> Tree m -> Tree m
- addMissingHashes :: (Monad m, Functor m) => (TreeItem m -> m Hash) -> Tree m -> m (Tree m)

# Documentation

data Tree m #

Abstraction of a filesystem tree. Please note that the Tree returned by the respective read operations will have TreeStub items in it. To obtain a Tree without such stubs, call expand on it, eg.:

    tree <- readDarcsPristine "." >>= expand

When a Tree is expanded, it becomes "final". All stubs are forced and the Tree can be traversed purely. Access to actual file contents stays in IO though.

A Tree may have a Hash associated with it. A pair of Tree's is identical whenever their hashes are (the reverse need not hold, since not all Trees come equipped with a hash).

Instances

ApplyMonadState Tree # |
Associated Types type ApplyMonadStateOperations (Tree :: (* -> *) -> *) :: (* -> *) -> Constraint #
ToTree Tree # |
Methods toTree :: Tree m -> Tree m #
Monad m => FilterTree Tree m # |
Methods filter :: (AnchoredPath -> TreeItem m -> Bool) -> Tree m -> Tree m #
ApplyMonad Tree FileModMonad # |
Associated Types type ApplyMonadBase (FileModMonad :: * -> *) :: * -> * # Methods nestedApply :: FileModMonad x -> Tree (ApplyMonadBase FileModMonad) -> FileModMonad (x, Tree (ApplyMonadBase FileModMonad)) # liftApply :: (Tree (ApplyMonadBase FileModMonad) -> ApplyMonadBase FileModMonad x) -> Tree (ApplyMonadBase FileModMonad) -> FileModMonad (x, Tree (ApplyMonadBase FileModMonad)) # getApplyState :: FileModMonad (Tree (ApplyMonadBase FileModMonad)) #
ApplyMonad Tree DefaultIO # |
Associated Types type ApplyMonadBase (DefaultIO :: * -> *) :: * -> * # Methods nestedApply :: DefaultIO x -> Tree (ApplyMonadBase DefaultIO) -> DefaultIO (x, Tree (ApplyMonadBase DefaultIO)) # liftApply :: (Tree (ApplyMonadBase DefaultIO) -> ApplyMonadBase DefaultIO x) -> Tree (ApplyMonadBase DefaultIO) -> DefaultIO (x, Tree (ApplyMonadBase DefaultIO)) # getApplyState :: DefaultIO (Tree (ApplyMonadBase DefaultIO)) #
(Functor m, Monad m) => ApplyMonadTrans Tree m # |
Associated Types type ApplyMonadOver (Tree :: (* -> *) -> *) (m :: * -> *) :: * -> * # Methods runApplyMonad :: ApplyMonadOver Tree m x -> Tree m -> m (x, Tree m) #
(Functor m, Monad m) => ApplyMonad Tree (TreeMonad m) # |
Associated Types type ApplyMonadBase (TreeMonad m :: * -> *) :: * -> * # Methods nestedApply :: TreeMonad m x -> Tree (ApplyMonadBase (TreeMonad m)) -> TreeMonad m (x, Tree (ApplyMonadBase (TreeMonad m))) # liftApply :: (Tree (ApplyMonadBase (TreeMonad m)) -> ApplyMonadBase (TreeMonad m) x) -> Tree (ApplyMonadBase (TreeMonad m)) -> TreeMonad m (x, Tree (ApplyMonadBase (TreeMonad m))) # getApplyState :: TreeMonad m (Tree (ApplyMonadBase (TreeMonad m))) #
ApplyMonad Tree (HashedIO p) # |
Associated Types type ApplyMonadBase (HashedIO p :: * -> *) :: * -> * # Methods nestedApply :: HashedIO p x -> Tree (ApplyMonadBase (HashedIO p)) -> HashedIO p (x, Tree (ApplyMonadBase (HashedIO p))) # liftApply :: (Tree (ApplyMonadBase (HashedIO p)) -> ApplyMonadBase (HashedIO p) x) -> Tree (ApplyMonadBase (HashedIO p)) -> HashedIO p (x, Tree (ApplyMonadBase (HashedIO p))) # getApplyState :: HashedIO p (Tree (ApplyMonadBase (HashedIO p))) #
type ApplyMonadStateOperations Tree # |
type ApplyMonadStateOperations Tree = ApplyMonadTree
type ApplyMonadOver Tree m # |
type ApplyMonadOver Tree m = TreeMonad m

data Blob m #

Constructors

Blob !(m ByteString) !Hash |

data TreeItem m #

Constructors

File !(Blob m) |
SubTree !(Tree m) |
Stub !(m (Tree m)) !Hash |

data ItemType #

Constructors

TreeType |
BlobType |

Instances

Eq ItemType # |
Methods (==) :: ItemType -> ItemType -> Bool # (/=) :: ItemType -> ItemType -> Bool #
Ord ItemType # |
Methods compare :: ItemType -> ItemType -> Ordering # (<) :: ItemType -> ItemType -> Bool # (<=) :: ItemType -> ItemType -> Bool # (>) :: ItemType -> ItemType -> Bool # (>=) :: ItemType -> ItemType -> Bool # max :: ItemType -> ItemType -> ItemType # min :: ItemType -> ItemType -> ItemType #
Show ItemType # |
Methods showsPrec :: Int -> ItemType -> ShowS # show :: ItemType -> String # showList :: [ItemType] -> ShowS #

data Hash #

Constructors

SHA256 !ByteString |
SHA1 !ByteString |
NoHash |

Instances

Eq Hash # |
Methods (==) :: Hash -> Hash -> Bool # (/=) :: Hash -> Hash -> Bool #
Data Hash # |
Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Hash -> c Hash # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Hash # toConstr :: Hash -> Constr # dataTypeOf :: Hash -> DataType # dataCast1 :: Typeable (* -> *) t => (forall d. Data d => c (t d)) -> Maybe (c Hash) # dataCast2 :: Typeable (* -> * -> *) t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Hash) # gmapT :: (forall b. Data b => b -> b) -> Hash -> Hash # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Hash -> r # gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Hash -> r # gmapQ :: (forall d. Data d => d -> u) -> Hash -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Hash -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Hash -> m Hash # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Hash -> m Hash # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Hash -> m Hash #
Ord Hash # |
Methods compare :: Hash -> Hash -> Ordering # (<) :: Hash -> Hash -> Bool # (<=) :: Hash -> Hash -> Bool # (>) :: Hash -> Hash -> Bool # (>=) :: Hash -> Hash -> Bool # max :: Hash -> Hash -> Hash # min :: Hash -> Hash -> Hash #
Read Hash # |
Methods readsPrec :: Int -> ReadS Hash # readList :: ReadS [Hash] # readPrec :: ReadPrec Hash # readListPrec :: ReadPrec [Hash] #
Show Hash # |
Methods showsPrec :: Int -> Hash -> ShowS # show :: Hash -> String # showList :: [Hash] -> ShowS #

makeTree :: Monad m => [(Name, TreeItem m)] -> Tree m #

makeTreeWithHash :: Monad m => [(Name, TreeItem m)] -> Hash -> Tree m #

emptyTree :: Monad m => Tree m #

emptyBlob :: Monad m => Blob m #

makeBlob :: Monad m => ByteString -> Blob m #

makeBlobBS :: Monad m => ByteString -> Blob m #

# Unfolding stubbed (lazy) Trees.

By default, Tree obtained by a read function is stubbed: it will contain Stub items that need to be executed in order to access the respective subtrees. `expand` will produce an unstubbed Tree.

expandUpdate :: Monad m => (AnchoredPath -> Tree m -> m (Tree m)) -> Tree m -> m (Tree m) #

expand :: Monad m => Tree m -> m (Tree m) #

Expand a stubbed Tree into a one with no stubs in it. You might want to filter the tree before expanding to save IO. This is the basic implementation, which may be overriden by some Tree instances (this is especially true of the Index case).

expandPath :: Monad m => Tree m -> AnchoredPath -> m (Tree m) #

Unfold a path in a (stubbed) Tree, such that the leaf node of the path is reachable without crossing any stubs. Moreover, the leaf ought not be a Stub in the resulting Tree. A non-existent path is expanded as far as it can be.

checkExpand :: (TreeItem IO -> IO Hash) -> Tree IO -> IO (Either [(AnchoredPath, Hash, Maybe Hash)] (Tree IO)) #

Check the disk version of a Tree: expands it, and checks each hash. Returns either the expanded tree or a list of AnchoredPaths where there are problems. The first argument is the hashing function used to create the tree.

# Tree access and lookup.

items :: Tree m -> Map Name (TreeItem m) #

list :: Tree m -> [(AnchoredPath, TreeItem m)] #

List all contents of a `Tree`.

listImmediate :: Tree m -> [(Name, TreeItem m)] #

treeHash :: Tree m -> Hash #

Get hash of a Tree. This is guaranteed to uniquely identify the Tree (including any blob content), as far as cryptographic hashes are concerned. Sha256 is recommended.

lookup :: Tree m -> Name -> Maybe (TreeItem m) #

Look up a `Tree` item (an immediate subtree or blob).

find :: Tree m -> AnchoredPath -> Maybe (TreeItem m) #

Find a `TreeItem` by its path. Gives `Nothing` if the path is invalid.

findFile :: Tree m -> AnchoredPath -> Maybe (Blob m) #

Find a `Blob` by its path. Gives `Nothing` if the path is invalid, or does not point to a Blob.

findTree :: Tree m -> AnchoredPath -> Maybe (Tree m) #

Find a `Tree` by its path. Gives `Nothing` if the path is invalid, or does not point to a Tree.

itemHash :: TreeItem m -> Hash #

Get a hash of a TreeItem. May be Nothing.

itemType :: TreeItem m -> ItemType #

zipCommonFiles :: (AnchoredPath -> Blob m -> Blob m -> a) -> Tree m -> Tree m -> [a] #

For every pair of corresponding blobs from the two supplied trees, evaluate the supplied function and accumulate the results in a list. Hint: to get IO actions through, just use sequence on the resulting list. NB. This won't expand any stubs.

zipFiles :: (AnchoredPath -> Maybe (Blob m) -> Maybe (Blob m) -> a) -> Tree m -> Tree m -> [a] #

For each file in each of the two supplied trees, evaluate the supplied function (supplying the corresponding file from the other tree, or Nothing) and accumulate the results in a list. Hint: to get IO actions through, just use sequence on the resulting list. NB. This won't expand any stubs.

zipTrees :: (AnchoredPath -> Maybe (TreeItem m) -> Maybe (TreeItem m) -> a) -> Tree m -> Tree m -> [a] #

diffTrees :: forall m. (Functor m, Monad m) => Tree m -> Tree m -> m (Tree m, Tree m) #

Cautiously extracts differing subtrees from a pair of Trees. It will never do any unneccessary expanding. Tree hashes are used to cut the comparison as high up the Tree branches as possible. The result is a pair of trees that do not share any identical subtrees. They are derived from the first and second parameters respectively and they are always fully expanded. It might be advantageous to feed the result into `zipFiles` or `zipTrees`.

# Files (Blobs).

readBlob :: Blob m -> m ByteString #

Read a Blob into a Lazy ByteString. Might be backed by an mmap, use with care.

# Filtering trees.

class Monad m => FilterTree a m where #

Minimal complete definition

filter

Methods

filter :: (AnchoredPath -> TreeItem m -> Bool) -> a m -> a m #

Given `pred tree`, produce a `Tree` that only has items for which `pred` returns `True`. The tree might contain stubs. When expanded, these will be subject to filtering as well.

Instances

Monad m => FilterTree Tree m # |
Methods filter :: (AnchoredPath -> TreeItem m -> Bool) -> Tree m -> Tree m #

restrict :: (FilterTree t m, Monad n) => Tree n -> t m -> t m #

Given two Trees, a `guide` and a `tree`, produces a new Tree that is a identical to `tree`, but only has those items that are present in both `tree` and `guide`. The `guide` Tree may not contain any stubs.

# Manipulating trees.

modifyTree :: Monad m => Tree m -> AnchoredPath -> Maybe (TreeItem m) -> Tree m #

Modify a Tree (by replacing, or removing or adding items).

updateTree :: (Functor m, Monad m) => (TreeItem m -> m (TreeItem m)) -> Tree m -> m (Tree m) #

Does not expand the tree.

partiallyUpdateTree :: (Functor m, Monad m) => (TreeItem m -> m (TreeItem m)) -> (AnchoredPath -> TreeItem m -> Bool) -> Tree m -> m (Tree m) #

Does not expand the tree.

updateSubtrees :: (Tree m -> Tree m) -> Tree m -> Tree m #

overlay :: (Functor m, Monad m) => Tree m -> Tree m -> Tree m #

Lay one tree over another. The resulting Tree will look like the base (1st parameter) Tree, although any items also present in the overlay Tree will be taken from the overlay. It is not allowed to overlay a different kind of an object, nor it is allowed for the overlay to add new objects to base. This means that the overlay Tree should be a subset of the base Tree (although any extraneous items will be ignored by the implementation).

addMissingHashes :: (Monad m, Functor m) => (TreeItem m -> m Hash) -> Tree m -> m (Tree m) #