8000 Refactor implementation of equality members of Branch and Tag types · rlazev/libgit2sharp@e6ba4c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit e6ba4c9

Browse files
committed
Refactor implementation of equality members of Branch and Tag types
1 parent 235ccd2 commit e6ba4c9

File tree

3 files changed

+59
-121
lines changed

3 files changed

+59
-121
lines changed

LibGit2Sharp/Branch.cs

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
using System;
22
using System.Globalization;
33
using System.Linq;
4-
using LibGit2Sharp.Core;
54
using LibGit2Sharp.Core.Compat;
65

76
namespace LibGit2Sharp
87
{
98
/// <summary>
109
/// A branch is a special kind of reference
1110
/// </summary>
12-
public class Branch : ReferenceWrapper<Commit>, IEquatable<Branch>
11+
public class Branch : ReferenceWrapper<Commit>
1312
{
14-
private static readonly LambdaEqualityHelper<Branch> equalityHelper =
15-
new LambdaEqualityHelper<Branch>(new Func<Branch, object>[] { x => x.CanonicalName, x => x.Tip });
16-
1713
private readonly Lazy<Branch> trackedBranch;
1814

1915
/// <summary>
@@ -134,39 +130,6 @@ public ICommitCollection Commits
134130
get { return repo.Commits.QueryBy(new Filter { Since = this }); }
135131
}
136132

137-
#region IEquatable<Branch> Members
138-
139-
/// <summary>
140-
/// Determines whether the specified <see cref = "Branch" /> is equal to the current <see cref = "Branch" />.
141-
/// </summary>
142-
/// <param name = "other">The <see cref = "Branch" /> to compare with the current <see cref = "Branch" />.</param>
143-
/// <returns>True if the specified <see cref = "Branch" /> is equal to the current <see cref = "Branch" />; otherwise, false.</returns>
144-
public bool Equals(Branch other)
145-
{
146-
return equalityHelper.Equals(this, other);
147-
}
148-
149-
#endregion
150-
151-
/// <summary>
152-
/// Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "Branch" />.
153-
/// </summary>
154-
/// <param name = "obj">The <see cref = "Object" /> to compare with the current <see cref = "Branch" />.</param>
155-
/// <returns>True if the specified <see cref = "Object" /> is equal to the current <see cref = "Branch" />; otherwise, false.</returns>
156-
public override bool Equals(object obj)
157-
{
158-
return Equals(obj as Branch);
159-
}
160-
161-
/// <summary>
162-
/// Returns the hash code for this instance.
163-
/// </summary>
164-
/// <returns>A 32-bit signed integer hash code.</returns>
165-
public override int GetHashCode()
166-
{
167-
return equalityHelper.GetHashCode(this);
168-
}
169-
170133
private Branch ResolveTrackedBranch()
171134
{
172135
var trackedRemote = repo.Config.Get<string>("branch", Name, "remote", null);
@@ -220,27 +183,5 @@ protected override string Shorten(string canonicalName)
220183

221184
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "'{0}' does not look like a valid branch name.", canonicalName));
222185
}
223-
224-
/// <summary>
225-
/// Tests if two <see cref = "Branch" /> are equal.
226-
/// </summary>
227-
/// <param name = "left">First <see cref = "Branch" /> to compare.</param>
228-
/// <param name = "right">Second <see cref = "Branch" /> to compare.</param>
229-
/// <returns>True if the two objects are equal; false otherwise.</returns>
230-
public static bool operator ==(Branch left, Branch right)
231-
{
232-
return Equals(left, right);
233-
}
234-
235-
/// <summary>
236-
/// Tests if two <see cref = "Branch" /> are different.
237-
/// </summary>
238-
/// <param name = "left">First <see cref = "Branch" /> to compare.</param>
239-
/// <param name = "right">Second <see cref = "Branch" /> to compare.</param>
240-
/// <returns>True if the two objects are different; false otherwise.</returns>
241-
public static bool operator !=(Branch left, Branch right)
242-
{
243-
return !Equals(left, right);
244-
}
245186
}
246187
}

