2024-03-17 18:37:06 -07:00
|
|
|
|
using System;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
using Avalonia.Threading;
|
|
|
|
|
|
|
|
|
|
namespace SourceGit.Commands
|
|
|
|
|
{
|
|
|
|
|
public static class SaveRevisionFile
|
|
|
|
|
{
|
|
|
|
|
public static void Run(string repo, string revision, string file, string saveTo)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var isLFSFiltered = new IsLFSFiltered(repo, file).Result();
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (isLFSFiltered)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var tmpFile = saveTo + ".tmp";
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (ExecCmd(repo, $"show {revision}:\"{file}\"", tmpFile))
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
ExecCmd(repo, $"lfs smudge", saveTo, tmpFile);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
File.Delete(tmpFile);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
ExecCmd(repo, $"show {revision}:\"{file}\"", saveTo);
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
|
|
|
|
|
|
private static bool ExecCmd(string repo, string args, string outputFile, string inputFile = null)
|
|
|
|
|
{
|
2021-04-29 05:05:55 -07:00
|
|
|
|
var starter = new ProcessStartInfo();
|
2024-02-05 23:08:37 -08:00
|
|
|
|
starter.WorkingDirectory = repo;
|
2024-02-20 19:29:28 -08:00
|
|
|
|
starter.FileName = Native.OS.GitInstallPath;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
starter.Arguments = args;
|
|
|
|
|
starter.UseShellExecute = false;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
starter.CreateNoWindow = true;
|
|
|
|
|
starter.WindowStyle = ProcessWindowStyle.Hidden;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
starter.RedirectStandardInput = true;
|
|
|
|
|
starter.RedirectStandardOutput = true;
|
|
|
|
|
starter.RedirectStandardError = true;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
using (var sw = File.OpenWrite(outputFile))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var proc = new Process() { StartInfo = starter };
|
|
|
|
|
proc.Start();
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (inputFile != null)
|
|
|
|
|
{
|
|
|
|
|
using (StreamReader sr = new StreamReader(inputFile))
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var line = sr.ReadLine();
|
|
|
|
|
if (line == null) break;
|
|
|
|
|
proc.StandardInput.WriteLine(line);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
proc.StandardOutput.BaseStream.CopyTo(sw);
|
|
|
|
|
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 file failed: " + e.Message);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
});
|
2024-02-05 23:08:37 -08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|