git_ui: Fix creating worktree not possible if default branch unavailable (#57918)

Follow up to #57704

This makes sure that we offer a worktree creation option in case
resolving the default branch fails

Release Notes:

- git: Fixed an issue where worktree creation would not be possible if
resolving default branch fails
This commit is contained in:
Bennet Bo Fenner 2026-05-28 16:15:40 +02:00 committed by GitHub
parent f0ed342c19
commit d74e47ea51
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -420,18 +420,18 @@ impl WorktreePickerDelegate {
fn build_fixed_entries(&self) -> Vec<WorktreeEntry> { fn build_fixed_entries(&self) -> Vec<WorktreeEntry> {
let mut entries = Vec::new(); let mut entries = Vec::new();
if !self.has_multiple_repositories { if self.has_multiple_repositories {
if let Some(ref default_branch) = self.default_branch { entries.push(WorktreeEntry::CreateFromCurrentBranch);
let is_different = self } else if let Some(ref default_branch) = self.default_branch {
.current_branch_name let is_different = self
.as_ref() .current_branch_name
.is_none_or(|current| current != &default_branch.branch_name); .as_ref()
entries.push(WorktreeEntry::CreateFromDefaultBranch { .is_none_or(|current| current != &default_branch.branch_name);
default_branch: default_branch.clone(), entries.push(WorktreeEntry::CreateFromDefaultBranch {
}); default_branch: default_branch.clone(),
if is_different { });
entries.push(WorktreeEntry::CreateFromCurrentBranch); if is_different {
} entries.push(WorktreeEntry::CreateFromCurrentBranch);
} }
} else { } else {
entries.push(WorktreeEntry::CreateFromCurrentBranch); entries.push(WorktreeEntry::CreateFromCurrentBranch);
@ -1539,6 +1539,37 @@ mod tests {
}); });
} }
#[gpui::test]
async fn test_current_branch_create_target_is_shown_without_default_branch(
cx: &mut TestAppContext,
) {
let (_fs, worktree_picker, _repository, _worktree_path, mut cx) =
init_worktree_picker_test(cx).await;
worktree_picker.update_in(&mut cx, |worktree_picker, window, cx| {
worktree_picker.picker.update(cx, |picker, cx| {
picker.delegate.default_branch = None;
picker.refresh(window, cx);
});
});
cx.run_until_parked();
worktree_picker.update(&mut cx, |worktree_picker, cx| {
worktree_picker.picker.update(cx, |picker, _| {
assert!(matches!(
picker.delegate.matches.first(),
Some(WorktreeEntry::CreateFromCurrentBranch)
));
assert!(
!picker.delegate.matches.iter().any(|entry| matches!(
entry,
WorktreeEntry::CreateFromDefaultBranch { .. }
))
);
});
});
}
#[gpui::test] #[gpui::test]
async fn test_delete_dirty_worktree_prompts_for_force_delete(cx: &mut TestAppContext) { async fn test_delete_dirty_worktree_prompts_for_force_delete(cx: &mut TestAppContext) {
let (fs, worktree_picker, repository, worktree_path, mut cx) = let (fs, worktree_picker, repository, worktree_path, mut cx) =