Skip to content

Commit 318e7ca

Browse files
committed
Make Remote IEquatable
1 parent 7f65f29 commit 318e7ca

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

LibGit2Sharp.Tests/RemoteFixture.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,31 @@ public void GettingRemoteThatDoesntExistThrows()
2626
repo.Remotes["test"].ShouldBeNull();
2727
}
2828
}
29+
30+
[Test]
31+
public void CanCheckEqualityOfRemote()
32+
{
33+
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoPath);
34+
35+
using (var repo = new Repository(path.RepositoryPath))
36+
{
37+
Remote oneOrigin = repo.Remotes["origin"];
38+
oneOrigin.ShouldNotBeNull();
39+
40+
Remote otherOrigin = repo.Remotes["origin"];
41+
otherOrigin.ShouldEqual(oneOrigin);
42+
43+
repo.Config.Set("remote.origin2.url", oneOrigin.Url);
44+
45+
/* LibGit2 cringes when a remote doesn't expose a fetch refspec */
46+
var originFetch = repo.Config.Get<string>("remote", "origin", "fetch", null);
47+
repo.Config.Set("remote.origin2.fetch", originFetch);
48+
49+
Remote differentRemote = repo.Remotes["origin2"];
50+
differentRemote.ShouldNotBeNull();
51+
52+
differentRemote.ShouldNotEqual(oneOrigin);
53+
}
54+
}
2955
}
3056
}

LibGit2Sharp/Remote.cs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
namespace LibGit2Sharp
1+
using System;
2+
using LibGit2Sharp.Core;
3+
4+
namespace LibGit2Sharp
25
{
36
/// <summary>
47
/// A remote repository whose branches are tracked.
58
/// </summary>
6-
public class Remote
9+
public class Remote : IEquatable<Remote>
710
{
11+
private static readonly LambdaEqualityHelper<Remote> equalityHelper =
12+
new LambdaEqualityHelper<Remote>(new Func<Remote, object>[] { x => x.Name, x => x.Url });
13+
814
/// <summary>
915
/// Gets the alias of this remote repository.
1016
/// </summary>
@@ -14,5 +20,56 @@ public class Remote
1420
/// Gets the urls to use to communicate with this remote repository.
1521
/// </summary>
1622
public string Url { get; internal set; }
23+
24+
/// <summary>
25+
/// Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "Remote" />.
26+
/// </summary>
27+
/// <param name = "obj">The <see cref = "Object" /> to compare with the current <see cref = "Remote" />.</param>
28+
/// <returns>True if the specified <see cref = "Object" /> is equal to the current <see cref = "Remote" />; otherwise, false.</returns>
29+
public override bool Equals(object obj)
30+
{
31+
return Equals(obj as Remote);
32+
}
33+
34+
/// <summary>
35+
/// Determines whether the specified <see cref = "Remote" /> is equal to the current <see cref = "Remote" />.
36+
/// </summary>
37+
/// <param name = "other">The <see cref = "Remote" /> to compare with the current <see cref = "Remote" />.</param>
38+
/// <returns>True if the specified <see cref = "Remote" /> is equal to the current <see cref = "Remote" />; otherwise, false.</returns>
39+
public bool Equals(Remote other)
40+
{
41+
return equalityHelper.Equals(this, other);
42+
}
43+
44+
/// <summary>
45+
/// Returns the hash code for this instance.
46+
/// </summary>
47+
/// <returns>A 32-bit signed integer hash code.</returns>
48+
public override int GetHashCode()
49+
{
50+
return equalityHelper.GetHashCode(this);
51+
}
52+
53+
/// <summary>
54+
/// Tests if two <see cref = "Remote" /> are equal.
55+
/// </summary>
56+
/// <param name = "left">First <see cref = "Remote" /> to compare.</param>
57+
/// <param name = "right">Second <see cref = "Remote" /> to compare.</param>
58+
/// <returns>True if the two objects are equal; false otherwise.</returns>
59+
public static bool operator ==(Remote left, Remote right)
60+
{
61+
return Equals(left, right);
62+
}
63+
64+
/// <summary>
65+
/// Tests if two <see cref = "Remote" /> are different.
66+
/// </summary>
67+
/// <param name = "left">First <see cref = "Remote" /> to compare.</param>
68+
/// <param name = "right">Second <see cref = "Remote" /> to compare.</param>
69+
/// <returns>True if the two objects are different; false otherwise.</returns>
70+
public static bool operator !=(Remote left, Remote right)
71+
{
72+
return !Equals(left, right);
73+
}
1774
}
1875
}

0 commit comments

Comments
 (0)