Skip to content

Skip branches checked out in linked worktrees during pruning - #22004

Merged
niik merged 4 commits into
desktop:developmentfrom
KRRT7:fix/branch-pruner-worktree-safety
May 11, 2026
Merged

niik merged 4 commits into
desktop:developmentfrom
KRRT7:fix/branch-pruner-worktree-safety

Conversation

@KRRT7

@KRRT7 KRRT7 commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

Summary

The BranchPruner deletes local branches that are merged into the default branch and whose upstream remote branch no longer exists. It already skips reserved branches, the current HEAD, and recently checked out branches — but it has no awareness of git worktrees.

A branch checked out in a linked worktree gets deleted, leaving that worktree in a broken state.

Changes

  • Add getWorktreeCheckedOutBranches() in app/src/lib/git/worktree.ts — parses git worktree list --porcelain and returns the set of canonical refs checked out in linked worktrees
  • Export it from app/src/lib/git/index.ts
  • Call it from BranchPruner.pruneLocalBranches() and exclude matched refs from the deletion set

Test coverage

  • 5 unit tests for the porcelain parser: no worktrees, single linked, excludes main, multiple linked, detached HEAD
  • 1 integration test for the pruner: creates a linked worktree with a prune-eligible branch and verifies it survives

Coverage (Node built-in --experimental-test-coverage):

File Line % Branch % Funcs %
worktree.ts 100 93 100
branch-pruner.ts 94 78 83

Uncovered lines in branch-pruner.ts are pre-existing (start()/stop() timer lifecycle, upstream ref edge cases) — not introduced by this change.

Fixes

Fixes #8043

The BranchPruner deletes local branches that are merged and whose
upstream is gone. It already skips reserved branches, the current
branch, and recently checked out branches — but it has no awareness
of git worktrees. A branch checked out in a linked worktree would be
deleted, leaving that worktree in a broken state.

Add `getWorktreeCheckedOutBranches` which parses `git worktree list
--porcelain` and returns the set of canonical refs checked out in
linked worktrees. The pruner now excludes these refs from deletion.

Fixes #8043
@KRRT7
KRRT7 requested a review from a team as a code owner April 24, 2026 07:19
@KRRT7
KRRT7 requested a review from sergiou87 April 24, 2026 07:19
@github-actions github-actions Bot added external Pull request originating outside of the Desktop core team needs-triage unmet-requirements and removed needs-triage labels Apr 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your pull request! Unfortunately, it doesn't meet the minimum requirements for review:

  • None of the referenced issues have the help wanted label

Please update your PR to address the above. Requirements:

  1. Include a detailed description of what this PR does
  2. Link to an issue with the help wanted label (use Fixes #123 or Closes #123 if it resolves the issue)

This PR will be automatically closed in 7 days if these requirements are not met.

niik and others added 3 commits May 11, 2026 11:22
Parse git worktree list --porcelain output using NUL delimiters
instead of newlines, making it safe for paths containing special
characters.

Co-authored-by: Copilot <[email protected]>
The function name implies it returns all worktree-checked-out branches.
Excluding the main worktree was a caller-specific optimization that
leaked into a general-purpose function. The branch-pruner already
independently checks against HEAD so this is safe.

Co-authored-by: Copilot <[email protected]>

@niik niik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @KRRT7, thanks a bunch. This looks good, I've update this to parse the worktree output with null delimiters and a few other small changes but this is a solid improvement! I'm gonna approve but I imagine I'll have to get another one of my colleagues to do so as well given that I've contributed to the branch. We'll get this merged shortly

@niik
niik enabled auto-merge May 11, 2026 09:44
@niik
niik merged commit 1e26db0 into desktop:development May 11, 2026
9 checks passed
@KRRT7

KRRT7 commented May 12, 2026

Copy link
Copy Markdown
Contributor Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external Pull request originating outside of the Desktop core team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BranchPruner does not skip branches in use in worktrees

3 participants