mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-06-16 07:55:00 +00:00
refactor: rewrite lfs pointer detection and image loading
Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
parent
eebadd67a1
commit
a023a9259b
14 changed files with 286 additions and 199 deletions
8
src/Models/ImageDecoder.cs
Normal file
8
src/Models/ImageDecoder.cs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
namespace SourceGit.Models
|
||||||
|
{
|
||||||
|
public enum ImageDecoder
|
||||||
|
{
|
||||||
|
None = 0,
|
||||||
|
Builtin,
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,22 @@
|
||||||
namespace SourceGit.Models
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
namespace SourceGit.Models
|
||||||
{
|
{
|
||||||
public class LFSObject
|
public partial class LFSObject
|
||||||
{
|
{
|
||||||
|
[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_FORMAT();
|
||||||
|
|
||||||
public string Oid { get; set; } = string.Empty;
|
public string Oid { get; set; } = string.Empty;
|
||||||
public long Size { get; set; } = 0;
|
public long Size { get; set; } = 0;
|
||||||
|
|
||||||
|
public static LFSObject Parse(string content)
|
||||||
|
{
|
||||||
|
var match = REG_FORMAT().Match(content);
|
||||||
|
if (match.Success)
|
||||||
|
return new() { Oid = match.Groups[1].Value, Size = long.Parse(match.Groups[2].Value) };
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
using Avalonia.Media.Imaging;
|
using System.Globalization;
|
||||||
|
using System.IO;
|
||||||
|
using Avalonia.Media.Imaging;
|
||||||
|
|
||||||
namespace SourceGit.Models
|
namespace SourceGit.Models
|
||||||
{
|
{
|
||||||
|
@ -9,10 +11,17 @@ namespace SourceGit.Models
|
||||||
|
|
||||||
public class RevisionImageFile
|
public class RevisionImageFile
|
||||||
{
|
{
|
||||||
public Bitmap Image { get; set; } = null;
|
public Bitmap Image { get; }
|
||||||
public long FileSize { get; set; } = 0;
|
public long FileSize { get; }
|
||||||
public string ImageType { get; set; } = string.Empty;
|
public string ImageType { get; }
|
||||||
public string ImageSize => Image != null ? $"{Image.PixelSize.Width} x {Image.PixelSize.Height}" : "0 x 0";
|
public string ImageSize => Image != null ? $"{Image.PixelSize.Width} x {Image.PixelSize.Height}" : "0 x 0";
|
||||||
|
|
||||||
|
public RevisionImageFile(string file, Bitmap img, long size)
|
||||||
|
{
|
||||||
|
Image = img;
|
||||||
|
FileSize = size;
|
||||||
|
ImageType = Path.GetExtension(file)!.Substring(1).ToUpper(CultureInfo.CurrentCulture);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class RevisionTextFile
|
public class RevisionTextFile
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Media.Imaging;
|
|
||||||
using Avalonia.Platform.Storage;
|
using Avalonia.Platform.Storage;
|
||||||
using Avalonia.Threading;
|
using Avalonia.Threading;
|
||||||
|
|
||||||
|
@ -103,9 +101,7 @@ namespace SourceGit.ViewModels
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (SetProperty(ref _searchChangeFilter, value))
|
if (SetProperty(ref _searchChangeFilter, value))
|
||||||
{
|
|
||||||
RefreshVisibleChanges();
|
RefreshVisibleChanges();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,14 +201,11 @@ namespace SourceGit.ViewModels
|
||||||
var isBinary = new Commands.IsBinary(_repo.FullPath, _commit.SHA, file.Path).Result();
|
var isBinary = new Commands.IsBinary(_repo.FullPath, _commit.SHA, file.Path).Result();
|
||||||
if (isBinary)
|
if (isBinary)
|
||||||
{
|
{
|
||||||
var ext = Path.GetExtension(file.Path);
|
var imgDecoder = ImageSource.GetDecoder(file.Path);
|
||||||
if (IMG_EXTS.Contains(ext))
|
if (imgDecoder != Models.ImageDecoder.None)
|
||||||
{
|
{
|
||||||
var stream = Commands.QueryFileContent.Run(_repo.FullPath, _commit.SHA, file.Path);
|
var source = ImageSource.FromRevision(_repo.FullPath, _commit.SHA, file.Path, imgDecoder);
|
||||||
var fileSize = stream.Length;
|
var image = new Models.RevisionImageFile(file.Path, source.Bitmap, source.Size);
|
||||||
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 };
|
|
||||||
Dispatcher.UIThread.Invoke(() => ViewRevisionFileContent = image);
|
Dispatcher.UIThread.Invoke(() => ViewRevisionFileContent = image);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -227,13 +220,20 @@ namespace SourceGit.ViewModels
|
||||||
|
|
||||||
var contentStream = Commands.QueryFileContent.Run(_repo.FullPath, _commit.SHA, file.Path);
|
var contentStream = Commands.QueryFileContent.Run(_repo.FullPath, _commit.SHA, file.Path);
|
||||||
var content = new StreamReader(contentStream).ReadToEnd();
|
var content = new StreamReader(contentStream).ReadToEnd();
|
||||||
var matchLFS = REG_LFS_FORMAT().Match(content);
|
var lfs = Models.LFSObject.Parse(content);
|
||||||
if (matchLFS.Success)
|
if (lfs != null)
|
||||||
{
|
{
|
||||||
var obj = new Models.RevisionLFSObject() { Object = new Models.LFSObject() };
|
var imgDecoder = ImageSource.GetDecoder(file.Path);
|
||||||
obj.Object.Oid = matchLFS.Groups[1].Value;
|
if (imgDecoder != Models.ImageDecoder.None)
|
||||||
obj.Object.Size = long.Parse(matchLFS.Groups[2].Value);
|
{
|
||||||
Dispatcher.UIThread.Invoke(() => ViewRevisionFileContent = obj);
|
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
|
else
|
||||||
{
|
{
|
||||||
|
@ -246,29 +246,15 @@ namespace SourceGit.ViewModels
|
||||||
Task.Run(() =>
|
Task.Run(() =>
|
||||||
{
|
{
|
||||||
var submoduleRoot = Path.Combine(_repo.FullPath, file.Path);
|
var submoduleRoot = Path.Combine(_repo.FullPath, file.Path);
|
||||||
var commit = new Commands.QuerySingleCommit(submoduleRoot, file.SHA).Result();
|
var commit = new Commands.QuerySingleCommit(submoduleRoot, _commit.SHA).Result();
|
||||||
if (commit != null)
|
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();
|
Commit = commit ?? new Models.Commit() { SHA = _commit.SHA },
|
||||||
var submodule = new Models.RevisionSubmodule()
|
FullMessage = new Models.CommitFullMessage { Message = message }
|
||||||
{
|
};
|
||||||
Commit = commit,
|
|
||||||
FullMessage = new Models.CommitFullMessage { Message = body }
|
|
||||||
};
|
|
||||||
|
|
||||||
Dispatcher.UIThread.Invoke(() => ViewRevisionFileContent = submodule);
|
Dispatcher.UIThread.Invoke(() => ViewRevisionFileContent = module);
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Dispatcher.UIThread.Invoke(() =>
|
|
||||||
{
|
|
||||||
ViewRevisionFileContent = new Models.RevisionSubmodule()
|
|
||||||
{
|
|
||||||
Commit = new Models.Commit() { SHA = file.SHA },
|
|
||||||
FullMessage = null,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -897,14 +883,6 @@ namespace SourceGit.ViewModels
|
||||||
[GeneratedRegex(@"\b([0-9a-fA-F]{6,40})\b")]
|
[GeneratedRegex(@"\b([0-9a-fA-F]{6,40})\b")]
|
||||||
private static partial Regex REG_SHA_FORMAT();
|
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 Repository _repo = null;
|
||||||
private Models.Commit _commit = null;
|
private Models.Commit _commit = null;
|
||||||
private Models.CommitFullMessage _fullMessage = null;
|
private Models.CommitFullMessage _fullMessage = null;
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using Avalonia.Media.Imaging;
|
|
||||||
using Avalonia.Threading;
|
using Avalonia.Threading;
|
||||||
|
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
@ -113,9 +111,6 @@ namespace SourceGit.ViewModels
|
||||||
|
|
||||||
Task.Run(() =>
|
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 numLines = Preferences.Instance.UseFullTextDiff ? 999999999 : _unifiedLines;
|
||||||
var ignoreWS = Preferences.Instance.IgnoreWhitespaceChangesInDiff;
|
var ignoreWS = Preferences.Instance.IgnoreWhitespaceChangesInDiff;
|
||||||
var latest = new Commands.Diff(_repo, _option, numLines, ignoreWS).Result();
|
var latest = new Commands.Diff(_repo, _option, numLines, ignoreWS).Result();
|
||||||
|
@ -164,28 +159,39 @@ namespace SourceGit.ViewModels
|
||||||
else if (latest.IsBinary)
|
else if (latest.IsBinary)
|
||||||
{
|
{
|
||||||
var oldPath = string.IsNullOrEmpty(_option.OrgPath) ? _option.Path : _option.OrgPath;
|
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();
|
var imgDiff = new Models.ImageDiff();
|
||||||
|
|
||||||
if (_option.Revisions.Count == 2)
|
if (_option.Revisions.Count == 2)
|
||||||
{
|
{
|
||||||
(imgDiff.Old, imgDiff.OldFileSize) = BitmapFromRevisionFile(_repo, _option.Revisions[0], oldPath);
|
var oldImage = ImageSource.FromRevision(_repo, _option.Revisions[0], oldPath, imgDecoder);
|
||||||
(imgDiff.New, imgDiff.NewFileSize) = BitmapFromRevisionFile(_repo, _option.Revisions[1], _option.Path);
|
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
|
else
|
||||||
{
|
{
|
||||||
if (!oldPath.Equals("/dev/null", StringComparison.Ordinal))
|
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);
|
var fullPath = Path.Combine(_repo, _option.Path);
|
||||||
if (File.Exists(fullPath))
|
if (File.Exists(fullPath))
|
||||||
{
|
{
|
||||||
imgDiff.New = new Bitmap(fullPath);
|
var newImage = ImageSource.FromFile(fullPath, imgDecoder);
|
||||||
imgDiff.NewFileSize = new FileInfo(fullPath).Length;
|
imgDiff.New = newImage.Bitmap;
|
||||||
|
imgDiff.NewFileSize = newImage.Size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rs = imgDiff;
|
rs = imgDiff;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -207,8 +213,9 @@ namespace SourceGit.ViewModels
|
||||||
}
|
}
|
||||||
else if (latest.IsLFS)
|
else if (latest.IsLFS)
|
||||||
{
|
{
|
||||||
if (IMG_EXTS.Contains(Path.GetExtension(_option.Path) ?? ".invalid"))
|
var imgDecoder = ImageSource.GetDecoder(_option.Path);
|
||||||
rs = new LFSImageDiff(_repo, latest.LFSDiff);
|
if (imgDecoder != Models.ImageDecoder.None)
|
||||||
|
rs = new LFSImageDiff(_repo, latest.LFSDiff, imgDecoder);
|
||||||
else
|
else
|
||||||
rs = latest.LFSDiff;
|
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)
|
private Models.RevisionSubmodule QuerySubmoduleRevision(string repo, string sha)
|
||||||
{
|
{
|
||||||
var commit = new Commands.QuerySingleCommit(repo, sha).Result();
|
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
|
private class Info
|
||||||
{
|
{
|
||||||
public string Argument { get; set; }
|
public string Argument { get; set; }
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using Avalonia.Collections;
|
using Avalonia.Collections;
|
||||||
using Avalonia.Media.Imaging;
|
|
||||||
using Avalonia.Threading;
|
using Avalonia.Threading;
|
||||||
|
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
@ -18,7 +15,7 @@ namespace SourceGit.ViewModels
|
||||||
public object Content { get; set; } = content;
|
public object Content { get; set; } = content;
|
||||||
}
|
}
|
||||||
|
|
||||||
public partial class FileHistoriesSingleRevision : ObservableObject
|
public class FileHistoriesSingleRevision : ObservableObject
|
||||||
{
|
{
|
||||||
public bool IsDiffMode
|
public bool IsDiffMode
|
||||||
{
|
{
|
||||||
|
@ -78,14 +75,11 @@ namespace SourceGit.ViewModels
|
||||||
var isBinary = new Commands.IsBinary(_repo.FullPath, _revision.SHA, _file).Result();
|
var isBinary = new Commands.IsBinary(_repo.FullPath, _revision.SHA, _file).Result();
|
||||||
if (isBinary)
|
if (isBinary)
|
||||||
{
|
{
|
||||||
var ext = Path.GetExtension(_file);
|
var imgDecoder = ImageSource.GetDecoder(_file);
|
||||||
if (IMG_EXTS.Contains(ext))
|
if (imgDecoder != Models.ImageDecoder.None)
|
||||||
{
|
{
|
||||||
var stream = Commands.QueryFileContent.Run(_repo.FullPath, _revision.SHA, _file);
|
var source = ImageSource.FromRevision(_repo.FullPath, _revision.SHA, _file, imgDecoder);
|
||||||
var fileSize = stream.Length;
|
var image = new Models.RevisionImageFile(_file, source.Bitmap, source.Size);
|
||||||
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 };
|
|
||||||
Dispatcher.UIThread.Invoke(() => ViewContent = new FileHistoriesRevisionFile(_file, image));
|
Dispatcher.UIThread.Invoke(() => ViewContent = new FileHistoriesRevisionFile(_file, image));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -100,13 +94,20 @@ namespace SourceGit.ViewModels
|
||||||
|
|
||||||
var contentStream = Commands.QueryFileContent.Run(_repo.FullPath, _revision.SHA, _file);
|
var contentStream = Commands.QueryFileContent.Run(_repo.FullPath, _revision.SHA, _file);
|
||||||
var content = new StreamReader(contentStream).ReadToEnd();
|
var content = new StreamReader(contentStream).ReadToEnd();
|
||||||
var matchLFS = REG_LFS_FORMAT().Match(content);
|
var lfs = Models.LFSObject.Parse(content);
|
||||||
if (matchLFS.Success)
|
if (lfs != null)
|
||||||
{
|
{
|
||||||
var lfs = new Models.RevisionLFSObject() { Object = new() };
|
var imgDecoder = ImageSource.GetDecoder(_file);
|
||||||
lfs.Object.Oid = matchLFS.Groups[1].Value;
|
if (imgDecoder != Models.ImageDecoder.None)
|
||||||
lfs.Object.Size = long.Parse(matchLFS.Groups[2].Value);
|
{
|
||||||
Dispatcher.UIThread.Invoke(() => ViewContent = new FileHistoriesRevisionFile(_file, lfs));
|
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
|
else
|
||||||
{
|
{
|
||||||
|
@ -120,25 +121,14 @@ namespace SourceGit.ViewModels
|
||||||
{
|
{
|
||||||
var submoduleRoot = Path.Combine(_repo.FullPath, _file);
|
var submoduleRoot = Path.Combine(_repo.FullPath, _file);
|
||||||
var commit = new Commands.QuerySingleCommit(submoduleRoot, obj.SHA).Result();
|
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();
|
Commit = commit ?? new Models.Commit() { SHA = obj.SHA },
|
||||||
var module = new Models.RevisionSubmodule()
|
FullMessage = new Models.CommitFullMessage { Message = message }
|
||||||
{
|
};
|
||||||
Commit = commit,
|
|
||||||
FullMessage = new Models.CommitFullMessage { Message = message }
|
Dispatcher.UIThread.Invoke(() => ViewContent = new FileHistoriesRevisionFile(_file, module));
|
||||||
};
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -153,11 +143,6 @@ namespace SourceGit.ViewModels
|
||||||
ViewContent = new DiffContext(_repo.FullPath, option, _viewContent as DiffContext);
|
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 Repository _repo = null;
|
||||||
private string _file = null;
|
private string _file = null;
|
||||||
private Models.Commit _revision = null;
|
private Models.Commit _revision = null;
|
||||||
|
|
78
src/ViewModels/ImageSource.cs
Normal file
78
src/ViewModels/ImageSource.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,5 @@
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using Avalonia.Media.Imaging;
|
|
||||||
using Avalonia.Threading;
|
using Avalonia.Threading;
|
||||||
|
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
|
||||||
namespace SourceGit.ViewModels
|
namespace SourceGit.ViewModels
|
||||||
|
@ -20,30 +17,27 @@ namespace SourceGit.ViewModels
|
||||||
private set => SetProperty(ref _image, value);
|
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;
|
LFS = lfs;
|
||||||
|
|
||||||
Task.Run(() =>
|
Task.Run(() =>
|
||||||
{
|
{
|
||||||
var img = new Models.ImageDiff();
|
var oldImage = ImageSource.FromLFSObject(repo, lfs.Old, decoder);
|
||||||
(img.Old, img.OldFileSize) = BitmapFromLFSObject(repo, lfs.Old);
|
var newImage = ImageSource.FromLFSObject(repo, lfs.New, decoder);
|
||||||
(img.New, img.NewFileSize) = BitmapFromLFSObject(repo, lfs.New);
|
|
||||||
|
var img = new Models.ImageDiff()
|
||||||
|
{
|
||||||
|
Old = oldImage.Bitmap,
|
||||||
|
OldFileSize = oldImage.Size,
|
||||||
|
New = newImage.Bitmap,
|
||||||
|
NewFileSize = newImage.Size
|
||||||
|
};
|
||||||
|
|
||||||
Dispatcher.UIThread.Invoke(() => Image = img);
|
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;
|
private Models.ImageDiff _image;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -261,10 +261,10 @@ namespace SourceGit.ViewModels
|
||||||
set => SetProperty(ref _useBlockNavigationInDiffView, value);
|
set => SetProperty(ref _useBlockNavigationInDiffView, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int LFSImageDiffActiveIdx
|
public int LFSImageActiveIdx
|
||||||
{
|
{
|
||||||
get => _lfsImageDiffActiveIdx;
|
get => _lfsImageActiveIdx;
|
||||||
set => SetProperty(ref _lfsImageDiffActiveIdx, value);
|
set => SetProperty(ref _lfsImageActiveIdx, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Models.ChangeViewMode UnstagedChangeViewMode
|
public Models.ChangeViewMode UnstagedChangeViewMode
|
||||||
|
@ -693,7 +693,7 @@ namespace SourceGit.ViewModels
|
||||||
private bool _showHiddenSymbolsInDiffView = false;
|
private bool _showHiddenSymbolsInDiffView = false;
|
||||||
private bool _useFullTextDiff = false;
|
private bool _useFullTextDiff = false;
|
||||||
private bool _useBlockNavigationInDiffView = false;
|
private bool _useBlockNavigationInDiffView = false;
|
||||||
private int _lfsImageDiffActiveIdx = 0;
|
private int _lfsImageActiveIdx = 0;
|
||||||
|
|
||||||
private Models.ChangeViewMode _unstagedChangeViewMode = Models.ChangeViewMode.List;
|
private Models.ChangeViewMode _unstagedChangeViewMode = Models.ChangeViewMode.List;
|
||||||
private Models.ChangeViewMode _stagedChangeViewMode = Models.ChangeViewMode.List;
|
private Models.ChangeViewMode _stagedChangeViewMode = Models.ChangeViewMode.List;
|
||||||
|
|
34
src/ViewModels/RevisionLFSImage.cs
Normal file
34
src/ViewModels/RevisionLFSImage.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -224,7 +224,29 @@
|
||||||
|
|
||||||
<!-- LFS Diff -->
|
<!-- LFS Diff -->
|
||||||
<DataTemplate DataType="m:LFSDiff">
|
<DataTemplate DataType="m:LFSDiff">
|
||||||
<v:LFSDiffView/>
|
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
|
||||||
|
<TextBlock Text="{DynamicResource Text.Diff.LFS}"
|
||||||
|
Margin="0,0,0,32"
|
||||||
|
FontSize="18" FontWeight="Bold"
|
||||||
|
Foreground="{DynamicResource Brush.FG2}"
|
||||||
|
HorizontalAlignment="Center"/>
|
||||||
|
<Path Width="64" Height="64" Data="{StaticResource Icons.LFS}" Fill="{DynamicResource Brush.FG2}"/>
|
||||||
|
<Grid Margin="0,16,0,0" HorizontalAlignment="Center" RowDefinitions="32,32" ColumnDefinitions="Auto,Auto,Auto">
|
||||||
|
<Border Grid.Row="0" Grid.Column="0" Height="16" Background="{DynamicResource Brush.Badge}" CornerRadius="8" VerticalAlignment="Center">
|
||||||
|
<TextBlock Classes="primary" Text="{DynamicResource Text.Diff.Binary.Old}" Margin="8,0" FontSize="10" Foreground="{DynamicResource Brush.BadgeFG}"/>
|
||||||
|
</Border>
|
||||||
|
|
||||||
|
<TextBlock Grid.Row="0" Grid.Column="1" Classes="primary" Text="{Binding Old.Size}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Right" FontSize="16" Margin="8,0"/>
|
||||||
|
<TextBlock Grid.Row="0" Grid.Column="2" Classes="primary" Text="{DynamicResource Text.Bytes}" Foreground="{DynamicResource Brush.FG2}" FontSize="16"/>
|
||||||
|
|
||||||
|
<Border Grid.Row="1" Grid.Column="0" Height="16" Background="Green" CornerRadius="8" VerticalAlignment="Center">
|
||||||
|
<TextBlock Classes="primary" Text="{DynamicResource Text.Diff.Binary.New}" Margin="8,0" FontSize="10"/>
|
||||||
|
</Border>
|
||||||
|
|
||||||
|
<TextBlock Grid.Row="1" Grid.Column="1" Classes="primary" Text="{Binding New.Size}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Right" FontSize="16" Margin="8,0"/>
|
||||||
|
<TextBlock Grid.Row="1" Grid.Column="2" Classes="primary" Text="{DynamicResource Text.Bytes}" Foreground="{DynamicResource Brush.FG2}" FontSize="16"/>
|
||||||
|
</Grid>
|
||||||
|
</StackPanel>
|
||||||
</DataTemplate>
|
</DataTemplate>
|
||||||
|
|
||||||
<!-- Submodule Diff -->
|
<!-- Submodule Diff -->
|
||||||
|
@ -282,7 +304,7 @@
|
||||||
|
|
||||||
<!-- LFS Image Diff -->
|
<!-- LFS Image Diff -->
|
||||||
<DataTemplate DataType="vm:LFSImageDiff">
|
<DataTemplate DataType="vm:LFSImageDiff">
|
||||||
<TabControl Margin="0,8,0,0" SelectedIndex="{Binding Source={x:Static vm:Preferences.Instance}, Path=LFSImageDiffActiveIdx, Mode=TwoWay}">
|
<TabControl Margin="0,8,0,0" SelectedIndex="{Binding Source={x:Static vm:Preferences.Instance}, Path=LFSImageActiveIdx, Mode=TwoWay}">
|
||||||
<TabControl.Styles>
|
<TabControl.Styles>
|
||||||
<Style Selector="TabControl /template/ ItemsPresenter#PART_ItemsPresenter > WrapPanel">
|
<Style Selector="TabControl /template/ ItemsPresenter#PART_ItemsPresenter > WrapPanel">
|
||||||
<Setter Property="HorizontalAlignment" Value="Center"/>
|
<Setter Property="HorizontalAlignment" Value="Center"/>
|
||||||
|
@ -294,13 +316,7 @@
|
||||||
<TextBlock Text="LFS" FontWeight="Bold"/>
|
<TextBlock Text="LFS" FontWeight="Bold"/>
|
||||||
</TabItem.Header>
|
</TabItem.Header>
|
||||||
|
|
||||||
<ContentControl Content="{Binding LFS}">
|
<ContentControl Content="{Binding LFS}"/>
|
||||||
<ContentControl.DataTemplates>
|
|
||||||
<DataTemplate DataType="m:LFSDiff">
|
|
||||||
<v:LFSDiffView/>
|
|
||||||
</DataTemplate>
|
|
||||||
</ContentControl.DataTemplates>
|
|
||||||
</ContentControl>
|
|
||||||
</TabItem>
|
</TabItem>
|
||||||
|
|
||||||
<TabItem>
|
<TabItem>
|
||||||
|
@ -308,13 +324,7 @@
|
||||||
<TextBlock Text="IMAGE" FontWeight="Bold"/>
|
<TextBlock Text="IMAGE" FontWeight="Bold"/>
|
||||||
</TabItem.Header>
|
</TabItem.Header>
|
||||||
|
|
||||||
<ContentControl Content="{Binding Image}">
|
<ContentControl Content="{Binding Image}"/>
|
||||||
<ContentControl.DataTemplates>
|
|
||||||
<DataTemplate DataType="m:ImageDiff">
|
|
||||||
<v:ImageDiffView/>
|
|
||||||
</DataTemplate>
|
|
||||||
</ContentControl.DataTemplates>
|
|
||||||
</ContentControl>
|
|
||||||
</TabItem>
|
</TabItem>
|
||||||
</TabControl>
|
</TabControl>
|
||||||
</DataTemplate>
|
</DataTemplate>
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
<UserControl xmlns="https://github.com/avaloniaui"
|
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
|
||||||
xmlns:m="using:SourceGit.Models"
|
|
||||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
|
||||||
x:Class="SourceGit.Views.LFSDiffView"
|
|
||||||
x:DataType="m:LFSDiff">
|
|
||||||
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
|
|
||||||
<TextBlock Text="{DynamicResource Text.Diff.LFS}"
|
|
||||||
Margin="0,0,0,32"
|
|
||||||
FontSize="18" FontWeight="Bold"
|
|
||||||
Foreground="{DynamicResource Brush.FG2}"
|
|
||||||
HorizontalAlignment="Center"/>
|
|
||||||
<Path Width="64" Height="64" Data="{StaticResource Icons.LFS}" Fill="{DynamicResource Brush.FG2}"/>
|
|
||||||
<Grid Margin="0,16,0,0" HorizontalAlignment="Center" RowDefinitions="32,32" ColumnDefinitions="Auto,Auto,Auto">
|
|
||||||
<Border Grid.Row="0" Grid.Column="0" Height="16" Background="{DynamicResource Brush.Badge}" CornerRadius="8" VerticalAlignment="Center">
|
|
||||||
<TextBlock Classes="primary" Text="{DynamicResource Text.Diff.Binary.Old}" Margin="8,0" FontSize="10" Foreground="{DynamicResource Brush.BadgeFG}"/>
|
|
||||||
</Border>
|
|
||||||
|
|
||||||
<TextBlock Grid.Row="0" Grid.Column="1" Classes="primary" Text="{Binding Old.Size}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Right" FontSize="16" Margin="8,0"/>
|
|
||||||
<TextBlock Grid.Row="0" Grid.Column="2" Classes="primary" Text="{DynamicResource Text.Bytes}" Foreground="{DynamicResource Brush.FG2}" FontSize="16"/>
|
|
||||||
|
|
||||||
<Border Grid.Row="1" Grid.Column="0" Height="16" Background="Green" CornerRadius="8" VerticalAlignment="Center">
|
|
||||||
<TextBlock Classes="primary" Text="{DynamicResource Text.Diff.Binary.New}" Margin="8,0" FontSize="10"/>
|
|
||||||
</Border>
|
|
||||||
|
|
||||||
<TextBlock Grid.Row="1" Grid.Column="1" Classes="primary" Text="{Binding New.Size}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Right" FontSize="16" Margin="8,0"/>
|
|
||||||
<TextBlock Grid.Row="1" Grid.Column="2" Classes="primary" Text="{DynamicResource Text.Bytes}" Foreground="{DynamicResource Brush.FG2}" FontSize="16"/>
|
|
||||||
</Grid>
|
|
||||||
</StackPanel>
|
|
||||||
</UserControl>
|
|
|
@ -1,12 +0,0 @@
|
||||||
using Avalonia.Controls;
|
|
||||||
|
|
||||||
namespace SourceGit.Views
|
|
||||||
{
|
|
||||||
public partial class LFSDiffView : UserControl
|
|
||||||
{
|
|
||||||
public LFSDiffView()
|
|
||||||
{
|
|
||||||
InitializeComponent();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -68,6 +68,32 @@
|
||||||
</ScrollViewer>
|
</ScrollViewer>
|
||||||
</Grid>
|
</Grid>
|
||||||
</DataTemplate>
|
</DataTemplate>
|
||||||
|
|
||||||
|
<DataTemplate DataType="vm:RevisionLFSImage">
|
||||||
|
<TabControl Margin="0,8,0,0" SelectedIndex="{Binding Source={x:Static vm:Preferences.Instance}, Path=LFSImageActiveIdx, Mode=TwoWay}">
|
||||||
|
<TabControl.Styles>
|
||||||
|
<Style Selector="TabControl /template/ ItemsPresenter#PART_ItemsPresenter > WrapPanel">
|
||||||
|
<Setter Property="HorizontalAlignment" Value="Center"/>
|
||||||
|
</Style>
|
||||||
|
</TabControl.Styles>
|
||||||
|
|
||||||
|
<TabItem>
|
||||||
|
<TabItem.Header>
|
||||||
|
<TextBlock Text="LFS" FontWeight="Bold"/>
|
||||||
|
</TabItem.Header>
|
||||||
|
|
||||||
|
<ContentControl Content="{Binding LFS}"/>
|
||||||
|
</TabItem>
|
||||||
|
|
||||||
|
<TabItem>
|
||||||
|
<TabItem.Header>
|
||||||
|
<TextBlock Text="IMAGE" FontWeight="Bold"/>
|
||||||
|
</TabItem.Header>
|
||||||
|
|
||||||
|
<ContentControl Content="{Binding Image}"/>
|
||||||
|
</TabItem>
|
||||||
|
</TabControl>
|
||||||
|
</DataTemplate>
|
||||||
</UserControl.DataTemplates>
|
</UserControl.DataTemplates>
|
||||||
</UserControl>
|
</UserControl>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue