Skip to content

Commit 8dd1ce9

Browse files
committed
refactor: rewrite Commands.QueryFileContent and use it instead of GetImageFileAsBitmap
1 parent 9a4f928 commit 8dd1ce9

File tree

4 files changed

+44
-64
lines changed

4 files changed

+44
-64
lines changed

src/Commands/GetImageFileAsBitmap.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/Commands/QueryFileContent.cs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
1-
using System.Text;
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
24

35
namespace SourceGit.Commands
46
{
5-
public class QueryFileContent : Command
7+
public static class QueryFileContent
68
{
7-
public QueryFileContent(string repo, string revision, string file)
9+
public static Stream Run(string repo, string revision, string file)
810
{
9-
WorkingDirectory = repo;
10-
Context = repo;
11-
Args = $"show {revision}:\"{file}\"";
12-
}
11+
var starter = new ProcessStartInfo();
12+
starter.WorkingDirectory = repo;
13+
starter.FileName = Native.OS.GitExecutable;
14+
starter.Arguments = $"show {revision}:\"{file}\"";
15+
starter.UseShellExecute = false;
16+
starter.CreateNoWindow = true;
17+
starter.WindowStyle = ProcessWindowStyle.Hidden;
18+
starter.RedirectStandardOutput = true;
1319

14-
public string Result()
15-
{
16-
Exec();
17-
return _builder.ToString();
18-
}
20+
try
21+
{
22+
var stream = new MemoryStream();
23+
var proc = new Process() { StartInfo = starter };
24+
proc.Start();
25+
proc.StandardOutput.BaseStream.CopyTo(stream);
26+
proc.WaitForExit();
27+
proc.Close();
1928

20-
protected override void OnReadline(string line)
21-
{
22-
_builder.Append(line);
23-
_builder.Append('\n');
29+
stream.Position = 0;
30+
return stream;
31+
}
32+
catch (Exception e)
33+
{
34+
App.RaiseException(repo, $"Failed to query file content: {e}");
35+
return null;
36+
}
2437
}
25-
26-
private readonly StringBuilder _builder = new StringBuilder();
2738
}
2839
}

src/ViewModels/CommitDetail.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading.Tasks;
55

66
using Avalonia.Controls;
7+
using Avalonia.Media.Imaging;
78
using Avalonia.Platform.Storage;
89
using Avalonia.Threading;
910

@@ -255,7 +256,7 @@ public ContextMenu CreateChangeContextMenu(Models.Change change)
255256
menu.Items.Add(blame);
256257
menu.Items.Add(explore);
257258
menu.Items.Add(new MenuItem { Header = "-" });
258-
}
259+
}
259260

260261
var copyPath = new MenuItem();
261262
copyPath.Header = App.Text("CopyPath");
@@ -473,7 +474,8 @@ private void RefreshViewRevisionFile(Models.Object file)
473474
var ext = Path.GetExtension(file.Path);
474475
if (IMG_EXTS.Contains(ext))
475476
{
476-
var bitmap = Commands.GetImageFileAsBitmap.Run(_repo, _commit.SHA, file.Path);
477+
var stream = Commands.QueryFileContent.Run(_repo, _commit.SHA, file.Path);
478+
var bitmap = stream != null ? new Bitmap(stream) : null as Bitmap;
477479
Dispatcher.UIThread.Invoke(() =>
478480
{
479481
ViewRevisionFileContent = new Models.RevisionImageFile() { Image = bitmap };
@@ -491,7 +493,8 @@ private void RefreshViewRevisionFile(Models.Object file)
491493
return;
492494
}
493495

494-
var content = new Commands.QueryFileContent(_repo, _commit.SHA, file.Path).Result();
496+
var contentStream = Commands.QueryFileContent.Run(_repo, _commit.SHA, file.Path);
497+
var content = new StreamReader(contentStream).ReadToEnd();
495498
if (content.StartsWith("version https://git-lfs.github.com/spec/", StringComparison.Ordinal))
496499
{
497500
var obj = new Models.RevisionLFSObject() { Object = new Models.LFSObject() };

src/ViewModels/DiffContext.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ public DiffContext(string repo, Models.DiffOption option, DiffContext previous =
101101
var imgDiff = new Models.ImageDiff();
102102
if (option.Revisions.Count == 2)
103103
{
104-
imgDiff.Old = Commands.GetImageFileAsBitmap.Run(repo, option.Revisions[0], oldPath);
105-
imgDiff.New = Commands.GetImageFileAsBitmap.Run(repo, option.Revisions[1], oldPath);
104+
imgDiff.Old = BitmapFromRevisionFile(repo, option.Revisions[0], oldPath);
105+
imgDiff.New = BitmapFromRevisionFile(repo, option.Revisions[1], oldPath);
106106
}
107107
else
108108
{
109109
var fullPath = Path.Combine(repo, _option.Path);
110-
imgDiff.Old = Commands.GetImageFileAsBitmap.Run(repo, "HEAD", oldPath);
110+
imgDiff.Old = BitmapFromRevisionFile(repo, "HEAD", oldPath);
111111
imgDiff.New = File.Exists(fullPath) ? new Bitmap(fullPath) : null;
112112
}
113113
rs = imgDiff;
@@ -163,6 +163,12 @@ public void OpenExternalMergeTool()
163163
Task.Run(() => Commands.MergeTool.OpenForDiff(_repo, exec, args, _option));
164164
}
165165

166+
private Bitmap BitmapFromRevisionFile(string repo, string revision, string file)
167+
{
168+
var stream = Commands.QueryFileContent.Run(repo, revision, file);
169+
return stream != null ? new Bitmap(stream) : null;
170+
}
171+
166172
private static readonly HashSet<string> IMG_EXTS = new HashSet<string>()
167173
{
168174
".ico", ".bmp", ".jpg", ".png", ".jpeg"

0 commit comments

Comments
 (0)