From 689b02cd616827ec25b99ddda8a84ca617b2de9c Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 13 Jul 2020 20:41:17 +0800 Subject: [PATCH] Change the way to preview binary in CommitViewer --- SourceGit/Git/Commit.cs | 12 +++++++----- SourceGit/UI/CommitViewer.xaml | 28 ++++++++++++++++++---------- SourceGit/UI/CommitViewer.xaml.cs | 11 +++++++++-- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/SourceGit/Git/Commit.cs b/SourceGit/Git/Commit.cs index 7cbafad4..2b3c5c89 100644 --- a/SourceGit/Git/Commit.cs +++ b/SourceGit/Git/Commit.cs @@ -190,19 +190,19 @@ namespace SourceGit.Git { /// /// /// - public string GetTextFileContent(Repository repo, string file) { + public string GetTextFileContent(Repository repo, string file, out bool isBinary) { var data = new List(); - var isBinary = false; var count = 0; + var binary = false; var errs = repo.RunCommand($"show {SHA}:\"{file}\"", line => { - if (isBinary) return; + if (binary) return; count++; if (data.Count >= 1000) return; if (line.IndexOf('\0') >= 0) { - isBinary = true; + binary = true; data.Clear(); data.Add("BINARY FILE PREVIEW NOT SUPPORTED!"); return; @@ -211,11 +211,13 @@ namespace SourceGit.Git { data.Add(line); }); - if (!isBinary && count > 1000) { + if (!binary && count > 1000) { data.Add("..."); data.Add($"Total {count} lines. Hide {count-1000} lines."); } + isBinary = binary; + if (errs != null) App.RaiseError(errs); return string.Join("\n", data); } diff --git a/SourceGit/UI/CommitViewer.xaml b/SourceGit/UI/CommitViewer.xaml index a7e71ade..7f68ffea 100644 --- a/SourceGit/UI/CommitViewer.xaml +++ b/SourceGit/UI/CommitViewer.xaml @@ -392,16 +392,24 @@ - - - - + + + + + + + + + + diff --git a/SourceGit/UI/CommitViewer.xaml.cs b/SourceGit/UI/CommitViewer.xaml.cs index 66bda1e6..526841c3 100644 --- a/SourceGit/UI/CommitViewer.xaml.cs +++ b/SourceGit/UI/CommitViewer.xaml.cs @@ -345,13 +345,20 @@ namespace SourceGit.UI { private async void FileTreeItemSelected(object sender, RoutedPropertyChangedEventArgs e) { filePreview.Text = ""; + maskPreviewNotSupported.Visibility = Visibility.Collapsed; var node = e.NewValue as Node; if (node == null || !node.IsFile) return; await Task.Run(() => { - var data = commit.GetTextFileContent(repo, node.FilePath); - Dispatcher.Invoke(() => filePreview.Text = data); + var isBinary = false; + var data = commit.GetTextFileContent(repo, node.FilePath, out isBinary); + + if (isBinary) { + Dispatcher.Invoke(() => maskPreviewNotSupported.Visibility = Visibility.Visible); + } else { + Dispatcher.Invoke(() => filePreview.Text = data); + } }); } #endregion