mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-21 04:04:59 +00:00
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>
This commit is contained in:
parent
0f6c8976af
commit
029fd6933f
2 changed files with 61 additions and 39 deletions
|
@ -1,31 +1,12 @@
|
||||||
using System.Collections.Generic;
|
namespace SourceGit.Commands
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace SourceGit.Commands
|
|
||||||
{
|
{
|
||||||
public class Clean : Command
|
public class Clean : Command
|
||||||
{
|
{
|
||||||
public Clean(string repo, bool includeIgnored)
|
public Clean(string repo)
|
||||||
{
|
{
|
||||||
WorkingDirectory = repo;
|
WorkingDirectory = repo;
|
||||||
Context = repo;
|
Context = repo;
|
||||||
Args = includeIgnored ? "clean -qfdx" : "clean -qfd";
|
Args = "clean -qfdx";
|
||||||
}
|
|
||||||
|
|
||||||
public Clean(string repo, List<string> files)
|
|
||||||
{
|
|
||||||
var builder = new StringBuilder();
|
|
||||||
builder.Append("clean -qfd --");
|
|
||||||
foreach (var f in files)
|
|
||||||
{
|
|
||||||
builder.Append(" \"");
|
|
||||||
builder.Append(f);
|
|
||||||
builder.Append("\"");
|
|
||||||
}
|
|
||||||
|
|
||||||
WorkingDirectory = repo;
|
|
||||||
Context = repo;
|
|
||||||
Args = builder.ToString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
using Avalonia.Threading;
|
||||||
|
|
||||||
namespace SourceGit.Commands
|
namespace SourceGit.Commands
|
||||||
{
|
{
|
||||||
|
@ -7,33 +10,71 @@ namespace SourceGit.Commands
|
||||||
{
|
{
|
||||||
public static void All(string repo, bool includeIgnored, Models.ICommandLog log)
|
public static void All(string repo, bool includeIgnored, Models.ICommandLog log)
|
||||||
{
|
{
|
||||||
|
var changes = new QueryLocalChanges(repo).Result();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
foreach (var c in changes)
|
||||||
|
{
|
||||||
|
if (c.WorkTree == Models.ChangeState.Untracked ||
|
||||||
|
c.WorkTree == Models.ChangeState.Added ||
|
||||||
|
c.Index == Models.ChangeState.Added ||
|
||||||
|
c.Index == Models.ChangeState.Renamed)
|
||||||
|
{
|
||||||
|
var fullPath = Path.Combine(repo, c.Path);
|
||||||
|
if (Directory.Exists(fullPath))
|
||||||
|
Directory.Delete(fullPath, true);
|
||||||
|
else
|
||||||
|
File.Delete(fullPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Dispatcher.UIThread.Invoke(() =>
|
||||||
|
{
|
||||||
|
App.RaiseException(repo, $"Failed to discard changes. Reason: {e.Message}");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
new Restore(repo) { Log = log }.Exec();
|
new Restore(repo) { Log = log }.Exec();
|
||||||
new Clean(repo, includeIgnored) { Log = log }.Exec();
|
if (includeIgnored)
|
||||||
|
new Clean(repo) { Log = log }.Exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Changes(string repo, List<Models.Change> changes, Models.ICommandLog log)
|
public static void Changes(string repo, List<Models.Change> changes, Models.ICommandLog log)
|
||||||
{
|
{
|
||||||
var needClean = new List<string>();
|
var restores = new List<string>();
|
||||||
var needCheckout = new List<string>();
|
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
foreach (var c in changes)
|
foreach (var c in changes)
|
||||||
{
|
{
|
||||||
if (c.WorkTree == Models.ChangeState.Untracked || c.WorkTree == Models.ChangeState.Added)
|
if (c.WorkTree == Models.ChangeState.Untracked || c.WorkTree == Models.ChangeState.Added)
|
||||||
needClean.Add(c.Path);
|
{
|
||||||
|
var fullPath = Path.Combine(repo, c.Path);
|
||||||
|
if (Directory.Exists(fullPath))
|
||||||
|
Directory.Delete(fullPath, true);
|
||||||
else
|
else
|
||||||
needCheckout.Add(c.Path);
|
File.Delete(fullPath);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
restores.Add(c.Path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Dispatcher.UIThread.Invoke(() =>
|
||||||
|
{
|
||||||
|
App.RaiseException(repo, $"Failed to discard changes. Reason: {e.Message}");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < needClean.Count; i += 10)
|
for (int i = 0; i < restores.Count; i += 10)
|
||||||
{
|
{
|
||||||
var count = Math.Min(10, needClean.Count - i);
|
var count = Math.Min(10, restores.Count - i);
|
||||||
new Clean(repo, needClean.GetRange(i, count)) { Log = log }.Exec();
|
new Restore(repo, restores.GetRange(i, count), "--worktree --recurse-submodules") { Log = log }.Exec();
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < needCheckout.Count; i += 10)
|
|
||||||
{
|
|
||||||
var count = Math.Min(10, needCheckout.Count - i);
|
|
||||||
new Restore(repo, needCheckout.GetRange(i, count), "--worktree --recurse-submodules") { Log = log }.Exec();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue