feat: Allow Swap Commits in Revision Compare

This commit is contained in:
Giuseppe Lippolis 2024-07-23 11:41:33 +02:00
parent cd1eed4356
commit 5cc30f7d10
8 changed files with 91 additions and 70 deletions

View file

@ -6,7 +6,7 @@ using Avalonia.Media;
namespace SourceGit.Models
{
public class Commit
public class Commit: IObjectId
{
public static double OpacityForNotMerged
{

7
src/Models/IObjectId.cs Normal file
View file

@ -0,0 +1,7 @@
namespace SourceGit.Models
{
public interface IObjectId
{
string SHA { get; }
}
}

View file

@ -9,7 +9,7 @@
Commit,
}
public class Object
public class Object: IObjectId
{
public string SHA { get; set; }
public ObjectType Type { get; set; }

View file

@ -189,6 +189,7 @@
<x:String x:Key="Text.Diff.VisualLines.Incr" xml:space="preserve">Increase Number of Visible Lines</x:String>
<x:String x:Key="Text.Diff.Welcome" xml:space="preserve">SELECT FILE TO VIEW CHANGES</x:String>
<x:String x:Key="Text.Diff.ShowHiddenSymbols" xml:space="preserve">Show hidden symbols</x:String>
<x:String x:Key="Text.Diff.SwapCommits" xml:space="preserve">Reverse Commits</x:String>
<x:String x:Key="Text.DiffWithMerger" xml:space="preserve">Open In Merge Tool</x:String>
<x:String x:Key="Text.Discard" xml:space="preserve">Discard Changes</x:String>
<x:String x:Key="Text.Discard.All" xml:space="preserve">All local changes in working copy.</x:String>

View file

@ -192,6 +192,7 @@
<x:String x:Key="Text.Diff.VisualLines.Incr" xml:space="preserve">增加可见的行数</x:String>
<x:String x:Key="Text.Diff.Welcome" xml:space="preserve">请选择需要对比的文件</x:String>
<x:String x:Key="Text.Diff.ShowHiddenSymbols" xml:space="preserve">显示隐藏符号</x:String>
<x:String x:Key="Text.Diff.SwapCommits" xml:space="preserve">反向提交</x:String>
<x:String x:Key="Text.DiffWithMerger" xml:space="preserve">使用外部比对工具查看</x:String>
<x:String x:Key="Text.Discard" xml:space="preserve">放弃更改确认</x:String>
<x:String x:Key="Text.Discard.All" xml:space="preserve">所有本地址未提交的修改。</x:String>

View file

@ -192,6 +192,7 @@
<x:String x:Key="Text.Diff.VisualLines.Incr" xml:space="preserve">增加可見的行數</x:String>
<x:String x:Key="Text.Diff.Welcome" xml:space="preserve">請選擇需要對比的檔案</x:String>
<x:String x:Key="Text.Diff.ShowHiddenSymbols" xml:space="preserve">顯示隱藏符號</x:String>
<x:String x:Key="Text.Diff.SwapCommits" xml:space="preserve">反向提交</x:String>
<x:String x:Key="Text.DiffWithMerger" xml:space="preserve">使用外部比對工具檢視</x:String>
<x:String x:Key="Text.Discard" xml:space="preserve">放棄更改確認</x:String>
<x:String x:Key="Text.Discard.All" xml:space="preserve">所有本地址未提交的修改。</x:String>

View file

@ -2,28 +2,26 @@
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class CompareTargetWorktree
public sealed class CompareTargetWorktree : Models.IObjectId
{
public string SHA => string.Empty;
}
public class RevisionCompare : ObservableObject
{
public Models.Commit StartPoint
public Models.IObjectId StartPoint
{
get;
private set;
}
public object EndPoint
public Models.IObjectId EndPoint
{
get;
private set;
@ -43,7 +41,7 @@ namespace SourceGit.ViewModels
if (SetProperty(ref _selectedChanges, value))
{
if (value != null && value.Count == 1)
DiffContext = new DiffContext(_repo, new Models.DiffOption(StartPoint.SHA, _endPoint, value[0]), _diffContext);
DiffContext = new DiffContext(_repo, new Models.DiffOption(StartPoint.SHA, EndPoint?.SHA ?? string.Empty, value[0]), _diffContext);
else
DiffContext = null;
}
@ -76,31 +74,13 @@ namespace SourceGit.ViewModels
if (endPoint == null)
{
EndPoint = new CompareTargetWorktree();
_endPoint = string.Empty;
}
else
{
EndPoint = endPoint;
_endPoint = endPoint.SHA;
}
Task.Run(() =>
{
_changes = new Commands.CompareRevisions(_repo, startPoint.SHA, _endPoint).Result();
var visible = _changes;
if (!string.IsNullOrWhiteSpace(_searchFilter))
{
visible = new List<Models.Change>();
foreach (var c in _changes)
{
if (c.Path.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase))
visible.Add(c);
}
}
Dispatcher.UIThread.Invoke(() => VisibleChanges = visible);
});
Task.Run(Refresh);
}
public void Cleanup()
@ -140,7 +120,7 @@ namespace SourceGit.ViewModels
diffWithMerger.Icon = App.CreateMenuIcon("Icons.OpenWith");
diffWithMerger.Click += (_, ev) =>
{
var opt = new Models.DiffOption(StartPoint.SHA, _endPoint, change);
var opt = new Models.DiffOption(StartPoint.SHA, EndPoint?.SHA ?? string.Empty, change);
var toolType = Preference.Instance.ExternalMergeToolType;
var toolPath = Preference.Instance.ExternalMergeToolPath;
@ -209,8 +189,32 @@ namespace SourceGit.ViewModels
}
}
private void Refresh()
{
_changes = new Commands.CompareRevisions(_repo, StartPoint.SHA, EndPoint?.SHA ?? string.Empty).Result();
var visible = _changes;
if (!string.IsNullOrWhiteSpace(_searchFilter))
{
visible = [];
foreach (var c in _changes)
{
if (c.Path.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase))
visible.Add(c);
}
}
Dispatcher.UIThread.Invoke(() => VisibleChanges = visible);
}
public void Swap()
{
(StartPoint, EndPoint) = (EndPoint, StartPoint);
OnPropertyChanged(string.Empty);
Task.Run(Refresh);
}
private string _repo;
private string _endPoint;
private List<Models.Change> _changes = null;
private List<Models.Change> _visibleChanges = null;
private List<Models.Change> _selectedChanges = null;

View file

@ -10,33 +10,10 @@
x:Class="SourceGit.Views.RevisionCompare"
x:DataType="vm:RevisionCompare"
Background="{DynamicResource Brush.Window}">
<Grid RowDefinitions="50,*" Margin="4">
<Grid Grid.Row="0" Margin="48,0,48,4" ColumnDefinitions="*,48,*">
<Border Grid.Column="0" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}" CornerRadius="4" Padding="4">
<Grid RowDefinitions="Auto,*">
<Grid Grid.Row="0" ColumnDefinitions="Auto,*,Auto,Auto,Auto">
<v:Avatar Width="16" Height="16"
VerticalAlignment="Center"
IsHitTestVisible="False"
User="{Binding StartPoint.Author}"/>
<TextBlock Grid.Column="1" Classes="monospace" Text="{Binding StartPoint.Author.Name}" Margin="8,0,0,0"/>
<Border Grid.Column="2" Background="{DynamicResource Brush.Accent}" CornerRadius="4" IsVisible="{Binding StartPoint.IsCurrentHead}">
<TextBlock Text="HEAD" Classes="monospace" Margin="4,0" Foreground="#FFDDDDDD"/>
</Border>
<TextBlock Grid.Column="3" Classes="monospace" Text="{Binding StartPoint.SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange" Margin="8,0,0,0" TextDecorations="Underline" PointerPressed="OnPressedSHA"/>
<TextBlock Grid.Column="4" Classes="monospace" Text="{Binding StartPoint.CommitterTimeStr}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>
</Grid>
<TextBlock Grid.Row="1" Classes="monospace" Text="{Binding StartPoint.Subject}" VerticalAlignment="Bottom"/>
</Grid>
</Border>
<Path Grid.Column="1" Width="16" Height="16" Fill="{DynamicResource Brush.FG2}" Data="{DynamicResource Icons.Down}" RenderTransformOrigin="50%,50%" RenderTransform="rotate(270deg)"/>
<Border Grid.Column="2" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}" CornerRadius="4" Padding="4">
<ContentControl Content="{Binding EndPoint}">
<ContentControl.DataTemplates>
<DataTemplate DataType="m:Commit">
<UserControl.Resources>
<DataTemplate DataType="m:Commit"
x:Key="CommitInfoTemplate">
<Grid RowDefinitions="Auto,*">
<Grid Grid.Row="0" ColumnDefinitions="Auto,*,Auto,Auto,Auto">
<v:Avatar Width="16" Height="16"
@ -55,11 +32,41 @@
</Grid>
</DataTemplate>
<DataTemplate DataType="vm:CompareTargetWorktree">
<DataTemplate DataType="vm:CompareTargetWorktree"
x:Key="WorktreeTemplate">
<Border HorizontalAlignment="Center" VerticalAlignment="Center" Background="{DynamicResource Brush.Accent}" CornerRadius="4">
<TextBlock Text="{DynamicResource Text.Worktree}" Classes="monospace" Margin="4,2" Foreground="#FFDDDDDD"/>
</Border>
</DataTemplate>
</UserControl.Resources>
<Grid RowDefinitions="50,*" Margin="4">
<Grid Grid.Row="0" Margin="48,0,48,4" ColumnDefinitions="*,48,*">
<Border Grid.Column="0" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}" CornerRadius="4" Padding="4">
<ContentControl Content="{Binding StartPoint}">
<ContentControl.DataTemplates>
<StaticResource ResourceKey="CommitInfoTemplate"/>
<StaticResource ResourceKey="WorktreeTemplate"/>
</ContentControl.DataTemplates>
</ContentControl>
</Border>
<Button Classes="icon_button"
Command="{Binding Swap, Mode = OneTime}"
Grid.Column="1"
HorizontalAlignment="Center"
ToolTip.Tip="{DynamicResource Text.Diff.SyntaxHighlight}">
<Path Grid.Column="1" Width="16" Height="16"
Fill="{DynamicResource Brush.FG2}"
Data="{DynamicResource Icons.Compare}"
/>
</Button>
<Border Grid.Column="2" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}" CornerRadius="4" Padding="4">
<ContentControl Content="{Binding EndPoint}">
<ContentControl.DataTemplates>
<StaticResource ResourceKey="CommitInfoTemplate"/>
<StaticResource ResourceKey="WorktreeTemplate"/>
</ContentControl.DataTemplates>
</ContentControl>
</Border>