code_review: PR (#280)

* tooltip for swap button
* move `Views.CompareTargetWorktree` to `Models.CompareTargetWorktree`
* remove unused `Models.IObjectId`
* fix swap not working when target is Worktree, because Commands.CompareRevisions's Args do not changed after swapping
This commit is contained in:
leo 2024-07-23 19:13:32 +08:00
parent 8d4e010fc2
commit 1583b08892
No known key found for this signature in database
10 changed files with 56 additions and 59 deletions

View file

@ -13,7 +13,9 @@ namespace SourceGit.Commands
{ {
WorkingDirectory = repo; WorkingDirectory = repo;
Context = repo; Context = repo;
Args = $"diff --name-status {start} {end}";
var based = string.IsNullOrEmpty(start) ? "-R" : start;
Args = $"diff --name-status {based} {end}";
} }
public List<Models.Change> Result() public List<Models.Change> Result()

View file

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

View file

@ -0,0 +1,6 @@
namespace SourceGit.Models
{
public class CompareTargetWorktree
{
}
}

View file

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

View file

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

View file

@ -189,7 +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.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.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.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.Diff.SwapCommits" xml:space="preserve">Swap</x:String>
<x:String x:Key="Text.DiffWithMerger" xml:space="preserve">Open In Merge Tool</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" 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> <x:String x:Key="Text.Discard.All" xml:space="preserve">All local changes in working copy.</x:String>

View file

@ -192,7 +192,7 @@
<x:String x:Key="Text.Diff.VisualLines.Incr" xml:space="preserve">增加可见的行数</x:String> <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.Welcome" xml:space="preserve">请选择需要对比的文件</x:String>
<x:String x:Key="Text.Diff.ShowHiddenSymbols" 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.Diff.SwapCommits" xml:space="preserve">交换比对双方</x:String>
<x:String x:Key="Text.DiffWithMerger" 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" xml:space="preserve">放弃更改确认</x:String>
<x:String x:Key="Text.Discard.All" xml:space="preserve">所有本地址未提交的修改。</x:String> <x:String x:Key="Text.Discard.All" xml:space="preserve">所有本地址未提交的修改。</x:String>

View file

@ -192,7 +192,7 @@
<x:String x:Key="Text.Diff.VisualLines.Incr" xml:space="preserve">增加可見的行數</x:String> <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.Welcome" xml:space="preserve">請選擇需要對比的檔案</x:String>
<x:String x:Key="Text.Diff.ShowHiddenSymbols" 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.Diff.SwapCommits" xml:space="preserve">交換比對雙方</x:String>
<x:String x:Key="Text.DiffWithMerger" 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" xml:space="preserve">放棄更改確認</x:String>
<x:String x:Key="Text.Discard.All" xml:space="preserve">所有本地址未提交的修改。</x:String> <x:String x:Key="Text.Discard.All" xml:space="preserve">所有本地址未提交的修改。</x:String>

View file

@ -2,29 +2,26 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Threading; using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels namespace SourceGit.ViewModels
{ {
public sealed class CompareTargetWorktree : Models.IObjectId
{
public string SHA => string.Empty;
}
public class RevisionCompare : ObservableObject public class RevisionCompare : ObservableObject
{ {
public Models.IObjectId StartPoint public object StartPoint
{ {
get; get => _startPoint;
private set; private set => SetProperty(ref _startPoint, value);
} }
public Models.IObjectId EndPoint public object EndPoint
{ {
get; get => _endPoint;
private set; private set => SetProperty(ref _endPoint, value);
} }
public List<Models.Change> VisibleChanges public List<Models.Change> VisibleChanges
@ -41,12 +38,17 @@ namespace SourceGit.ViewModels
if (SetProperty(ref _selectedChanges, value)) if (SetProperty(ref _selectedChanges, value))
{ {
if (value != null && value.Count == 1) if (value != null && value.Count == 1)
DiffContext = new DiffContext(_repo, new Models.DiffOption(StartPoint.SHA, EndPoint?.SHA ?? string.Empty, value[0]), _diffContext); {
var option = new Models.DiffOption(GetSHA(_startPoint), GetSHA(_endPoint), value[0]);
DiffContext = new DiffContext(_repo, option, _diffContext);
}
else else
{
DiffContext = null; DiffContext = null;
} }
} }
} }
}
public string SearchFilter public string SearchFilter
{ {
@ -69,16 +71,8 @@ namespace SourceGit.ViewModels
public RevisionCompare(string repo, Models.Commit startPoint, Models.Commit endPoint) public RevisionCompare(string repo, Models.Commit startPoint, Models.Commit endPoint)
{ {
_repo = repo; _repo = repo;
StartPoint = startPoint; _startPoint = (object)startPoint ?? new Models.CompareTargetWorktree();
_endPoint = (object)endPoint ?? new Models.CompareTargetWorktree();
if (endPoint == null)
{
EndPoint = new CompareTargetWorktree();
}
else
{
EndPoint = endPoint;
}
Task.Run(Refresh); Task.Run(Refresh);
} }
@ -86,6 +80,8 @@ namespace SourceGit.ViewModels
public void Cleanup() public void Cleanup()
{ {
_repo = null; _repo = null;
_startPoint = null;
_endPoint = null;
if (_changes != null) if (_changes != null)
_changes.Clear(); _changes.Clear();
if (_visibleChanges != null) if (_visibleChanges != null)
@ -102,6 +98,12 @@ namespace SourceGit.ViewModels
repo?.NavigateToCommit(commitSHA); repo?.NavigateToCommit(commitSHA);
} }
public void Swap()
{
(StartPoint, EndPoint) = (_endPoint, _startPoint);
Task.Run(Refresh);
}
public void ClearSearchFilter() public void ClearSearchFilter()
{ {
SearchFilter = string.Empty; SearchFilter = string.Empty;
@ -120,7 +122,7 @@ namespace SourceGit.ViewModels
diffWithMerger.Icon = App.CreateMenuIcon("Icons.OpenWith"); diffWithMerger.Icon = App.CreateMenuIcon("Icons.OpenWith");
diffWithMerger.Click += (_, ev) => diffWithMerger.Click += (_, ev) =>
{ {
var opt = new Models.DiffOption(StartPoint.SHA, EndPoint?.SHA ?? string.Empty, change); var opt = new Models.DiffOption(GetSHA(_startPoint), GetSHA(_endPoint), change);
var toolType = Preference.Instance.ExternalMergeToolType; var toolType = Preference.Instance.ExternalMergeToolType;
var toolPath = Preference.Instance.ExternalMergeToolPath; var toolPath = Preference.Instance.ExternalMergeToolPath;
@ -191,7 +193,7 @@ namespace SourceGit.ViewModels
private void Refresh() private void Refresh()
{ {
_changes = new Commands.CompareRevisions(_repo, StartPoint.SHA, EndPoint?.SHA ?? string.Empty).Result(); _changes = new Commands.CompareRevisions(_repo, GetSHA(_startPoint), GetSHA(_endPoint)).Result();
var visible = _changes; var visible = _changes;
if (!string.IsNullOrWhiteSpace(_searchFilter)) if (!string.IsNullOrWhiteSpace(_searchFilter))
@ -207,14 +209,14 @@ namespace SourceGit.ViewModels
Dispatcher.UIThread.Invoke(() => VisibleChanges = visible); Dispatcher.UIThread.Invoke(() => VisibleChanges = visible);
} }
public void Swap() private string GetSHA(object obj)
{ {
(StartPoint, EndPoint) = (EndPoint, StartPoint); return obj is Models.Commit commit ? commit.SHA : string.Empty;
OnPropertyChanged(string.Empty);
Task.Run(Refresh);
} }
private string _repo; private string _repo;
private object _startPoint = null;
private object _endPoint = null;
private List<Models.Change> _changes = null; private List<Models.Change> _changes = null;
private List<Models.Change> _visibleChanges = null; private List<Models.Change> _visibleChanges = null;
private List<Models.Change> _selectedChanges = null; private List<Models.Change> _selectedChanges = null;

View file

@ -10,16 +10,12 @@
x:Class="SourceGit.Views.RevisionCompare" x:Class="SourceGit.Views.RevisionCompare"
x:DataType="vm:RevisionCompare" x:DataType="vm:RevisionCompare"
Background="{DynamicResource Brush.Window}"> Background="{DynamicResource Brush.Window}">
<UserControl.Resources> <UserControl.Resources>
<DataTemplate DataType="m:Commit" <!-- Template for Models.Commit -->
x:Key="CommitInfoTemplate"> <DataTemplate DataType="m:Commit" x:Key="CommitInfoTemplate">
<Grid RowDefinitions="Auto,*"> <Grid RowDefinitions="Auto,*">
<Grid Grid.Row="0" ColumnDefinitions="Auto,*,Auto,Auto,Auto"> <Grid Grid.Row="0" ColumnDefinitions="Auto,*,Auto,Auto,Auto">
<v:Avatar Width="16" Height="16" <v:Avatar Width="16" Height="16" VerticalAlignment="Center" IsHitTestVisible="False" User="{Binding Author}"/>
VerticalAlignment="Center"
IsHitTestVisible="False"
User="{Binding Author}"/>
<TextBlock Grid.Column="1" Classes="monospace" Text="{Binding Author.Name}" Margin="8,0,0,0"/> <TextBlock Grid.Column="1" Classes="monospace" Text="{Binding Author.Name}" Margin="8,0,0,0"/>
<Border Grid.Column="2" Background="{DynamicResource Brush.Accent}" CornerRadius="4" IsVisible="{Binding IsCurrentHead}"> <Border Grid.Column="2" Background="{DynamicResource Brush.Accent}" CornerRadius="4" IsVisible="{Binding IsCurrentHead}">
<TextBlock Text="HEAD" Classes="monospace" Margin="4,0" Foreground="#FFDDDDDD"/> <TextBlock Text="HEAD" Classes="monospace" Margin="4,0" Foreground="#FFDDDDDD"/>
@ -32,8 +28,8 @@
</Grid> </Grid>
</DataTemplate> </DataTemplate>
<DataTemplate DataType="vm:CompareTargetWorktree" <!-- Template for Models.CompareTargetWorktree -->
x:Key="WorktreeTemplate"> <DataTemplate DataType="m:CompareTargetWorktree" x:Key="WorktreeTemplate">
<Border HorizontalAlignment="Center" VerticalAlignment="Center" Background="{DynamicResource Brush.Accent}" CornerRadius="4"> <Border HorizontalAlignment="Center" VerticalAlignment="Center" Background="{DynamicResource Brush.Accent}" CornerRadius="4">
<TextBlock Text="{DynamicResource Text.Worktree}" Classes="monospace" Margin="4,2" Foreground="#FFDDDDDD"/> <TextBlock Text="{DynamicResource Text.Worktree}" Classes="monospace" Margin="4,2" Foreground="#FFDDDDDD"/>
</Border> </Border>
@ -41,7 +37,9 @@
</UserControl.Resources> </UserControl.Resources>
<Grid RowDefinitions="50,*" Margin="4"> <Grid RowDefinitions="50,*" Margin="4">
<!-- Compare Revision Info -->
<Grid Grid.Row="0" Margin="48,0,48,4" ColumnDefinitions="*,48,*"> <Grid Grid.Row="0" Margin="48,0,48,4" ColumnDefinitions="*,48,*">
<!-- Base Revision -->
<Border Grid.Column="0" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}" CornerRadius="4" Padding="4"> <Border Grid.Column="0" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}" CornerRadius="4" Padding="4">
<ContentControl Content="{Binding StartPoint}"> <ContentControl Content="{Binding StartPoint}">
<ContentControl.DataTemplates> <ContentControl.DataTemplates>
@ -51,17 +49,12 @@
</ContentControl> </ContentControl>
</Border> </Border>
<Button Classes="icon_button" <!-- Swap Button -->
Command="{Binding Swap, Mode = OneTime}" <Button Classes="icon_button" Command="{Binding Swap}" Grid.Column="1" HorizontalAlignment="Center" ToolTip.Tip="{DynamicResource Text.Diff.SwapCommits}">
Grid.Column="1" <Path Grid.Column="1" Width="16" Height="16" Fill="{DynamicResource Brush.FG2}" Data="{DynamicResource Icons.Compare}"/>
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> </Button>
<!-- Right Revision -->
<Border Grid.Column="2" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}" CornerRadius="4" Padding="4"> <Border Grid.Column="2" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}" CornerRadius="4" Padding="4">
<ContentControl Content="{Binding EndPoint}"> <ContentControl Content="{Binding EndPoint}">
<ContentControl.DataTemplates> <ContentControl.DataTemplates>
@ -72,6 +65,7 @@
</Border> </Border>
</Grid> </Grid>
<!-- Changes View -->
<Grid Grid.Row="1"> <Grid Grid.Row="1">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="256" MinWidth="200" MaxWidth="480"/> <ColumnDefinition Width="256" MinWidth="200" MaxWidth="480"/>