git_ui: Don't offer git init for single-file worktrees (#64948)
## Summary
Closes #62008
When a single file is opened as the project (for example `zed -n
~/demo/plain.txt`), the worktree root is the file itself. The Git panel
still showed **Initialize Repository**, and clicking it always failed
with a raw `Not a directory (os error 20)` toast. That's because `git
init` was run with the file as its working directory.
This change:
- ignores single-file worktrees when choosing where to run `git init`.
If only single-file worktrees are open, `git: init` shows the existing
"Unable to initialize a git repository / Open a directory first" prompt
instead of the OS error.
- hides the **Initialize Repository** button in the Git panel when no
directory worktree is open. The "No Git Repositories" label stays.
- replaces the `worktrees.first().unwrap()` in `git_init` with a slice
pattern.
When directory and single-file worktrees are open together, the
directory ones can still be initialized as before.
## Screenshots
`plain.txt` opened on its own as the project, with the Git panel open.
**Before**:

**After**:

## Testing
Added `test_git_init_ignores_single_file_worktrees`. It opens a single
file as the project, runs `git_init`, and checks that the prompt appears
and that no `.git` directory is created. The test fails without the fix
and passes with it. All `git_ui` tests pass.
Manual steps:
1. `mkdir ~/demo && echo hello > ~/demo/plain.txt`
2. `zed -n ~/demo/plain.txt`
3. Open the Git panel. It shows "No Git Repositories" with no
**Initialize Repository** button.
4. Run `git: init` from the command palette. It shows "Open a directory
first" instead of `Not a directory (os error 20)`.
## Self-review
- [x] I've reviewed my diff for quality, security, reliability, and
performance.
- [x] UI changes follow the
[checklist](https://zed.dev/docs/development/ui-checklist).
- [x] Tests cover the new or changed behavior.
Release Notes:
- Fixed the Git panel offering to initialize a repository when a single
file is opened as the project, which always failed with "Not a
directory".
74c134a3c1Viraj Bhartiya committed on 10/1/2026, 6:12:24 AM· committed by GitHubparentf8c2cc8