Skip to content

Commit 20ff55f

Browse files
committed
feature: show repo status in Welcome page (#1867)
Signed-off-by: leo <longshuang@msn.cn>
1 parent fe471ac commit 20ff55f

File tree

10 files changed

+121
-16
lines changed

10 files changed

+121
-16
lines changed

src/Commands/CountLocalChangesWithoutUntracked.cs renamed to src/Commands/CountLocalChanges.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
namespace SourceGit.Commands
55
{
6-
public class CountLocalChangesWithoutUntracked : Command
6+
public class CountLocalChanges : Command
77
{
8-
public CountLocalChangesWithoutUntracked(string repo)
8+
public CountLocalChanges(string repo, bool includeUntracked)
99
{
10+
var option = includeUntracked ? "-uall" : "-uno";
1011
WorkingDirectory = repo;
1112
Context = repo;
12-
Args = "--no-optional-locks status -uno --ignore-submodules=all --porcelain";
13+
Args = $"--no-optional-locks status {option} --ignore-submodules=all --porcelain";
1314
}
1415

1516
public async Task<int> GetResultAsync()

src/ViewModels/Checkout.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public override async Task<bool> Sure()
5858

5959
if (!DiscardLocalChanges)
6060
{
61-
var changes = await new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath).GetResultAsync();
61+
var changes = await new Commands.CountLocalChanges(_repo.FullPath, false).GetResultAsync();
6262
if (changes > 0)
6363
{
6464
succ = await new Commands.Stash(_repo.FullPath)

src/ViewModels/CheckoutAndFastForward.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public override async Task<bool> Sure()
6363

6464
if (!DiscardLocalChanges)
6565
{
66-
var changes = await new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath).GetResultAsync();
66+
var changes = await new Commands.CountLocalChanges(_repo.FullPath, false).GetResultAsync();
6767
if (changes > 0)
6868
{
6969
succ = await new Commands.Stash(_repo.FullPath)

src/ViewModels/CheckoutCommit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public override async Task<bool> Sure()
5858

5959
if (!DiscardLocalChanges)
6060
{
61-
var changes = await new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath).GetResultAsync();
61+
var changes = await new Commands.CountLocalChanges(_repo.FullPath, false).GetResultAsync();
6262
if (changes > 0)
6363
{
6464
succ = await new Commands.Stash(_repo.FullPath)

src/ViewModels/CreateBranch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public override async Task<bool> Sure()
142142
var needPopStash = false;
143143
if (!DiscardLocalChanges)
144144
{
145-
var changes = await new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath).GetResultAsync();
145+
var changes = await new Commands.CountLocalChanges(_repo.FullPath, false).GetResultAsync();
146146
if (changes > 0)
147147
{
148148
succ = await new Commands.Stash(_repo.FullPath)

src/ViewModels/Pull.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public override async Task<bool> Sure()
119119
Use(log);
120120

121121
var updateSubmodules = IsRecurseSubmoduleVisible && RecurseSubmodules;
122-
var changes = await new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath).GetResultAsync();
122+
var changes = await new Commands.CountLocalChanges(_repo.FullPath, false).GetResultAsync();
123123
var needPopStash = false;
124124
if (changes > 0)
125125
{

src/ViewModels/RepositoryNode.cs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections.Generic;
22
using System.IO;
33
using System.Text.Json.Serialization;
4-
4+
using System.Threading.Tasks;
55
using CommunityToolkit.Mvvm.ComponentModel;
66

77
namespace SourceGit.ViewModels
@@ -62,6 +62,20 @@ public int Depth
6262
set;
6363
} = 0;
6464

65+
[JsonIgnore]
66+
public Models.Branch CurrentBranch
67+
{
68+
get => _currentBranch;
69+
private set => SetProperty(ref _currentBranch, value);
70+
}
71+
72+
[JsonIgnore]
73+
public int LocalChanges
74+
{
75+
get => _localChanges;
76+
private set => SetProperty(ref _localChanges, value);
77+
}
78+
6579
public List<RepositoryNode> SubNodes
6680
{
6781
get;
@@ -122,11 +136,44 @@ public void Delete()
122136
activePage.Popup = new DeleteRepositoryNode(this);
123137
}
124138

139+
public async Task UpdateStatusAsync()
140+
{
141+
if (!_isRepository || !Directory.Exists(_id))
142+
{
143+
CurrentBranch = null;
144+
LocalChanges = 0;
145+
146+
if (SubNodes.Count > 0)
147+
{
148+
foreach (var subNode in SubNodes)
149+
await subNode.UpdateStatusAsync();
150+
}
151+
152+
return;
153+
}
154+
155+
LocalChanges = await new Commands.CountLocalChanges(_id, true) { RaiseError = false }.GetResultAsync();
156+
157+
var branches = await new Commands.QueryBranches(_id) { RaiseError = false }.GetResultAsync();
158+
foreach (var branch in branches)
159+
{
160+
if (branch.IsCurrent)
161+
{
162+
CurrentBranch = branch;
163+
return;
164+
}
165+
}
166+
167+
CurrentBranch = null;
168+
}
169+
125170
private string _id = string.Empty;
126171
private string _name = string.Empty;
127172
private bool _isRepository = false;
128173
private int _bookmark = 0;
129174
private bool _isExpanded = false;
130175
private bool _isVisible = true;
176+
private Models.Branch _currentBranch = null;
177+
private int _localChanges = 0;
131178
}
132179
}

src/ViewModels/Welcome.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public void Refresh()
5252
Rows.AddRange(rows);
5353
}
5454

55+
public async Task UpdateStatusAsync()
56+
{
57+
foreach (var node in Preferences.Instance.RepositoryNodes)
58+
await node.UpdateStatusAsync();
59+
}
60+
5561
public void ToggleNodeIsExpanded(RepositoryNode node)
5662
{
5763
node.IsExpanded = !node.IsExpanded;

src/Views/Welcome.axaml

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:c="using:SourceGit.Converters"
6+
xmlns:m="using:SourceGit.Models"
67
xmlns:vm="using:SourceGit.ViewModels"
78
xmlns:v="using:SourceGit.Views"
89
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
@@ -106,7 +107,7 @@
106107
<DataTemplate DataType="vm:RepositoryNode">
107108
<Grid Background="Transparent"
108109
Height="30"
109-
ColumnDefinitions="16,18,Auto,*"
110+
ColumnDefinitions="16,18,*,Auto,16"
110111
Margin="{Binding Depth, Converter={x:Static c:IntConverters.ToTreeMargin}}"
111112
ClipToBounds="True"
112113
ContextRequested="OnTreeNodeContextRequested"
@@ -142,19 +143,63 @@
142143

143144
<StackPanel Grid.Column="2" Orientation="Horizontal">
144145
<TextBlock VerticalAlignment="Center" Text="{Binding Name}"/>
146+
<TextBlock Margin="4,0,0,0" VerticalAlignment="Center" Foreground="{DynamicResource Brush.FG2}" Text="{Binding Id, StringFormat=({0})}">
147+
<TextBlock.IsVisible>
148+
<MultiBinding Converter="{x:Static BoolConverters.And}">
149+
<Binding Path="IsRepository"/>
150+
<Binding Path="IsInvalid" Converter="{x:Static BoolConverters.Not}"/>
151+
</MultiBinding>
152+
</TextBlock.IsVisible>
153+
</TextBlock>
145154
<Path Margin="2,0,0,0"
146155
Width="12" Height="12"
147156
Data="{StaticResource Icons.Error}"
148157
Fill="Orange"
149158
IsVisible="{Binding IsInvalid}"/>
150159
</StackPanel>
151160

152-
<Border Grid.Column="3" Margin="8,0" VerticalAlignment="Center" ClipToBounds="True">
153-
<TextBlock HorizontalAlignment="Right"
154-
Foreground="{DynamicResource Brush.FG2}"
155-
Text="{Binding Id, Converter={x:Static c:PathConverters.RelativeToHome}}"
156-
IsVisible="{Binding IsRepository}"/>
157-
</Border>
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}"
183+
FontFamily="{DynamicResource Fonts.Monospace}"
184+
FontSize="12"
185+
IsVisible="{Binding IsTrackStatusVisible}"/>
186+
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>
158203
</Grid>
159204
</DataTemplate>
160205
</ListBox.ItemTemplate>

src/Views/Welcome.axaml.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ public Welcome()
6161
InitializeComponent();
6262
}
6363

64+
protected override async void OnLoaded(RoutedEventArgs e)
65+
{
66+
base.OnLoaded(e);
67+
await ViewModels.Welcome.Instance.UpdateStatusAsync();
68+
}
69+
6470
protected override void OnKeyDown(KeyEventArgs e)
6571
{
6672
base.OnKeyDown(e);

0 commit comments

Comments
 (0)