sourcegit/src/Commands/SaveRevisionFile.cs

61 lines
2.3 KiB
C#
Raw Normal View History

using System;
2021-04-29 05:05:55 -07:00
using System.Diagnostics;
using System.IO;
namespace SourceGit.Commands {
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 {
ExecCmd(repo, $"show {revision}:\"{file}\"", saveTo);
2021-04-29 05:05:55 -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();
starter.WorkingDirectory = repo;
starter.FileName = Native.OS.GitInstallPath;
starter.Arguments = args;
starter.UseShellExecute = false;
2021-04-29 05:05:55 -07:00
starter.CreateNoWindow = true;
starter.WindowStyle = ProcessWindowStyle.Hidden;
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
}
}
}