Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Register Git submodules under their own project path (#61859)

Ayush Patil998fbf30ff29
diff --git a/crates/project/src/git_store.rs b/crates/project/src/git_store.rs
index 2c279742dfc..91426b8bd00 100644
--- a/crates/project/src/git_store.rs
+++ b/crates/project/src/git_store.rs
@@ -9569,6 +9569,12 @@ pub async fn resolve_git_worktree_to_main_repo(fs: &dyn Fs, path: &Path) -> Opti
let content = fs.load(&dot_git).await.ok()?;
let gitdir_rel = content.strip_prefix("gitdir:")?.trim();
let gitdir_abs = fs.canonicalize(&path.join(gitdir_rel)).await.ok()?;
+ // Submodules also use a `.git` file, but they are independent projects whose
+ // identity is their own working directory (`path`), not the superproject's
+ // `.git/modules/<name>` git dir. Leave them unresolved.
+ if is_submodule_git_dir(&gitdir_abs) {
+ return None;
+ }
// Read commondir to find the main .git directory
let commondir_content = fs.load(&gitdir_abs.join("commondir")).await.ok()?;
let common_dir = fs
@@ -9706,6 +9712,28 @@ pub fn repo_identity_path(common_dir: &Path) -> &Path {
}
}
+/// Returns true if `git_dir` is a Git submodule's git directory.
+///
+/// Submodules store their git directory inside the superproject at
+/// `<superproject>/.git/modules/<name>`. Unlike a linked worktree, a submodule
+/// is an independent project whose identity is its own working directory, so its
+/// path must not be resolved to the superproject's `.git/modules/...` directory
+/// (from which the working directory cannot be derived).
+pub fn is_submodule_git_dir(git_dir: &Path) -> bool {
+ let mut previous_was_git_dir = false;
+ for component in git_dir.components() {
+ if let std::path::Component::Normal(name) = component {
+ if previous_was_git_dir && name == std::ffi::OsStr::new("modules") {
+ return true;
+ }
+ previous_was_git_dir = name.to_string_lossy().ends_with(".git");
+ } else {
+ previous_was_git_dir = false;
+ }
+ }
+ false
+}
+
/// Returns a short name for a linked worktree suitable for UI display
///
/// Uses the main worktree path to come up with a short name that disambiguates
@@ -10169,6 +10197,53 @@ mod tests {
});
}
+ #[test]
+ fn test_is_submodule_git_dir() {
+ assert!(is_submodule_git_dir(Path::new("/Foo/.git/modules/Bar")));
+ assert!(is_submodule_git_dir(Path::new(
+ "/Foo/.git/modules/nested/modules/Baz"
+ )));
+ // Bare superprojects keep their git dir in a `*.git` directory.
+ assert!(is_submodule_git_dir(Path::new("/Foo/foo.git/modules/Bar")));
+
+ // Not submodules: a normal repo, a linked worktree, and a bare repo.
+ assert!(!is_submodule_git_dir(Path::new("/Foo/Bar/.git")));
+ assert!(!is_submodule_git_dir(Path::new(
+ "/repo/.git/worktrees/feature"
+ )));
+ assert!(!is_submodule_git_dir(Path::new("/foo/.bare")));
+ // A directory literally named `modules` that isn't under a git dir.
+ assert!(!is_submodule_git_dir(Path::new("/Foo/modules/Bar")));
+ }
+
+ #[gpui::test]
+ async fn test_resolve_git_worktree_to_main_repo_ignores_submodule(cx: &mut TestAppContext) {
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree(
+ Path::new("/Foo"),
+ json!({
+ ".git": {
+ "modules": {
+ "Bar": {
+ "HEAD": "ref: refs/heads/main"
+ }
+ }
+ },
+ "Bar": {
+ ".git": "gitdir: ../.git/modules/Bar\n",
+ "src": { "main.rs": "" }
+ }
+ }),
+ )
+ .await;
+
+ // A submodule must not resolve to the superproject's `.git/modules/<name>`;
+ // it stays at its own working directory (handled by the caller falling back
+ // to the raw path).
+ let resolved = resolve_git_worktree_to_main_repo(fs.as_ref(), Path::new("/Foo/Bar")).await;
+ assert_eq!(resolved, None);
+ }
+
#[gpui::test]
async fn test_open_uncommitted_diff_skips_symlinks(cx: &mut TestAppContext) {
use util::rel_path::rel_path;
diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs
index fd5954a99e7..3cc9c5ca647 100644
--- a/crates/project/src/project.rs
+++ b/crates/project/src/project.rs
@@ -49,7 +49,8 @@ pub use agent_server_store::{AgentId, AgentServerStore, AgentServersUpdated, Ext
pub use git_store::{
ConflictRegion, ConflictSet, ConflictSetSnapshot, ConflictSetUpdate,
git_traversal::{ChildEntriesGitIter, GitEntry, GitEntryRef, GitTraversal},
- linked_worktree_short_name, repo_identity_path, worktrees_directory_for_repo,
+ is_submodule_git_dir, linked_worktree_short_name, repo_identity_path,
+ worktrees_directory_for_repo,
};
pub use manifest_tree::ManifestTree;
pub use project_search::{Search, SearchResults};
diff --git a/crates/project/src/worktree_store.rs b/crates/project/src/worktree_store.rs
index 375dde99120..89aeeb76c0e 100644
--- a/crates/project/src/worktree_store.rs
+++ b/crates/project/src/worktree_store.rs
@@ -1483,6 +1483,7 @@ impl WorktreeStore {
let folder_path = snapshot.abs_path().to_path_buf();
let main_path = snapshot
.root_repo_common_dir()
+ .filter(|dir| !crate::git_store::is_submodule_git_dir(dir))
.map(|dir| crate::git_store::repo_identity_path(dir))
.filter(|repo_path| {
snapshot.root_repo_is_linked_worktree()
diff --git a/crates/workspace/src/persistence.rs b/crates/workspace/src/persistence.rs
index f3f2710cd0f..c64150eae12 100644
--- a/crates/workspace/src/persistence.rs
+++ b/crates/workspace/src/persistence.rs
@@ -5290,6 +5290,47 @@ mod tests {
assert_eq!(result.identity_paths.paths(), &[PathBuf::from("/foo")]);
}
+ #[gpui::test]
+ async fn test_recent_workspace_identity_for_submodule(cx: &mut gpui::TestAppContext) {
+ let fs = fs::FakeFs::new(cx.executor());
+
+ // Superproject `/Foo` with a submodule `Bar`. A submodule's `.git` is a
+ // file pointing into the superproject's `.git/modules/<name>` directory,
+ // structurally like a linked worktree's `.git` file.
+ fs.insert_tree(
+ "/Foo",
+ json!({
+ ".git": {
+ "modules": {
+ "Bar": {
+ "HEAD": "ref: refs/heads/main"
+ }
+ }
+ },
+ "Bar": {
+ ".git": "gitdir: ../.git/modules/Bar\n",
+ "src": { "main.rs": "" }
+ },
+ "src": { "lib.rs": "" }
+ }),
+ )
+ .await;
+
+ let t0 = Utc::now();
+
+ let result = local_recent_workspace(
+ WorkspaceId(1),
+ PathList::new(&["/Foo/Bar"]),
+ t0,
+ fs.as_ref(),
+ )
+ .await;
+
+ // Submodules are independent projects: their identity is their own
+ // working directory, not the superproject's `.git/modules/<name>`.
+ assert_eq!(result.identity_paths.paths(), &[PathBuf::from("/Foo/Bar")]);
+ }
+
#[gpui::test]
async fn test_recent_workspace_identity_deduplicates_main_and_linked_worktree(
cx: &mut gpui::TestAppContext,
diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs
index 573e01dd097..044670f17db 100644
--- a/crates/workspace/src/workspace.rs
+++ b/crates/workspace/src/workspace.rs
@@ -9754,6 +9754,7 @@ pub async fn apply_restored_multiworkspace_state(
if key.host().is_none()
&& let Some(common_dir) =
project::discover_root_repo_common_dir(path, fs.as_ref()).await
+ && !project::is_submodule_git_dir(&common_dir)
{
let main_path = project::repo_identity_path(&common_dir);
resolved_paths.push(main_path.to_path_buf());