From 94daa46db9ae370679927ee3e004b964cb3af601 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 9 Dec 2024 21:12:58 +0800 Subject: [PATCH] code_review: PR #793 * do NOT modify the existing merge, and add a new constructor for `Commands.Merge` instead * rewrite `ViewModels.MergeMultiple` - since `_histories.Commits.Find` may returns null, use `List` instead of `List` - supports display merge target as both `Models.Commit` and `Models.Branch` * rename translation key `Text.MergeMultiple.Commit` to `Text.MergeMultiple.Targets`, and add translations for zh_CN and zh_TW * some UI/UX changes --- src/Commands/Merge.cs | 31 ++++++++++++++--- src/Models/MergeStrategy.cs | 4 +-- src/Resources/Locales/en_US.axaml | 6 ++-- src/Resources/Locales/zh_CN.axaml | 6 ++++ src/Resources/Locales/zh_TW.axaml | 6 ++++ src/ViewModels/Merge.cs | 2 +- src/ViewModels/MergeMultiple.cs | 58 ++++++++++++++++++++++++------- src/ViewModels/Pull.cs | 2 +- src/ViewModels/Repository.cs | 3 +- src/Views/MergeMultiple.axaml | 44 +++++++++++++++-------- 10 files changed, 122 insertions(+), 40 deletions(-) diff --git a/src/Commands/Merge.cs b/src/Commands/Merge.cs index a524af05..bd1f3653 100644 --- a/src/Commands/Merge.cs +++ b/src/Commands/Merge.cs @@ -1,18 +1,41 @@ using System; +using System.Collections.Generic; +using System.Text; namespace SourceGit.Commands { public class Merge : Command { - public Merge(string repo, string source, string mode, string strategy, Action outputHandler) + public Merge(string repo, string source, string mode, Action outputHandler) { _outputHandler = outputHandler; WorkingDirectory = repo; Context = repo; TraitErrorAsOutput = true; - if (strategy != null) - strategy = string.Concat("--strategy=", strategy); - Args = $"merge --progress {strategy} {source} {mode}"; + Args = $"merge --progress {source} {mode}"; + } + + public Merge(string repo, List targets, bool autoCommit, string strategy, Action outputHandler) + { + _outputHandler = outputHandler; + WorkingDirectory = repo; + Context = repo; + TraitErrorAsOutput = true; + + var builder = new StringBuilder(); + builder.Append("merge --progress "); + if (!string.IsNullOrEmpty(strategy)) + builder.Append($"--strategy={strategy} "); + if (!autoCommit) + builder.Append("--no-commit "); + + foreach (var t in targets) + { + builder.Append(t); + builder.Append(' '); + } + + Args = builder.ToString(); } protected override void OnReadline(string line) diff --git a/src/Models/MergeStrategy.cs b/src/Models/MergeStrategy.cs index a79bcf59..ab1d446b 100644 --- a/src/Models/MergeStrategy.cs +++ b/src/Models/MergeStrategy.cs @@ -9,10 +9,10 @@ namespace SourceGit.Models public string Arg { get; internal set; } public static List ForMultiple { get; private set; } = [ - new MergeStrategy(string.Empty, "Let Git automatically select a strategy", null), + new MergeStrategy("Default", "Let Git automatically select a strategy", string.Empty), new MergeStrategy("Octopus", "Attempt merging multiple heads", "octopus"), new MergeStrategy("Ours", "Record the merge without modifying the tree", "ours"), - ]; + ]; public MergeStrategy(string n, string d, string a) { diff --git a/src/Resources/Locales/en_US.axaml b/src/Resources/Locales/en_US.axaml index c50f3c4f..2369c3a6 100644 --- a/src/Resources/Locales/en_US.axaml +++ b/src/Resources/Locales/en_US.axaml @@ -58,7 +58,7 @@ Fetch ${0}$ into ${1}$... Git Flow - Finish ${0}$ Merge ${0}$ into ${1}$... - Merge selected {0} branches + Merge selected {0} branches into current Pull ${0}$ Pull ${0}$ into ${1}$... Push ${0}$ @@ -406,10 +406,10 @@ Into: Merge Option: Source Branch: - Merge commits - Commit(s): + Merge (Multiple) Commit all changes Strategy: + Targets: Move Repository Node Select parent node for: Name: diff --git a/src/Resources/Locales/zh_CN.axaml b/src/Resources/Locales/zh_CN.axaml index d567abaa..95224ec2 100644 --- a/src/Resources/Locales/zh_CN.axaml +++ b/src/Resources/Locales/zh_CN.axaml @@ -61,6 +61,7 @@ 拉取(fetch) ${0}$ 至 ${1}$... GIT工作流 - 完成 ${0}$ 合并 ${0}$ 到 ${1}$... + 合并 {0} 个分支到当前分支 拉回(pull) ${0}$ 拉回(pull) ${0}$ 内容至 ${1}$... 推送(push)${0}$ @@ -113,6 +114,7 @@ 复制提交指纹 自定义操作 交互式变基(rebase -i) ${0}$ 到此处 + 合并(merge)... 变基(rebase) ${0}$ 到此处 重置(reset) ${0}$ 到此处 回滚此提交 @@ -407,6 +409,10 @@ 目标分支 : 合并方式 : 合并分支 : + 合并(多目标) + 提交变化 + 合并策略 : + 目标列表 : 调整仓库分组 请选择目标分组: 名称 : diff --git a/src/Resources/Locales/zh_TW.axaml b/src/Resources/Locales/zh_TW.axaml index 2803c400..6eb31d6b 100644 --- a/src/Resources/Locales/zh_TW.axaml +++ b/src/Resources/Locales/zh_TW.axaml @@ -61,6 +61,7 @@ 提取 (fetch) ${0}$ 到 ${1}$... Git 工作流 - 完成 ${0}$ 合併 ${0}$ 到 ${1}$... + 合併 {0} 個分支到目前分支 拉取 (pull) ${0}$ 拉取 (pull) ${0}$ 內容至 ${1}$... 推送 (push) ${0}$ @@ -113,6 +114,7 @@ 複製提交編號 自訂動作 互動式重定基底 (rebase -i) ${0}$ 到此處 + 合併 (merge)... 重定基底 (rebase) ${0}$ 到此處 重設 (reset) ${0}$ 到此處 復原此提交 @@ -407,6 +409,10 @@ 目標分支: 合併方式: 合併分支: + 合併(多目標) + 提交變更 + 合併策略: + 目標列表: 調整存放庫分組 請選擇目標分組: 名稱: diff --git a/src/ViewModels/Merge.cs b/src/ViewModels/Merge.cs index 116797a8..b7630101 100644 --- a/src/ViewModels/Merge.cs +++ b/src/ViewModels/Merge.cs @@ -37,7 +37,7 @@ namespace SourceGit.ViewModels return Task.Run(() => { - var succ = new Commands.Merge(_repo.FullPath, Source, SelectedMode.Arg, null, SetProgressDescription).Exec(); + var succ = new Commands.Merge(_repo.FullPath, Source, SelectedMode.Arg, SetProgressDescription).Exec(); CallUIThread(() => _repo.SetWatcherEnabled(true)); return succ; }); diff --git a/src/ViewModels/MergeMultiple.cs b/src/ViewModels/MergeMultiple.cs index 03d24773..dd984a15 100644 --- a/src/ViewModels/MergeMultiple.cs +++ b/src/ViewModels/MergeMultiple.cs @@ -1,18 +1,15 @@ using System.Collections.Generic; using System.Threading.Tasks; -using SourceGit.Models; namespace SourceGit.ViewModels { public class MergeMultiple : Popup { - public List Strategies = ["octopus", "ours"]; - - public List Targets + public List Targets { get; private set; - } + } = []; public bool AutoCommit { @@ -20,18 +17,27 @@ namespace SourceGit.ViewModels set; } - public MergeStrategy Strategy + public Models.MergeStrategy Strategy { get; set; } - public MergeMultiple(Repository repo, List targets) + public MergeMultiple(Repository repo, List commits) { _repo = repo; - Targets = targets; + Targets.AddRange(commits); AutoCommit = true; - Strategy = MergeStrategy.ForMultiple.Find(s => s.Arg == null); + Strategy = Models.MergeStrategy.ForMultiple[0]; + View = new Views.MergeMultiple() { DataContext = this }; + } + + public MergeMultiple(Repository repo, List branches) + { + _repo = repo; + Targets.AddRange(branches); + AutoCommit = true; + Strategy = Models.MergeStrategy.ForMultiple[0]; View = new Views.MergeMultiple() { DataContext = this }; } @@ -44,9 +50,9 @@ namespace SourceGit.ViewModels { var succ = new Commands.Merge( _repo.FullPath, - string.Join(" ", Targets.ConvertAll(c => c.Decorators.Find(d => d.Type == DecoratorType.RemoteBranchHead || d.Type == DecoratorType.LocalBranchHead)?.Name ?? c.Decorators.Find(d => d.Type == DecoratorType.Tag)?.Name ?? c.SHA)), - AutoCommit ? string.Empty : "--no-commit", - Strategy?.Arg, + ConvertTargetToMergeSources(), + AutoCommit, + Strategy.Arg, SetProgressDescription).Exec(); CallUIThread(() => _repo.SetWatcherEnabled(true)); @@ -54,6 +60,34 @@ namespace SourceGit.ViewModels }); } + private List ConvertTargetToMergeSources() + { + var ret = new List(); + foreach (var t in Targets) + { + if (t is Models.Branch branch) + { + ret.Add(branch.FriendlyName); + } + else if (t is Models.Commit commit) + { + var d = commit.Decorators.Find(x => + { + return x.Type == Models.DecoratorType.LocalBranchHead || + x.Type == Models.DecoratorType.RemoteBranchHead || + x.Type == Models.DecoratorType.Tag; + }); + + if (d != null) + ret.Add(d.Name); + else + ret.Add(commit.SHA); + } + } + + return ret; + } + private readonly Repository _repo = null; } } diff --git a/src/ViewModels/Pull.cs b/src/ViewModels/Pull.cs index 2d0e8da2..e7c62980 100644 --- a/src/ViewModels/Pull.cs +++ b/src/ViewModels/Pull.cs @@ -172,7 +172,7 @@ namespace SourceGit.ViewModels else { SetProgressDescription($"Merge {_selectedBranch.FriendlyName} into {_current.Name} ..."); - rs = new Commands.Merge(_repo.FullPath, _selectedBranch.FriendlyName, "", null, SetProgressDescription).Exec(); + rs = new Commands.Merge(_repo.FullPath, _selectedBranch.FriendlyName, "", SetProgressDescription).Exec(); } } else diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index d593171e..fd4899a4 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -13,7 +13,6 @@ using Avalonia.Media.Imaging; using Avalonia.Threading; using CommunityToolkit.Mvvm.ComponentModel; -using SourceGit.Models; namespace SourceGit.ViewModels { @@ -954,7 +953,7 @@ namespace SourceGit.ViewModels public void MergeMultipleBranches(List branches) { if (PopupHost.CanCreatePopup()) - PopupHost.ShowPopup(new MergeMultiple(this, branches.ConvertAll(b => _histories?.Commits?.Find(c => c.SHA == b.Head)))); + PopupHost.ShowPopup(new MergeMultiple(this, branches)); } public void CreateNewTag() diff --git a/src/Views/MergeMultiple.axaml b/src/Views/MergeMultiple.axaml index 01a6b976..9fcdaa3d 100644 --- a/src/Views/MergeMultiple.axaml +++ b/src/Views/MergeMultiple.axaml @@ -12,13 +12,14 @@ + + Text="{DynamicResource Text.MergeMultiple.Targets}"/> - - - - - - - + + + + + + + + + + + + + + + + + + + + - - - - + +