refactor: rewrite git restore integration

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-06-02 12:14:22 +08:00
parent 80df53cf04
commit 78f9ae2fa9
No known key found for this signature in database
5 changed files with 71 additions and 18 deletions

View file

@ -5,17 +5,23 @@ namespace SourceGit.Commands
{
public class Restore : Command
{
public Restore(string repo, bool onlyStaged)
/// <summary>
/// Only used to discard all changes in the working directory and staged area.
/// </summary>
/// <param name="repo"></param>
public Restore(string repo)
{
WorkingDirectory = repo;
Context = repo;
if (onlyStaged)
Args = "restore --source=HEAD --staged .";
else
Args = "restore --source=HEAD --staged --worktree --recurse-submodules .";
Args = "restore --source=HEAD --staged --worktree --recurse-submodules .";
}
/// <summary>
/// Discard changes with git (&lt; 2.25.0) that does not support the `--pathspec-from-file` option.
/// </summary>
/// <param name="repo"></param>
/// <param name="files"></param>
/// <param name="extra"></param>
public Restore(string repo, List<string> files, string extra)
{
WorkingDirectory = repo;
@ -30,5 +36,24 @@ namespace SourceGit.Commands
builder.Append(' ').Append('"').Append(f).Append('"');
Args = builder.ToString();
}
/// <summary>
/// Discard changes with git (&gt;= 2.25.0) that supports the `--pathspec-from-file` option.
/// </summary>
/// <param name="repo"></param>
/// <param name="pathspecFile"></param>
/// <param name="extra"></param>
public Restore(string repo, string pathspecFile, string extra)
{
WorkingDirectory = repo;
Context = repo;
var builder = new StringBuilder();
builder.Append("restore ");
if (!string.IsNullOrEmpty(extra))
builder.Append(extra).Append(" ");
builder.Append("--pathspec-from-file=\"").Append(pathspecFile).Append('"');
Args = builder.ToString();
}
}
}