Improve branch switch fallback with clearer warnings

Richard Feldman created

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.

Change summary

crates/sidebar/src/sidebar.rs | 21 +++++++++++++++++++--
1 file changed, 19 insertions(+), 2 deletions(-)

Detailed changes

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."
+                                );
+                            }
                         }
                     }
                 }