sourcegit/src/Commands/QueryFileContent.cs

40 lines
1.2 KiB
C#
Raw Normal View History

using System;
using System.Diagnostics;
using System.IO;
2021-04-29 05:05:55 -07:00
namespace SourceGit.Commands
{
public static class QueryFileContent
{
public static Stream Run(string repo, string revision, string file)
{
var starter = new ProcessStartInfo();
starter.WorkingDirectory = repo;
starter.FileName = Native.OS.GitExecutable;
starter.Arguments = $"show {revision}:\"{file}\"";
starter.UseShellExecute = false;
starter.CreateNoWindow = true;
starter.WindowStyle = ProcessWindowStyle.Hidden;
starter.RedirectStandardOutput = true;
2021-04-29 05:05:55 -07:00
try
{
var stream = new MemoryStream();
var proc = new Process() { StartInfo = starter };
proc.Start();
proc.StandardOutput.BaseStream.CopyTo(stream);
proc.WaitForExit();
proc.Close();
2021-04-29 05:05:55 -07:00
stream.Position = 0;
return stream;
}
catch (Exception e)
{
App.RaiseException(repo, $"Failed to query file content: {e}");
return null;
}
2021-04-29 05:05:55 -07:00
}
}
}