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]