2 * Copyright (C) 2009-2010, Henon <meinrad.recheis@gmail.com>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 using System
.Collections
.Generic
;
43 using ObjectId
= GitSharp
.Core
.ObjectId
;
44 using CoreRef
= GitSharp
.Core
.Ref
;
45 using CoreCommit
= GitSharp
.Core
.Commit
;
46 using CoreTree
= GitSharp
.Core
.Tree
;
47 using FileTreeEntry
= GitSharp
.Core
.FileTreeEntry
;
53 /// Represents a directory in the git repository.
55 public class Tree
: AbstractTreeNode
57 internal Tree(Repository repo
, ObjectId id
) : base(repo
, id
) { }
59 internal Tree(Repository repo
, CoreTree tree
)
62 _internal_tree
= tree
;
65 private CoreTree _internal_tree
;
67 internal CoreTree InternalTree
71 if (_internal_tree
== null)
74 _internal_tree
= _repo
._internal_repo
.MapTree(_id
);
78 // the commit object is invalid. however, we can not allow exceptions here because they would not be expected.
80 return _internal_tree
;
84 public override string Name
88 if (InternalTree
== null)
90 if (InternalTree
.IsRoot
)
92 return InternalTree
.Name
;
97 /// True if the tree has no parent.
103 if (InternalTree
== null)
105 return InternalTree
.IsRoot
;
109 public override Tree Parent
113 if (InternalTree
== null)
115 if (InternalTree
.Parent
== null)
117 return new Tree(_repo
, InternalTree
.Parent
);
122 /// Entries of the tree. These are either Tree or Leaf objects representing sub-directories or files.
124 public IEnumerable
<AbstractObject
> Children
128 if (InternalTree
== null)
131 // no GitLink support in JGit, so just skip them here to not cause problems
132 return InternalTree
.Members
.Where(te
=> !(te
is GitLinkTreeEntry
)).Select(
135 if (tree_entry
is FileTreeEntry
)
136 return new Leaf(_repo
, tree_entry
as FileTreeEntry
) as AbstractObject
;
138 return new Tree(_repo
, tree_entry
as CoreTree
) as AbstractObject
;
144 /// Tree entries representing this directory's subdirectories
146 public IEnumerable
<Tree
> Trees
150 return Children
.Where(child
=> child
.IsTree
).Cast
<Tree
>().ToArray();
155 /// Leaf entries representing this directory's files
157 public IEnumerable
<Leaf
> Leaves
161 return Children
.Where(child
=> child
.IsBlob
).Cast
<Leaf
>().ToArray();
165 public override string Path
169 if (InternalTree
== null)
171 if (InternalTree
.IsRoot
)
173 return InternalTree
.FullName
;
177 public override int Permissions
182 if (InternalTree
== null)
184 return InternalTree
.Mode
.Bits
;
188 public override string ToString()
190 return "Tree[" + ShortHash
+ "]";
194 /// Find a Blob or Tree by traversing the tree along the given path. You can access not only direct children
195 /// of the tree but any descendant of this tree.
197 /// The path's directory seperators may be both forward or backslash, it is converted automatically to the internal representation.
199 /// Throws IOException.
201 /// <param name="path">Relative path to a file or directory in the git tree. For directories a trailing slash is allowed</param>
202 /// <returns>A tree or blob object representing the referenced object</returns>
203 public AbstractObject
this[string path
]
209 var tree_entry
= _internal_tree
.FindBlobMember(path
);
210 if (tree_entry
== null)
211 tree_entry
= _internal_tree
.findTreeMember(path
);
212 if (tree_entry
== null)
214 if (tree_entry
.IsTree
)
215 return new Tree(_repo
, tree_entry
as CoreTree
);
216 else if (tree_entry
.IsBlob
)
217 return new Leaf(_repo
, tree_entry
as FileTreeEntry
);
218 else // if (tree_entry.IsCommit || tree_entry.IsTag)
219 return AbstractObject
.Wrap(_repo
, tree_entry
.Id
);
223 public static implicit operator CoreTree(Tree t
)
225 return t
!= null ? t
._internal_tree
: null;