From e5be9608ddd4cb0f2f179e1c0014f0a3a0337260 Mon Sep 17 00:00:00 2001 From: Likeai <65346310+likeaixxx@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:14:48 +0000 Subject: [PATCH] worktree: Only treat a directory as a bare repository when HEAD and config are files (#67024) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary `is_dot_git` decides whether a directory is a bare repository by checking that a `HEAD` and a `config` entry exist inside it. The check only tests for existence, so any directory that happens to contain entries with those names is treated as a bare repository. A small Rust crate is enough to trigger this: exp/ ├── Cargo.toml └── src/ ├── main.rs └── exp/ ├── mod.rs ├── head/ │ └── mod.rs └── config/ └── mod.rs where `src/exp/mod.rs` simply declares the two modules: pub mod config; pub mod head; On case-insensitive filesystems (macOS by default), `src/exp/HEAD` resolves to the `head` module directory, and `src/exp/config` exists, so `src/exp` matches the bare-repository heuristic. Zed then registers its parent, the ordinary source directory `src/`, as a Git repository. That repository has no working directory, so opening it fails in `Repository::check_access` with `bare repositories do not have a working directory`. The Git panel maps any access failure to `GitAccess::No` and shows the "detected dubious ownership" empty state, where **Trust Directory** cannot help, since the real problem is not ownership. Because the Git panel follows the repository of the active file, opening `src/exp/config/mod.rs` silently switches away from the real repository to the bogus `src` entry. The stale entry survives restarts until the project root is reopened. This is unrelated to linked worktrees; the same layout reproduces in a plain checkout. On case-sensitive filesystems the directory must literally be named `HEAD` to match. This change requires both entries to be files rather than merely present, so directories named `HEAD`/`head` or `config` no longer register as Git metadata. The `.git` fast path is untouched, and real bare repositories still match because their `HEAD` and `config` are files (symlinked `HEAD` included, as `Fs::is_file` resolves symlinks). ## Testing Reproduced and manually verified on macOS with a case-insensitive volume: 1. Create a crate named `exp` with modules `exp::head` and `exp::config` (`src/exp/head/mod.rs`, `src/exp/config/mod.rs`) and commit it. 2. Open the crate in Zed and then open `src/exp/config/mod.rs`. 3. Before: the repository selector lists two repositories, `exp` and a bogus `src`, and the Git panel for `src` shows the dubious-ownership empty state. Selecting `exp` restores Git, but opening the file again switches back to `src`. After: only `exp` is listed, and status, staging and committing work for files under `src/exp/`. 4. Confirm no regression for real bare repositories by opening a bare clone and a linked worktree of it; both are still detected and their Git features work. No automated test was added for this case. ## 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 Git repository detection treating any directory containing `HEAD` and `config` entries as a bare repository. 截图Zed 1116101115 **After fixed** 截图zed 1116111127 --------- Co-authored-by: Kirill Bulatov --- crates/worktree/src/worktree.rs | 81 +++++++++++++++++-- .../tests/integration/worktree_tests.rs | 15 ++-- 2 files changed, 78 insertions(+), 18 deletions(-) diff --git a/crates/worktree/src/worktree.rs b/crates/worktree/src/worktree.rs index 4ca56be7faa..83722f81bcf 100644 --- a/crates/worktree/src/worktree.rs +++ b/crates/worktree/src/worktree.rs @@ -3741,13 +3741,19 @@ async fn is_dot_git(path: &Path, fs: &dyn Fs) -> bool { // If we're in a bare repository, we are not inside a `.git` folder. In a // bare repository, the root folder contains what would normally be in the - // `.git` folder. - let head_metadata = fs.metadata(&path.join("HEAD")).await; - if !matches!(head_metadata, Ok(Some(_))) { - return false; - } - let config_metadata = fs.metadata(&path.join("config")).await; - matches!(config_metadata, Ok(Some(_))) + // `.git` folder, so `HEAD` and `config` must be files: source directories + // can contain entries with those names (`head` matches `HEAD` on + // case-insensitive filesystems). + // + // `HEAD` may be a symlink (`core.preferSymlinkRefs`) that dangles once its + // ref only lives in `packed-refs`; Git resolves it by name, so a broken + // link still counts. + fs.is_file(&path.join("config")).await && is_file_or_symlink(&path.join("HEAD"), fs).await +} + +/// Whether the path is a file or a symlink, without resolving the link. +async fn is_file_or_symlink(path: &Path, fs: &dyn Fs) -> bool { + fs.is_file(path).await || fs.read_link(path).await.is_ok() } async fn build_gitignore(abs_path: &Path, fs: &dyn Fs) -> Result { @@ -7440,7 +7446,7 @@ mod tests { use super::{ BackgroundScanner, BackgroundScannerPhase, BackgroundScannerState, IgnoreStack, NullWatcher, RemovedEntries, STREAM_BLOCK_BYTES, ScanRequest, UpdateIgnoreStatusJob, - Worktree, stream_utf8_into_rope, + Worktree, is_dot_git, stream_utf8_into_rope, }; use collections::{HashMap, HashSet}; use fs::{FakeFs, Fs, RemoveOptions, RenameOptions}; @@ -7718,6 +7724,65 @@ mod tests { } } + #[gpui::test] + async fn test_is_dot_git(cx: &mut TestAppContext) { + let fs = FakeFs::new(cx.background_executor.clone()); + fs.insert_tree( + path!("/root"), + json!({ + "bare": { + "HEAD": "ref: refs/heads/main\n", + "config": "[core]\n\trepositoryformatversion = 0\n", + }, + "head_dir": { + "HEAD": {}, + "config": "[core]\n", + }, + "config_dir": { + "HEAD": "ref: refs/heads/main\n", + "config": {}, + }, + "dangling_head": { + "config": "[core]\n", + }, + "dangling_config": { + "HEAD": "ref: refs/heads/main\n", + }, + "source": { + "head": {}, + "config": "[core]\n", + }, + }), + ) + .await; + fs.create_symlink( + Path::new(path!("/root/dangling_head/HEAD")), + "refs/heads/main".into(), + ) + .await + .unwrap(); + fs.create_symlink( + Path::new(path!("/root/dangling_config/config")), + "missing".into(), + ) + .await + .unwrap(); + + assert!(is_dot_git(Path::new(path!("/root/bare")), fs.as_ref()).await); + assert!(!is_dot_git(Path::new(path!("/root/head_dir")), fs.as_ref()).await); + assert!(!is_dot_git(Path::new(path!("/root/config_dir")), fs.as_ref()).await); + // With `core.preferSymlinkRefs`, `HEAD` can point at a ref that only + // exists in `packed-refs`, so a dangling link still counts. + assert!(is_dot_git(Path::new(path!("/root/dangling_head")), fs.as_ref()).await); + // `config` has to resolve to a real file. + assert!(!is_dot_git(Path::new(path!("/root/dangling_config")), fs.as_ref()).await); + + // A `head` directory must not be mistaken for `HEAD` on case-insensitive filesystems. + fs.set_case_sensitive(false); + assert!(!is_dot_git(Path::new(path!("/root/source")), fs.as_ref()).await); + assert!(!is_dot_git(Path::new(path!("/root/source/head")), fs.as_ref()).await); + } + #[gpui::test] async fn test_stream_utf8_falls_back_on_non_utf8() { // Each of these must bail so the caller re-reads and decodes the slow diff --git a/crates/worktree/tests/integration/worktree_tests.rs b/crates/worktree/tests/integration/worktree_tests.rs index 6b97d3550b4..479942b2ecf 100644 --- a/crates/worktree/tests/integration/worktree_tests.rs +++ b/crates/worktree/tests/integration/worktree_tests.rs @@ -2125,20 +2125,15 @@ async fn test_open_gitignored_files(cx: &mut TestAppContext) { let path = PathBuf::from("/root/one/node_modules/c/lib"); - // No work happens when files and directories change within an unloaded directory. - let prev_fs_call_count = fs.read_dir_call_count() + fs.metadata_call_count(); - // When we open a directory, we check each ancestor whether it's a git - // repository. That means we have an fs.metadata call per ancestor that we - // need to subtract here. - let ancestors = path.ancestors().count(); + // No work happens when files and directories change within an unloaded + // directory. Checking whether ancestors are git repositories uses `is_file` + // and `read_link`, which `FakeFs` does not count as directory scans. + let prev_read_dir_count = fs.read_dir_call_count(); fs.create_dir(path.as_ref()).await.unwrap(); cx.executor().run_until_parked(); - assert_eq!( - fs.read_dir_call_count() + fs.metadata_call_count() - prev_fs_call_count - ancestors, - 0 - ); + assert_eq!(fs.read_dir_call_count(), prev_read_dir_count); } #[gpui::test]