refactor: use git restore instead of git reset to unstage local changes (#1373)

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-05-30 09:43:45 +08:00
parent 46231a759c
commit e40ca4bbe0
No known key found for this signature in database
3 changed files with 13 additions and 5 deletions

View file

@ -36,7 +36,7 @@ namespace SourceGit.Commands
}); });
} }
new Restore(repo) { Log = log }.Exec(); new Restore(repo, false) { Log = log }.Exec();
if (includeIgnored) if (includeIgnored)
new Clean(repo) { Log = log }.Exec(); new Clean(repo) { Log = log }.Exec();
} }

View file

@ -5,11 +5,15 @@ namespace SourceGit.Commands
{ {
public class Restore : Command public class Restore : Command
{ {
public Restore(string repo) public Restore(string repo, bool onlyStaged)
{ {
WorkingDirectory = repo; WorkingDirectory = repo;
Context = repo; Context = repo;
Args = "restore . --source=HEAD --staged --worktree --recurse-submodules";
if (onlyStaged)
Args = "restore --source=HEAD --staged .";
else
Args = "restore --source=HEAD --staged --worktree --recurse-submodules .";
} }
public Restore(string repo, List<string> files, string extra) public Restore(string repo, List<string> files, string extra)

View file

@ -1664,14 +1664,18 @@ namespace SourceGit.ViewModels
} }
else if (count == _staged.Count) else if (count == _staged.Count)
{ {
await Task.Run(() => new Commands.Reset(_repo.FullPath).Use(log).Exec()); await Task.Run(() => new Commands.Restore(_repo.FullPath, true).Use(log).Exec());
} }
else else
{ {
for (int i = 0; i < count; i += 10) for (int i = 0; i < count; i += 10)
{ {
var step = changes.GetRange(i, Math.Min(10, count - i)); var step = changes.GetRange(i, Math.Min(10, count - i));
await Task.Run(() => new Commands.Reset(_repo.FullPath, step).Use(log).Exec()); var files = new List<string>();
foreach (var c in step)
files.Add(c.Path);
await Task.Run(() => new Commands.Restore(_repo.FullPath, files, "--staged").Use(log).Exec());
} }
} }
log.Complete(); log.Complete();