forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.cs
114 lines (98 loc) · 3.75 KB
/
Tree.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
namespace LibGit2Sharp
{
/// <summary>
/// A container which references a list of other <see cref="Tree"/>s and <see cref="Blob"/>s.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class Tree : GitObject, IEnumerable<TreeEntry>
{
private readonly FilePath path;
private readonly ILazy<int> lazyCount;
/// <summary>
/// Needed for mocking purposes.
/// </summary>
protected Tree()
{ }
internal Tree(Repository repo, ObjectId id, FilePath path)
: base(repo, id)
{
this.path = path ?? "";
lazyCount = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_tree_entrycount);
}
/// <summary>
/// Gets the number of <see cref="TreeEntry"/> immediately under this <see cref="Tree"/>.
/// </summary>
public virtual int Count { get { return lazyCount.Value; } }
/// <summary>
/// Gets the <see cref="TreeEntry"/> pointed at by the <paramref name="relativePath"/> in this <see cref="Tree"/> instance.
/// </summary>
/// <param name="relativePath">The relative path to the <see cref="TreeEntry"/> from this instance.</param>
/// <returns><c>null</c> if nothing has been found, the <see cref="TreeEntry"/> otherwise.</returns>
public virtual TreeEntry this[string relativePath]
{
get { return RetrieveFromPath(relativePath); }
}
private TreeEntry RetrieveFromPath(FilePath relativePath)
{
if (relativePath.IsNullOrEmpty())
{
return null;
}
using (TreeEntrySafeHandle_Owned treeEntryPtr = Proxy.git_tree_entry_bypath(repo.Handle, Id, relativePath))
{
if (treeEntryPtr == null)
{
return null;
}
string posixPath = relativePath.Posix;
string filename = posixPath.Split('/').Last();
string parentPath = posixPath.Substring(0, posixPath.Length - filename.Length);
return new TreeEntry(treeEntryPtr, Id, repo, path.Combine(parentPath));
}
}
internal string Path
{
get { return path.Native; }
}
#region IEnumerable<TreeEntry> Members
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns>
public virtual IEnumerator<TreeEntry> GetEnumerator()
{
using (var obj = new ObjectSafeWrapper(Id, repo.Handle))
{
for (uint i = 0; i < Count; i++)
{
TreeEntrySafeHandle handle = Proxy.git_tree_entry_byindex(obj.ObjectPtr, i);
yield return new TreeEntry(handle, Id, repo, path);
}
}
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An <see cref="IEnumerator"/> object that can be used to iterate through the collection.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
private string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture,
"{0}, Count = {1}", Id.ToString(7), Count);
}
}
}
}