diff --git a/src/Resources/Locales/en_US.axaml b/src/Resources/Locales/en_US.axaml index d3ffb203a..1dd554e85 100644 --- a/src/Resources/Locales/en_US.axaml +++ b/src/Resources/Locales/en_US.axaml @@ -348,6 +348,7 @@ Submodule: Delete Branch Branch: + Delete worktrees for this branch Force delete even if it contains unmerged commit(s) You are about to delete a remote branch!!! Also delete remote branch ${0}$ diff --git a/src/ViewModels/DeleteBranch.cs b/src/ViewModels/DeleteBranch.cs index 483f321dd..12b0b49c2 100644 --- a/src/ViewModels/DeleteBranch.cs +++ b/src/ViewModels/DeleteBranch.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; namespace SourceGit.ViewModels @@ -32,6 +33,22 @@ public bool Force set; } + public List AttachedWorktrees + { + get; + } = []; + + public bool HasAttachedWorktrees + { + get => AttachedWorktrees.Count > 0; + } + + public bool DeleteWorktrees + { + get; + set; + } + public DeleteBranch(Repository repo, Models.Branch branch) { _repo = repo; @@ -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 Sure() @@ -61,6 +87,28 @@ public override async Task 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); diff --git a/src/Views/DeleteBranch.axaml b/src/Views/DeleteBranch.axaml index d27b11ea7..e9c0b4bfd 100644 --- a/src/Views/DeleteBranch.axaml +++ b/src/Views/DeleteBranch.axaml @@ -18,7 +18,7 @@ Text="{DynamicResource Text.DeleteBranch}"/> - + @@ -55,7 +55,14 @@ - + + + +