Skip to content

Commit 4c4af6e

Browse files
committed
feature: supports to discard all local changes without untracked files (#1586)
Signed-off-by: leo <[email protected]>
1 parent 935d30d commit 4c4af6e

File tree

9 files changed

+99
-41
lines changed

9 files changed

+99
-41
lines changed

src/Commands/Clean.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22
{
33
public class Clean : Command
44
{
5-
public Clean(string repo)
5+
public Clean(string repo, Models.CleanMode mode)
66
{
77
WorkingDirectory = repo;
88
Context = repo;
9-
Args = "clean -qfdx";
9+
10+
Args = mode switch
11+
{
12+
Models.CleanMode.OnlyUntrackedFiles => "clean -qfd",
13+
Models.CleanMode.OnlyIgnoredFiles => "clean -qfdX",
14+
_ => "clean -qfdx",
15+
};
1016
}
1117
}
1218
}

src/Commands/Discard.cs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,43 @@ public static class Discard
1010
/// <summary>
1111
/// Discard all local changes (unstaged & staged)
1212
/// </summary>
13-
/// <param name="repo"></param>
14-
/// <param name="includeIgnored"></param>
15-
/// <param name="log"></param>
16-
public static async Task AllAsync(string repo, bool includeIgnored, Models.ICommandLog log)
13+
public static async Task AllAsync(string repo, bool includeUntracked, bool includeIgnored, Models.ICommandLog log)
1714
{
18-
var changes = await new QueryLocalChanges(repo).GetResultAsync().ConfigureAwait(false);
19-
try
15+
if (includeUntracked)
2016
{
21-
foreach (var c in changes)
17+
// Untracked paths that contains `.git` file (detached submodule) must be removed manually.
18+
var changes = await new QueryLocalChanges(repo).GetResultAsync().ConfigureAwait(false);
19+
try
2220
{
23-
if (c.WorkTree == Models.ChangeState.Untracked ||
24-
c.WorkTree == Models.ChangeState.Added ||
25-
c.Index == Models.ChangeState.Added ||
26-
c.Index == Models.ChangeState.Renamed)
21+
foreach (var c in changes)
2722
{
28-
var fullPath = Path.Combine(repo, c.Path);
29-
if (Directory.Exists(fullPath))
30-
Directory.Delete(fullPath, true);
31-
else
32-
File.Delete(fullPath);
23+
if (c.WorkTree == Models.ChangeState.Untracked ||
24+
c.WorkTree == Models.ChangeState.Added ||
25+
c.Index == Models.ChangeState.Added ||
26+
c.Index == Models.ChangeState.Renamed)
27+
{
28+
var fullPath = Path.Combine(repo, c.Path);
29+
if (Directory.Exists(fullPath))
30+
Directory.Delete(fullPath, true);
31+
}
3332
}
3433
}
34+
catch (Exception e)
35+
{
36+
App.RaiseException(repo, $"Failed to discard changes. Reason: {e.Message}");
37+
}
38+
39+
if (includeIgnored)
40+
await new Clean(repo, Models.CleanMode.All).Use(log).ExecAsync().ConfigureAwait(false);
41+
else
42+
await new Clean(repo, Models.CleanMode.OnlyUntrackedFiles).Use(log).ExecAsync().ConfigureAwait(false);
3543
}
36-
catch (Exception e)
44+
else if (includeIgnored)
3745
{
38-
App.RaiseException(repo, $"Failed to discard changes. Reason: {e.Message}");
46+
await new Clean(repo, Models.CleanMode.OnlyIgnoredFiles).Use(log).ExecAsync().ConfigureAwait(false);
3947
}
4048

4149
await new Reset(repo, "HEAD", "--hard").Use(log).ExecAsync().ConfigureAwait(false);
42-
43-
if (includeIgnored)
44-
await new Clean(repo).Use(log).ExecAsync().ConfigureAwait(false);
4550
}
4651

4752
/// <summary>

src/Models/CleanMode.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace SourceGit.Models
2+
{
3+
public enum CleanMode
4+
{
5+
OnlyUntrackedFiles = 0,
6+
OnlyIgnoredFiles,
7+
All,
8+
}
9+
}

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@
325325
<x:String x:Key="Text.Discard.All" xml:space="preserve">All local changes in working copy.</x:String>
326326
<x:String x:Key="Text.Discard.Changes" xml:space="preserve">Changes:</x:String>
327327
<x:String x:Key="Text.Discard.IncludeIgnored" xml:space="preserve">Include ignored files</x:String>
328+
<x:String x:Key="Text.Discard.IncludeUntracked" xml:space="preserve">Include untracked files</x:String>
328329
<x:String x:Key="Text.Discard.Total" xml:space="preserve">{0} changes will be discarded</x:String>
329330
<x:String x:Key="Text.Discard.Warning" xml:space="preserve">You can't undo this action!!!</x:String>
330331
<x:String x:Key="Text.EditRepositoryNode.Bookmark" xml:space="preserve">Bookmark:</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@
329329
<x:String x:Key="Text.Discard.All" xml:space="preserve">所有本仓库未提交的修改。</x:String>
330330
<x:String x:Key="Text.Discard.Changes" xml:space="preserve">变更 :</x:String>
331331
<x:String x:Key="Text.Discard.IncludeIgnored" xml:space="preserve">包括所有已忽略的文件</x:String>
332+
<x:String x:Key="Text.Discard.IncludeUntracked" xml:space="preserve">包括未跟踪的文件</x:String>
332333
<x:String x:Key="Text.Discard.Total" xml:space="preserve">总计{0}项选中更改</x:String>
333334
<x:String x:Key="Text.Discard.Warning" xml:space="preserve">本操作不支持回退,请确认后继续!!!</x:String>
334335
<x:String x:Key="Text.EditRepositoryNode.Bookmark" xml:space="preserve">书签 :</x:String>

src/Resources/Locales/zh_TW.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@
329329
<x:String x:Key="Text.Discard.All" xml:space="preserve">所有本機未提交的變更。</x:String>
330330
<x:String x:Key="Text.Discard.Changes" xml:space="preserve">變更:</x:String>
331331
<x:String x:Key="Text.Discard.IncludeIgnored" xml:space="preserve">包括所有已忽略的檔案</x:String>
332+
<x:String x:Key="Text.Discard.IncludeUntracked" xml:space="preserve">包含未追蹤檔案</x:String>
332333
<x:String x:Key="Text.Discard.Total" xml:space="preserve">將捨棄總計 {0} 項已選取的變更</x:String>
333334
<x:String x:Key="Text.Discard.Warning" xml:space="preserve">您無法復原此操作,請確認後再繼續!</x:String>
334335
<x:String x:Key="Text.EditRepositoryNode.Bookmark" xml:space="preserve">書籤:</x:String>

src/ViewModels/Discard.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ namespace SourceGit.ViewModels
55
{
66
public class DiscardAllMode
77
{
8+
public bool IncludeUntracked
9+
{
10+
get;
11+
set;
12+
} = false;
13+
814
public bool IncludeIgnored
915
{
1016
get;
@@ -65,7 +71,7 @@ public override async Task<bool> Sure()
6571
Use(log);
6672

6773
if (Mode is DiscardAllMode all)
68-
await Commands.Discard.AllAsync(_repo.FullPath, all.IncludeIgnored, log);
74+
await Commands.Discard.AllAsync(_repo.FullPath, all.IncludeUntracked, all.IncludeIgnored, log);
6975
else
7076
await Commands.Discard.ChangesAsync(_repo.FullPath, _changes, log);
7177

src/ViewModels/Pull.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public override async Task<bool> Sure()
125125
{
126126
if (DiscardLocalChanges)
127127
{
128-
await Commands.Discard.AllAsync(_repo.FullPath, false, log);
128+
await Commands.Discard.AllAsync(_repo.FullPath, false, false, log);
129129
}
130130
else
131131
{

src/Views/Discard.axaml

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,10 @@
1212
Classes="bold"
1313
Text="{DynamicResource Text.Discard}"/>
1414

15-
<StackPanel Margin="0,6,0,0" Orientation="Horizontal">
16-
<Path Width="14" Height="14"
17-
Data="{StaticResource Icons.Error}"
18-
Fill="DarkOrange"/>
19-
<TextBlock Margin="4,0,0,0"
20-
Text="{DynamicResource Text.Discard.Warning}"
21-
Foreground="DarkOrange"/>
22-
</StackPanel>
23-
2415
<ContentControl Margin="0,16,0,8" Content="{Binding Mode}">
2516
<ContentControl.DataTemplates>
2617
<DataTemplate DataType="vm:DiscardAllMode">
27-
<Grid RowDefinitions="32,32" ColumnDefinitions="120,*">
18+
<Grid RowDefinitions="32,32,32,Auto" ColumnDefinitions="120,*">
2819
<TextBlock Grid.Row="0" Grid.Column="0"
2920
Margin="0,0,8,0"
3021
HorizontalAlignment="Right"
@@ -33,35 +24,73 @@
3324
Text="{DynamicResource Text.Discard.All}"/>
3425

3526
<CheckBox Grid.Row="1" Grid.Column="1"
27+
Content="{DynamicResource Text.Discard.IncludeUntracked}"
28+
IsChecked="{Binding IncludeUntracked, Mode=TwoWay}"/>
29+
30+
<CheckBox Grid.Row="2" Grid.Column="1"
3631
Content="{DynamicResource Text.Discard.IncludeIgnored}"
3732
IsChecked="{Binding IncludeIgnored, Mode=TwoWay}"/>
33+
34+
<Grid Grid.Row="3" Grid.Column="1" ColumnDefinitions="Auto,*" Margin="0,6,0,0">
35+
<Path Grid.Column="0"
36+
Width="14" Height="14"
37+
Data="{StaticResource Icons.Error}"
38+
Fill="DarkOrange"/>
39+
<TextBlock Grid.Column="1" Margin="8,0,0,0"
40+
Text="{DynamicResource Text.Discard.Warning}"
41+
TextWrapping="Wrap"
42+
Foreground="DarkOrange"/>
43+
</Grid>
3844
</Grid>
3945
</DataTemplate>
4046

4147
<DataTemplate DataType="vm:DiscardSingleFile">
42-
<Grid Height="32" ColumnDefinitions="120,*">
43-
<TextBlock Grid.Column="0"
48+
<Grid RowDefinitions="32,Auto" ColumnDefinitions="120,*">
49+
<TextBlock Grid.Row="0" Grid.Column="0"
4450
Margin="0,0,8,0"
4551
HorizontalAlignment="Right"
4652
Text="{DynamicResource Text.Discard.Changes}"/>
47-
<StackPanel Grid.Column="1" Orientation="Horizontal">
53+
54+
<StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal">
4855
<Path Width="12" Height="12" Data="{StaticResource Icons.File}"/>
4956
<TextBlock Text="{Binding Path}" Margin="4,0,0,0"/>
5057
</StackPanel>
58+
59+
<Grid Grid.Row="1" Grid.Column="1" ColumnDefinitions="Auto,*" Margin="0,6,0,0">
60+
<Path Grid.Column="0"
61+
Width="14" Height="14"
62+
Data="{StaticResource Icons.Error}"
63+
Fill="DarkOrange"/>
64+
<TextBlock Grid.Column="1" Margin="8,0,0,0"
65+
Text="{DynamicResource Text.Discard.Warning}"
66+
TextWrapping="Wrap"
67+
Foreground="DarkOrange"/>
68+
</Grid>
5169
</Grid>
5270
</DataTemplate>
5371

5472
<DataTemplate DataType="vm:DiscardMultipleFiles">
55-
<Grid Height="32" ColumnDefinitions="120,*">
56-
<TextBlock Grid.Column="0"
73+
<Grid RowDefinitions="32,Auto" ColumnDefinitions="120,*">
74+
<TextBlock Grid.Row="0" Grid.Column="0"
5775
Margin="0,0,8,0"
5876
HorizontalAlignment="Right"
5977
Text="{DynamicResource Text.Discard.Changes}"/>
60-
<StackPanel Grid.Column="1" Orientation="Horizontal">
78+
<StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal">
6179
<Path Width="12" Height="12" Data="{StaticResource Icons.File}"/>
6280
<TextBlock Text="{Binding Count, Converter={x:Static c:StringConverters.FormatByResourceKey}, ConverterParameter='Discard.Total'}"
6381
Margin="4,0,0,0"/>
6482
</StackPanel>
83+
84+
<Grid Grid.Row="1" Grid.Column="1" ColumnDefinitions="Auto,*" Margin="0,6,0,0">
85+
<Path Grid.Column="0"
86+
Width="14" Height="14"
87+
Data="{StaticResource Icons.Error}"
88+
Fill="DarkOrange"/>
89+
<TextBlock Grid.Column="1" Margin="8,0,0,0"
90+
Text="{DynamicResource Text.Discard.Warning}"
91+
TextWrapping="Wrap"
92+
Foreground="DarkOrange"/>
93+
</Grid>
6594
</Grid>
6695
</DataTemplate>
6796
</ContentControl.DataTemplates>

0 commit comments

Comments
 (0)