Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 3170b57

Browse files
shanaStanleyGoldman
authored andcommitted
Allow lock IDs to be strings, some servers return that instead of ints (#788)
Fixes #786
1 parent a07177e commit 3170b57

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

src/GitHub.Api/Git/GitLock.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public struct GitLock
99
{
1010
public static GitLock Default = new GitLock();
1111

12-
public int id;
12+
public string id;
1313
public string path;
1414
public GitUser owner;
1515
[NotSerialized] public string lockedAtString;
@@ -21,6 +21,7 @@ public DateTimeOffset locked_at
2121
if (!DateTimeOffset.TryParseExact(lockedAtString, Constants.Iso8601Formats,
2222
CultureInfo.InvariantCulture, Constants.DateTimeStyle, out dt))
2323
{
24+
locked_at = DateTimeOffset.MinValue;
2425
return DateTimeOffset.MinValue;
2526
}
2627
return dt;
@@ -30,15 +31,15 @@ public DateTimeOffset locked_at
3031
lockedAtString = value.ToUniversalTime().ToString(Constants.Iso8601FormatZ, CultureInfo.InvariantCulture);
3132
}
3233
}
33-
[NotSerialized] public int ID => id;
34-
[NotSerialized] public NPath Path => path.ToNPath();
34+
[NotSerialized] public string ID => id ?? String.Empty;
35+
[NotSerialized] public NPath Path => path?.ToNPath() ?? NPath.Default;
3536
[NotSerialized] public GitUser Owner => owner;
3637
[NotSerialized] public DateTimeOffset LockedAt => locked_at;
3738

38-
public GitLock(int id, NPath path, GitUser owner, DateTimeOffset locked_at)
39+
public GitLock(string id, NPath path, GitUser owner, DateTimeOffset locked_at)
3940
{
4041
this.id = id;
41-
this.path = path;
42+
this.path = path.IsInitialized ? path.ToString() : null;
4243
this.owner = owner;
4344
this.lockedAtString = locked_at.ToUniversalTime().ToString(Constants.Iso8601FormatZ, CultureInfo.InvariantCulture);
4445
}
@@ -58,7 +59,7 @@ public bool Equals(GitLock other)
5859
public override int GetHashCode()
5960
{
6061
int hash = 17;
61-
hash = hash * 23 + id.GetHashCode();
62+
hash = hash * 23 + ID.GetHashCode();
6263
hash = hash * 23 + Path.GetHashCode();
6364
hash = hash * 23 + owner.GetHashCode();
6465
hash = hash * 23 + locked_at.GetHashCode();
@@ -67,7 +68,7 @@ public override int GetHashCode()
6768

6869
public static bool operator ==(GitLock lhs, GitLock rhs)
6970
{
70-
return lhs.id == rhs.id && lhs.Path == rhs.Path && lhs.owner == rhs.owner && lhs.locked_at == rhs.locked_at;
71+
return lhs.ID == rhs.ID && lhs.Path == rhs.Path && lhs.owner == rhs.owner && lhs.locked_at == rhs.locked_at;
7172
}
7273

7374
public static bool operator !=(GitLock lhs, GitLock rhs)
@@ -76,7 +77,7 @@ public override int GetHashCode()
7677
}
7778
public override string ToString()
7879
{
79-
return $"{{id:{id}, path:{Path}, owner:{{{owner}}}, locked_at:'{locked_at}'}}";
80+
return $"{{ID:{ID}, path:{Path}, owner:{{{owner}}}, locked_at:'{locked_at}'}}";
8081
}
8182
}
8283
}

src/GitHub.Api/IO/NiceIO.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,8 @@ public override int GetHashCode()
474474
int hash = 17;
475475
// Suitable nullity checks etc, of course :)
476476
hash = hash * 23 + _isInitialized.GetHashCode();
477+
if (!_isInitialized)
478+
return hash;
477479
hash = hash * 23 + _isRelative.GetHashCode();
478480
foreach (var element in _elements)
479481
hash = hash * 23 + (IsUnix ? element : element.ToUpperInvariant()).GetHashCode();

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/LocksView.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public void Load(List<GitLock> locks, List<GitStatusEntry> gitStatusEntries)
215215
for (int i = 0; i < gitStatusEntries.Count; i++)
216216
statusEntries.Add(gitStatusEntries[i].Path.ToNPath().ToString(SlashMode.Forward), i);
217217
var selectedLockId = SelectedEntry != null && SelectedEntry.GitLock != GitLock.Default
218-
? (int?) SelectedEntry.GitLock.ID
218+
? SelectedEntry.GitLock.ID
219219
: null;
220220

