2024-02-05 23:08:37 -08:00
|
|
|
|
using System;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace SourceGit.Commands {
|
2024-02-05 23:08:37 -08:00
|
|
|
|
public static class SaveRevisionFile {
|
|
|
|
|
public static void Run(string repo, string revision, string file, string saveTo) {
|
|
|
|
|
var isLFSFiltered = new IsLFSFiltered(repo, file).Result();
|
|
|
|
|
if (isLFSFiltered) {
|
|
|
|
|
var tmpFile = saveTo + ".tmp";
|
|
|
|
|
if (ExecCmd(repo, $"show {revision}:\"{file}\"", tmpFile)) {
|
|
|
|
|
ExecCmd(repo, $"lfs smudge", saveTo, tmpFile);
|
|
|
|
|
}
|
|
|
|
|
File.Delete(tmpFile);
|
2021-04-29 05:05:55 -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-02-05 23:08:37 -08: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;
|
|
|
|
|
starter.FileName = Native.OS.GitExecutableFile;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
using (var sw = File.OpenWrite(outputFile)) {
|
|
|
|
|
try {
|
|
|
|
|
var proc = new Process() { StartInfo = starter };
|
|
|
|
|
proc.Start();
|
|
|
|
|
|
|
|
|
|
if (inputFile != null) {
|
|
|
|
|
using (StreamReader sr = new StreamReader(inputFile)) {
|
|
|
|
|
while (true) {
|
|
|
|
|
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;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
App.RaiseException(repo, "Save file failed: " + e.Message);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|