Skip to content

Commit 02ecd58

Browse files
committed
refactor: use new command QueryRepositoryStatus to update repository status in Welcome page
Signed-off-by: leo <longshuang@msn.cn>
1 parent c651e71 commit 02ecd58

File tree

5 files changed

+133
-68
lines changed

5 files changed

+133
-68
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Text.RegularExpressions;
3+
using System.Threading.Tasks;
4+
5+
namespace SourceGit.Commands
6+
{
7+
public partial class QueryRepositoryStatus : Command
8+
{
9+
[GeneratedRegex(@"ahead\s(\d+)")]
10+
private static partial Regex REG_AHEAD();
11+
12+
[GeneratedRegex(@"behind\s(\d+)")]
13+
private static partial Regex REG_BEHIND();
14+
15+
public QueryRepositoryStatus(string repo)
16+
{
17+
WorkingDirectory = repo;
18+
RaiseError = false;
19+
}
20+
21+
public async Task<Models.RepositoryStatus> GetResultAsync()
22+
{
23+
Args = "branch -l -v --format=\"%(refname:short)%00%(HEAD)%00%(upstream:track,nobracket)\"";
24+
var rs = await ReadToEndAsync().ConfigureAwait(false);
25+
if (!rs.IsSuccess)
26+
return null;
27+
28+
var status = new Models.RepositoryStatus();
29+
var lines = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
30+
foreach (var line in lines)
31+
{
32+
var parts = line.Split('\0');
33+
if (parts.Length != 3 || !parts[1].Equals("*", StringComparison.Ordinal))
34+
continue;
35+
36+
status.CurrentBranch = parts[0];
37+
if (!string.IsNullOrEmpty(parts[2]))
38+
ParseTrackStatus(status, parts[2]);
39+
}
40+
41+
status.LocalChanges = await new CountLocalChanges(WorkingDirectory, true) { RaiseError = false }
42+
.GetResultAsync()
43+
.ConfigureAwait(false);
44+
45+
return status;
46+
}
47+
48+
private void ParseTrackStatus(Models.RepositoryStatus status, string input)
49+
{
50+
var aheadMatch = REG_AHEAD().Match(input);
51+
if (aheadMatch.Success)
52+
status.Ahead = int.Parse(aheadMatch.Groups[1].Value);
53+
54+
var behindMatch = REG_BEHIND().Match(input);
55+
if (behindMatch.Success)
56+
status.Behind = int.Parse(behindMatch.Groups[1].Value);
57+
}
58+
}
59+
}

src/Models/Branch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class Branch
2626

2727
public bool HasWorktree => !IsCurrent && !string.IsNullOrEmpty(WorktreePath);
2828
public string FriendlyName => IsLocal ? Name : $"{Remote}/{Name}";
29-
public bool IsTrackStatusVisible => Ahead.Count + Behind.Count > 0;
29+
public bool IsTrackStatusVisible => Ahead.Count > 0 || Behind.Count > 0;
3030

3131
public string TrackStatusDescription
3232
{

src/Models/RepositoryStatus.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace SourceGit.Models
2+
{
3+
public class RepositoryStatus
4+
{
5+
public string CurrentBranch { get; set; } = string.Empty;
6+
public int Ahead { get; set; } = 0;
7+
public int Behind { get; set; } = 0;
8+
public int LocalChanges { get; set; } = 0;
9+
10+
public bool IsTrackingStatusVisible
11+
{
12+
get
13+
{
14+
return Ahead > 0 || Behind > 0;
15+
}
16+
}
17+
18+
public string TrackingDescription
19+
{
20+
get
21+
{
22+
if (Ahead > 0)
23+
return Behind > 0 ? $"{Ahead}{Behind}↓" : $"{Ahead}↑";
24+
25+
return Behind > 0 ? $"{Behind}↓" : string.Empty;
26+
}
27+
}
28+
}
29+
}

src/ViewModels/RepositoryNode.cs

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,10 @@ public int Depth
6464
} = 0;
6565

6666
[JsonIgnore]
67-
public Models.Branch CurrentBranch
67+
public Models.RepositoryStatus Status
6868
{
69-
get => _currentBranch;
70-
private set => SetProperty(ref _currentBranch, value);
71-
}
72-
73-
[JsonIgnore]
74-
public int LocalChanges
75-
{
76-
get => _localChanges;
77-
private set => SetProperty(ref _localChanges, value);
69+
get => _status;
70+
private set => SetProperty(ref _status, value);
7871
}
7972

