Skip to content

Commit ed496a4

Browse files
committed
feature: supports to view image diff when lfs object points to a image
Signed-off-by: leo <[email protected]>
1 parent 06a7750 commit ed496a4

File tree

6 files changed

+171
-24
lines changed

6 files changed

+171
-24
lines changed

src/Commands/QueryFileContent.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,39 @@ public static Stream Run(string repo, string revision, string file)
3535

3636
return stream;
3737
}
38+
39+
public static Stream FromLFS(string repo, string oid, long size)
40+
{
41+
var starter = new ProcessStartInfo();
42+
starter.WorkingDirectory = repo;
43+
starter.FileName = Native.OS.GitExecutable;
44+
starter.Arguments = $"lfs smudge";
45+
starter.UseShellExecute = false;
46+
starter.CreateNoWindow = true;
47+
starter.WindowStyle = ProcessWindowStyle.Hidden;
48+
starter.RedirectStandardInput = true;
49+
starter.RedirectStandardOutput = true;
50+
51+
var stream = new MemoryStream();
52+
try
53+
{
54+
var proc = new Process() { StartInfo = starter };
55+
proc.Start();
56+
proc.StandardInput.WriteLine("version https://git-lfs.github.com/spec/v1");
57+
proc.StandardInput.WriteLine($"oid sha256:{oid}");
58+
proc.StandardInput.WriteLine($"size {size}");
59+
proc.StandardOutput.BaseStream.CopyTo(stream);
60+
proc.WaitForExit();
61+
proc.Close();
62+
63+
stream.Position = 0;
64+
}
65+
catch (Exception e)
66+
{
67+
App.RaiseException(repo, $"Failed to query file content: {e}");
68+
}
69+
70+
return stream;
71+
}
3872
}
3973
}

