8000 Make IndexEntry implement IEquatable to ease the check of the equalit… · Folcon/libgit2sharp@058c057 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit 058c057

Browse files
committed
Make IndexEntry implement IEquatable to ease the check of the equality of two instances
1 parent 2097212 commit 058c057

File tree

1 file changed

+59
-5
lines changed

1 file changed

+59
-5
lines changed

LibGit2Sharp/IndexEntry.cs

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
namespace LibGit2Sharp
66
{
77
/// <summary>
8-
/// A reference to a <see cref="Blob"/> known by the <see cref="Index"/>.
8+
/// A reference to a <see cref = "Blob" /> known by the <see cref = "Index" />.
99
/// </summary>
10-
public class IndexEntry
10+
public class IndexEntry : IEquatable<IndexEntry>
1111
{
12+
private static readonly LambdaEqualityHelper<IndexEntry> equalityHelper =
13+
new LambdaEqualityHelper<IndexEntry>(new Func<IndexEntry, object>[] { x => x.Path, x => x.Id, x => x.State });
14+
1215
private Func<FileStatus> state;
1316

1417
/// <summary>
15-
/// State of the version of the <see cref="Blob"/> pointed at by this <see cref="IndexEntry"/>,
16-
/// compared against the <see cref="Blob"/> known from the <see cref="Repository.Head"/> and the file in the working directory.
18+
/// State of the version of the <see cref = "Blob" /> pointed at by this <see cref = "IndexEntry" />,
19+
/// compared against the <see cref = "Blob" /> known from the <see cref = "Repository.Head" /> and the file in the working directory.
1720
/// </summary>
1821
public FileStatus State
1922
{
@@ -26,7 +29,7 @@ public FileStatus State
2629
public string Path { get; private set; }
2730

2831
/// <summary>
29-
/// Gets the id of the <see cref="Blob"/> pointed at by this index entry.
32+
/// Gets the id of the <see cref = "Blob" /> pointed at by this index entry.
3033
/// </summary>
3134
public ObjectId Id { get; private set; }
3235

@@ -40,5 +43,56 @@ internal static IndexEntry CreateFromPtr(Repository repo, IntPtr ptr)
4043
state = () => repo.Index.RetrieveStatus(entry.Path)
4144
};
4245
}
46+
47+
/// <summary>
48+
/// Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "IndexEntry" />.
49+
/// </summary>
50+
/// <param name = "obj">The <see cref = "Object" /> to compare with the current <see cref = "IndexEntry" />.</param>
51+
/// <returns>True if the specified <see cref = "Object" /> is equal to the current <see cref = "IndexEntry" />; otherwise, false.</returns>
52+
public override bool Equals(object obj)
53+
{
54+
return Equals(obj as IndexEntry);
55+
}
56+
57+
/// <summary>
58+
/// Determines whether the specified <see cref = "IndexEntry" /> is equal to the current <see cref = "IndexEntry" />.
59+
/// </summary>
60+
/// <param name = "other">The <see cref = "IndexEntry" /> to compare with the current <see cref = "IndexEntry" />.</param>
61+
/// <returns>True if the specified <see cref = "IndexEntry" /> is equal to the current <see cref = "IndexEntry" />; otherwise, false.</returns>
62+
public bool Equals(IndexEntry other)
63+
{
64+
return equalityHelper.Equals(this, other);
65+
}
66+
67+
/// <summary>
68+
/// Returns the hash code for this instance.
69+
/// </summary>
70+
/// <returns>A 32-bit signed integer hash code.</returns>
71+
public override int GetHashCode()
72+
{
73+
return equalityHelper.GetHashCode(this);
74+
}
75+
76+
/// <summary>
77+
/// Tests if two <see cref = "IndexEntry" /> are equal.
78+
/// </summary>
79+
/// <param name = "left">First <see cref = "IndexEntry" /> to compare.</param>
80+
/// <param name = "right">Second <see cref = "IndexEntry" /> to compare.</param>
81+
/// <returns>True if the two objects are equal; false otherwise.</returns>
82+
public static bool operator ==(IndexEntry left, IndexEntry right)
83+
{
84+
return Equals(left, right);
85+
}
86+
87+
/// <summary>
88+
/// Tests if two <see cref = "IndexEntry" /> are different.
89+
/// </summary>
90+
/// <param name = "left">First <see cref = "IndexEntry" /> to compare.</param>
91+
/// <param name = "right">Second <see cref = "IndexEntry" /> to compare.</param>
92+
/// <returns>True if the two objects are different; false otherwise.</returns>
93+
public static bool operator !=(IndexEntry left, IndexEntry right)
94+
{
95+
return !Equals(left, right);
96+
}
4397
}
4498
}

0 commit comments

Comments
 (0)
0