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

Commit e29863d

Browse files
committed
Listing locks in json format so they include the lock time
1 parent 8055a49 commit e29863d

File tree

17 files changed

+98
-184
lines changed

17 files changed

+98
-184
lines changed

src/GitHub.Api/Git/GitClient.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public ITask<GitUser> SetConfigNameAndEmail(string username, string email)
171171

172172
public ITask<List<GitLock>> ListLocks(bool local, BaseOutputListProcessor<GitLock> processor = null)
173173
{
174-
return new GitListLocksTask(new GitObjectFactory(environment), local, cancellationToken, processor)
174+
return new GitListLocksTask(local, cancellationToken, processor)
175175
.Configure(processManager);
176176
}
177177

@@ -342,8 +342,8 @@ public struct GitUser
342342
public string name;
343343
public string email;
344344

345-
public string Name { get { return name; } }
346-
public string Email { get { return email; } }
345+
public string Name => name;
346+
public string Email { get { return String.IsNullOrEmpty(email) ? String.Empty : email; } }
347347

348348
public GitUser(string name, string email)
349349
{
@@ -355,7 +355,7 @@ public override int GetHashCode()
355355
{
356356
int hash = 17;
357357
hash = hash * 23 + (name?.GetHashCode() ?? 0);
358-
hash = hash * 23 + (email?.GetHashCode() ?? 0);
358+
hash = hash * 23 + Email.GetHashCode();
359359
return hash;
360360
}
361361

@@ -370,8 +370,7 @@ public bool Equals(GitUser other)
370370
{
371371
return
372372
String.Equals(name, other.name) &&
373-
String.Equals(email, other.email)
374-
;
373+
Email.Equals(other.Email);
375374
}
376375

377376
public static bool operator ==(GitUser lhs, GitUser rhs)

src/GitHub.Api/Git/GitLock.cs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,58 @@ public struct GitLock
77
{
88
public static GitLock Default = new GitLock();
99

10-
public int ID;
11-
public string Path;
12-
public string FullPath;
13-
public string User;
10+
public int id;
11+
public string path;
12+
public GitUser owner;
13+
public DateTimeOffset locked_at;
1414

15-
public GitLock(string path, string fullPath, string user, int id)
15+
[NotSerialized] public int ID => id;
16+
[NotSerialized] public NPath Path => path.ToNPath();
17+
[NotSerialized] public GitUser Owner => owner;
18+
[NotSerialized] public DateTimeOffset LockedAt => locked_at;
19+
20+
public GitLock(int id, NPath path, GitUser owner, DateTimeOffset locked_at)
1621
{
17-
Path = path;
18-
FullPath = fullPath;
19-
User = user;
20-
ID = id;
22+
this.id = id;
23+
this.path = path;
24+
this.owner = owner;
25+
this.locked_at = locked_at;
2126
}
2227

2328
public override bool Equals(object other)
2429
{
2530
if (other is GitLock)
26-
{
27-
return this.Equals((GitLock)other);
28-
}
31+
return Equals((GitLock)other);
2932
return false;
3033
}
3134

32-
public bool Equals(GitLock p)
35+
public bool Equals(GitLock other)
3336
{
34-
return ID == p.ID;
37+
return this == other;
3538
}
3639

3740
public override int GetHashCode()
3841
{
39-
return 17 * 23 + ID.GetHashCode();
42+
int hash = 17;
43+
hash = hash * 23 + id.GetHashCode();
44+
hash = hash * 23 + Path.GetHashCode();
45+
hash = hash * 23 + owner.GetHashCode();
46+
hash = hash * 23 + locked_at.GetHashCode();
47+
return hash;
4048
}
4149

4250
public static bool operator ==(GitLock lhs, GitLock rhs)
4351
{
44-
return lhs.Equals(rhs);
52+
return lhs.id == rhs.id && lhs.Path == rhs.Path && lhs.owner == rhs.owner && lhs.locked_at == rhs.locked_at;
4553
}
4654

4755
public static bool operator !=(GitLock lhs, GitLock rhs)
4856
{
49-
return !(lhs.Equals(rhs));
57+
return !(lhs == rhs);
5058
}
5159
public override string ToString()
5260
{
53-
return $"{{GitLock ({User}) '{Path}'}}";
61+
return $"{{id:{id}, path:{Path}, owner:{{{owner}}}, locked_at:'{locked_at}'}}";
5462
}
5563
}
5664
}

src/GitHub.Api/Git/GitObjectFactory.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,5 @@ public GitStatusEntry CreateGitStatusEntry(string path, GitFileStatus status, st
2020

2121
return new GitStatusEntry(relativePath, absolutePath, projectPath, status, originalPath?.ToNPath(), staged);
2222
}
23-
24-
public GitLock CreateGitLock(string path, string user, int id)
25-
{
26-
var npath = new NPath(path).MakeAbsolute();
27-
var fullPath = npath.RelativeTo(environment.RepositoryPath);
28-
29-
return new GitLock(path, fullPath, user, id);
30-
}
3123
}
3224
}

src/GitHub.Api/Git/IGitObjectFactory.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ namespace GitHub.Unity
33
interface IGitObjectFactory
44
{
55
GitStatusEntry CreateGitStatusEntry(string path, GitFileStatus status, string originalPath = null, bool staged = false);
6-
GitLock CreateGitLock(string path, string user, int id);
76
}
87
}

