From 3e54ab0227a8c5e9a03a88007ba286f75dc920f5 Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 28 Jun 2024 14:11:13 +0800 Subject: [PATCH] enhance: makes `Create New Branch` option as the default selected mode for adding new worktree --- src/Commands/Worktree.cs | 9 +++++++-- src/ViewModels/AddWorktree.cs | 20 ++++++++------------ src/Views/AddWorktree.axaml | 30 +++++++++++++++--------------- 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/Commands/Worktree.cs b/src/Commands/Worktree.cs index 31814427..a0c57df7 100644 --- a/src/Commands/Worktree.cs +++ b/src/Commands/Worktree.cs @@ -62,7 +62,7 @@ namespace SourceGit.Commands return worktrees; } - public bool Add(string fullpath, string name, string tracking, Action outputHandler) + public bool Add(string fullpath, string name, bool createNew, string tracking, Action outputHandler) { Args = "worktree add "; @@ -70,7 +70,12 @@ namespace SourceGit.Commands Args += "--track "; if (!string.IsNullOrEmpty(name)) - Args += $"-B {name} "; + { + if (createNew) + Args += $"-b {name} "; + else + Args += $"-B {name} "; + } Args += $"\"{fullpath}\" "; diff --git a/src/ViewModels/AddWorktree.cs b/src/ViewModels/AddWorktree.cs index c034fb75..04cda5b5 100644 --- a/src/ViewModels/AddWorktree.cs +++ b/src/ViewModels/AddWorktree.cs @@ -15,17 +15,17 @@ namespace SourceGit.ViewModels set => SetProperty(ref _path, value); } - public bool UseExistingBranch + public bool CreateNewBranch { - get => _useExistingBranch; + get => _createNewBranch; set { - if (SetProperty(ref _useExistingBranch, value, true)) + if (SetProperty(ref _createNewBranch, value, true)) { if (value) - SelectedBranch = LocalBranches.Count > 0 ? LocalBranches[0] : string.Empty; - else SelectedBranch = string.Empty; + else + SelectedBranch = LocalBranches.Count > 0 ? LocalBranches[0] : string.Empty; } } } @@ -74,11 +74,6 @@ namespace SourceGit.ViewModels RemoteBranches.Add($"{branch.Remote}/{branch.Name}"); } - if (LocalBranches.Count > 0) - SelectedBranch = LocalBranches[0]; - else - SelectedBranch = string.Empty; - if (RemoteBranches.Count > 0) SelectedTrackingBranch = RemoteBranches[0]; else @@ -114,11 +109,12 @@ namespace SourceGit.ViewModels _repo.SetWatcherEnabled(false); ProgressDescription = "Adding worktree ..."; + var branchName = _selectedBranch; var tracking = _setTrackingBranch ? SelectedTrackingBranch : string.Empty; return Task.Run(() => { - var succ = new Commands.Worktree(_repo.FullPath).Add(_path, _selectedBranch, tracking, SetProgressDescription); + var succ = new Commands.Worktree(_repo.FullPath).Add(_path, branchName, _createNewBranch, tracking, SetProgressDescription); CallUIThread(() => _repo.SetWatcherEnabled(true)); return succ; }); @@ -126,7 +122,7 @@ namespace SourceGit.ViewModels private Repository _repo = null; private string _path = string.Empty; - private bool _useExistingBranch = true; + private bool _createNewBranch = true; private string _selectedBranch = string.Empty; private bool _setTrackingBranch = false; } diff --git a/src/Views/AddWorktree.axaml b/src/Views/AddWorktree.axaml index 4d75d136..13acd2aa 100644 --- a/src/Views/AddWorktree.axaml +++ b/src/Views/AddWorktree.axaml @@ -36,25 +36,32 @@ Margin="0,0,8,0" Text="{DynamicResource Text.AddWorktree.WhatToCheckout}"/> - + GroupName="WhatToCheckout" + IsChecked="{Binding CreateNewBranch, Mode=TwoWay}"/> + + + IsEnabled="{Binding !CreateNewBranch, Mode=OneWay}" + IsVisible="{Binding !CreateNewBranch, Mode=OneWay}"> @@ -63,14 +70,7 @@ - - +