optimize<LFS>: do NOT test LFS filter when LFS is not enabled

This commit is contained in:
leo 2021-06-07 14:14:53 +08:00
parent 2cb93d5a86
commit 9d6a411887
10 changed files with 67 additions and 33 deletions

View file

@ -1,16 +0,0 @@
namespace SourceGit.Commands {
/// <summary>
/// 检测目录是否被LFS管理
/// </summary>
public class IsLFSFiltered : Command {
public IsLFSFiltered(string cwd, string path) {
Cwd = cwd;
Args = $"check-attr -a -z \"{path}\"";
}
public bool Result() {
var rs = ReadToEnd();
return rs.Output.Contains("filter\0lfs");
}
}
}

31
src/Commands/LFS.cs Normal file
View file

@ -0,0 +1,31 @@
using System.IO;
namespace SourceGit.Commands {
/// <summary>
/// LFS相关
/// </summary>
public class LFS {
private string repo;
public LFS(string repo) {
this.repo = repo;
}
public bool IsEnabled() {
var path = Path.Combine(repo, ".git", "hooks", "pre-push");
if (!File.Exists(path)) return false;
var content = File.ReadAllText(path);
return content.Contains("git lfs pre-push");
}
public bool IsFiltered(string path) {
var cmd = new Command();
cmd.Cwd = repo;
cmd.Args = $"check-attr -a -z \"{path}\"";
var rs = cmd.ReadToEnd();
return rs.Output.Contains("filter\0lfs");
}
}
}

View file