src/GitHub.Api/Git/Tasks/GitListLocksTask.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ namespace GitHub.Unity
44
{
55
class GitListLocksTask : ProcessTaskWithListOutput<GitLock>
66
{
7-
private const string TaskName = "git lfs locks";
87
private readonly string args;
98

10-
public GitListLocksTask(IGitObjectFactory gitObjectFactory, bool local,
9+
public GitListLocksTask(bool local,
1110
CancellationToken token, BaseOutputListProcessor<GitLock> processor = null)
12-
: base(token, processor ?? new LockOutputProcessor(gitObjectFactory))
11+
: base(token, processor ?? new LocksOutputProcessor())
1312
{
14-
Name = TaskName;
15-
args = "lfs locks";
13+
args = "lfs locks --json";
1614
if (local)
1715
{
1816
args += " --local";
1917
}
18+
Name = args;
2019
}
2120

2221
public override string ProcessArguments => args;

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166
<Compile Include="Extensions\AsyncExtensions.cs" />
167167
<Compile Include="Platform\Settings.cs" />
168168
<Compile Include="OutputProcessors\BranchListOutputProcessor.cs" />
169-
<Compile Include="OutputProcessors\LockOutputProcessor.cs" />
169+
<Compile Include="OutputProcessors\LocksOutputProcessor.cs" />
170170
<Compile Include="OutputProcessors\LogEntryOutputProcessor.cs" />
171171
<Compile Include="OutputProcessors\ProcessManager.cs" />
172172
<Compile Include="OutputProcessors\StatusOutputProcessor.cs" />

src/GitHub.Api/Helpers/Constants.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ static class Constants
1111
public const string TraceLoggingKey = "EnableTraceLogging";
1212
public const string WebTimeoutKey = "WebTimeout";
1313
public const string Iso8601Format = @"yyyy-MM-dd\THH\:mm\:ss.fffzzz";
14+
public const string Iso8601FormatZ = @"yyyy-MM-dd\THH\:mm\:ss\Z";
1415
public static readonly string[] Iso8601Formats = {
16+
@"yyyy-MM-dd\THH\:mm\:ss\Z",
1517
@"yyyy-MM-dd\THH\:mm\:ss.fffffffzzz",
1618
@"yyyy-MM-dd\THH\:mm\:ss.fffzzz",
17-
@"yyyy-MM-dd\THH\:mm\:sszzz"
19+
@"yyyy-MM-dd\THH\:mm\:sszzz",
1820
};
1921
public const string SkipVersionKey = "SkipVersion";
2022

src/GitHub.Api/Helpers/SimpleJson.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,9 +1255,11 @@ class PocoJsonSerializerStrategy : IJsonSerializerStrategy
12551255
@"yyyy-MM-dd\THH\:mm\:sszzz",
12561256
@"yyyy-MM-dd\THH\:mm\:ss.fffffffzzz",
12571257
@"yyyy-MM-dd\THH\:mm\:ss.fffzzz",
1258+
@"yyyy-MM-dd\THH\:mm\:ssZ",
12581259
@"yyyy-MM-dd\THH:mm:ss.fffffffzzz",
12591260
@"yyyy-MM-dd\THH:mm:ss.fffzzz",
12601261
@"yyyy-MM-dd\THH:mm:sszzz",
1262+
@"yyyy-MM-dd\THH:mm:ss\Z",
12611263
};
12621264

12631265
public PocoJsonSerializerStrategy()
@@ -1364,6 +1366,8 @@ public virtual object DeserializeObject(object value, Type type)
13641366
{
13651367
if (str.Length != 0) // We know it can't be null now.
13661368
{
1369+
if (type == typeof(NPath) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(NPath)))
1370+
return new NPath(str);
13671371
if (type == typeof(DateTime) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(DateTime)))
13681372
return DateTime.ParseExact(str, Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
13691373
if (type == typeof(DateTimeOffset) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(DateTimeOffset)))
@@ -1495,7 +1499,9 @@ protected virtual object SerializeEnum(Enum p)
14951499
protected virtual bool TrySerializeKnownTypes(object input, out object output)
14961500
{
14971501
bool returnValue = true;
1498-
if (input is DateTime)
1502+
if (input is NPath)
1503+
output = input.ToString();
1504+
else if (input is DateTime)
14991505
output = ((DateTime)input).ToUniversalTime().ToString(Iso8601Format[0], CultureInfo.InvariantCulture);
15001506
else if (input is DateTimeOffset)
15011507
output = ((DateTimeOffset)input).ToUniversalTime().ToString(Iso8601Format[0], CultureInfo.InvariantCulture);
@@ -2158,6 +2164,8 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
21582164

21592165
namespace GitHub.Unity
21602166
{
2167+
using GitHub.Unity.Json;
2168+
21612169
[System.AttributeUsage(System.AttributeTargets.Property |
21622170
System.AttributeTargets.Field)]
21632171
public sealed class NotSerializedAttribute : Attribute

src/GitHub.Api/OutputProcessors/LockOutputProcessor.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
3+
namespace GitHub.Unity
4+
{
5+
class LocksOutputProcessor : BaseOutputListProcessor<GitLock>
6+
{
7+
public override void LineReceived(string line)
8+
{
9+
if (string.IsNullOrEmpty(line))
10+
{
11+
//Do Nothing
12+
return;
13+
}
14+
15+
try
16+
{
17+
var locks = line.FromJson<GitLock[]>(lowerCase: true);
18+
foreach (var lck in locks)
19+
{
20+
RaiseOnEntry(lck);
21+
}
22+
}
23+
catch(Exception ex)
24+
{
25+
Logger.Error(ex, $"Failed to parse lock line {line}");
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)