LibGit2Sharp/ReferenceWrapper.cs

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ namespace LibGit2Sharp
88
/// A base class for things that wrap a <see cref = "Reference" /> (branch, tag, etc).
99
/// </summary>
1010
/// <typeparam name="TObject">The type of the referenced Git object.</typeparam>
11-
public abstract class ReferenceWrapper<TObject> where TObject : GitObject
11+
public abstract class ReferenceWrapper<TObject> : IEquatable<ReferenceWrapper<TObject>> where TObject : GitObject
1212
{
1313
/// <summary>
14-
/// The repo.
14+
/// The repository.
1515
/// </summary>
1616
protected readonly Repository repo;
1717
private readonly Lazy<TObject> objectBuilder;
1818

19-
/// <summary>
20-
/// Initializes a new instance of the <see cref = "ReferenceWrapper{TObject}" /> class.
21-
/// </summary>
22-
/// <param name="repo">The repo.</param>
19+
private static readonly LambdaEqualityHelper<ReferenceWrapper<TObject>> equalityHelper =
20+
new LambdaEqualityHelper<ReferenceWrapper<TObject>>(new Func<ReferenceWrapper<TObject>, object>[] { x => x.CanonicalName, x => x.TargetObject });
21+
22+
/// <param name="repo">The repository.</param>
2323
/// <param name="reference">The reference.</param>
2424
/// <param name="canonicalNameSelector">A function to construct the reference's canonical name.</param>
2525
protected internal ReferenceWrapper(Repository repo, Reference reference, Func<Reference, string> canonicalNameSelector)
@@ -87,5 +87,56 @@ private TObject RetrieveTargetObject(Reference reference)
8787

8888
return repo.Lookup<TObject>(target.Id);
8989
}
90+
91+
/// <summary>
92+
/// Determines whether the specified <see cref = "ReferenceWrapper{TObject}" /> is equal to the current <see cref = "ReferenceWrapper{TObject}" />.
93+
/// </summary>
94+
/// <param name = "other">The <see cref = "ReferenceWrapper{TObject}" /> to compare with the current <see cref = "ReferenceWrapper{TObject}" />.</param>
95+
/// <returns>True if the specified <see cref = "ReferenceWrapper{TObject}" /> is equal to the current <see cref = "ReferenceWrapper{TObject}" />; otherwise, false.</returns>
96+
public bool Equals(ReferenceWrapper<TObject> other)
97+
{
98+
return equalityHelper.Equals(this, other);
99+
}
100+
101+
/// <summary>
102+
/// Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "ReferenceWrapper{TObject}" />.
103+
/// </summary>
104+
/// <param name = "obj">The <see cref = "Object" /> to compare with the current <see cref = "ReferenceWrapper{TObject}" />.</param>
105+
/// <returns>True if the specified <see cref = "Object" /> is equal to the current <see cref = "ReferenceWrapper{TObject}" />; otherwise, false.</returns>
106+
public override bool Equals(object obj)
107+
{
108+
return Equals(obj as ReferenceWrapper<TObject>);
109+
}
110+
111+
/// <summary>
112+
/// Returns the hash code for this instance.
113+
/// </summary>
114+
/// <returns>A 32-bit signed integer hash code.</returns>
115+
public override int GetHashCode()
116+
{
117+
return equalityHelper.GetHashCode(this);
118+
}
119+
120+
/// <summary>
121+
/// Tests if two <see cref = "ReferenceWrapper{TObject}" /> are equal.
122+
/// </summary>
123+
/// <param name = "left">First <see cref = "ReferenceWrapper{TObject}" /> to compare.</param>
124+
/// <param name = "right">Second <see cref = "ReferenceWrapper{TObject}" /> to compare.</param>
125+
/// <returns>True if the two objects are equal; false otherwise.</returns>
126+
public static bool operator ==(ReferenceWrapper<TObject> left, ReferenceWrapper<TObject> right)
127+
{
128+
return Equals(left, right);
129+
}
130+
131+
/// <summary>
132+
/// Tests if two <see cref = "ReferenceWrapper{TObject}" /> are different.
133+
/// </summary>
134+
/// <param name = "left">First <see cref = "ReferenceWrapper{TObject}" /> to compare.</param>
135+
/// <param name = "right">Second <see cref = "ReferenceWrapper{TObject}" /> to compare.</param>
136+
/// <returns>True if the two objects are different; false otherwise.</returns>
137+
public static bool operator !=(ReferenceWrapper<TObject> left, ReferenceWrapper<TObject> right)
138+
{
139+
return !Equals(left, right);
140+
}
90141
}
91142
}

