From dcaeaef48a45d84c23e39bb1d3aa57e15f47f78d Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 11 Dec 2024 11:33:20 +0800 Subject: [PATCH] refactor: re-design conflict panel --- src/ViewModels/Conflict.cs | 78 ++++++++++++++++++++++ src/ViewModels/WorkingCopy.cs | 118 ++++++++++++++-------------------- src/Views/WorkingCopy.axaml | 65 +++++++++++++++++-- 3 files changed, 187 insertions(+), 74 deletions(-) create mode 100644 src/ViewModels/Conflict.cs diff --git a/src/ViewModels/Conflict.cs b/src/ViewModels/Conflict.cs new file mode 100644 index 00000000..41a0a137 --- /dev/null +++ b/src/ViewModels/Conflict.cs @@ -0,0 +1,78 @@ +namespace SourceGit.ViewModels +{ + public class Conflict + { + public object Theirs + { + get; + private set; + } + + public object Mine + { + get; + private set; + } + + public bool IsResolved + { + get; + private set; + } + + public Conflict(Repository repo, WorkingCopy wc, Models.Change change) + { + _wc = wc; + _change = change; + + IsResolved = new Commands.IsConflictResolved(repo.FullPath, change).ReadToEnd().IsSuccess; + + var context = wc.InProgressContext; + if (context is CherryPickInProgress cherryPick) + { + Theirs = cherryPick.Head; + Mine = repo.CurrentBranch; + } + else if (context is RebaseInProgress rebase) + { + Theirs = repo.Branches.Find(x => x.IsLocal && x.Name == rebase.HeadName) ?? + new Models.Branch() + { + IsLocal = true, + Name = rebase.HeadName, + FullName = $"refs/heads/{rebase.HeadName}" + }; + + Mine = rebase.Onto; + } + else if (context is RevertInProgress revert) + { + Theirs = revert.Head; + Mine = repo.CurrentBranch; + } + else if (context is MergeInProgress merge) + { + Theirs = merge.Source; + Mine = repo.CurrentBranch; + } + } + + public void UseTheirs() + { + _wc.UseTheirs([_change]); + } + + public void UseMine() + { + _wc.UseMine([_change]); + } + + public void OpenExternalMergeTool() + { + _wc.UseExternalMergeTool(_change); + } + + private WorkingCopy _wc = null; + private Models.Change _change = null; + } +} diff --git a/src/ViewModels/WorkingCopy.cs b/src/ViewModels/WorkingCopy.cs index 3c6add56..d748b7ef 100644 --- a/src/ViewModels/WorkingCopy.cs +++ b/src/ViewModels/WorkingCopy.cs @@ -11,26 +11,6 @@ using CommunityToolkit.Mvvm.ComponentModel; namespace SourceGit.ViewModels { - public class ConflictContext : ObservableObject - { - public bool IsResolved - { - get => _isResolved; - set => SetProperty(ref _isResolved, value); - } - - public ConflictContext(string repo, Models.Change change) - { - Task.Run(() => - { - var result = new Commands.IsConflictResolved(repo, change).ReadToEnd().IsSuccess; - Dispatcher.UIThread.Post(() => IsResolved = result); - }); - } - - private bool _isResolved = false; - } - public class WorkingCopy : ObservableObject { public bool IncludeUntracked @@ -440,6 +420,54 @@ namespace SourceGit.ViewModels } } + public async void UseTheirs(List changes) + { + var files = new List(); + foreach (var change in changes) + { + if (change.IsConflit) + files.Add(change.Path); + } + + _repo.SetWatcherEnabled(false); + var succ = await Task.Run(() => new Commands.Checkout(_repo.FullPath).UseTheirs(files)); + if (succ) + { + await Task.Run(() => new Commands.Add(_repo.FullPath, changes).Exec()); + } + _repo.MarkWorkingCopyDirtyManually(); + _repo.SetWatcherEnabled(true); + } + + public async void UseMine(List changes) + { + var files = new List(); + foreach (var change in changes) + { + if (change.IsConflit) + files.Add(change.Path); + } + + _repo.SetWatcherEnabled(false); + var succ = await Task.Run(() => new Commands.Checkout(_repo.FullPath).UseMine(files)); + if (succ) + { + await Task.Run(() => new Commands.Add(_repo.FullPath, changes).Exec()); + } + _repo.MarkWorkingCopyDirtyManually(); + _repo.SetWatcherEnabled(true); + } + + public async void UseExternalMergeTool(Models.Change change) + { + var toolType = Preference.Instance.ExternalMergeToolType; + var toolPath = Preference.Instance.ExternalMergeToolPath; + + _repo.SetWatcherEnabled(false); + await Task.Run(() => Commands.MergeTool.OpenForMerge(_repo.FullPath, toolType, toolPath, change.Path)); + _repo.SetWatcherEnabled(true); + } + public void ContinueMerge() { if (_inProgressContext != null) @@ -1438,59 +1466,11 @@ namespace SourceGit.ViewModels if (change == null) DetailContext = null; else if (change.IsConflit && isUnstaged) - DetailContext = new ConflictContext(_repo.FullPath, change); + DetailContext = new Conflict(_repo, this, change); else DetailContext = new DiffContext(_repo.FullPath, new Models.DiffOption(change, isUnstaged), _detailContext as DiffContext); } - private async void UseTheirs(List changes) - { - var files = new List(); - foreach (var change in changes) - { - if (change.IsConflit) - files.Add(change.Path); - } - - _repo.SetWatcherEnabled(false); - var succ = await Task.Run(() => new Commands.Checkout(_repo.FullPath).UseTheirs(files)); - if (succ) - { - await Task.Run(() => new Commands.Add(_repo.FullPath, changes).Exec()); - } - _repo.MarkWorkingCopyDirtyManually(); - _repo.SetWatcherEnabled(true); - } - - private async void UseMine(List changes) - { - var files = new List(); - foreach (var change in changes) - { - if (change.IsConflit) - files.Add(change.Path); - } - - _repo.SetWatcherEnabled(false); - var succ = await Task.Run(() => new Commands.Checkout(_repo.FullPath).UseMine(files)); - if (succ) - { - await Task.Run(() => new Commands.Add(_repo.FullPath, changes).Exec()); - } - _repo.MarkWorkingCopyDirtyManually(); - _repo.SetWatcherEnabled(true); - } - - private async void UseExternalMergeTool(Models.Change change) - { - var toolType = Preference.Instance.ExternalMergeToolType; - var toolPath = Preference.Instance.ExternalMergeToolPath; - - _repo.SetWatcherEnabled(false); - await Task.Run(() => Commands.MergeTool.OpenForMerge(_repo.FullPath, toolType, toolPath, change.Path)); - _repo.SetWatcherEnabled(true); - } - private void DoCommit(bool autoStage, bool autoPush, bool allowEmpty) { if (!PopupHost.CanCreatePopup()) diff --git a/src/Views/WorkingCopy.axaml b/src/Views/WorkingCopy.axaml index 594a72b9..bd794f02 100644 --- a/src/Views/WorkingCopy.axaml +++ b/src/Views/WorkingCopy.axaml @@ -2,6 +2,7 @@ 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" xmlns:vm="using:SourceGit.ViewModels" xmlns:v="using:SourceGit.Views" xmlns:c="using:SourceGit.Converters" @@ -169,13 +170,67 @@ - + - + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +