Skip to content

Commit 01ed9ca

Browse files
committed
refactor: merge BranchTrackStatus to Branch
Signed-off-by: leo <[email protected]>
1 parent 9bbb858 commit 01ed9ca

12 files changed

+74
-79
lines changed

src/Commands/QueryBranches.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,41 @@ public QueryBranches(string repo)
2626
return branches;
2727

2828
var lines = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
29-
var remoteHeads = new Dictionary<string, string>();
29+
var mismatched = new HashSet<string>();
30+
var remotes = new Dictionary<string, Models.Branch>();
3031
foreach (var line in lines)
3132
{
32-
var b = ParseLine(line);
33+
var b = ParseLine(line, mismatched);
3334
if (b != null)
3435
{
3536
branches.Add(b);
3637
if (!b.IsLocal)
37-
remoteHeads.Add(b.FullName, b.Head);
38+
remotes.Add(b.FullName, b);
3839
}
3940
}
4041

4142
foreach (var b in branches)
4243
{
4344
if (b.IsLocal && !string.IsNullOrEmpty(b.Upstream))
4445
{
45-
if (remoteHeads.TryGetValue(b.Upstream, out var upstreamHead))
46+
if (remotes.TryGetValue(b.Upstream, out var upstream))
4647
{
4748
b.IsUpstreamGone = false;
48-
b.TrackStatus ??= await new QueryTrackStatus(WorkingDirectory, b.Head, upstreamHead).GetResultAsync().ConfigureAwait(false);
49+
50+
if (mismatched.Contains(b.FullName))
51+
await new QueryTrackStatus(WorkingDirectory).GetResultAsync(b, upstream).ConfigureAwait(false);
4952
}
5053
else
5154
{
5255
b.IsUpstreamGone = true;
53-
b.TrackStatus ??= new Models.BranchTrackStatus();
5456
}
5557
}
5658
}
5759

5860
return branches;
5961
}
6062

