mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-19 06:59:22 +01:00
worktree: Only treat a directory as a bare repository when HEAD and config are files (#67024)
## 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.
<img width="2040" height="1122" alt="截图Zed 1116101115"
src="https://github.com/user-attachments/assets/0629d47b-7cf1-41b2-95e3-0e9b631ca4ba"
/>
**After fixed**
<img width="1830" height="994" alt="截图zed 1116111127"
src="https://github.com/user-attachments/assets/fd1081c9-353e-48b0-b61a-eaf26ec6eb8e"
/>
---------
Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
This commit is contained in:
@@ -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<Gitignore> {
|
||||
@@ -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
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user