221221
var scrollValue = scroll.y;
@@ -249,7 +249,7 @@ public void Load(List<GitLock> locks, List<GitStatusEntry> gitStatusEntries)
249249
for (var index = 0; index < gitLockEntries.Count; index++)
250250
{
251251
var gitLockEntry = gitLockEntries[index];
252-
if (selectedLockId.HasValue && selectedLockId.Value == gitLockEntry.GitLock.ID)
252+
if (selectedLockId == gitLockEntry.GitLock.ID)
253253
{
254254
selectedEntry = gitLockEntry;
255255
selectionPresent = true;

src/tests/UnitTests/IO/LockOutputProcessorTests.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace UnitTests
1010
{
1111
[TestFixture]
12-
class LockOutputProcessorTests : BaseOutputProcessorTests
12+
class LocksTests : BaseOutputProcessorTests
1313
{
1414
private void AssertProcessOutput(IEnumerable<string> lines, GitLock[] expected)
1515
{
@@ -51,25 +51,39 @@ public void ShouldParseZeroLocksFormat2()
5151
}
5252

5353
[Test]
54-
public void ShouldParseTwoLocksFormat1()
54+
public void ShouldParseTwoLocksFormat()
5555
{
5656
var now = DateTimeOffset.ParseExact(DateTimeOffset.UtcNow.ToString(Constants.Iso8601FormatZ), Constants.Iso8601Formats, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
5757
var output = new[]
5858
{
5959
@"[{""id"":""12"", ""path"":""folder/somefile.png"", ""owner"":{""name"":""GitHub User""}, ""locked_at"":""" + now.ToString(Constants.Iso8601FormatZ) + "\"}" +
60-
@" ,{""id"":""21"", ""path"":""somezip.zip"", ""owner"":{""name"":""GitHub User""}, ""locked_at"":""" + now.ToString(Constants.Iso8601FormatZ) + "\"}]",
60+
@" ,{""id"":""2f9cfde9c159d50e235cc1402c3e534b0bf2198afb20760697a5f9b07bf04fb3"", ""path"":""somezip.zip"", ""owner"":{""name"":""GitHub User""}, ""locked_at"":""" + now.ToString(Constants.Iso8601FormatZ) + "\"}]",
6161
string.Empty,
6262
"2 lock(s) matched query.",
6363
null
6464
};
6565

6666
var expected = new[] {
67-
new GitLock(12, "folder/somefile.png".ToNPath(), new GitUser("GitHub User", ""), now),
68-
new GitLock(21, "somezip.zip".ToNPath(), new GitUser("GitHub User", ""), now)
67+
new GitLock("12", "folder/somefile.png".ToNPath(), new GitUser("GitHub User", ""), now),
68+
new GitLock("2f9cfde9c159d50e235cc1402c3e534b0bf2198afb20760697a5f9b07bf04fb3", "somezip.zip".ToNPath(), new GitUser("GitHub User", ""), now)
6969
};
7070

7171

72-
AssertProcessOutput(output, expected);
72+
AssertProcessOutput(output, expected);
73+
}
74+
75+
[Test]
76+
public void GitLockComparisons()
77+
{
78+
GitLock lock1 = GitLock.Default;
79+
GitLock lock2 = GitLock.Default;
80+
Assert.IsTrue(lock1.Equals(lock2));
81+
Assert.IsTrue(lock1 == lock2);
82+
// these are the defaults
83+
lock1 = new GitLock(null, NPath.Default, GitUser.Default, DateTimeOffset.MinValue);
84+
lock2 = new GitLock();
85+
Assert.IsTrue(lock1.Equals(lock2));
86+
Assert.IsTrue(lock1 == lock2);
7387
}
7488
}
7589
}

0 commit comments

Comments
 (0)