sourcegit/src/Commands/Stash.cs

87 lines
2.3 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Text;
2021-04-29 05:05:55 -07:00
namespace SourceGit.Commands
{
public class Stash : Command
{
public Stash(string repo)
{
WorkingDirectory = repo;
Context = repo;
2021-04-29 05:05:55 -07:00
}
public bool Push(string message)
{
Args = $"stash push -m \"{message}\"";
return Exec();
}
public bool Push(List<Models.Change> changes, string message, bool onlyStaged)
{
var pathsBuilder = new StringBuilder();
if (onlyStaged)
{
foreach (var c in changes)
pathsBuilder.Append($"\"{c.Path}\" ");
var paths = pathsBuilder.ToString();
Args = $"stash push --staged -m \"{message}\" -- {paths}";
}
else
{
var needAdd = new List<Models.Change>();
foreach (var c in changes)
{
pathsBuilder.Append($"\"{c.Path}\" ");
if (c.WorkTree == Models.ChangeState.Added || c.WorkTree == Models.ChangeState.Untracked)
{
needAdd.Add(c);
if (needAdd.Count > 10)
{
new Add(WorkingDirectory, needAdd).Exec();
needAdd.Clear();
}
}
}
if (needAdd.Count > 0)
{
new Add(WorkingDirectory, needAdd).Exec();
needAdd.Clear();
}
var paths = pathsBuilder.ToString();
Args = $"stash push -m \"{message}\" -- {paths}";
}
return Exec();
2021-04-29 05:05:55 -07:00
}
public bool Apply(string name)
{
2021-04-29 05:05:55 -07:00
Args = $"stash apply -q {name}";
return Exec();
}
public bool Pop(string name)
{
2021-04-29 05:05:55 -07:00
Args = $"stash pop -q {name}";
return Exec();
}
public bool Drop(string name)
{
2021-04-29 05:05:55 -07:00
Args = $"stash drop -q {name}";
return Exec();
}
public bool Clear()
{
Args = "stash clear";
return Exec();
}
2021-04-29 05:05:55 -07:00
}
}