refactor: rewrite lfs pointer detection and image loading

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-06-05 21:06:31 +08:00
parent eebadd67a1
commit a023a9259b
No known key found for this signature in database
14 changed files with 286 additions and 199 deletions

View file

@ -1,13 +1,11 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Media.Imaging;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
@ -103,9 +101,7 @@ namespace SourceGit.ViewModels
set
{
if (SetProperty(ref _searchChangeFilter, value))
{
RefreshVisibleChanges();
}
}
}
@ -205,14 +201,11 @@ namespace SourceGit.ViewModels
var isBinary = new Commands.IsBinary(_repo.FullPath, _commit.SHA, file.Path).Result();
if (isBinary)
{
var ext = Path.GetExtension(file.Path);
if (IMG_EXTS.Contains(ext))
var imgDecoder = ImageSource.GetDecoder(file.Path);
if (imgDecoder != Models.ImageDecoder.None)
{
var stream = Commands.QueryFileContent.Run(_repo.FullPath, _commit.SHA, file.Path);
var fileSize = stream.Length;
var bitmap = fileSize > 0 ? new Bitmap(stream) : null;
var imageType = ext!.Substring(1).ToUpper(CultureInfo.CurrentCulture);
var image = new Models.RevisionImageFile() { Image = bitmap, FileSize = fileSize, ImageType = imageType };
var source = ImageSource.FromRevision(_repo.FullPath, _commit.SHA, file.Path, imgDecoder);
var image = new Models.RevisionImageFile(file.Path, source.Bitmap, source.Size);
Dispatcher.UIThread.Invoke(() => ViewRevisionFileContent = image);
}
else
@ -227,13 +220,20 @@ namespace SourceGit.ViewModels
var contentStream = Commands.QueryFileContent.Run(_repo.FullPath, _commit.SHA, file.Path);
var content = new StreamReader(contentStream).ReadToEnd();
var matchLFS = REG_LFS_FORMAT().Match(content);
if (matchLFS.Success)
var lfs = Models.LFSObject.Parse(content);
if (lfs != null)
{
var obj = new Models.RevisionLFSObject() { Object = new Models.LFSObject() };
obj.Object.Oid = matchLFS.Groups[1].Value;
obj.Object.Size = long.Parse(matchLFS.Groups[2].Value);
Dispatcher.UIThread.Invoke(() => ViewRevisionFileContent = obj);
var imgDecoder = ImageSource.GetDecoder(file.Path);
if (imgDecoder != Models.ImageDecoder.None)
{
var combined = new RevisionLFSImage(_repo.FullPath, file.Path, lfs, imgDecoder);
Dispatcher.UIThread.Invoke(() => ViewRevisionFileContent = combined);
}
else
{
var rlfs = new Models.RevisionLFSObject() { Object = lfs };
Dispatcher.UIThread.Invoke(() => ViewRevisionFileContent = rlfs);
}
}
else
{
@ -246,29 +246,15 @@ namespace SourceGit.ViewModels
Task.Run(() =>
{
var submoduleRoot = Path.Combine(_repo.FullPath, file.Path);
var commit = new Commands.QuerySingleCommit(submoduleRoot, file.SHA).Result();
if (commit != null)
var commit = new Commands.QuerySingleCommit(submoduleRoot, _commit.SHA).Result();
var message = commit != null ? new Commands.QueryCommitFullMessage(submoduleRoot, _commit.SHA).Result() : null;
var module = new Models.RevisionSubmodule()
{
var body = new Commands.QueryCommitFullMessage(submoduleRoot, file.SHA).Result();
var submodule = new Models.RevisionSubmodule()
{
Commit = commit,
FullMessage = new Models.CommitFullMessage { Message = body }
};
Commit = commit ?? new Models.Commit() { SHA = _commit.SHA },
FullMessage = new Models.CommitFullMessage { Message = message }
};
Dispatcher.UIThread.Invoke(() => ViewRevisionFileContent = submodule);
}
else
{
Dispatcher.UIThread.Invoke(() =>
{
ViewRevisionFileContent = new Models.RevisionSubmodule()
{
Commit = new Models.Commit() { SHA = file.SHA },
FullMessage = null,
};
});
}
Dispatcher.UIThread.Invoke(() => ViewRevisionFileContent = module);
});
break;
default:
@ -897,14 +883,6 @@ namespace SourceGit.ViewModels
[GeneratedRegex(@"\b([0-9a-fA-F]{6,40})\b")]
private static partial Regex REG_SHA_FORMAT();
[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>()
{
".ico", ".bmp", ".jpg", ".png", ".jpeg", ".webp"
};
private Repository _repo = null;
private Models.Commit _commit = null;
private Models.CommitFullMessage _fullMessage = null;

View file

@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Avalonia.Media.Imaging;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
@ -113,9 +111,6 @@ namespace SourceGit.ViewModels
Task.Run(() =>
{
// NOTE: Here we override the UnifiedLines value (if UseFullTextDiff is on).
// There is no way to tell a git-diff to use "ALL lines of context",
// so instead we set a very high number for the "lines of context" parameter.
var numLines = Preferences.Instance.UseFullTextDiff ? 999999999 : _unifiedLines;
var ignoreWS = Preferences.Instance.IgnoreWhitespaceChangesInDiff;
var latest = new Commands.Diff(_repo, _option, numLines, ignoreWS).Result();
@ -164,28 +159,39 @@ namespace SourceGit.ViewModels
else if (latest.IsBinary)
{
var oldPath = string.IsNullOrEmpty(_option.OrgPath) ? _option.Path : _option.OrgPath;
var ext = Path.GetExtension(_option.Path);
var imgDecoder = ImageSource.GetDecoder(_option.Path);
if (IMG_EXTS.Contains(ext))
if (imgDecoder != Models.ImageDecoder.None)
{
var imgDiff = new Models.ImageDiff();
if (_option.Revisions.Count == 2)
{
(imgDiff.Old, imgDiff.OldFileSize) = BitmapFromRevisionFile(_repo, _option.Revisions[0], oldPath);
(imgDiff.New, imgDiff.NewFileSize) = BitmapFromRevisionFile(_repo, _option.Revisions[1], _option.Path);
var oldImage = ImageSource.FromRevision(_repo, _option.Revisions[0], oldPath, imgDecoder);
var newImage = ImageSource.FromRevision(_repo, _option.Revisions[1], _option.Path, imgDecoder);
imgDiff.Old = oldImage.Bitmap;
imgDiff.OldFileSize = oldImage.Size;
imgDiff.New = newImage.Bitmap;
imgDiff.NewFileSize = newImage.Size;
}
else
{
if (!oldPath.Equals("/dev/null", StringComparison.Ordinal))
(imgDiff.Old, imgDiff.OldFileSize) = BitmapFromRevisionFile(_repo, "HEAD", oldPath);
{
var oldImage = ImageSource.FromRevision(_repo, "HEAD", oldPath, imgDecoder);
imgDiff.Old = oldImage.Bitmap;
imgDiff.OldFileSize = oldImage.Size;
}
var fullPath = Path.Combine(_repo, _option.Path);
if (File.Exists(fullPath))
{
imgDiff.New = new Bitmap(fullPath);
imgDiff.NewFileSize = new FileInfo(fullPath).Length;
var newImage = ImageSource.FromFile(fullPath, imgDecoder);
imgDiff.New = newImage.Bitmap;
imgDiff.NewFileSize = newImage.Size;
}
}
rs = imgDiff;
}
else
@ -207,8 +213,9 @@ namespace SourceGit.ViewModels
}
else if (latest.IsLFS)
{
if (IMG_EXTS.Contains(Path.GetExtension(_option.Path) ?? ".invalid"))
rs = new LFSImageDiff(_repo, latest.LFSDiff);
var imgDecoder = ImageSource.GetDecoder(_option.Path);
if (imgDecoder != Models.ImageDecoder.None)
rs = new LFSImageDiff(_repo, latest.LFSDiff, imgDecoder);
else
rs = latest.LFSDiff;
}
@ -229,13 +236,6 @@ namespace SourceGit.ViewModels
});
}
private (Bitmap, long) BitmapFromRevisionFile(string repo, string revision, string file)
{
var stream = Commands.QueryFileContent.Run(repo, revision, file);
var size = stream.Length;
return size > 0 ? (new Bitmap(stream), size) : (null, size);
}
private Models.RevisionSubmodule QuerySubmoduleRevision(string repo, string sha)
{
var commit = new Commands.QuerySingleCommit(repo, sha).Result();
@ -256,11 +256,6 @@ namespace SourceGit.ViewModels
};
}
private static readonly HashSet<string> IMG_EXTS = new HashSet<string>()
{
".ico", ".bmp", ".jpg", ".png", ".jpeg", ".webp"
};
private class Info
{
public string Argument { get; set; }

View file

@ -1,11 +1,8 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Avalonia.Collections;
using Avalonia.Media.Imaging;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
@ -18,7 +15,7 @@ namespace SourceGit.ViewModels
public object Content { get; set; } = content;
}
public partial class FileHistoriesSingleRevision : ObservableObject
public class FileHistoriesSingleRevision : ObservableObject
{
public bool IsDiffMode
{
@ -78,14 +75,11 @@ namespace SourceGit.ViewModels
var isBinary = new Commands.IsBinary(_repo.FullPath, _revision.SHA, _file).Result();
if (isBinary)
{
var ext = Path.GetExtension(_file);
if (IMG_EXTS.Contains(ext))
var imgDecoder = ImageSource.GetDecoder(_file);
if (imgDecoder != Models.ImageDecoder.None)
{
var stream = Commands.QueryFileContent.Run(_repo.FullPath, _revision.SHA, _file);
var fileSize = stream.Length;
var bitmap = fileSize > 0 ? new Bitmap(stream) : null;
var imageType = Path.GetExtension(_file)!.TrimStart('.').ToUpper(CultureInfo.CurrentCulture);
var image = new Models.RevisionImageFile() { Image = bitmap, FileSize = fileSize, ImageType = imageType };
var source = ImageSource.FromRevision(_repo.FullPath, _revision.SHA, _file, imgDecoder);
var image = new Models.RevisionImageFile(_file, source.Bitmap, source.Size);
Dispatcher.UIThread.Invoke(() => ViewContent = new FileHistoriesRevisionFile(_file, image));
}
else
@ -100,13 +94,20 @@ namespace SourceGit.ViewModels
var contentStream = Commands.QueryFileContent.Run(_repo.FullPath, _revision.SHA, _file);
var content = new StreamReader(contentStream).ReadToEnd();
var matchLFS = REG_LFS_FORMAT().Match(content);
if (matchLFS.Success)
var lfs = Models.LFSObject.Parse(content);
if (lfs != null)
{
var lfs = new Models.RevisionLFSObject() { Object = new() };
lfs.Object.Oid = matchLFS.Groups[1].Value;
lfs.Object.Size = long.Parse(matchLFS.Groups[2].Value);
Dispatcher.UIThread.Invoke(() => ViewContent = new FileHistoriesRevisionFile(_file, lfs));
var imgDecoder = ImageSource.GetDecoder(_file);
if (imgDecoder != Models.ImageDecoder.None)
{
var combined = new RevisionLFSImage(_repo.FullPath, _file, lfs, imgDecoder);
Dispatcher.UIThread.Invoke(() => ViewContent = new FileHistoriesRevisionFile(_file, combined));
}
else
{
var rlfs = new Models.RevisionLFSObject() { Object = lfs };
Dispatcher.UIThread.Invoke(() => ViewContent = new FileHistoriesRevisionFile(_file, rlfs));
}
}
else
{
@ -120,25 +121,14 @@ namespace SourceGit.ViewModels
{
var submoduleRoot = Path.Combine(_repo.FullPath, _file);
var commit = new Commands.QuerySingleCommit(submoduleRoot, obj.SHA).Result();
if (commit != null)
var message = commit != null ? new Commands.QueryCommitFullMessage(submoduleRoot, obj.SHA).Result() : null;
var module = new Models.RevisionSubmodule()
{
var message = new Commands.QueryCommitFullMessage(submoduleRoot, obj.SHA).Result();
var module = new Models.RevisionSubmodule()
{
Commit = commit,
FullMessage = new Models.CommitFullMessage { Message = message }
};
Dispatcher.UIThread.Invoke(() => ViewContent = new FileHistoriesRevisionFile(_file, module));
}
else
{
var module = new Models.RevisionSubmodule()
{
Commit = new Models.Commit() { SHA = obj.SHA },
FullMessage = null
};
Dispatcher.UIThread.Invoke(() => ViewContent = new FileHistoriesRevisionFile(_file, module));
}
Commit = commit ?? new Models.Commit() { SHA = obj.SHA },
FullMessage = new Models.CommitFullMessage { Message = message }
};
Dispatcher.UIThread.Invoke(() => ViewContent = new FileHistoriesRevisionFile(_file, module));
});
break;
default:
@ -153,11 +143,6 @@ namespace SourceGit.ViewModels
ViewContent = new DiffContext(_repo.FullPath, option, _viewContent as DiffContext);
}
[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 = [".ico", ".bmp", ".jpg", ".png", ".jpeg", ".webp"];
private Repository _repo = null;
private string _file = null;
private Models.Commit _revision = null;

View file

@ -0,0 +1,78 @@
using System.IO;
using Avalonia.Media.Imaging;
namespace SourceGit.ViewModels
{
public class ImageSource
{
public Bitmap Bitmap { get; }
public long Size { get; }
public ImageSource(Bitmap bitmap, long size)
{
Bitmap = bitmap;
Size = size;
}
public static Models.ImageDecoder GetDecoder(string file)
{
var ext = Path.GetExtension(file) ?? ".invalid_img";
switch (ext)
{
case ".ico":
case ".bmp":
case ".jpg":
case ".jpeg":
case ".png":
case ".webp":
return Models.ImageDecoder.Builtin;
default:
return Models.ImageDecoder.None;
}
}
public static ImageSource FromFile(string fullpath, Models.ImageDecoder decoder)
{
using (var stream = File.OpenRead(fullpath))
return LoadFromStream(stream, decoder);
}
public static ImageSource FromRevision(string repo, string revision, string file, Models.ImageDecoder decoder)
{
var stream = Commands.QueryFileContent.Run(repo, revision, file);
return LoadFromStream(stream, decoder);
}
public static ImageSource FromLFSObject(string repo, Models.LFSObject lfs, Models.ImageDecoder decoder)
{
if (string.IsNullOrEmpty(lfs.Oid) || lfs.Size == 0)
return new ImageSource(null, 0);
var stream = Commands.QueryFileContent.FromLFS(repo, lfs.Oid, lfs.Size);
return LoadFromStream(stream, decoder);
}
private static ImageSource LoadFromStream(Stream stream, Models.ImageDecoder decoder)
{
var size = stream.Length;
if (size > 0)
{
if (decoder == Models.ImageDecoder.Builtin)
{
try
{
var bitmap = new Bitmap(stream);
return new ImageSource(bitmap, size);
}
catch
{
// Just ignore.
}
}
}
return new ImageSource(null, 0);
}
}
}

View file

@ -1,8 +1,5 @@
using System.Threading.Tasks;
using Avalonia.Media.Imaging;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
@ -20,30 +17,27 @@ namespace SourceGit.ViewModels
private set => SetProperty(ref _image, value);
}
public LFSImageDiff(string repo, Models.LFSDiff lfs)
public LFSImageDiff(string repo, Models.LFSDiff lfs, Models.ImageDecoder decoder)
{
LFS = lfs;
Task.Run(() =>
{
var img = new Models.ImageDiff();
(img.Old, img.OldFileSize) = BitmapFromLFSObject(repo, lfs.Old);
(img.New, img.NewFileSize) = BitmapFromLFSObject(repo, lfs.New);
var oldImage = ImageSource.FromLFSObject(repo, lfs.Old, decoder);
var newImage = ImageSource.FromLFSObject(repo, lfs.New, decoder);
var img = new Models.ImageDiff()
{
Old = oldImage.Bitmap,
OldFileSize = oldImage.Size,
New = newImage.Bitmap,
NewFileSize = newImage.Size
};
Dispatcher.UIThread.Invoke(() => Image = img);
});
}
private (Bitmap, long) BitmapFromLFSObject(string repo, Models.LFSObject lfs)
{
if (string.IsNullOrEmpty(lfs.Oid) || lfs.Size == 0)
return (null, 0);
var stream = Commands.QueryFileContent.FromLFS(repo, lfs.Oid, lfs.Size);
var size = stream.Length;
return size > 0 ? (new Bitmap(stream), size) : (null, size);
}
private Models.ImageDiff _image;
}
}

