From 8f3c2fdc32fa2d0969cb4ae3eb4d06cea3cb5c89 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 29 Apr 2021 21:22:17 +0800 Subject: [PATCH] fix: fix discard with files not only dropped changes selected but also others --- src/Commands/Checkout.cs | 2 +- src/Commands/Clean.cs | 16 ++++++++++++ src/Commands/Discard.cs | 42 ++++++++++++++++++-------------- src/Views/Popups/Discard.xaml.cs | 7 +++++- 4 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/Commands/Checkout.cs b/src/Commands/Checkout.cs index f3d976da..b6d6ec41 100644 --- a/src/Commands/Checkout.cs +++ b/src/Commands/Checkout.cs @@ -33,7 +33,7 @@ namespace SourceGit.Commands { public bool Files(List files) { StringBuilder builder = new StringBuilder(); - builder.Append("checkout -qf --"); + builder.Append("checkout -f -q --"); foreach (var f in files) { builder.Append(" \""); builder.Append(f); diff --git a/src/Commands/Clean.cs b/src/Commands/Clean.cs index 83de46cd..38a9a477 100644 --- a/src/Commands/Clean.cs +++ b/src/Commands/Clean.cs @@ -1,3 +1,6 @@ +using System.Collections.Generic; +using System.Text; + namespace SourceGit.Commands { /// /// 清理指令 @@ -8,5 +11,18 @@ namespace SourceGit.Commands { Cwd = repo; Args = "clean -qfd"; } + + public Clean(string repo, List files) { + StringBuilder builder = new StringBuilder(); + builder.Append("clean -qfd --"); + foreach (var f in files) { + builder.Append(" \""); + builder.Append(f); + builder.Append("\""); + } + + Cwd = repo; + Args = builder.ToString(); + } } } diff --git a/src/Commands/Discard.cs b/src/Commands/Discard.cs index 42b4005e..7358ae51 100644 --- a/src/Commands/Discard.cs +++ b/src/Commands/Discard.cs @@ -7,31 +7,37 @@ namespace SourceGit.Commands { /// public class Discard { private string repo = null; - private List files = new List(); - public Discard(string repo, List changes) { + public Discard(string repo) { this.repo = repo; - - if (changes != null && changes.Count > 0) { - foreach (var c in changes) { - if (c.WorkTree == Models.Change.Status.Untracked || c.WorkTree == Models.Change.Status.Added) continue; - files.Add(c.Path); - } - } } - public bool Exec() { - if (files.Count == 0) { - new Reset(repo, "HEAD", "--hard").Exec(); - } else { - for (int i = 0; i < files.Count; i += 10) { - var count = Math.Min(10, files.Count - i); - new Checkout(repo).Files(files.GetRange(i, count)); + public void Whole() { + new Reset(repo, "HEAD", "--hard").Exec(); + new Clean(repo).Exec(); + } + + public void Changes(List changes) { + var needClean = new List(); + var needCheckout = new List(); + + foreach (var c in changes) { + if (c.WorkTree == Models.Change.Status.Untracked || c.WorkTree == Models.Change.Status.Added) { + needClean.Add(c.Path); + } else { + needCheckout.Add(c.Path); } } - new Clean(repo).Exec(); - return true; + for (int i = 0; i < needClean.Count; i += 10) { + var count = Math.Min(10, needClean.Count - i); + new Clean(repo, needClean.GetRange(i, count)).Exec(); + } + + for (int i = 0; i < needCheckout.Count; i += 10) { + var count = Math.Min(10, needCheckout.Count - i); + new Checkout(repo).Files(needCheckout.GetRange(i, count)); + } } } } diff --git a/src/Views/Popups/Discard.xaml.cs b/src/Views/Popups/Discard.xaml.cs index 54720667..da855147 100644 --- a/src/Views/Popups/Discard.xaml.cs +++ b/src/Views/Popups/Discard.xaml.cs @@ -33,7 +33,12 @@ namespace SourceGit.Views.Popups { public override Task Start() { return Task.Run(() => { Models.Watcher.SetEnabled(repo, false); - new Commands.Discard(repo, changes).Exec(); + var cmd = new Commands.Discard(repo); + if (changes == null || changes.Count == 0) { + cmd.Whole(); + } else { + cmd.Changes(changes); + } Models.Watcher.SetEnabled(repo, true); return true; });