Skip to content

Commit 4e49f72

Browse files
committed
feature: support deleting attached worktree while deleting a single branch (#2806)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 7771c8c commit 4e49f72

4 files changed

Lines changed: 67 additions & 23 deletions

File tree

‎src/Resources/Locales/en_US.axaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@
348348
<x:String x:Key="Text.DeinitSubmodule.Path" xml:space="preserve">Submodule:</x:String>
349349
<x:String x:Key="Text.DeleteBranch" xml:space="preserve">Delete Branch</x:String>
350350
<x:String x:Key="Text.DeleteBranch.Branch" xml:space="preserve">Branch:</x:String>
351+
<x:String x:Key="Text.DeleteBranch.DeleteAttachedWorktree" xml:space="preserve">Delete worktree for this branch</x:String>
351352
<x:String x:Key="Text.DeleteBranch.Force" xml:space="preserve">Force delete even if it contains unmerged commit(s)</x:String>
352353
<x:String x:Key="Text.DeleteBranch.IsRemoteTip" xml:space="preserve">You are about to delete a remote branch!!!</x:String>
353354
<x:String x:Key="Text.DeleteBranch.WithTrackingRemote" xml:space="preserve">Also delete remote branch ${0}$</x:String>

‎src/Resources/Locales/zh_CN.axaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@
352352
<x:String x:Key="Text.DeinitSubmodule.Path" xml:space="preserve">子模块 :</x:String>
353353
<x:String x:Key="Text.DeleteBranch" xml:space="preserve">删除分支确认</x:String>
354354
<x:String x:Key="Text.DeleteBranch.Branch" xml:space="preserve">分支名 :</x:String>
355+
<x:String x:Key="Text.DeleteBranch.DeleteAttachedWorktree" xml:space="preserve">同时删除与使用此分支的工作树</x:String>
355356
<x:String x:Key="Text.DeleteBranch.Force" xml:space="preserve">强制删除,即使包含未合并的提交</x:String>
356357
<x:String x:Key="Text.DeleteBranch.IsRemoteTip" xml:space="preserve">您正在删除远程上的分支,请务必小心!!!</x:String>
357358
<x:String x:Key="Text.DeleteBranch.WithTrackingRemote" xml:space="preserve">同时删除远程分支 ${0}$</x:String>

‎src/ViewModels/DeleteBranch.cs‎

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public Models.Branch Upstream
1515
get;
1616
}
1717

18+
public Worktree AttachedWorktree
19+
{
20+
get;
21+
}
22+
1823
public string DeleteUpstreamTip
1924
{
2025
get;
@@ -32,18 +37,39 @@ public bool Force
3237
set;
3338
}
3439

