From 2fabe30db1c34ddb355238feedd80d934b382a0e Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Wed, 1 Apr 2026 13:26:07 -0400 Subject: [PATCH] Improve branch switch fallback with clearer warnings When the branch can't be switched to (e.g. it's checked out in another worktree), the create_branch fallback also fails if the branch already exists. Instead of silently discarding this error, log a clear warning explaining the worktree is in detached HEAD state. --- crates/sidebar/src/sidebar.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/sidebar/src/sidebar.rs b/crates/sidebar/src/sidebar.rs index 8925dc8508431c857bc20d3b87d495503b7ab76f..f12fb60edf15d76efccc05559ae69505fecc3140 100644 --- a/crates/sidebar/src/sidebar.rs +++ b/crates/sidebar/src/sidebar.rs @@ -2553,11 +2553,28 @@ impl Sidebar { let switch_ok = matches!(switch_receiver.await, Ok(Ok(()))); if !switch_ok { + // The branch may already exist but be checked out in + // another worktree. Attempt to create it in case it + // was deleted; if it already exists, just accept the + // detached HEAD and warn. let create_receiver = worktree_repo.update(cx, |repo, _cx| { repo.create_branch(original_branch.clone(), None) }); - if let Ok(result) = create_receiver.await { - result.log_err(); + match create_receiver.await { + Ok(Ok(())) => {} + Ok(Err(_)) => { + log::warn!( + "Could not switch to branch '{original_branch}' — \ + it may be checked out in another worktree. \ + The restored worktree is in detached HEAD state." + ); + } + Err(_) => { + log::warn!( + "Branch creation for '{original_branch}' was canceled; \ + the restored worktree is in detached HEAD state." + ); + } } } }