enhance: better LFS file detect method

This commit is contained in:
leo 2024-07-01 13:30:57 +08:00
parent f08acebb5e
commit 0da30b6b89
No known key found for this signature in database
4 changed files with 30 additions and 34 deletions

View file

@ -6,7 +6,15 @@
{ {
WorkingDirectory = repo; WorkingDirectory = repo;
Context = 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; RaiseError = false;
} }

View file

@ -4,7 +4,6 @@ namespace SourceGit.Commands
{ {
public partial class QueryFileSize : Command public partial class QueryFileSize : Command
{ {
[GeneratedRegex(@"^\d+\s+\w+\s+[0-9a-f]+\s+(\d+)\s+.*$")] [GeneratedRegex(@"^\d+\s+\w+\s+[0-9a-f]+\s+(\d+)\s+.*$")]
private static partial Regex REG_FORMAT(); private static partial Regex REG_FORMAT();
@ -25,10 +24,8 @@ namespace SourceGit.Commands
{ {
var match = REG_FORMAT().Match(rs.StdOut); var match = REG_FORMAT().Match(rs.StdOut);
if (match.Success) if (match.Success)
{
return long.Parse(match.Groups[1].Value); return long.Parse(match.Groups[1].Value);
} }
}
return 0; return 0;
} }

View file

@ -10,7 +10,7 @@ namespace SourceGit.Commands
{ {
public static void Run(string repo, string revision, string file, string saveTo) 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) if (isLFSFiltered)
{ {
var tmpFile = saveTo + ".tmp"; var tmpFile = saveTo + ".tmp";

View file

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia.Controls; using Avalonia.Controls;
@ -12,7 +13,7 @@ using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels namespace SourceGit.ViewModels
{ {
public class CommitDetail : ObservableObject public partial class CommitDetail : ObservableObject
{ {
public DiffContext DiffContext public DiffContext DiffContext
{ {
@ -164,31 +165,17 @@ namespace SourceGit.ViewModels
var contentStream = Commands.QueryFileContent.Run(_repo, _commit.SHA, file.Path); var contentStream = Commands.QueryFileContent.Run(_repo, _commit.SHA, file.Path);
var content = new StreamReader(contentStream).ReadToEnd(); 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 obj = new Models.RevisionLFSObject() { Object = new Models.LFSObject() };
var lines = content.Split('\n', StringSplitOptions.RemoveEmptyEntries); obj.Object.Oid = matchLFS.Groups[1].Value;
if (lines.Length == 3) obj.Object.Size = long.Parse(matchLFS.Groups[2].Value);
{
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;
}
}
Dispatcher.UIThread.Invoke(() => ViewRevisionFileContent = obj);
}
else
{
Dispatcher.UIThread.Invoke(() => Dispatcher.UIThread.Invoke(() =>
{ {
ViewRevisionFileContent = new Models.RevisionTextFile() ViewRevisionFileContent = new Models.RevisionTextFile()
@ -197,6 +184,7 @@ namespace SourceGit.ViewModels
Content = content Content = content
}; };
}); });
}
}); });
break; break;
case Models.ObjectType.Commit: 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<string> IMG_EXTS = new HashSet<string>() private static readonly HashSet<string> IMG_EXTS = new HashSet<string>()
{ {
".ico", ".bmp", ".jpg", ".png", ".jpeg" ".ico", ".bmp", ".jpg", ".png", ".jpeg"