|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | + |
| 5 | +using Avalonia.Threading; |
3 | 6 |
|
4 | 7 | namespace SourceGit.Commands |
5 | 8 | { |
6 | 9 | public static class Discard |
7 | 10 | { |
8 | 11 | public static void All(string repo, bool includeIgnored, Models.ICommandLog log) |
9 | 12 | { |
| 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 | + |
10 | 39 | new Restore(repo) { Log = log }.Exec(); |
11 | | - new Clean(repo, includeIgnored) { Log = log }.Exec(); |
| 40 | + if (includeIgnored) |
| 41 | + new Clean(repo) { Log = log }.Exec(); |
12 | 42 | } |
13 | 43 |
|
14 | 44 | public static void Changes(string repo, List<Models.Change> changes, Models.ICommandLog log) |
15 | 45 | { |
16 | | - var needClean = new List<string>(); |
17 | | - var needCheckout = new List<string>(); |
| 46 | + var restores = new List<string>(); |
18 | 47 |
|
19 | | - foreach (var c in changes) |
| 48 | + try |
20 | 49 | { |
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 | + } |
25 | 65 | } |
26 | | - |
27 | | - for (int i = 0; i < needClean.Count; i += 10) |
| 66 | + catch (Exception e) |
28 | 67 | { |
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 | + }); |
31 | 72 | } |
32 | 73 |
|
33 | | - for (int i = 0; i < needCheckout.Count; i += 10) |
| 74 | + for (int i = 0; i < restores.Count; i += 10) |
34 | 75 | { |
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(); |
37 | 78 | } |
38 | 79 | } |
39 | 80 | } |
|
0 commit comments