feature: supports to view image diff when lfs object points to a image

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-06-04 20:53:42 +08:00
parent 06a77502bc
commit ed496a41fb
No known key found for this signature in database
6 changed files with 171 additions and 24 deletions

View file

@ -35,5 +35,39 @@ namespace SourceGit.Commands
return stream;
}
public static Stream FromLFS(string repo, string oid, long size)
{
var starter = new ProcessStartInfo();
starter.WorkingDirectory = repo;
starter.FileName = Native.OS.GitExecutable;
starter.Arguments = $"lfs smudge";
starter.UseShellExecute = false;
starter.CreateNoWindow = true;
starter.WindowStyle = ProcessWindowStyle.Hidden;
starter.RedirectStandardInput = true;
starter.RedirectStandardOutput = true;
var stream = new MemoryStream();
try
{
var proc = new Process() { StartInfo = starter };
proc.Start();
proc.StandardInput.WriteLine("version https://git-lfs.github.com/spec/v1");
proc.StandardInput.WriteLine($"oid sha256:{oid}");
proc.StandardInput.WriteLine($"size {size}");
proc.StandardOutput.BaseStream.CopyTo(stream);
proc.WaitForExit();
proc.Close();
stream.Position = 0;
}
catch (Exception e)
{
App.RaiseException(repo, $"Failed to query file content: {e}");
}
return stream;
}
}
}

View file

@ -207,7 +207,10 @@ namespace SourceGit.ViewModels
}
else if (latest.IsLFS)
{
rs = latest.LFSDiff;
if (IMG_EXTS.Contains(Path.GetExtension(_option.Path) ?? ".invalid"))
rs = new LFSImageDiff(_repo, latest.LFSDiff);
else
rs = latest.LFSDiff;
}
else
{

View file

@ -0,0 +1,49 @@
using System.Threading.Tasks;
using Avalonia.Media.Imaging;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class LFSImageDiff : ObservableObject
{
public Models.LFSDiff LFS
{
get;
}
public Models.ImageDiff Image
{
get => _image;
private set => SetProperty(ref _image, value);
}
public LFSImageDiff(string repo, Models.LFSDiff lfs)
{
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);
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

@ -224,29 +224,7 @@
<!-- LFS Diff -->
<DataTemplate 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>
<v:LFSDiffView/>
</DataTemplate>
<!-- Submodule Diff -->
@ -302,6 +280,45 @@
<v:ImageDiffView/>
</DataTemplate>
<!-- LFS Image Diff -->
<DataTemplate DataType="vm:LFSImageDiff">
<TabControl Margin="0,8,0,0">
<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}">
<ContentControl.DataTemplates>
<DataTemplate DataType="m:LFSDiff">
<v:LFSDiffView/>
</DataTemplate>
</ContentControl.DataTemplates>
</ContentControl>
</TabItem>
<TabItem>
<TabItem.Header>
<TextBlock Text="IMAGE" FontWeight="Bold"/>
</TabItem.Header>
<ContentControl Content="{Binding Image}">
<ContentControl.DataTemplates>
<DataTemplate DataType="m:ImageDiff">
<v:ImageDiffView/>
</DataTemplate>
</ContentControl.DataTemplates>
</ContentControl>
</TabItem>
</TabControl>
</DataTemplate>
<!-- Text Diff -->
<DataTemplate DataType="m:TextDiff">
<v:TextDiffView UseSideBySideDiff="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSideBySideDiff, Mode=OneWay}"

View file

@ -0,0 +1,32 @@
<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>

View file

@ -0,0 +1,12 @@
using Avalonia.Controls;
namespace SourceGit.Views
{
public partial class LFSDiffView : UserControl
{
public LFSDiffView()
{
InitializeComponent();
}
}
}