src/ViewModels/DiffContext.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,10 @@ private void LoadDiffContent()
207207
}
208208
else if (latest.IsLFS)
209209
{
210-
rs = latest.LFSDiff;
210+
if (IMG_EXTS.Contains(Path.GetExtension(_option.Path) ?? ".invalid"))
211+
rs = new LFSImageDiff(_repo, latest.LFSDiff);
212+
else
213+
rs = latest.LFSDiff;
211214
}
212215
else
213216
{

src/ViewModels/LFSImageDiff.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.Threading.Tasks;
2+
3+
using Avalonia.Media.Imaging;
4+
using Avalonia.Threading;
5+
6+
using CommunityToolkit.Mvvm.ComponentModel;
7+
8+
namespace SourceGit.ViewModels
9+
{
10+
public class LFSImageDiff : ObservableObject
11+
{
12+
public Models.LFSDiff LFS
13+
{
14+
get;
15+
}
16+
17+
public Models.ImageDiff Image
18+
{
19+
get => _image;
20+
private set => SetProperty(ref _image, value);
21+
}
22+
23+
public LFSImageDiff(string repo, Models.LFSDiff lfs)
24+
{
25+
LFS = lfs;
26+
27+
Task.Run(() =>
28+
{
29+
var img = new Models.ImageDiff();
30+
(img.Old, img.OldFileSize) = BitmapFromLFSObject(repo, lfs.Old);
31+
(img.New, img.NewFileSize) = BitmapFromLFSObject(repo, lfs.New);
32+
33+
Dispatcher.UIThread.Invoke(() => Image = img);
34+
});
35+
}
36+
37+
private (Bitmap, long) BitmapFromLFSObject(string repo, Models.LFSObject lfs)
38+
{
39+
if (string.IsNullOrEmpty(lfs.Oid) || lfs.Size == 0)
40+
return (null, 0);
41+
42+
var stream = Commands.QueryFileContent.FromLFS(repo, lfs.Oid, lfs.Size);
43+
var size = stream.Length;
44+
return size > 0 ? (new Bitmap(stream), size) : (null, size);
45+
}
46+
47+
private Models.ImageDiff _image;
48+
}
49+
}

src/Views/DiffView.axaml

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -224,29 +224,7 @@
224224

225225
<!-- LFS Diff -->
226226
<DataTemplate DataType="m:LFSDiff">
227-
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
228-
<TextBlock Text="{DynamicResource Text.Diff.LFS}"
229-
Margin="0,0,0,32"
230-
FontSize="18" FontWeight="Bold"
231-
Foreground="{DynamicResource Brush.FG2}"
232-
HorizontalAlignment="Center"/>
233-
<Path Width="64" Height="64" Data="{StaticResource Icons.LFS}" Fill="{DynamicResource Brush.FG2}"/>
234-
<Grid Margin="0,16,0,0" HorizontalAlignment="Center" RowDefinitions="32,32" ColumnDefinitions="Auto,Auto,Auto">
235-
<Border Grid.Row="0" Grid.Column="0" Height="16" Background="{DynamicResource Brush.Badge}" CornerRadius="8" VerticalAlignment="Center">
236-
<TextBlock Classes="primary" Text="{DynamicResource Text.Diff.Binary.Old}" Margin="8,0" FontSize="10" Foreground="{DynamicResource Brush.BadgeFG}"/>
237-
</Border>
238-
239-
<TextBlock Grid.Row="0" Grid.Column="1" Classes="primary" Text="{Binding Old.Size}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Right" FontSize="16" Margin="8,0"/>
240-
<TextBlock Grid.Row="0" Grid.Column="2" Classes="primary" Text="{DynamicResource Text.Bytes}" Foreground="{DynamicResource Brush.FG2}" FontSize="16"/>
241-
242-
<Border Grid.Row="1" Grid.Column="0" Height="16" Background="Green" CornerRadius="8" VerticalAlignment="Center">
243-
<TextBlock Classes="primary" Text="{DynamicResource Text.Diff.Binary.New}" Margin="8,0" FontSize="10"/>
244-
</Border>
245-
246-
<TextBlock Grid.Row="1" Grid.Column="1" Classes="primary" Text="{Binding New.Size}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Right" FontSize="16" Margin="8,0"/>
247-
<TextBlock Grid.Row="1" Grid.Column="2" Classes="primary" Text="{DynamicResource Text.Bytes}" Foreground="{DynamicResource Brush.FG2}" FontSize="16"/>
248-
</Grid>
249-
</StackPanel>
227+
<v:LFSDiffView/>
250228
</DataTemplate>
251229

252230
<!-- Submodule Diff -->
@@ -302,6 +280,45 @@
302280
<v:ImageDiffView/>
303281
</DataTemplate>
304282

283+
<!-- LFS Image Diff -->
284+
<DataTemplate DataType="vm:LFSImageDiff">
285+
<TabControl Margin="0,8,0,0">
286+
<TabControl.Styles>
287+
<Style Selector="TabControl /template/ ItemsPresenter#PART_ItemsPresenter > WrapPanel">
288+
<Setter Property="HorizontalAlignment" Value="Center"/>
289+
</Style>
290+
</TabControl.Styles>
291+
292+
<TabItem>
293+
<TabItem.Header>
294+
<TextBlock Text="LFS" FontWeight="Bold"/>
295+
</TabItem.Header>
296+
297+
<ContentControl Content="{Binding LFS}">
298+
<ContentControl.DataTemplates>
299+
<DataTemplate DataType="m:LFSDiff">
300+
<v:LFSDiffView/>
301+
</DataTemplate>
302+
</ContentControl.DataTemplates>
303+
</ContentControl>
304+
</TabItem>
305+
306+
<TabItem>
307+
<TabItem.Header>
308+
<TextBlock Text="IMAGE" FontWeight="Bold"/>
309+
</TabItem.Header>
310+
311+
<ContentControl Content="{Binding Image}">
312+
<ContentControl.DataTemplates>
313+
<DataTemplate DataType="m:ImageDiff">
314+
<v:ImageDiffView/>
315+
</DataTemplate>
316+
</ContentControl.DataTemplates>
317+
</ContentControl>
318+
</TabItem>
319+
</TabControl>
320+
</DataTemplate>
321+
305322
<!-- Text Diff -->
306323
<DataTemplate DataType="m:TextDiff">
307324
<v:TextDiffView UseSideBySideDiff="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSideBySideDiff, Mode=OneWay}"

src/Views/LFSDiffView.axaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<UserControl xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:m="using:SourceGit.Models"
6+
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
7+
x:Class="SourceGit.Views.LFSDiffView"
8+
x:DataType="m:LFSDiff">
9+
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
10+
<TextBlock Text="{DynamicResource Text.Diff.LFS}"
11+
Margin="0,0,0,32"
12+
FontSize="18" FontWeight="Bold"
13+
Foreground="{DynamicResource Brush.FG2}"
14+
HorizontalAlignment="Center"/>
15+
<Path Width="64" Height="64" Data="{StaticResource Icons.LFS}" Fill="{DynamicResource Brush.FG2}"/>
16+
<Grid Margin="0,16,0,0" HorizontalAlignment="Center" RowDefinitions="32,32" ColumnDefinitions="Auto,Auto,Auto">
17+
<Border Grid.Row="0" Grid.Column="0" Height="16" Background="{DynamicResource Brush.Badge}" CornerRadius="8" VerticalAlignment="Center">
18+
<TextBlock Classes="primary" Text="{DynamicResource Text.Diff.Binary.Old}" Margin="8,0" FontSize="10" Foreground="{DynamicResource Brush.BadgeFG}"/>
19+
</Border>
20+
21+
<TextBlock Grid.Row="0" Grid.Column="1" Classes="primary" Text="{Binding Old.Size}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Right" FontSize="16" Margin="8,0"/>
22+
<TextBlock Grid.Row="0" Grid.Column="2" Classes="primary" Text="{DynamicResource Text.Bytes}" Foreground="{DynamicResource Brush.FG2}" FontSize="16"/>
23+
24+
<Border Grid.Row="1" Grid.Column="0" Height="16" Background="Green" CornerRadius="8" VerticalAlignment="Center">
25+
<TextBlock Classes="primary" Text="{DynamicResource Text.Diff.Binary.New}" Margin="8,0" FontSize="10"/>
26+
</Border>
27+
28+
<TextBlock Grid.Row="1" Grid.Column="1" Classes="primary" Text="{Binding New.Size}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Right" FontSize="16" Margin="8,0"/>
29+
<TextBlock Grid.Row="1" Grid.Column="2" Classes="primary" Text="{DynamicResource Text.Bytes}" Foreground="{DynamicResource Brush.FG2}" FontSize="16"/>
30+
</Grid>
31+
</StackPanel>
32+
</UserControl>

src/Views/LFSDiffView.axaml.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Avalonia.Controls;
2+
3+
namespace SourceGit.Views
4+
{
5+
public partial class LFSDiffView : UserControl
6+
{
7+
public LFSDiffView()
8+
{
9+
InitializeComponent();
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)