fix<Discard>: fix discard with files not only dropped changes selected but also others

This commit is contained in:
leo 2021-04-29 21:22:17 +08:00
parent 30ab8ae954
commit 8f3c2fdc32
4 changed files with 47 additions and 20 deletions

View file

@ -33,7 +33,7 @@ namespace SourceGit.Commands {
public bool Files(List<string> files) { public bool Files(List<string> files) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append("checkout -qf --"); builder.Append("checkout -f -q --");
foreach (var f in files) { foreach (var f in files) {
builder.Append(" \""); builder.Append(" \"");
builder.Append(f); builder.Append(f);

View file

@ -1,3 +1,6 @@
using System.Collections.Generic;
using System.Text;
namespace SourceGit.Commands { namespace SourceGit.Commands {
/// <summary> /// <summary>
/// 清理指令 /// 清理指令
@ -8,5 +11,18 @@ namespace SourceGit.Commands {
Cwd = repo; Cwd = repo;
Args = "clean -qfd"; Args = "clean -qfd";
} }
public Clean(string repo, List<string> 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();
}
} }
} }

View file

@ -7,31 +7,37 @@ namespace SourceGit.Commands {
/// </summary> /// </summary>
public class Discard { public class Discard {
private string repo = null; private string repo = null;
private List<string> files = new List<string>();
public Discard(string repo, List<Models.Change> changes) { public Discard(string repo) {
this.repo = 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() { public void Whole() {
if (files.Count == 0) {
new Reset(repo, "HEAD", "--hard").Exec(); new Reset(repo, "HEAD", "--hard").Exec();
new Clean(repo).Exec();
}
public void Changes(List<Models.Change> changes) {
var needClean = new List<string>();
var needCheckout = new List<string>();
foreach (var c in changes) {
if (c.WorkTree == Models.Change.Status.Untracked || c.WorkTree == Models.Change.Status.Added) {
needClean.Add(c.Path);
} else { } else {
for (int i = 0; i < files.Count; i += 10) { needCheckout.Add(c.Path);
var count = Math.Min(10, files.Count - i);
new Checkout(repo).Files(files.GetRange(i, count));
} }
} }
new Clean(repo).Exec(); for (int i = 0; i < needClean.Count; i += 10) {
return true; 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));
}
} }
} }
} }

View file

@ -33,7 +33,12 @@ namespace SourceGit.Views.Popups {
public override Task<bool> Start() { public override Task<bool> Start() {
return Task.Run(() => { return Task.Run(() => {
Models.Watcher.SetEnabled(repo, false); 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); Models.Watcher.SetEnabled(repo, true);
return true; return true;
}); });