8073
public List<RepositoryNode> SubNodes
@@ -141,8 +134,7 @@ public async Task UpdateStatusAsync(bool force)
141134
{
142135
if (!_isRepository || !Directory.Exists(_id))
143136
{
144-
CurrentBranch = null;
145-
LocalChanges = 0;
137+
Status = null;
146138

147139
if (SubNodes.Count > 0)
148140
{
@@ -161,19 +153,7 @@ public async Task UpdateStatusAsync(bool force)
161153
}
162154

163155
_lastUpdateStatus = DateTime.Now;
164-
LocalChanges = await new Commands.CountLocalChanges(_id, true) { RaiseError = false }.GetResultAsync();
165-
166-
var branches = await new Commands.QueryBranches(_id) { RaiseError = false }.GetResultAsync();
167-
foreach (var branch in branches)
168-
{
169-
if (branch.IsCurrent)
170-
{
171-
CurrentBranch = branch;
172-
return;
173-
}
174-
}
175-
176-
CurrentBranch = null;
156+
Status = await new Commands.QueryRepositoryStatus(_id).GetResultAsync();
177157
}
178158

179159
private string _id = string.Empty;
@@ -182,8 +162,7 @@ public async Task UpdateStatusAsync(bool force)
182162
private int _bookmark = 0;
183163
private bool _isExpanded = false;
184164
private bool _isVisible = true;
185-
private Models.Branch _currentBranch = null;
186-
private int _localChanges = 0;
165+
private Models.RepositoryStatus _status = null;
187166
private DateTime _lastUpdateStatus = DateTime.UnixEpoch.ToLocalTime();
188167
}
189168
}

src/Views/Welcome.axaml

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -158,48 +158,46 @@
158158
IsVisible="{Binding IsInvalid}"/>
159159
</StackPanel>
160160

161-
<StackPanel Grid.Column="3" Orientation="Horizontal" IsVisible="{Binding IsRepository}">
162-
<Border Margin="3,0,0,0"
163-
Height="18"
164-
CornerRadius="9"
165-
Padding="6,0"
166-
Background="{DynamicResource Brush.FG1}"
167-
VerticalAlignment="Center"
168-
IsVisible="{Binding LocalChanges, Converter={x:Static c:IntConverters.IsGreaterThanZero}}">
169-
<TextBlock Text="{Binding LocalChanges}"
170-
FontFamily="{DynamicResource Fonts.Monospace}"
171-
FontSize="11"
172-
Foreground="{DynamicResource Brush.Window}"/>
173-
</Border>
174-
175-
<ContentControl Margin="3,0,0,0"
176-
Content="{Binding CurrentBranch}"
177-
IsVisible="{Binding CurrentBranch, Converter={x:Static ObjectConverters.IsNotNull}}">
178-
<ContentControl.DataTemplates>
179-
<DataTemplate DataType="m:Branch">
180-
<StackPanel Orientation="Horizontal">
181-
<TextBlock Margin="3,0"
182-
Text="{Binding TrackStatusDescription}"
161+
<ContentControl Grid.Column="3"
162+
Content="{Binding Status, Mode=OneWay}"
163+
IsVisible="{Binding Status, Converter={x:Static ObjectConverters.NotEqual}}">
164+
<ContentControl.DataTemplates>
165+
<DataTemplate DataType="m:RepositoryStatus">
166+
<StackPanel Orientation="Horizontal">
167+
<Border Margin="3,0"
168+
Height="18"
169+
CornerRadius="9"
170+
Padding="6,0"
171+
Background="{DynamicResource Brush.FG1}"
172+
VerticalAlignment="Center"
173+
IsVisible="{Binding LocalChanges, Converter={x:Static c:IntConverters.IsGreaterThanZero}}">
174+
<TextBlock Text="{Binding LocalChanges}"
183175
FontFamily="{DynamicResource Fonts.Monospace}"
184-
FontSize="12"
185-
IsVisible="{Binding IsTrackStatusVisible}"/>
176+
FontSize="11"
177+
Foreground="{DynamicResource Brush.Window}"/>
178+
</Border>
186179

187-
<Border Margin="3,0,0,0"
188-
BorderThickness="1"
189-
BorderBrush="{DynamicResource Brush.FG2}"
190-
Height="20"
191-
CornerRadius="4"
192-
Padding="4,0">
193-
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
194-
<Path Width="12" Height="12" Data="{StaticResource Icons.Branch}"/>
195-
<TextBlock Margin="2,0,0,0" Text="{Binding Name}" FontSize="12"/>
196-
</StackPanel>
197-
</Border>
198-
</StackPanel>
199-
</DataTemplate>
200-
</ContentControl.DataTemplates>
201-
</ContentControl>
202-
</StackPanel>
180+
<TextBlock Margin="3,0"
181+
Text="{Binding TrackingDescription}"
182+
FontFamily="{DynamicResource Fonts.Monospace}"
183+
FontSize="12"
184+
IsVisible="{Binding IsTrackingStatusVisible}"/>
185+
186+
<Border Margin="3,0,0,0"
187+
BorderThickness="1"
188+
BorderBrush="{DynamicResource Brush.FG2}"
189+
Height="20"
190+
CornerRadius="4"
191+
Padding="4,0">
192+
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
193+
<Path Width="12" Height="12" Data="{StaticResource Icons.Branch}"/>
194+
<TextBlock Margin="2,0,0,0" Text="{Binding CurrentBranch}" FontSize="12"/>
195+
</StackPanel>
196+
</Border>
197+
</StackPanel>
198+
</DataTemplate>
199+
</ContentControl.DataTemplates>
200+
</ContentControl>
203201
</Grid>
204202
</DataTemplate>
205203
</ListBox.ItemTemplate>

0 commit comments

Comments
 (0)