Skip to content

Commit 029fd69

Browse files
committed
refactor: new way to discard selected or all local changes
This modification aims to solve the problem that the deleted submodule cannot be discarded. Signed-off-by: leo <longshuang@msn.cn>
1 parent 0f6c897 commit 029fd69

File tree

2 files changed

+59
-37
lines changed

2 files changed

+59
-37
lines changed

src/Commands/Clean.cs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
1-
using System.Collections.Generic;
2-
using System.Text;
3-
4-
namespace SourceGit.Commands
1+
namespace SourceGit.Commands
52
{
63
public class Clean : Command
74
{
8-
public Clean(string repo, bool includeIgnored)
5+
public Clean(string repo)
96
{
107
WorkingDirectory = repo;
118
Context = repo;
12-
Args = includeIgnored ? "clean -qfdx" : "clean -qfd";
13-
}
14-
15-
public Clean(string repo, List<string> files)
16-
{
17-
var builder = new StringBuilder();
18-
builder.Append("clean -qfd --");
19-
foreach (var f in files)
20-
{
21-
builder.Append(" \"");
22-
builder.Append(f);
23-
builder.Append("\"");
24-
}
25-
26-
WorkingDirectory = repo;
27-
Context = repo;
28-
Args = builder.ToString();
9+
Args = "clean -qfdx";
2910
}
3011
}
3112
}

src/Commands/Discard.cs

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,80 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
4+
5+
using Avalonia.Threading;
36

47
namespace SourceGit.Commands
58
{
69
public static class Discard
710
{
811
public static void All(string repo, bool includeIgnored, Models.ICommandLog log)
912
{
13+
var changes = new QueryLocalChanges(repo).Result();
14+
try
15+
{
16+
foreach (var c in changes)
17+
{
18+
if (c.WorkTree == Models.ChangeState.Untracked ||
19+
c.WorkTree == Models.ChangeState.Added ||
20+
c.Index == Models.ChangeState.Added ||
21+
c.Index == Models.ChangeState.Renamed)
22+
{
23+
var fullPath = Path.Combine(repo, c.Path);
24+
if (Directory.Exists(fullPath))
25+
Directory.Delete(fullPath, true);
26+
else
27+
File.Delete(fullPath);
28+
}
29+
}
30+
}
31+
catch (Exception e)
32+
{
33+
Dispatcher.UIThread.Invoke(() =>
34+
{
35+
App.RaiseException(repo, $"Failed to discard changes. Reason: {e.Message}");
36+
});
37+
}
38+
1039
new Restore(repo) { Log = log }.Exec();
11-
new Clean(repo, includeIgnored) { Log = log }.Exec();
40+
if (includeIgnored)
41+
new Clean(repo) { Log = log }.Exec();
1242
}
1343

1444
public static void Changes(string repo, List<Models.Change> changes, Models.ICommandLog log)
1545
{
16-
var needClean = new List<string>();
17-
var needCheckout = new List<string>();
46+
var restores = new List<string>();
1847

19-
foreach (var c in changes)
48+
try
2049
{
21-
if (c.WorkTree == Models.ChangeState.Untracked || c.WorkTree == Models.ChangeState.Added)
22-
needClean.Add(c.Path);
23-
else
24-
needCheckout.Add(c.Path);
50+
foreach (var c in changes)
51+
{
52+
if (c.WorkTree == Models.ChangeState.Untracked || c.WorkTree == Models.ChangeState.Added)
53+
{
54+
var fullPath = Path.Combine(repo, c.Path);
55+
if (Directory.Exists(fullPath))
56+
Directory.Delete(fullPath, true);
57+
else
58+
File.Delete(fullPath);
59+
}
60+
else
61+
{
62+
restores.Add(c.Path);
63+
}
64+
}
2565
}
26-
27-
for (int i = 0; i < needClean.Count; i += 10)
66+
catch (Exception e)
2867
{
29-
var count = Math.Min(10, needClean.Count - i);
30-
new Clean(repo, needClean.GetRange(i, count)) { Log = log }.Exec();
68+
Dispatcher.UIThread.Invoke(() =>
69+
{
70+
App.RaiseException(repo, $"Failed to discard changes. Reason: {e.Message}");
71+
});
3172
}
3273

33-
for (int i = 0; i < needCheckout.Count; i += 10)
74+
for (int i = 0; i < restores.Count; i += 10)
3475
{
35-
var count = Math.Min(10, needCheckout.Count - i);
36-
new Restore(repo, needCheckout.GetRange(i, count), "--worktree --recurse-submodules") { Log = log }.Exec();
76+
var count = Math.Min(10, restores.Count - i);
77+
new Restore(repo, restores.GetRange(i, count), "--worktree --recurse-submodules") { Log = log }.Exec();
3778
}
3879
}
3980
}

0 commit comments

Comments
 (0)