61-
private Models.Branch ParseLine(string line)
63+
private Models.Branch ParseLine(string line, HashSet<string> mismatched)
6264
{
6365
var parts = line.Split('\0');
6466
if (parts.Length != 7)
@@ -103,11 +105,11 @@ private Models.Branch ParseLine(string line)
103105
branch.Upstream = parts[4];
104106
branch.IsUpstreamGone = false;
105107

106-
if (!branch.IsLocal ||
107-
string.IsNullOrEmpty(branch.Upstream) ||
108-
string.IsNullOrEmpty(parts[5]) ||
109-
parts[5].Equals("=", StringComparison.Ordinal))
110-
branch.TrackStatus = new Models.BranchTrackStatus();
108+
if (branch.IsLocal &&
109+
!string.IsNullOrEmpty(branch.Upstream) &&
110+
!string.IsNullOrEmpty(parts[5]) &&
111+
!parts[5].Equals("=", StringComparison.Ordinal))
112+
mismatched.Add(branch.FullName);
111113

112114
branch.WorktreePath = parts[6];
113115
return branch;

src/Commands/QueryTrackStatus.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,28 @@ namespace SourceGit.Commands
55
{
66
public class QueryTrackStatus : Command
77
{
8-
public QueryTrackStatus(string repo, string local, string upstream)
8+
public QueryTrackStatus(string repo)
99
{
1010
WorkingDirectory = repo;
1111
Context = repo;
12-
Args = $"rev-list --left-right {local}...{upstream}";
1312
}
1413

15-
public async Task<Models.BranchTrackStatus> GetResultAsync()
14+
public async Task GetResultAsync(Models.Branch local, Models.Branch remote)
1615
{
17-
var status = new Models.BranchTrackStatus();
16+
Args = $"rev-list --left-right {local.Head}...{remote.Head}";
1817

1918
var rs = await ReadToEndAsync().ConfigureAwait(false);
2019
if (!rs.IsSuccess)
21-
return status;
20+
return;
2221

2322
var lines = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
2423
foreach (var line in lines)
2524
{
2625
if (line[0] == '>')
27-
status.Behind.Add(line.Substring(1));
26+
local.Behind.Add(line.Substring(1));
2827
else
29-
status.Ahead.Add(line.Substring(1));
28+
local.Ahead.Add(line.Substring(1));
3029
}
31-
32-
return status;
3330
}
3431
}
3532
}

src/Models/Branch.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,6 @@
22

33
namespace SourceGit.Models
44
{
5-
public class BranchTrackStatus
6-
{
7-
public List<string> Ahead { get; set; } = new List<string>();
8-
public List<string> Behind { get; set; } = new List<string>();
9-
10-
public bool IsVisible => Ahead.Count > 0 || Behind.Count > 0;
11-
12-
public override string ToString()
13-
{
14-
if (Ahead.Count == 0 && Behind.Count == 0)
15-
return string.Empty;
16-
17-
var track = "";
18-
if (Ahead.Count > 0)
19-
track += $"{Ahead.Count}↑";
20-
if (Behind.Count > 0)
21-
track += $" {Behind.Count}↓";
22-
return track.Trim();
23-
}
24-
}
25-
265
public enum BranchSortMode
276
{
287
Name = 0,
@@ -39,11 +18,33 @@ public class Branch
3918
public bool IsCurrent { get; set; }
4019
public bool IsDetachedHead { get; set; }
4120
public string Upstream { get; set; }
42-
public BranchTrackStatus TrackStatus { get; set; }
21+
public List<string> Ahead { get; set; } = [];
22+
public List<string> Behind { get; set; } = [];
4323
public string Remote { get; set; }
4424
public bool IsUpstreamGone { get; set; }
4525
public string WorktreePath { get; set; }
4626

27+
public bool IsTrackStatusVisible
28+
{
29+
get
30+
{
31+
return Ahead.Count + Behind.Count > 0;
32+
}
33+
}
34+
35+
public string TrackStatusDescription
36+
{
37+
get
38+
{
39+
var ahead = Ahead.Count;
40+
var behind = Behind.Count;
41+
if (ahead > 0)
42+
return behind > 0 ? $"{ahead}{behind}↓" : $"{ahead}↑";
43+
44+
return behind > 0 ? $"{behind}↓" : string.Empty;
45+
}
46+
}
47+
4748
public bool HasWorktree => !IsCurrent && !string.IsNullOrEmpty(WorktreePath);
4849
public string FriendlyName => IsLocal ? Name : $"{Remote}/{Name}";
4950
}

src/ViewModels/Histories.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,12 @@ public async Task<bool> CheckoutBranchByDecoratorAsync(Models.Decorator decorato
219219
return false;
220220

221221
var lb = _repo.Branches.Find(x => x.IsLocal && x.Upstream == rb.FullName);
222-
if (lb == null || lb.TrackStatus.Ahead.Count > 0)
222+
if (lb == null || lb.Ahead.Count > 0)
223223
{
224224
if (_repo.CanCreatePopup())
225225
_repo.ShowPopup(new CreateBranch(_repo, rb));
226226
}
227-
else if (lb.TrackStatus.Behind.Count > 0)
227+
else if (lb.Behind.Count > 0)
228228
{
229229
if (_repo.CanCreatePopup())
230230
_repo.ShowPopup(new CheckoutAndFastForward(_repo, lb, rb));
@@ -265,7 +265,7 @@ public async Task CheckoutBranchByCommitAsync(Models.Commit commit)
265265
continue;
266266

267267
var lb = _repo.Branches.Find(x => x.IsLocal && x.Upstream == rb.FullName);
268-
if (lb is { TrackStatus.Ahead.Count: 0 })
268+
if (lb.Ahead.Count == 0)
269269
{
270270
if (_repo.CanCreatePopup())
271271
_repo.ShowPopup(new CheckoutAndFastForward(_repo, lb, rb));

src/ViewModels/Repository.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ public void RefreshBranches()
11821182
if (_workingCopy != null)
11831183
_workingCopy.HasRemotes = remotes.Count > 0;
11841184

1185-
var hasPendingPullOrPush = CurrentBranch?.TrackStatus.IsVisible ?? false;
1185+
var hasPendingPullOrPush = CurrentBranch?.IsTrackStatusVisible ?? false;
11861186
GetOwnerPage()?.ChangeDirtyState(Models.DirtyState.HasPendingPullOrPush, !hasPendingPullOrPush);
11871187
});
11881188
}, token);
@@ -1465,9 +1465,9 @@ public async Task CheckoutBranchAsync(Models.Branch branch)
14651465
{
14661466
if (b.IsLocal &&
14671467
b.Upstream.Equals(branch.FullName, StringComparison.Ordinal) &&
1468-
b.TrackStatus.Ahead.Count == 0)
1468+
b.Ahead.Count == 0)
14691469
{
1470-
if (b.TrackStatus.Behind.Count > 0)
1470+
if (b.Behind.Count > 0)
14711471
ShowPopup(new CheckoutAndFastForward(this, b, branch));
14721472
else if (!b.IsCurrent)
14731473
await CheckoutBranchAsync(b);

src/Views/BranchTree.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
Margin="0,4,0,0"
7575
HorizontalAlignment="Left" VerticalAlignment="Center"
7676
Text="{DynamicResource Text.BranchTree.Status}"
77-
IsVisible="{Binding TrackStatus.IsVisible, Mode=OneWay}"/>
77+
IsVisible="{Binding IsTrackStatusVisible, Mode=OneWay}"/>
7878
<v:BranchTreeNodeTrackStatusTooltip Grid.Row="1" Grid.Column="1"
7979
Margin="8,4,0,0"/>
8080

src/Views/BranchTree.axaml.cs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,11 @@ protected override Size MeasureOverride(Size availableSize)
183183

184184
if (DataContext is ViewModels.BranchTreeNode { Backend: Models.Branch branch })
185185
{
186-
var status = branch.TrackStatus.ToString();
187-
if (!string.IsNullOrEmpty(status))
186+
var desc = branch.TrackStatusDescription;
187+
if (!string.IsNullOrEmpty(desc))
188188
{
189189
_label = new FormattedText(
190-
status,
190+
desc,
191191
CultureInfo.CurrentCulture,
192192
FlowDirection.LeftToRight,
193193
new Typeface(FontFamily),
@@ -212,22 +212,18 @@ protected override void OnDataContextChanged(EventArgs e)
212212

213213
Text = string.Empty;
214214

215-
if (DataContext is not Models.Branch { TrackStatus: { IsVisible: true } track })
215+
if (DataContext is not Models.Branch { IsTrackStatusVisible: true } branch)
216216
{
217217
SetCurrentValue(IsVisibleProperty, false);
218218
return;
219219
}
220220

221-
if (track.Ahead.Count > 0)
222-
{
223-
Text = track.Behind.Count > 0 ?
224-
App.Text("BranchTree.AheadBehind", track.Ahead.Count, track.Behind.Count) :
225-
App.Text("BranchTree.Ahead", track.Ahead.Count);
226-
}
227-
else if (track.Behind.Count > 0)
228-
{
229-
Text = App.Text("BranchTree.Behind", track.Behind.Count);
230-
}
221+
var ahead = branch.Ahead.Count;
222+
var behind = branch.Behind.Count;
223+
if (ahead > 0)
224+
Text = behind > 0 ? App.Text("BranchTree.AheadBehind", ahead, behind) : App.Text("BranchTree.Ahead", ahead);
225+
else
226+
Text = App.Text("BranchTree.Behind", behind);
231227

232228
SetCurrentValue(IsVisibleProperty, true);
233229
}
@@ -638,7 +634,7 @@ private ContextMenu CreateContextMenuForLocalBranch(ViewModels.Repository repo,
638634
var fastForward = new MenuItem();
639635
fastForward.Header = App.Text("BranchCM.FastForward", upstream.FriendlyName);
640636
fastForward.Icon = App.CreateMenuIcon("Icons.FastForward");
641-
fastForward.IsEnabled = branch.TrackStatus.Ahead.Count == 0 && branch.TrackStatus.Behind.Count > 0;
637+
fastForward.IsEnabled = branch.Ahead.Count == 0 && branch.Behind.Count > 0;
642638
fastForward.Click += async (_, e) =>
643639
{
644640
if (repo.CanCreatePopup())
@@ -685,7 +681,7 @@ private ContextMenu CreateContextMenuForLocalBranch(ViewModels.Repository repo,
685681
var fastForward = new MenuItem();
686682
fastForward.Header = App.Text("BranchCM.FastForward", upstream.FriendlyName);
687683
fastForward.Icon = App.CreateMenuIcon("Icons.FastForward");
688-
fastForward.IsEnabled = branch.TrackStatus.Ahead.Count == 0 && branch.TrackStatus.Behind.Count > 0;
684+
fastForward.IsEnabled = branch.Ahead.Count == 0 && branch.Behind.Count > 0;
689685
fastForward.Click += async (_, e) =>
690686
{
691687
if (repo.CanCreatePopup())
@@ -697,7 +693,7 @@ private ContextMenu CreateContextMenuForLocalBranch(ViewModels.Repository repo,
697693
var fetchInto = new MenuItem();
698694
fetchInto.Header = App.Text("BranchCM.FetchInto", upstream.FriendlyName, branch.Name);
699695
fetchInto.Icon = App.CreateMenuIcon("Icons.Fetch");
700-
fetchInto.IsEnabled = branch.TrackStatus.Ahead.Count == 0;
696+
fetchInto.IsEnabled = branch.Ahead.Count == 0;
701697
fetchInto.Click += async (_, e) =>
702698
{
703699
if (repo.CanCreatePopup())

src/Views/CheckoutAndFastForward.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
VerticalAlignment="Center"
3333
CornerRadius="9"
3434
Background="{DynamicResource Brush.Badge}"
35-
IsVisible="{Binding LocalBranch.TrackStatus.IsVisible}">
35+
IsVisible="{Binding LocalBranch.IsTrackStatusVisible, Mode=OneWay}">
3636
<TextBlock Foreground="{DynamicResource Brush.BadgeFG}"
3737
FontFamily="{DynamicResource Fonts.Monospace}"
3838
FontSize="10"
39-
Text="{Binding LocalBranch.TrackStatus}"/>
39+
Text="{Binding LocalBranch.TrackStatusDescription, Mode=OneWay}"/>
4040
</Border>
4141
</StackPanel>
4242

src/Views/CommitStatusIndicator.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,13 @@ public override void Render(DrawingContext context)
5252

5353
protected override Size MeasureOverride(Size availableSize)
5454
{
55-
if (DataContext is Models.Commit commit && CurrentBranch is not null)
55+
if (DataContext is Models.Commit commit && CurrentBranch is { } b)
5656
{
5757
var sha = commit.SHA;
58-
var track = CurrentBranch.TrackStatus;
5958

60-
if (track.Ahead.Contains(sha))
59+
if (b.Ahead.Contains(sha))
6160
_status = Status.Ahead;
62-
else if (track.Behind.Contains(sha))
61+
else if (b.Behind.Contains(sha))
6362
_status = Status.Behind;
6463
else
6564
_status = Status.Normal;

src/Views/DeleteBranch.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
VerticalAlignment="Center"
2424
CornerRadius="9"
2525
Background="{DynamicResource Brush.Badge}"
26-
IsVisible="{Binding Target.TrackStatus.IsVisible}">
26+
IsVisible="{Binding Target.IsTrackStatusVisible}">
2727
<TextBlock Foreground="{DynamicResource Brush.BadgeFG}"
2828
FontFamily="{DynamicResource Fonts.Monospace}"
2929
FontSize="10"
30-
Text="{Binding Target.TrackStatus}"/>
30+
Text="{Binding Target.TrackStatusDescription}"/>
3131
</Border>
3232
</StackPanel>
3333

0 commit comments

Comments
 (0)