From 0da30b6b89927145dd0eb82a2490fef8c0239598 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 1 Jul 2024 13:30:57 +0800 Subject: [PATCH] enhance: better LFS file detect method --- src/Commands/IsLFSFiltered.cs | 10 ++++++- src/Commands/QueryFileSize.cs | 3 -- src/Commands/SaveRevisionFile.cs | 2 +- src/ViewModels/CommitDetail.cs | 49 +++++++++++++------------------- 4 files changed, 30 insertions(+), 34 deletions(-) diff --git a/src/Commands/IsLFSFiltered.cs b/src/Commands/IsLFSFiltered.cs index b29039de..2a7234bb 100644 --- a/src/Commands/IsLFSFiltered.cs +++ b/src/Commands/IsLFSFiltered.cs @@ -6,7 +6,15 @@ { WorkingDirectory = repo; Context = repo; - Args = $"check-attr -a -z \"{path}\""; + Args = $"check-attr -z filter \"{path}\""; + RaiseError = false; + } + + public IsLFSFiltered(string repo, string sha, string path) + { + WorkingDirectory = repo; + Context = repo; + Args = $"check-attr --source {sha} -z filter \"{path}\""; RaiseError = false; } diff --git a/src/Commands/QueryFileSize.cs b/src/Commands/QueryFileSize.cs index 5ce7641e..c36984dd 100644 --- a/src/Commands/QueryFileSize.cs +++ b/src/Commands/QueryFileSize.cs @@ -4,7 +4,6 @@ namespace SourceGit.Commands { public partial class QueryFileSize : Command { - [GeneratedRegex(@"^\d+\s+\w+\s+[0-9a-f]+\s+(\d+)\s+.*$")] private static partial Regex REG_FORMAT(); @@ -25,9 +24,7 @@ namespace SourceGit.Commands { var match = REG_FORMAT().Match(rs.StdOut); if (match.Success) - { return long.Parse(match.Groups[1].Value); - } } return 0; diff --git a/src/Commands/SaveRevisionFile.cs b/src/Commands/SaveRevisionFile.cs index 6c200940..99e89093 100644 --- a/src/Commands/SaveRevisionFile.cs +++ b/src/Commands/SaveRevisionFile.cs @@ -10,7 +10,7 @@ namespace SourceGit.Commands { public static void Run(string repo, string revision, string file, string saveTo) { - var isLFSFiltered = new IsLFSFiltered(repo, file).Result(); + var isLFSFiltered = new IsLFSFiltered(repo, revision, file).Result(); if (isLFSFiltered) { var tmpFile = saveTo + ".tmp"; diff --git a/src/ViewModels/CommitDetail.cs b/src/ViewModels/CommitDetail.cs index 6039db68..35014cf4 100644 --- a/src/ViewModels/CommitDetail.cs +++ b/src/ViewModels/CommitDetail.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Text.RegularExpressions; using System.Threading.Tasks; using Avalonia.Controls; @@ -12,7 +13,7 @@ using CommunityToolkit.Mvvm.ComponentModel; namespace SourceGit.ViewModels { - public class CommitDetail : ObservableObject + public partial class CommitDetail : ObservableObject { public DiffContext DiffContext { @@ -164,39 +165,26 @@ namespace SourceGit.ViewModels var contentStream = Commands.QueryFileContent.Run(_repo, _commit.SHA, file.Path); var content = new StreamReader(contentStream).ReadToEnd(); - if (content.StartsWith("version https://git-lfs.github.com/spec/", StringComparison.Ordinal)) + var matchLFS = REG_LFS_FORMAT().Match(content); + if (matchLFS.Success) { var obj = new Models.RevisionLFSObject() { Object = new Models.LFSObject() }; - var lines = content.Split('\n', StringSplitOptions.RemoveEmptyEntries); - if (lines.Length == 3) - { - foreach (var line in lines) - { - if (line.StartsWith("oid sha256:", StringComparison.Ordinal)) - { - obj.Object.Oid = line.Substring(11); - } - else if (line.StartsWith("size ", StringComparison.Ordinal)) - { - obj.Object.Size = long.Parse(line.Substring(5)); - } - } - Dispatcher.UIThread.Invoke(() => - { - ViewRevisionFileContent = obj; - }); - return; - } - } + obj.Object.Oid = matchLFS.Groups[1].Value; + obj.Object.Size = long.Parse(matchLFS.Groups[2].Value); - Dispatcher.UIThread.Invoke(() => + Dispatcher.UIThread.Invoke(() => ViewRevisionFileContent = obj); + } + else { - ViewRevisionFileContent = new Models.RevisionTextFile() + Dispatcher.UIThread.Invoke(() => { - FileName = file.Path, - Content = content - }; - }); + ViewRevisionFileContent = new Models.RevisionTextFile() + { + FileName = file.Path, + Content = content + }; + }); + } }); break; case Models.ObjectType.Commit: @@ -464,6 +452,9 @@ namespace SourceGit.ViewModels } } + [GeneratedRegex(@"^version https://git-lfs.github.com/spec/v\d+\r?\noid sha256:([0-9a-f]+)\r?\nsize (\d+)[\r\n]*$")] + private static partial Regex REG_LFS_FORMAT(); + private static readonly HashSet IMG_EXTS = new HashSet() { ".ico", ".bmp", ".jpg", ".png", ".jpeg"