8000 Add TreeEntry.Type · MicrosoftWebMatrix/libgit2sharp@b8d9559 · GitHub
[go: up one dir, main page]

Skip to content

Commit b8d9559

Browse files
committed
Add TreeEntry.Type
Slightly enhanced the filtering of tree entries.
1 parent 62208a3 commit b8d9559

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,14 @@ public static extern int git_repository_discover(StringBuilder repository_path,
236236
public static extern IntPtr git_tree_entry_byname(IntPtr tree, string filename);
237237

238238
[DllImport(libgit2)]
239-
public static extern IntPtr git_tree_entry_id(IntPtr tree);
239+
public static extern IntPtr git_tree_entry_id(IntPtr entry);
240240

241241
[DllImport(libgit2)]
242242
public static extern IntPtr git_tree_entry_name(IntPtr entry);
243243

244+
[DllImport(libgit2)]
245+
public static extern GitObjectType git_tree_entry_type(IntPtr entry);
246+
244247
[DllImport(libgit2)]
245248
public static extern uint git_tree_entrycount(IntPtr tree);
246249
}

LibGit2Sharp/Tree.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,21 @@ public IEnumerable<Tree> Trees
3333
{
3434
get
3535
{
36-
return this.Select(e => e.Target).OfType<Tree>();
36+
return this
37+
.Where(e => e.Type == GitObjectType.Tree)
38+
.Select(e => e.Target)
39+
.Cast<Tree>();
3740
}
3841
}
3942

4043
public IEnumerable<Blob> Files
4144
{
4245
get
4346
{
44-
return this.Select(e => e.Target).OfType<Blob>();
47+
return this
48+
.Where(e => e.Type == GitObjectType.Blob)
49+
.Select(e => e.Target)
50+
.Cast<Blob>();
4551
}
4652
}
4753

LibGit2Sharp/TreeEntry.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace LibGit2Sharp
66
{
7+
/// <summary>
8+
/// Representation of an entry in a <see cref="Tree"/>.
9+
/// </summary>
710
public class TreeEntry : IEquatable<TreeEntry>
811
{
912
private readonly ObjectId parentTreeId;
@@ -14,29 +17,46 @@ public class TreeEntry : IEquatable<TreeEntry>
1417
private static readonly LambdaEqualityHelper<TreeEntry> equalityHelper =
1518
new LambdaEqualityHelper<TreeEntry>(new Func<TreeEntry, object>[] { x => x.Name, x => x.parentTreeId });
1619

17-
public TreeEntry(IntPtr obj, ObjectId parentTreeId, Repository repo)
20+
internal TreeEntry(IntPtr obj, ObjectId parentTreeId, Repository repo)
1821
{
1922
this.parentTreeId = parentTreeId;
2023
this.repo = repo;
2124
IntPtr gitTreeEntryId = NativeMethods.git_tree_entry_id(obj);
2225
targetOid = new ObjectId((GitOid)Marshal.PtrToStructure(gitTreeEntryId, typeof(GitOid)));
26+
Type = NativeMethods.git_tree_entry_type(obj);
2327

2428
Attributes = NativeMethods.git_tree_entry_attributes(obj);
2529
Name = NativeMethods.git_tree_entry_name(obj).MarshallAsString();
2630
}
2731

32+
/// <summary>
33+
/// Gets the UNIX file attributes.
34+
/// </summary>
2835
public int Attributes { get; private set; }
29-
36+
37+
/// <summary>
38+
/// Gets the filename.
39+
/// <para>The filename is expressed in a relative form. Path segments are separated with a forward slash."/></para>
40+
/// </summary>
3041
public string Name { get; private set; }
3142

43+
/// <summary>
44+
/// Gets the <see cref="GitObject"/> being pointed at.
45+
/// </summary>
3246
public GitObject Target { get { return target ?? (target = RetreiveTreeEntryTarget()); } }
3347

48+
/// <summary>
49+
/// Gets the <see cref="GitObjectType"/> of the <see cref="Target"/> being pointed at.
50+
/// </summary>
51+
public GitObjectType Type { get; private set; }
52+
3453
private GitObject RetreiveTreeEntryTarget()
3554
{
3655
GitObject treeEntryTarget = repo.Lookup(targetOid);
3756

57+
//TODO: Warning submodules will appear as targets of type Commit
3858
Ensure.ArgumentConformsTo(treeEntryTarget.GetType(), t => typeof(Blob).IsAssignableFrom(t) || typeof(Tree).IsAssignableFrom(t), "treeEntryTarget");
39-
59+
4060
return treeEntryTarget;
4161
}
4262

0 commit comments

Comments
 (0)
0