LibGit2Sharp/Tag.cs

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ namespace LibGit2Sharp
66
/// <summary>
77
/// A Tag
88
/// </summary>
9-
public class Tag : ReferenceWrapper<GitObject>, IEquatable<Tag>
9+
public class Tag : ReferenceWrapper<GitObject>
1010
{
11-
private static readonly LambdaEqualityHelper<Tag> equalityHelper =
12-
new LambdaEqualityHelper<Tag>(new Func<Tag, object>[] { x => x.CanonicalName, x => x.Target });
13-
1411
internal Tag(Repository repo, Reference reference, string canonicalName)
1512
: base(repo, reference, _ => canonicalName)
1613
{
@@ -62,56 +59,5 @@ protected override string Shorten(string canonicalName)
6259

6360
return canonicalName.Substring("refs/tags/".Length);
6461
}
65-
66-
/// <summary>
67-
/// Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "Tag" />.
68-
/// </summary>
69-
/// <param name = "obj">The <see cref = "Object" /> to compare with the current <see cref = "Tag" />.</param>
70-
/// <returns>True if the specified <see cref = "Object" /> is equal to the current <see cref = "Tag" />; otherwise, false.</returns>
71-
public override bool Equals(object obj)
72-
{
73-
return Equals(obj as Tag);
74-
}
75-
76-
/// <summary>
77-
/// Determines whether the specified <see cref = "Tag" /> is equal to the current <see cref = "Tag" />.
78-
/// </summary>
79-
/// <param name = "other">The <see cref = "Tag" /> to compare with the current <see cref = "Tag" />.</param>
80-
/// <returns>True if the specified <see cref = "Tag" /> is equal to the current <see cref = "Tag" />; otherwise, false.</returns>
81-
public bool Equals(Tag other)
82-
{
83-
return equalityHelper.Equals(this, other);
84-
}
85-
86-
/// <summary>
87-
/// Returns the hash code for this instance.
88-
/// </summary>
89-
/// <returns>A 32-bit signed integer hash code.</returns>
90-
public override int GetHashCode()
91-
{
92-
return equalityHelper.GetHashCode(this);
93-
}
94-
95-
/// <summary>
96-
/// Tests if two <see cref = "Tag" /> are equal.
97-
/// </summary>
98-
/// <param name = "left">First <see cref = "Tag" /> to compare.</param>
99-
/// <param name = "right">Second <see cref = "Tag" /> to compare.</param>
100-
/// <returns>True if the two objects are equal; false otherwise.</returns>
101-
public static bool operator ==(Tag left, Tag right)
102-
{
103-
return Equals(left, right);
104-
}
105-
106-
/// <summary>
107-
/// Tests if two <see cref = "Tag" /> are different.
108-
/// </summary>
109-
/// <param name = "left">First <see cref = "Tag" /> to compare.</param>
110-
/// <param name = "right">Second <see cref = "Tag" /> to compare.</param>
111-
/// <returns>True if the two objects are different; false otherwise.</returns>
112-
public static bool operator !=(Tag left, Tag right)
113-
{
114-
return !Equals(left, right);
115-
}
11662
}
11763
}

0 commit comments

Comments
 (0)
0