diff options
author | Keith Dahlby <dahlbyk@gmail.com> | 2012-03-24 01:17:41 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2012-03-24 16:38:11 +0400 |
commit | 7334af1b6d91c4dc8956872f9ce6f5d6c9dc7404 (patch) | |
tree | 4e9c7317eae6a1a81df101d0cb57e80eecb6f8f3 /LibGit2Sharp/Tree.cs | |
parent | b5a60266f738467f64c8de29bb148740eb540ba9 (diff) |
Add Path property to TreeEntry type
Fix issue #122
Diffstat (limited to 'LibGit2Sharp/Tree.cs')
-rw-r--r-- | LibGit2Sharp/Tree.cs | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/LibGit2Sharp/Tree.cs b/LibGit2Sharp/Tree.cs index b6f5f2c5..0e9df714 100644 --- a/LibGit2Sharp/Tree.cs +++ b/LibGit2Sharp/Tree.cs @@ -11,11 +11,15 @@ namespace LibGit2Sharp /// </summary> public class Tree : GitObject, IEnumerable<TreeEntry> { - private Repository repo; + private readonly Repository repo; + private readonly FilePath path; - internal Tree(ObjectId id) + internal Tree(ObjectId id, FilePath path, int treeEntriesCount, Repository repository) : base(id) { + Count = treeEntriesCount; + repo = repository; + this.path = path ?? ""; } /// <summary> @@ -35,7 +39,7 @@ namespace LibGit2Sharp private TreeEntry RetrieveFromPath(FilePath relativePath) { - if (string.IsNullOrEmpty(relativePath.Posix)) + if (relativePath.IsNullOrEmpty()) { return null; } @@ -53,14 +57,18 @@ namespace LibGit2Sharp Ensure.Success(res); - IntPtr e = NativeMethods.git_tree_entry_byname(objectPtr, relativePath.Posix.Split('/').Last()); + string posixPath = relativePath.Posix; + string filename = posixPath.Split('/').Last(); + + IntPtr e = NativeMethods.git_tree_entry_byname(objectPtr, filename); if (e == IntPtr.Zero) { return null; } - return new TreeEntry(e, Id, repo); + string parentPath = posixPath.Substring(0, posixPath.Length - filename.Length); + return new TreeEntry(e, Id, repo, path.Combine(parentPath)); } } @@ -92,6 +100,11 @@ namespace LibGit2Sharp } } + internal string Path + { + get { return path.Native; } + } + #region IEnumerable<TreeEntry> Members /// <summary> @@ -105,7 +118,7 @@ namespace LibGit2Sharp for (uint i = 0; i < Count; i++) { IntPtr e = NativeMethods.git_tree_entry_byindex(obj.ObjectPtr, i); - yield return new TreeEntry(e, Id, repo); + yield return new TreeEntry(e, Id, repo, path); } } } @@ -121,9 +134,9 @@ namespace LibGit2Sharp #endregion - internal static Tree BuildFromPtr(IntPtr obj, ObjectId id, Repository repo) + internal static Tree BuildFromPtr(IntPtr obj, ObjectId id, Repository repo, FilePath path) { - var tree = new Tree(id) { repo = repo, Count = (int)NativeMethods.git_tree_entrycount(obj) }; + var tree = new Tree(id, path, (int)NativeMethods.git_tree_entrycount(obj), repo); return tree; } } |