View file

@ -261,10 +261,10 @@ namespace SourceGit.ViewModels
set => SetProperty(ref _useBlockNavigationInDiffView, value);
}
public int LFSImageDiffActiveIdx
public int LFSImageActiveIdx
{
get => _lfsImageDiffActiveIdx;
set => SetProperty(ref _lfsImageDiffActiveIdx, value);
get => _lfsImageActiveIdx;
set => SetProperty(ref _lfsImageActiveIdx, value);
}
public Models.ChangeViewMode UnstagedChangeViewMode
@ -693,7 +693,7 @@ namespace SourceGit.ViewModels
private bool _showHiddenSymbolsInDiffView = false;
private bool _useFullTextDiff = false;
private bool _useBlockNavigationInDiffView = false;
private int _lfsImageDiffActiveIdx = 0;
private int _lfsImageActiveIdx = 0;
private Models.ChangeViewMode _unstagedChangeViewMode = Models.ChangeViewMode.List;
private Models.ChangeViewMode _stagedChangeViewMode = Models.ChangeViewMode.List;

View file

@ -0,0 +1,34 @@
using System.Threading.Tasks;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class RevisionLFSImage : ObservableObject
{
public Models.RevisionLFSObject LFS
{
get;
}
public Models.RevisionImageFile Image
{
get => _image;
private set => SetProperty(ref _image, value);
}
public RevisionLFSImage(string repo, string file, Models.LFSObject lfs, Models.ImageDecoder decoder)
{
LFS = new Models.RevisionLFSObject() { Object = lfs };
Task.Run(() =>
{
var source = ImageSource.FromLFSObject(repo, lfs, decoder);
var img = new Models.RevisionImageFile(file, source.Bitmap, source.Size);
Dispatcher.UIThread.Invoke(() => Image = img);
});
}
private Models.RevisionImageFile _image = null;
}
}