@ -13,7 +13,7 @@ namespace SourceGit.Commands {
var tmp = Path.GetTempFileName();
var cmd = $"\"{Models.Preference.Instance.Git.Path}\" --no-pager ";
var isLFS = new IsLFSFiltered(repo, path).Result();
var isLFS = new LFS(repo).IsFiltered(path);
if (isLFS) {
cmd += $"show {sha}:\"{path}\" > {tmp}.lfs\n";
cmd += $"\"{Models.Preference.Instance.Git.Path}\" --no-pager lfs smudge < {tmp}.lfs > \"{saveTo}\"\n";

View file

@ -49,7 +49,7 @@ namespace SourceGit.Views {
txtFile.Text = $"{file}@{revision.Substring(0, 8)}";
Task.Run(() => {
var lfs = new Commands.IsLFSFiltered(repo, file).Result();
var lfs = new Commands.LFS(repo).IsFiltered(file);
if (lfs) {
Dispatcher.Invoke(() => {
loading.IsAnimating = false;

View file

@ -11,10 +11,12 @@ namespace SourceGit.Views {
public partial class Histories : Window {
private string repo = null;
private string file = null;
private bool isLFSEnabled = false;
public Histories(string repo, string file) {
this.repo = repo;
this.file = file;
this.isLFSEnabled = new Commands.LFS(repo).IsEnabled();
InitializeComponent();
@ -57,7 +59,8 @@ namespace SourceGit.Views {
diffViewer.Diff(repo, new Widgets.DiffViewer.Option() {
RevisionRange = new string[] { start, commit.SHA },
Path = file
Path = file,
UseLFS = isLFSEnabled,
});
}

View file

@ -17,6 +17,7 @@ namespace SourceGit.Views.Widgets {
private List<Models.Change> cachedChanges = new List<Models.Change>();
private string filter = null;
private bool isSelecting = false;
private bool isLFSEnabled = false;
public class ChangeNode {
public string Path { get; set; } = "";
@ -34,6 +35,7 @@ namespace SourceGit.Views.Widgets {
this.repo = repo;
this.range = range;
this.cachedChanges = changes;
this.isLFSEnabled = new Commands.LFS(repo).IsEnabled();
UpdateVisible();
}
@ -214,7 +216,8 @@ namespace SourceGit.Views.Widgets {
diffViewer.Diff(repo, new DiffViewer.Option() {
RevisionRange = revisions,
Path = change.Path,
OrgPath = change.OriginalPath
OrgPath = change.OriginalPath,
UseLFS = isLFSEnabled,
});
}

View file

@ -25,6 +25,7 @@ namespace SourceGit.Views.Widgets {
public string Path = "";
public string OrgPath = null;
public string ExtraArgs = "";
public bool UseLFS = false;
}
public class Block {
@ -112,15 +113,17 @@ namespace SourceGit.Views.Widgets {
if (!string.IsNullOrEmpty(opt.OrgPath)) args += $"\"{opt.OrgPath}\" ";
args += $"\"{opt.Path}\"";
var isLFSObject = new Commands.IsLFSFiltered(repo, opt.Path).Result();
if (isLFSObject) {
var lc = new Commands.QueryLFSObjectChange(repo, args).Result();
if (lc.IsValid) {
SetLFSChange(lc, dummy);
} else {
SetSame(dummy);
if (opt.UseLFS) {
var isLFSObject = new Commands.LFS(repo).IsFiltered(opt.Path);
if (isLFSObject) {
var lc = new Commands.QueryLFSObjectChange(repo, args).Result();
if (lc.IsValid) {
SetLFSChange(lc, dummy);
} else {
SetSame(dummy);
}
return;
}
return;
}
var rs = new Commands.Diff(repo, args).Result();

View file

@ -17,6 +17,7 @@ namespace SourceGit.Views.Widgets {
public partial class RevisionFiles : UserControl {
private string repo = null;
private string sha = null;
private bool isLFSEnabled = false;
/// <summary>
/// 文件列表树节点
@ -37,6 +38,7 @@ namespace SourceGit.Views.Widgets {
public void SetData(string repo, string sha, Commands.Context cancelToken) {
this.repo = repo;
this.sha = sha;
this.isLFSEnabled = new Commands.LFS(repo).IsEnabled();
var cmd = new Commands.RevisionObjects(repo, sha) { Ctx = cancelToken };
Task.Run(() => {
@ -220,7 +222,7 @@ namespace SourceGit.Views.Widgets {
layerImagePreview.Visibility = Visibility.Visible;
imgPreviewData.Source = new BitmapImage(new Uri(tmp, UriKind.Absolute));
} else if (new Commands.IsLFSFiltered(repo, node.Path).Result()) {
} else if (isLFSEnabled && new Commands.LFS(repo).IsFiltered(node.Path)) {
var lfs = new Commands.QueryLFSObject(repo, sha, node.Path).Result();
layerRevisionPreview.Visibility = Visibility.Visible;
iconRevisionPreview.Data = FindResource("Icon.LFS") as Geometry;

View file

@ -12,9 +12,11 @@ namespace SourceGit.Views.Widgets {
public partial class Stashes : UserControl {
private string repo = null;
private string selected = null;
private bool isLFSEnabled = false;
public Stashes(string repo) {
this.repo = repo;
this.isLFSEnabled = new Commands.LFS(repo).IsEnabled();
InitializeComponent();
}
@ -44,7 +46,8 @@ namespace SourceGit.Views.Widgets {
diffViewer.Diff(repo, new DiffViewer.Option() {
RevisionRange = new string[] { selected + "^", selected },
Path = change.Path,
OrgPath = change.OriginalPath
OrgPath = change.OriginalPath,
UseLFS = isLFSEnabled,
});
}

View file

@ -11,11 +11,13 @@ namespace SourceGit.Views.Widgets {
/// </summary>
public partial class WorkingCopy : UserControl {
private Models.Repository repo = null;
private bool isLFSEnabled = false;
public string CommitMessage { get; set; }
public WorkingCopy(Models.Repository repo) {
this.repo = repo;
this.isLFSEnabled = new Commands.LFS(repo.Path).IsEnabled();
InitializeComponent();
@ -136,13 +138,15 @@ namespace SourceGit.Views.Widgets {
diffViewer.Diff(repo.Path, new DiffViewer.Option() {
ExtraArgs = "--no-index",
Path = change.Path,
OrgPath = "/dev/null"
OrgPath = "/dev/null",
UseLFS = isLFSEnabled
});
break;
default:
diffViewer.Diff(repo.Path, new DiffViewer.Option() {
Path = change.Path,
OrgPath = change.OriginalPath
OrgPath = change.OriginalPath,
UseLFS = isLFSEnabled
});
break;
}
@ -150,7 +154,8 @@ namespace SourceGit.Views.Widgets {
diffViewer.Diff(repo.Path, new DiffViewer.Option() {
ExtraArgs = "--cached",
Path = change.Path,
OrgPath = change.OriginalPath
OrgPath = change.OriginalPath,
UseLFS = isLFSEnabled
});
}
}