Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Resources/Locales/en_US.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@
<x:String x:Key="Text.DeinitSubmodule.Path" xml:space="preserve">Submodule:</x:String>
<x:String x:Key="Text.DeleteBranch" xml:space="preserve">Delete Branch</x:String>
<x:String x:Key="Text.DeleteBranch.Branch" xml:space="preserve">Branch:</x:String>
<x:String x:Key="Text.DeleteBranch.DeleteWorktrees" xml:space="preserve">Delete worktrees for this branch</x:String>
<x:String x:Key="Text.DeleteBranch.Force" xml:space="preserve">Force delete even if it contains unmerged commit(s)</x:String>
<x:String x:Key="Text.DeleteBranch.IsRemoteTip" xml:space="preserve">You are about to delete a remote branch!!!</x:String>
<x:String x:Key="Text.DeleteBranch.WithTrackingRemote" xml:space="preserve">Also delete remote branch ${0}$</x:String>
Expand Down
48 changes: 48 additions & 0 deletions src/ViewModels/DeleteBranch.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SourceGit.ViewModels
Expand Down Expand Up @@ -32,6 +33,22 @@ public bool Force
set;
}

public List<Worktree> AttachedWorktrees
{
get;
} = [];

public bool HasAttachedWorktrees
{
get => AttachedWorktrees.Count > 0;
}

public bool DeleteWorktrees
{
get;
set;
}

public DeleteBranch(Repository repo, Models.Branch branch)
{
_repo = repo;
Expand All @@ -46,6 +63,15 @@ public DeleteBranch(Repository repo, Models.Branch branch)
DeleteUpstreamTip = App.Text("DeleteBranch.WithTrackingRemote", upstream.FriendlyName);
}
}

if (branch.IsLocal)
{
foreach (var wt in _repo.Worktrees)
{
if (!wt.IsMain && !wt.IsCurrent && wt.Backend.Branch.Equals(branch.FullName, StringComparison.Ordinal))
AttachedWorktrees.Add(wt);
}
}
}

public override async Task<bool> Sure()
Expand All @@ -61,6 +87,28 @@ public override async Task<bool> Sure()
{
do
{
if (DeleteWorktrees)
{
// Re-read worktrees, since a previous (failed) attempt may have already removed some of them.
succ = true;
var current = await new Commands.Worktree(_repo.FullPath).ReadAllAsync();
foreach (var wt in AttachedWorktrees)
{
if (!current.Exists(x => x.FullPath.Equals(wt.FullPath, StringComparison.Ordinal)))
continue;

succ = await new Commands.Worktree(_repo.FullPath)
.Use(log)
.RemoveAsync(wt.FullPath, Force);

if (!succ)
break;
}

if (!succ)
break;
}

succ = await new Commands.Branch(_repo.FullPath, Target.Name)
.Use(log)
.DeleteLocalAsync(Force);
Expand Down
11 changes: 9 additions & 2 deletions src/Views/DeleteBranch.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Text="{DynamicResource Text.DeleteBranch}"/>
</StackPanel>

<Grid Margin="0,16,8,0" RowDefinitions="32,Auto,Auto,Auto" ColumnDefinitions="120,*">
<Grid Margin="0,16,8,0" RowDefinitions="32,Auto,Auto,Auto,Auto" ColumnDefinitions="120,*">
<TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" Text="{DynamicResource Text.DeleteBranch.Branch}"/>
<StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal">
<Path Width="14" Height="14" Margin="8,0" Data="{StaticResource Icons.Branch}"/>
Expand Down Expand Up @@ -55,7 +55,14 @@
</CheckBox>
</Border>

<StackPanel Grid.Row="3" Grid.Column="1"
<Border Grid.Row="3" Grid.Column="1"
Height="32"
IsVisible="{Binding HasAttachedWorktrees, Mode=OneWay}">
<CheckBox Content="{DynamicResource Text.DeleteBranch.DeleteWorktrees}"
IsChecked="{Binding DeleteWorktrees, Mode=TwoWay}"/>
</Border>

<StackPanel Grid.Row="4" Grid.Column="1"
Height="32"
Orientation="Horizontal"
IsVisible="{Binding !Target.IsLocal, Mode=OneWay}">
Expand Down