2024-02-05 23:08:37 -08:00
|
|
|
|
using System.Collections.Generic;
|
2021-04-29 23:21:52 -07:00
|
|
|
|
using System.IO;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
namespace SourceGit.Commands
|
|
|
|
|
{
|
|
|
|
|
public class Stash : Command
|
|
|
|
|
{
|
|
|
|
|
public Stash(string repo)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
WorkingDirectory = repo;
|
|
|
|
|
Context = repo;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public bool Push(string message)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
Args = $"stash push -m \"{message}\"";
|
|
|
|
|
return Exec();
|
|
|
|
|
}
|
2023-10-10 01:59:47 -07:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public bool Push(List<Models.Change> changes, string message)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var temp = Path.GetTempFileName();
|
|
|
|
|
var stream = new FileStream(temp, FileMode.Create);
|
|
|
|
|
var writer = new StreamWriter(stream);
|
2023-10-10 01:59:47 -07:00
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var needAdd = new List<Models.Change>();
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var c in changes)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
writer.WriteLine(c.Path);
|
2023-10-10 01:59:47 -07:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (c.WorkTree == Models.ChangeState.Added || c.WorkTree == Models.ChangeState.Untracked)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
needAdd.Add(c);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (needAdd.Count > 10)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
new Add(WorkingDirectory, needAdd).Exec();
|
|
|
|
|
needAdd.Clear();
|
2023-10-10 01:59:47 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (needAdd.Count > 0)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
new Add(WorkingDirectory, needAdd).Exec();
|
|
|
|
|
needAdd.Clear();
|
|
|
|
|
}
|
2023-10-10 01:59:47 -07:00
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
writer.Flush();
|
|
|
|
|
stream.Flush();
|
|
|
|
|
writer.Close();
|
|
|
|
|
stream.Close();
|
2023-10-10 01:59:47 -07:00
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
Args = $"stash push -m \"{message}\" --pathspec-from-file=\"{temp}\"";
|
|
|
|
|
var succ = Exec();
|
|
|
|
|
File.Delete(temp);
|
|
|
|
|
return succ;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public bool Apply(string name)
|
|
|
|
|
{
|
2021-04-29 05:05:55 -07:00
|
|
|
|
Args = $"stash apply -q {name}";
|
|
|
|
|
return Exec();
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public bool Pop(string name)
|
|
|
|
|
{
|
2021-04-29 05:05:55 -07:00
|
|
|
|
Args = $"stash pop -q {name}";
|
|
|
|
|
return Exec();
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public bool Drop(string name)
|
|
|
|
|
{
|
2021-04-29 05:05:55 -07:00
|
|
|
|
Args = $"stash drop -q {name}";
|
|
|
|
|
return Exec();
|
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public bool Clear()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
Args = "stash clear";
|
|
|
|
|
return Exec();
|
|
|
|
|
}
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|