fix<CommitViewer>: fix save revision file to path error when selected file is managed by LFS

This commit is contained in:
leo 2020-11-18 20:05:41 +08:00
parent 0e48344401
commit db799f5c92
3 changed files with 37 additions and 29 deletions

View file

@ -1,4 +1,5 @@
using System; using System;
using System.IO;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -261,6 +262,39 @@ namespace SourceGit.Git {
return string.Join("\n", data); return string.Join("\n", data);
} }
/// <summary>
/// Save file to.
/// </summary>
/// <param name="repo"></param>
/// <param name="file"></param>
/// <param name="saveTo"></param>
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) { private static void ParseSHA(Commit commit, string data) {
var decoratorStart = data.IndexOf('('); var decoratorStart = data.IndexOf('(');
if (decoratorStart < 0) { if (decoratorStart < 0) {

View file

@ -184,32 +184,6 @@ namespace SourceGit.Git {
return RunCommand(Path, args, outputHandler, includeError); return RunCommand(Path, args, outputHandler, includeError);
} }
/// <summary>
/// Create process and redirect output to file.
/// </summary>
/// <param name="args">Git command arguments.</param>
/// <param name="redirectTo">File path to redirect output into.</param>
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();
}
/// <summary> /// <summary>
/// Assert command result and then update branches and commits. /// Assert command result and then update branches and commits.
/// </summary> /// </summary>

View file

@ -298,12 +298,12 @@ namespace SourceGit.UI {
saveAs.Header = "Save As ..."; saveAs.Header = "Save As ...";
saveAs.Click += (obj, ev) => { saveAs.Click += (obj, ev) => {
var dialog = new System.Windows.Forms.FolderBrowserDialog(); var dialog = new System.Windows.Forms.FolderBrowserDialog();
dialog.Description = change.Path; dialog.Description = path;
dialog.ShowNewFolderButton = true; dialog.ShowNewFolderButton = true;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
var savePath = Path.Combine(dialog.SelectedPath, Path.GetFileName(path)); 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); menu.Items.Add(saveAs);
@ -526,7 +526,7 @@ namespace SourceGit.UI {
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
var path = Path.Combine(dialog.SelectedPath, node.Name); 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); menu.Items.Add(saveAs);