Skip to content

Commit e51824b

Browse files
committed
refactor: use --json while querying LFS locks instead of parse line-by-line with regex (#1858)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 38552a1 commit e51824b

File tree

5 files changed

+34
-31
lines changed

5 files changed

+34
-31
lines changed

src/App.JsonCodeGen.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public override void Write(Utf8JsonWriter writer, DataGridLength value, JsonSeri
6565
[JsonSerializable(typeof(Models.ThemeOverrides))]
6666
[JsonSerializable(typeof(Models.Version))]
6767
[JsonSerializable(typeof(Models.RepositorySettings))]
68+
[JsonSerializable(typeof(List<Models.LFSLock>))]
6869
[JsonSerializable(typeof(List<Models.VisualStudioInstance>))]
6970
[JsonSerializable(typeof(ViewModels.Preferences))]
7071
internal partial class JsonCodeGen : JsonSerializerContext { }

src/Commands/LFS.cs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Text;
4-
using System.Text.RegularExpressions;
3+
using System.Text.Json;
54
using System.Threading.Tasks;
65

76
namespace SourceGit.Commands
87
{
9-
public partial class LFS : Command
8+
public class LFS : Command
109
{
11-
[GeneratedRegex(@"^(.+)\s+([\w.]+)\s+\w+:(\d+)$")]
12-
private static partial Regex REG_LOCK();
13-
1410
public LFS(string repo)
1511
{
1612
WorkingDirectory = repo;
@@ -60,30 +56,23 @@ public async Task PruneAsync()
6056

6157
public async Task<List<Models.LFSLock>> GetLocksAsync(string remote)
6258
{
63-
Args = $"lfs locks --remote={remote}";
59+
Args = $"lfs locks --json --remote={remote}";
6460

6561
var rs = await ReadToEndAsync().ConfigureAwait(false);
66-
var locks = new List<Models.LFSLock>();
67-
6862
if (rs.IsSuccess)
6963
{
70-
var lines = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
71-
foreach (var line in lines)
64+
try
65+
{
66+
var locks = JsonSerializer.Deserialize(rs.StdOut, JsonCodeGen.Default.ListLFSLock);
67+
return locks;
68+
}
69+
catch
7270
{
73-
var match = REG_LOCK().Match(line);
74-
if (match.Success)
75-
{
76-
locks.Add(new Models.LFSLock()
77-
{
78-
File = match.Groups[1].Value,
79-
User = match.Groups[2].Value,
80-
ID = long.Parse(match.Groups[3].Value),
81-
});
82-
}
71+
// Ignore exceptions.
8372
}
8473
}
8574

86-
return locks;
75+
return [];
8776
}
8877

8978
public async Task<bool> LockAsync(string remote, string file)

src/Models/LFSLock.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1-
namespace SourceGit.Models
1+
using System.Text.Json.Serialization;
2+
3+
namespace SourceGit.Models
24
{
5+
public class LFSLockOwner
6+
{
7+
[JsonPropertyName("name")]
8+
public string Name { get; set; } = string.Empty;
9+
}
10+
311
public class LFSLock
412
{
5-
public string File { get; set; } = string.Empty;
6-
public string User { get; set; } = string.Empty;
7-
public long ID { get; set; } = 0;
13+
[JsonPropertyName("id")]
14+
public string ID { get; set; } = string.Empty;
15+
16+
[JsonPropertyName("path")]
17+
public string Path { get; set; } = string.Empty;
18+
19+
[JsonPropertyName("owner")]
20+
public LFSLockOwner Owner { get; set; } = null;
821
}
922
}

src/ViewModels/LFSLocks.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public async Task UnlockAsync(Models.LFSLock lfsLock, bool force)
6262

6363
IsLoading = true;
6464

65-
var succ = await _repo.UnlockLFSFileAsync(_remote, lfsLock.File, force, false);
65+
var succ = await _repo.UnlockLFSFileAsync(_remote, lfsLock.Path, force, false);
6666
if (succ)
6767
{
6868
_cachedLocks.Remove(lfsLock);
@@ -84,7 +84,7 @@ private void UpdateVisibleLocks()
8484
{
8585
foreach (var lfsLock in _cachedLocks)
8686
{
87-
if (lfsLock.User.Equals(_userName, StringComparison.Ordinal))
87+
if (lfsLock.Owner.Name.Equals(_userName, StringComparison.Ordinal))
8888
visible.Add(lfsLock);
8989
}
9090
}

src/Views/LFSLocks.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@
8080
<Grid ColumnDefinitions="26,*,100,32,32">
8181
<Path Grid.Column="0" Width="14" Height="14" Margin="8,0,4,0" Data="{StaticResource Icons.File}"/>
8282
<Border Grid.Column="1" Margin="4,0" ClipToBounds="True">
83-
<TextBlock Text="{Binding File}" HorizontalAlignment="Left"/>
83+
<TextBlock Text="{Binding Path}" HorizontalAlignment="Left"/>
8484
</Border>
8585
<Border Grid.Column="2" Margin="8,0" ClipToBounds="True">
86-
<TextBlock Text="{Binding User}" HorizontalAlignment="Left"/>
86+
<TextBlock Text="{Binding Owner.Name}" HorizontalAlignment="Left"/>
8787
</Border>
8888
<Button Grid.Column="3" Classes="icon_button" Click="OnUnlockButtonClicked" ToolTip.Tip="{DynamicResource Text.GitLFS.Locks.Unlock}">
8989
<Path Width="14" Height="14" Data="{StaticResource Icons.Unlock}"/>

0 commit comments

Comments
 (0)