Skip to content

Commit 9f9b8f3

Browse files
committed
enhance: auto select tracking branch after Tracking remote branch option is toggled while creating worktree (#1983)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 2e20b0e commit 9f9b8f3

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/ViewModels/AddWorktree.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel.DataAnnotations;
34
using System.IO;
45
using System.Threading.Tasks;
@@ -51,7 +52,11 @@ public string SelectedBranch
5152
public bool SetTrackingBranch
5253
{
5354
get => _setTrackingBranch;
54-
set => SetProperty(ref _setTrackingBranch, value);
55+
set
56+
{
57+
if (SetProperty(ref _setTrackingBranch, value))
58+
AutoSelectTrackingBranch();
59+
}
5560
}
5661

5762
public string SelectedTrackingBranch
@@ -73,11 +78,6 @@ public AddWorktree(Repository repo)
7378
else
7479
RemoteBranches.Add(branch.FriendlyName);
7580
}
76-
77-
if (RemoteBranches.Count > 0)
78-
SelectedTrackingBranch = RemoteBranches[0];
79-
else
80-
SelectedTrackingBranch = string.Empty;
8181
}
8282

8383
public static ValidationResult ValidateWorktreePath(string path, ValidationContext ctx)
@@ -123,6 +123,23 @@ public override async Task<bool> Sure()
123123
return succ;
124124
}
125125

126+
private void AutoSelectTrackingBranch()
127+
{
128+
if (!_setTrackingBranch || RemoteBranches.Count == 0)
129+
return;
130+
131+
var name = string.IsNullOrEmpty(_selectedBranch) ? System.IO.Path.GetFileName(_path.TrimEnd('/', '\\')) : _selectedBranch;
132+
var remoteBranch = RemoteBranches.Find(b => b.Substring(b.IndexOf('/') + 1).Equals(name, StringComparison.Ordinal));
133+
if (string.IsNullOrEmpty(remoteBranch))
134+
remoteBranch = RemoteBranches[0];
135+
136+
if (!remoteBranch.Equals(SelectedTrackingBranch, StringComparison.Ordinal))
137+
{
138+
SelectedTrackingBranch = remoteBranch;
139+
OnPropertyChanged(nameof(SelectedTrackingBranch));
140+
}
141+
}
142+
126143
private Repository _repo = null;
127144
private string _path = string.Empty;
128145
private bool _createNewBranch = true;

src/Views/AddWorktree.axaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:vm="using:SourceGit.ViewModels"
6+
xmlns:c="using:SourceGit.Converters"
67
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
78
x:Class="SourceGit.Views.AddWorktree"
89
x:DataType="vm:AddWorktree">
@@ -17,7 +18,7 @@
1718
Text="{DynamicResource Text.AddWorktree}"/>
1819
</StackPanel>
1920

20-
<Grid Margin="0,16,0,0" RowDefinitions="32,32,32,Auto,32" ColumnDefinitions="150,*">
21+
<Grid Margin="0,16,0,0" RowDefinitions="32,32,32,Auto,Auto" ColumnDefinitions="150,*">
2122
<TextBlock Grid.Row="0" Grid.Column="0"
2223
HorizontalAlignment="Right" VerticalAlignment="Center"
2324
Margin="0,0,8,0"
@@ -101,8 +102,10 @@
101102
</ComboBox>
102103

103104
<CheckBox Grid.Row="4" Grid.Column="1"
105+
Height="32"
104106
Content="{DynamicResource Text.AddWorktree.Tracking.Toggle}"
105-
IsChecked="{Binding SetTrackingBranch, Mode=TwoWay}"/>
107+
IsChecked="{Binding SetTrackingBranch, Mode=TwoWay}"
108+
IsVisible="{Binding RemoteBranches, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}"/>
106109
</Grid>
107110
</StackPanel>
108111
</UserControl>

0 commit comments

Comments
 (0)