From 52ef0db42799e6c1234f1258cffe1a763bf4b8ef Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 27 May 2024 21:05:15 +0800 Subject: [PATCH] feature: add a context menu item to compare selected branch/revision with current worktree --- src/Resources/Locales/en_US.axaml | 3 ++ src/Resources/Locales/zh_CN.axaml | 3 ++ src/ViewModels/Histories.cs | 23 ++++++++++--- src/ViewModels/Repository.cs | 40 ++++++++++++++++++++++- src/ViewModels/RevisionCompare.cs | 26 ++++++++++++--- src/Views/RevisionCompare.axaml | 54 ++++++++++++++++++++----------- 6 files changed, 119 insertions(+), 30 deletions(-) diff --git a/src/Resources/Locales/en_US.axaml b/src/Resources/Locales/en_US.axaml index 68f92ec1..bc2bc9c0 100644 --- a/src/Resources/Locales/en_US.axaml +++ b/src/Resources/Locales/en_US.axaml @@ -34,6 +34,7 @@ BLAME ON THIS FILE IS NOT SUPPORTED!!! Checkout${0}$ Compare with HEAD + Compare with Worktree Copy Branch Name Delete${0}$ Delete selected {0} branches @@ -79,6 +80,7 @@ Cherry-Pick This Commit Checkout Commit Compare with HEAD + Compare with Worktree Copy SHA Rebase${0}$to Here Reset${0}$to Here @@ -478,4 +480,5 @@ STAGE ALL VIEW ASSUME UNCHANGED Right-click the selected file(s), and make your choice to resolve conflicts. + Current Worktree diff --git a/src/Resources/Locales/zh_CN.axaml b/src/Resources/Locales/zh_CN.axaml index 12dccf35..3dffd574 100644 --- a/src/Resources/Locales/zh_CN.axaml +++ b/src/Resources/Locales/zh_CN.axaml @@ -34,6 +34,7 @@ 选中文件不支持该操作!!! 检出(checkout)${0}$ 与当前HEAD比较 + 与本地工作树比较 复制分支名 删除${0}$ 删除选中的 {0} 个分支 @@ -79,6 +80,7 @@ 挑选(cherry-pick)此提交 检出此提交 与当前HEAD比较 + 与本地工作树比较 复制提交指纹 变基(rebase)${0}$到此处 重置(reset)${0}$到此处 @@ -478,4 +480,5 @@ 暂存所有 查看忽略变更文件 请选中冲突文件,打开右键菜单,选择合适的解决方式 + 本地工作树 diff --git a/src/ViewModels/Histories.cs b/src/ViewModels/Histories.cs index 98659e2f..d759126c 100644 --- a/src/ViewModels/Histories.cs +++ b/src/ViewModels/Histories.cs @@ -318,10 +318,10 @@ namespace SourceGit.ViewModels if (current.Head != commit.SHA) { - var compare = new MenuItem(); - compare.Header = App.Text("CommitCM.CompareWithHead"); - compare.Icon = App.CreateMenuIcon("Icons.Compare"); - compare.Click += (o, e) => + var compareWithHead = new MenuItem(); + compareWithHead.Header = App.Text("CommitCM.CompareWithHead"); + compareWithHead.Icon = App.CreateMenuIcon("Icons.Compare"); + compareWithHead.Click += (o, e) => { var head = _commits.Find(x => x.SHA == current.Head); if (head == null) @@ -338,8 +338,21 @@ namespace SourceGit.ViewModels e.Handled = true; }; + menu.Items.Add(compareWithHead); + + if (_repo.WorkingCopyChangesCount > 0) + { + var compareWithWorktree = new MenuItem(); + compareWithWorktree.Header = App.Text("CommitCM.CompareWithWorktree"); + compareWithWorktree.Icon = App.CreateMenuIcon("Icons.Compare"); + compareWithWorktree.Click += (o, e) => + { + DetailContext = new RevisionCompare(_repo.FullPath, commit, null); + e.Handled = true; + }; + menu.Items.Add(compareWithWorktree); + } - menu.Items.Add(compare); menu.Items.Add(new MenuItem() { Header = "-" }); } diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index c35ddecf..df9b3f47 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -947,6 +947,25 @@ namespace SourceGit.ViewModels e.Handled = true; }; + if (WorkingCopyChangesCount > 0) + { + var compareWithWorktree = new MenuItem(); + compareWithWorktree.Header = App.Text("BranchCM.CompareWithWorktree"); + compareWithWorktree.Icon = App.CreateMenuIcon("Icons.Compare"); + compareWithWorktree.Click += (o, e) => + { + SearchResultSelectedCommit = null; + + if (_histories != null) + { + var target = new Commands.QuerySingleCommit(FullPath, branch.Head).Result(); + _histories.AutoSelectedCommit = null; + _histories.DetailContext = new RevisionCompare(FullPath, target, null); + } + }; + menu.Items.Add(compareWithWorktree); + } + menu.Items.Add(new MenuItem() { Header = "-" }); menu.Items.Add(compare); } @@ -1238,8 +1257,27 @@ namespace SourceGit.ViewModels e.Handled = true; }; - menu.Items.Add(compare); + + if (WorkingCopyChangesCount > 0) + { + var compareWithWorktree = new MenuItem(); + compareWithWorktree.Header = App.Text("BranchCM.CompareWithWorktree"); + compareWithWorktree.Icon = App.CreateMenuIcon("Icons.Compare"); + compareWithWorktree.Click += (o, e) => + { + SearchResultSelectedCommit = null; + + if (_histories != null) + { + var target = new Commands.QuerySingleCommit(FullPath, branch.Head).Result(); + _histories.AutoSelectedCommit = null; + _histories.DetailContext = new RevisionCompare(FullPath, target, null); + } + }; + menu.Items.Add(compareWithWorktree); + } + menu.Items.Add(new MenuItem() { Header = "-" }); } } diff --git a/src/ViewModels/RevisionCompare.cs b/src/ViewModels/RevisionCompare.cs index 1ed85c9e..a2fd25ca 100644 --- a/src/ViewModels/RevisionCompare.cs +++ b/src/ViewModels/RevisionCompare.cs @@ -10,6 +10,11 @@ using CommunityToolkit.Mvvm.ComponentModel; namespace SourceGit.ViewModels { + public class CompareTargetWorktree + { + public string SHA => string.Empty; + } + public class RevisionCompare : ObservableObject { public Models.Commit StartPoint @@ -18,7 +23,7 @@ namespace SourceGit.ViewModels private set; } - public Models.Commit EndPoint + public object EndPoint { get; private set; @@ -51,7 +56,7 @@ namespace SourceGit.ViewModels else { SelectedNode = FileTreeNode.SelectByPath(_changeTree, value.Path); - DiffContext = new DiffContext(_repo, new Models.DiffOption(StartPoint.SHA, EndPoint.SHA, value), _diffContext); + DiffContext = new DiffContext(_repo, new Models.DiffOption(StartPoint.SHA, _endPoint, value), _diffContext); } } } @@ -98,11 +103,21 @@ namespace SourceGit.ViewModels { _repo = repo; StartPoint = startPoint; - EndPoint = endPoint; + + 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.SHA).Result(); + _changes = new Commands.CompareRevisions(_repo, startPoint.SHA, _endPoint).Result(); var visible = _changes; if (!string.IsNullOrWhiteSpace(_searchFilter)) @@ -162,7 +177,7 @@ namespace SourceGit.ViewModels diffWithMerger.Icon = App.CreateMenuIcon("Icons.Diff"); diffWithMerger.Click += (_, ev) => { - var opt = new Models.DiffOption(StartPoint.SHA, EndPoint.SHA, change); + var opt = new Models.DiffOption(StartPoint.SHA, _endPoint, change); var type = Preference.Instance.ExternalMergeToolType; var exec = Preference.Instance.ExternalMergeToolPath; @@ -234,6 +249,7 @@ namespace SourceGit.ViewModels } private string _repo = string.Empty; + private string _endPoint = string.Empty; private List _changes = null; private List _visibleChanges = null; private List _changeTree = null; diff --git a/src/Views/RevisionCompare.axaml b/src/Views/RevisionCompare.axaml index c541cfe2..ac3f91ee 100644 --- a/src/Views/RevisionCompare.axaml +++ b/src/Views/RevisionCompare.axaml @@ -11,39 +11,55 @@ x:DataType="vm:RevisionCompare" Background="{DynamicResource Brush.Window}"> - - + + - + - - + + + + + - + - - - - - - - - + + + + + + + + + + + + + + - - + + + + + + + + +