2024-03-17 18:37:06 -07:00
|
|
|
|
using System;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
using Avalonia.Threading;
|
|
|
|
|
|
|
|
|
|
namespace SourceGit.Commands
|
|
|
|
|
{
|
|
|
|
|
public static class SaveChangesAsPatch
|
|
|
|
|
{
|
|
|
|
|
public static bool Exec(string repo, List<Models.Change> changes, bool isUnstaged, string saveTo)
|
|
|
|
|
{
|
|
|
|
|
using (var sw = File.Create(saveTo))
|
|
|
|
|
{
|
|
|
|
|
foreach (var change in changes)
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (!ProcessSingleChange(repo, new Models.DiffOption(change, isUnstaged), sw))
|
|
|
|
|
return false;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private static bool ProcessSingleChange(string repo, Models.DiffOption opt, FileStream writer)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var starter = new ProcessStartInfo();
|
|
|
|
|
starter.WorkingDirectory = repo;
|
2024-04-05 22:14:22 -07:00
|
|
|
|
starter.FileName = Native.OS.GitExecutable;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
starter.Arguments = $"diff --ignore-cr-at-eol --unified=4 {opt}";
|
|
|
|
|
starter.UseShellExecute = false;
|
|
|
|
|
starter.CreateNoWindow = true;
|
|
|
|
|
starter.WindowStyle = ProcessWindowStyle.Hidden;
|
|
|
|
|
starter.RedirectStandardOutput = true;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var proc = new Process() { StartInfo = starter };
|
|
|
|
|
proc.Start();
|
|
|
|
|
proc.StandardOutput.BaseStream.CopyTo(writer);
|
|
|
|
|
proc.WaitForExit();
|
|
|
|
|
var rs = proc.ExitCode == 0;
|
|
|
|
|
proc.Close();
|
|
|
|
|
|
|
|
|
|
return rs;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
|
|
|
{
|
2024-02-25 19:29:57 -08:00
|
|
|
|
App.RaiseException(repo, "Save change to patch failed: " + e.Message);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
});
|
2024-02-05 23:08:37 -08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|