40+
public bool DeleteWorktree
41+
{
42+
get;
43+
set;
44+
}
45+
3546
public DeleteBranch(Repository repo, Models.Branch branch)
3647
{
3748
_repo = repo;
3849
Target = branch;
3950

40-
if (branch.IsLocal && !string.IsNullOrEmpty(branch.Upstream))
51+
if (branch.IsLocal)
4152
{
42-
var upstream = _repo.Branches.Find(x => x.FullName.Equals(branch.Upstream, StringComparison.Ordinal));
43-
if (upstream != null && upstream.Name.Equals(branch.Name, StringComparison.Ordinal))
53+
if (!string.IsNullOrEmpty(branch.Upstream))
54+
{
55+
var upstream = _repo.Branches.Find(x => x.FullName.Equals(branch.Upstream, StringComparison.Ordinal));
56+
if (upstream != null && upstream.Name.Equals(branch.Name, StringComparison.Ordinal))
57+
{
58+
Upstream = upstream;
59+
DeleteUpstreamTip = App.Text("DeleteBranch.WithTrackingRemote", upstream.FriendlyName);
60+
}
61+
}
62+
63+
if (branch.HasWorktree)
4464
{
45-
Upstream = upstream;
46-
DeleteUpstreamTip = App.Text("DeleteBranch.WithTrackingRemote", upstream.FriendlyName);
65+
foreach (var wt in repo.Worktrees)
66+
{
67+
if (wt.IsAttachedTo(branch))
68+
{
69+
AttachedWorktree = wt;
70+
break;
71+
}
72+
}
4773
}
4874
}
4975
}
@@ -61,6 +87,19 @@ public override async Task<bool> Sure()
6187
{
6288
do
6389
{
90+
if (AttachedWorktree != null && DeleteWorktree)
91+
{
92+
succ = await new Commands.Worktree(_repo.FullPath)
93+
.Use(log)
94+
.RemoveAsync(AttachedWorktree.FullPath, Force);
95+
96+
if (!succ)
97+
{
98+
log.Complete();
99+
return false;
100+
}
101+
}
102+
64103
succ = await new Commands.Branch(_repo.FullPath, Target.Name)
65104
.Use(log)
66105
.DeleteLocalAsync(Force);

‎src/Views/DeleteBranch.axaml‎

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
Text="{DynamicResource Text.DeleteBranch}"/>
1919
</StackPanel>
2020

21-
<Grid Margin="0,16,8,0" RowDefinitions="32,Auto,Auto,Auto" ColumnDefinitions="120,*">
21+
<Grid Margin="0,16,8,0" RowDefinitions="32,Auto,Auto,Auto,Auto" ColumnDefinitions="120,*">
2222
<TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" Text="{DynamicResource Text.DeleteBranch.Branch}"/>
2323
<StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal">
2424
<Path Width="14" Height="14" Margin="8,0" Data="{StaticResource Icons.Branch}"/>
@@ -36,26 +36,29 @@
3636
Text="{Binding Target.TrackStatusDescription}"/>
3737
</Border>
3838
</StackPanel>
39+
40+
<CheckBox Grid.Row="1" Grid.Column="1"
41+
Height="32"
42+
IsChecked="{Binding DeleteUpstream, Mode=TwoWay}"
43+
IsVisible="{Binding Upstream, Mode=OneWay, Converter={x:Static ObjectConverters.IsNotNull}}">
44+
<v:NameHighlightedTextBlock Foreground="{DynamicResource Brush.FG1}"
45+
Text="{Binding DeleteUpstreamTip, Mode=OneWay}"
46+
VerticalAlignment="Center"/>
47+
</CheckBox>
3948

40-
<Border Grid.Row="1" Grid.Column="1"
41-
Height="32"
42-
IsVisible="{Binding Target.IsLocal, Mode=OneWay}">
43-
<CheckBox Content="{DynamicResource Text.DeleteBranch.Force}"
44-
IsChecked="{Binding Force, Mode=TwoWay}"
45-
ToolTip.Tip="--force"/>
46-
</Border>
49+
<CheckBox Grid.Row="2" Grid.Column="1"
50+
IsChecked="{Binding DeleteWorktree, Mode=TwoWay}"
51+
IsVisible="{Binding AttachedWorktree, Mode=OneWay, Converter={x:Static ObjectConverters.IsNotNull}}"
52+
Content="{DynamicResource Text.DeleteBranch.DeleteAttachedWorktree}"/>
4753

48-
<Border Grid.Row="2" Grid.Column="1"
49-
Height="32"
50-
IsVisible="{Binding Upstream, Mode=OneWay, Converter={x:Static ObjectConverters.IsNotNull}}">
51-
<CheckBox IsChecked="{Binding DeleteUpstream, Mode=TwoWay}">
52-
<v:NameHighlightedTextBlock Foreground="{DynamicResource Brush.FG1}"
53-
Text="{Binding DeleteUpstreamTip, Mode=OneWay}"
54-
VerticalAlignment="Center"/>
55-
</CheckBox>
56-
</Border>
54+
<CheckBox Grid.Row="3" Grid.Column="1"
55+
Height="32"
56+
Content="{DynamicResource Text.DeleteBranch.Force}"
57+
IsChecked="{Binding Force, Mode=TwoWay}"
58+
ToolTip.Tip="--force"
59+
IsVisible="{Binding Target.IsLocal, Mode=OneWay}"/>
5760

58-
<StackPanel Grid.Row="3" Grid.Column="1"
61+
<StackPanel Grid.Row="4" Grid.Column="1"
5962
Height="32"
6063
Orientation="Horizontal"
6164
IsVisible="{Binding !Target.IsLocal, Mode=OneWay}">

0 commit comments

Comments
 (0)