From 9d6a411887c726691c4d6795f8097f1f1fcc010d Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 7 Jun 2021 14:14:53 +0800 Subject: [PATCH] optimize: do NOT test LFS filter when LFS is not enabled --- src/Commands/IsLFSFiltered.cs | 16 ------------- src/Commands/LFS.cs | 31 +++++++++++++++++++++++++ src/Commands/SaveRevisionFile.cs | 2 +- src/Views/Blame.xaml.cs | 2 +- src/Views/Histories.xaml.cs | 5 +++- src/Views/Widgets/CommitChanges.xaml.cs | 5 +++- src/Views/Widgets/DiffViewer.xaml.cs | 19 ++++++++------- src/Views/Widgets/RevisionFiles.xaml.cs | 4 +++- src/Views/Widgets/Stashes.xaml.cs | 5 +++- src/Views/Widgets/WorkingCopy.xaml.cs | 11 ++++++--- 10 files changed, 67 insertions(+), 33 deletions(-) delete mode 100644 src/Commands/IsLFSFiltered.cs create mode 100644 src/Commands/LFS.cs diff --git a/src/Commands/IsLFSFiltered.cs b/src/Commands/IsLFSFiltered.cs deleted file mode 100644 index 65a4f1f1..00000000 --- a/src/Commands/IsLFSFiltered.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace SourceGit.Commands { - /// - /// 检测目录是否被LFS管理 - /// - 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"); - } - } -} diff --git a/src/Commands/LFS.cs b/src/Commands/LFS.cs new file mode 100644 index 00000000..1faf258b --- /dev/null +++ b/src/Commands/LFS.cs @@ -0,0 +1,31 @@ +using System.IO; + +namespace SourceGit.Commands { + /// + /// LFS相关 + /// + 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"); + } + } +} diff --git a/src/Commands/SaveRevisionFile.cs b/src/Commands/SaveRevisionFile.cs index 8e02d8c5..a83fc618 100644 --- a/src/Commands/SaveRevisionFile.cs +++ b/src/Commands/SaveRevisionFile.cs @@ -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"; diff --git a/src/Views/Blame.xaml.cs b/src/Views/Blame.xaml.cs index 0da2a234..d3af7cdd 100644 --- a/src/Views/Blame.xaml.cs +++ b/src/Views/Blame.xaml.cs @@ -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; diff --git a/src/Views/Histories.xaml.cs b/src/Views/Histories.xaml.cs index 2f25f8c7..97986946 100644 --- a/src/Views/Histories.xaml.cs +++ b/src/Views/Histories.xaml.cs @@ -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, }); } diff --git a/src/Views/Widgets/CommitChanges.xaml.cs b/src/Views/Widgets/CommitChanges.xaml.cs index 8bb12247..10c464a5 100644 --- a/src/Views/Widgets/CommitChanges.xaml.cs +++ b/src/Views/Widgets/CommitChanges.xaml.cs @@ -17,6 +17,7 @@ namespace SourceGit.Views.Widgets { private List cachedChanges = new List(); 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, }); } diff --git a/src/Views/Widgets/DiffViewer.xaml.cs b/src/Views/Widgets/DiffViewer.xaml.cs index 984180c4..f7c1fa94 100644 --- a/src/Views/Widgets/DiffViewer.xaml.cs +++ b/src/Views/Widgets/DiffViewer.xaml.cs @@ -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(); diff --git a/src/Views/Widgets/RevisionFiles.xaml.cs b/src/Views/Widgets/RevisionFiles.xaml.cs index 904ac54b..95ddcb9d 100644 --- a/src/Views/Widgets/RevisionFiles.xaml.cs +++ b/src/Views/Widgets/RevisionFiles.xaml.cs @@ -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; /// /// 文件列表树节点 @@ -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; diff --git a/src/Views/Widgets/Stashes.xaml.cs b/src/Views/Widgets/Stashes.xaml.cs index 36d174e2..a428ab74 100644 --- a/src/Views/Widgets/Stashes.xaml.cs +++ b/src/Views/Widgets/Stashes.xaml.cs @@ -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, }); } diff --git a/src/Views/Widgets/WorkingCopy.xaml.cs b/src/Views/Widgets/WorkingCopy.xaml.cs index 3f293db4..f09da042 100644 --- a/src/Views/Widgets/WorkingCopy.xaml.cs +++ b/src/Views/Widgets/WorkingCopy.xaml.cs @@ -11,11 +11,13 @@ namespace SourceGit.Views.Widgets { /// 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 }); } }