Update README.txt
[GitSharp.git] / GitSharp / Tree.cs
blob356b6e384b2a22a9e4078a95225ecfbecc589654
1 /*
2 * Copyright (C) 2009-2010, Henon <meinrad.recheis@gmail.com>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
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
21 * written permission.
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.
38 using System;
39 using System.Collections.Generic;
40 using System.Linq;
41 using System.Text;
42 using GitSharp.Core;
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;
49 namespace GitSharp
52 /// <summary>
53 /// Represents a directory in the git repository.
54 /// </summary>
55 public class Tree : AbstractTreeNode
57 internal Tree(Repository repo, ObjectId id) : base(repo, id) { }
59 internal Tree(Repository repo, CoreTree tree)
60 : base(repo, tree.Id)
62 _internal_tree = tree;
65 private CoreTree _internal_tree;
67 internal CoreTree InternalTree
69 get
71 if (_internal_tree == null)
72 try
74 _internal_tree = _repo._internal_repo.MapTree(_id);
76 catch (Exception)
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
86 get
88 if (InternalTree == null)
89 return null;
90 if (InternalTree.IsRoot)
91 return "";
92 return InternalTree.Name;
96 /// <summary>
97 /// True if the tree has no parent.
98 /// </summary>
99 public bool IsRoot
103 if (InternalTree == null)
104 return true;
105 return InternalTree.IsRoot;
109 public override Tree Parent
113 if (InternalTree == null)
114 return null;
115 if (InternalTree.Parent == null)
116 return null;
117 return new Tree(_repo, InternalTree.Parent);
121 /// <summary>
122 /// Entries of the tree. These are either Tree or Leaf objects representing sub-directories or files.
123 /// </summary>
124 public IEnumerable<AbstractObject> Children
128 if (InternalTree == null)
129 return new Leaf[0];
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(
133 tree_entry =>
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;
139 }).ToArray();
143 /// <summary>
144 /// Tree entries representing this directory's subdirectories
145 /// </summary>
146 public IEnumerable<Tree> Trees
150 return Children.Where(child => child.IsTree).Cast<Tree>().ToArray();
154 /// <summary>
155 /// Leaf entries representing this directory's files
156 /// </summary>
157 public IEnumerable<Leaf> Leaves
161 return Children.Where(child => child.IsBlob).Cast<Leaf>().ToArray();
165 public override string Path
169 if (InternalTree == null)
170 return null;
171 if (InternalTree.IsRoot)
172 return "";
173 return InternalTree.FullName;
177 public override int Permissions
182 if (InternalTree == null)
183 return 0;
184 return InternalTree.Mode.Bits;
188 public override string ToString()
190 return "Tree[" + ShortHash + "]";
193 /// <summary>
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.
196 /// <para/>
197 /// The path's directory seperators may be both forward or backslash, it is converted automatically to the internal representation.
198 /// <para/>
199 /// Throws IOException.
200 /// </summary>
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]
207 if (path == "")
208 return this;
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)
213 return 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;