From db799f5c927d0ec394f2ca00e3c0687039a54dc3 Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 18 Nov 2020 20:05:41 +0800 Subject: [PATCH] fix: fix save revision file to path error when selected file is managed by LFS --- src/Git/Commit.cs | 34 ++++++++++++++++++++++++++++++++++ src/Git/Repository.cs | 26 -------------------------- src/UI/CommitViewer.xaml.cs | 6 +++--- 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/Git/Commit.cs b/src/Git/Commit.cs index c6243c53..6b0b474a 100644 --- a/src/Git/Commit.cs +++ b/src/Git/Commit.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Collections.Generic; using System.Diagnostics; using System.Text.RegularExpressions; @@ -261,6 +262,39 @@ namespace SourceGit.Git { return string.Join("\n", data); } + /// + /// Save file to. + /// + /// + /// + /// + public void SaveFileTo(Repository repo, string file, string saveTo) { + var tmp = Path.GetTempFileName(); + var bat = tmp + ".bat"; + var cmd = ""; + + if (repo.IsLFSFiltered(file)) { + cmd += $"git --no-pager show {SHA}:\"{file}\" > {tmp}.lfs\n"; + cmd += $"git --no-pager lfs smudge < {tmp}.lfs > {saveTo}\n"; + } else { + cmd = $"git --no-pager show {SHA}:\"{file}\" > {saveTo}\n"; + } + + File.WriteAllText(bat, cmd); + + var starter = new ProcessStartInfo(); + starter.FileName = bat; + starter.WorkingDirectory = repo.Path; + starter.CreateNoWindow = true; + starter.WindowStyle = ProcessWindowStyle.Hidden; + + var proc = Process.Start(starter); + proc.WaitForExit(); + proc.Close(); + + File.Delete(bat); + } + private static void ParseSHA(Commit commit, string data) { var decoratorStart = data.IndexOf('('); if (decoratorStart < 0) { diff --git a/src/Git/Repository.cs b/src/Git/Repository.cs index 8b3ba99f..f0bbfe03 100644 --- a/src/Git/Repository.cs +++ b/src/Git/Repository.cs @@ -184,32 +184,6 @@ namespace SourceGit.Git { return RunCommand(Path, args, outputHandler, includeError); } - /// - /// Create process and redirect output to file. - /// - /// Git command arguments. - /// File path to redirect output into. - public void RunAndRedirect(string args, string redirectTo) { - var startInfo = new ProcessStartInfo(); - startInfo.FileName = Preference.Instance.GitExecutable; - startInfo.Arguments = "--no-pager " + args; - startInfo.WorkingDirectory = Path; - startInfo.UseShellExecute = false; - startInfo.CreateNoWindow = true; - startInfo.RedirectStandardOutput = true; - startInfo.RedirectStandardError = true; - - var proc = new Process() { StartInfo = startInfo }; - proc.Start(); - - using (var writer = new FileStream(redirectTo, FileMode.OpenOrCreate)) { - proc.StandardOutput.BaseStream.CopyTo(writer); - } - - proc.WaitForExit(); - proc.Close(); - } - /// /// Assert command result and then update branches and commits. /// diff --git a/src/UI/CommitViewer.xaml.cs b/src/UI/CommitViewer.xaml.cs index 98bbd16b..f63cc373 100644 --- a/src/UI/CommitViewer.xaml.cs +++ b/src/UI/CommitViewer.xaml.cs @@ -298,12 +298,12 @@ namespace SourceGit.UI { saveAs.Header = "Save As ..."; saveAs.Click += (obj, ev) => { var dialog = new System.Windows.Forms.FolderBrowserDialog(); - dialog.Description = change.Path; + dialog.Description = path; dialog.ShowNewFolderButton = true; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { var savePath = Path.Combine(dialog.SelectedPath, Path.GetFileName(path)); - repo.RunAndRedirect($"show {commit.SHA}:\"{path}\"", savePath); + commit.SaveFileTo(repo, path, savePath); } }; menu.Items.Add(saveAs); @@ -526,7 +526,7 @@ namespace SourceGit.UI { if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { var path = Path.Combine(dialog.SelectedPath, node.Name); - repo.RunAndRedirect($"show {commit.SHA}:\"{node.FilePath}\"", path); + commit.SaveFileTo(repo, node.FilePath, path); } }; menu.Items.Add(saveAs);