Skip to content

Commit 4e57cd5

Browse files
committed
feature: add an option to clean up ignored files when discard all changes in repo (#531)
1 parent a59600c commit 4e57cd5

File tree

12 files changed

+104
-46
lines changed

12 files changed

+104
-46
lines changed

src/Commands/Clean.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ namespace SourceGit.Commands
55
{
66
public class Clean : Command
77
{
8-
public Clean(string repo)
8+
public Clean(string repo, bool includeIgnored)
99
{
1010
WorkingDirectory = repo;
1111
Context = repo;
12-
Args = "clean -qfd";
12+
Args = includeIgnored ? "clean -qfdx" : "clean -qfd";
1313
}
1414

1515
public Clean(string repo, List<string> files)
1616
{
17-
StringBuilder builder = new StringBuilder();
17+
var builder = new StringBuilder();
1818
builder.Append("clean -qfd --");
1919
foreach (var f in files)
2020
{

src/Commands/Discard.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace SourceGit.Commands
55
{
66
public static class Discard
77
{
8-
public static void All(string repo)
8+
public static void All(string repo, bool includeIgnored)
99
{
1010
new Restore(repo).Exec();
11-
new Clean(repo).Exec();
11+
new Clean(repo, includeIgnored).Exec();
1212
}
1313

1414
public static void Changes(string repo, List<Models.Change> changes)

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@
224224
<x:String x:Key="Text.Discard" xml:space="preserve">Discard Changes</x:String>
225225
<x:String x:Key="Text.Discard.All" xml:space="preserve">All local changes in working copy.</x:String>
226226
<x:String x:Key="Text.Discard.Changes" xml:space="preserve">Changes:</x:String>
227+
<x:String x:Key="Text.Discard.IncludeIgnored" xml:space="preserve">Include ignored files</x:String>
227228
<x:String x:Key="Text.Discard.Total" xml:space="preserve">Total {0} changes will be discard</x:String>
228229
<x:String x:Key="Text.Discard.Warning" xml:space="preserve">You can't undo this action!!!</x:String>
229230
<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
@@ -227,6 +227,7 @@
227227
<x:String x:Key="Text.Discard" xml:space="preserve">放弃更改确认</x:String>
228228
<x:String x:Key="Text.Discard.All" xml:space="preserve">所有本地址未提交的修改。</x:String>
229229
<x:String x:Key="Text.Discard.Changes" xml:space="preserve">变更 :</x:String>
230+
<x:String x:Key="Text.Discard.IncludeIgnored" xml:space="preserve">包括所有已忽略的文件</x:String>
230231
<x:String x:Key="Text.Discard.Total" xml:space="preserve">总计{0}项选中更改</x:String>
231232
<x:String x:Key="Text.Discard.Warning" xml:space="preserve">本操作不支持回退,请确认后继续!!!</x:String>
232233
<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
@@ -227,6 +227,7 @@
227227
<x:String x:Key="Text.Discard" xml:space="preserve">捨棄變更</x:String>
228228
<x:String x:Key="Text.Discard.All" xml:space="preserve">所有本機未提交的變更。</x:String>
229229
<x:String x:Key="Text.Discard.Changes" xml:space="preserve">變更:</x:String>
230+
<x:String x:Key="Text.Discard.IncludeIgnored" xml:space="preserve">包括所有已忽略的檔案</x:String>
230231
<x:String x:Key="Text.Discard.Total" xml:space="preserve">將捨棄總計 {0} 項已選取的變更</x:String>
231232
<x:String x:Key="Text.Discard.Warning" xml:space="preserve">您無法復原此操作,請確認後再繼續!</x:String>
232233
<x:String x:Key="Text.EditRepositoryNode.Bookmark" xml:space="preserve">書籤:</x:String>

src/ViewModels/Checkout.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ public class Checkout : Popup
77
public string Branch
88
{
99
get;
10-
private set;
1110
}
1211

1312
public Models.DealWithLocalChanges PreAction
@@ -49,7 +48,7 @@ public override Task<bool> Sure()
4948
else if (PreAction == Models.DealWithLocalChanges.Discard)
5049
{
5150
SetProgressDescription("Discard local changes ...");
52-
Commands.Discard.All(_repo.FullPath);
51+
Commands.Discard.All(_repo.FullPath, false);
5352
}
5453
}
5554

src/ViewModels/CheckoutCommit.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ public class CheckoutCommit : Popup
77
public Models.Commit Commit
88
{
99
get;
10-
private set;
1110
}
1211

1312
public bool AutoStash
@@ -49,7 +48,7 @@ public override Task<bool> Sure()
4948
else
5049
{
5150
SetProgressDescription("Discard local changes ...");
52-
Commands.Discard.All(_repo.FullPath);
51+
Commands.Discard.All(_repo.FullPath, false);
5352
}
5453
}
5554

src/ViewModels/CreateBranch.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public string Name
1717
public object BasedOn
1818
{
1919
get;
20-
private set;
2120
}
2221

2322
public Models.DealWithLocalChanges PreAction
@@ -105,7 +104,7 @@ public override Task<bool> Sure()
105104
else if (PreAction == Models.DealWithLocalChanges.Discard)
106105
{
107106
SetProgressDescription("Discard local changes...");
108-
Commands.Discard.All(_repo.FullPath);
107+
Commands.Discard.All(_repo.FullPath, false);
109108
}
110109
}
111110

src/ViewModels/Discard.cs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,45 @@
33

44
namespace SourceGit.ViewModels
55
{
6+
public class DiscardAllMode
7+
{
8+
public bool IncludeIgnored
9+
{
10+
get;
11+
set;
12+
} = false;
13+
}
14+
15+
public class DiscardSingleFile
16+
{
17+
public string Path
18+
{
19+
get;
20+
set;
21+
} = string.Empty;
22+
}
23+
24+
public class DiscardMultipleFiles
25+
{
26+
public int Count
27+
{
28+
get;
29+
set;
30+
} = 0;
31+
}
32+
633
public class Discard : Popup
734
{
835
public object Mode
936
{
1037
get;
11-
private set;
1238
}
1339

1440
public Discard(Repository repo)
1541
{
1642
_repo = repo;
1743

18-
Mode = new Models.Null();
44+
Mode = new DiscardAllMode();
1945
View = new Views.Discard { DataContext = this };
2046
}
2147

@@ -25,11 +51,11 @@ public Discard(Repository repo, List<Models.Change> changes)
2551
_changes = changes;
2652

2753
if (_changes == null)
28-
Mode = new Models.Null();
54+
Mode = new DiscardAllMode();
2955
else if (_changes.Count == 1)
30-
Mode = _changes[0].Path;
56+
Mode = new DiscardSingleFile() { Path = _changes[0].Path };
3157
else
32-
Mode = _changes.Count;
58+
Mode = new DiscardMultipleFiles() { Count = _changes.Count };
3359

3460
View = new Views.Discard() { DataContext = this };
3561
}
@@ -41,8 +67,8 @@ public override Task<bool> Sure()
4167

4268
return Task.Run(() =>
4369
{
44-
if (_changes == null)
45-
Commands.Discard.All(_repo.FullPath);
70+
if (Mode is DiscardAllMode all)
71+
Commands.Discard.All(_repo.FullPath, all.IncludeIgnored);
4672
else
4773
Commands.Discard.Changes(_repo.FullPath, _changes);
4874

src/ViewModels/Pull.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public override Task<bool> Sure()
139139
else if (PreAction == Models.DealWithLocalChanges.Discard)
140140
{
141141
SetProgressDescription("Discard local changes ...");
142-
Commands.Discard.All(_repo.FullPath);
142+
Commands.Discard.All(_repo.FullPath, false);
143143
}
144144
}
145145

0 commit comments

Comments
 (0)