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.

Fix worktree creation on Windows remote from Unix host (#63075)

Xin Zhaofdc1005e71c2
diff --git a/crates/agent_ui/src/thread_metadata_store.rs b/crates/agent_ui/src/thread_metadata_store.rs
index 4a406b43b81..d859bfc3d26 100644
--- a/crates/agent_ui/src/thread_metadata_store.rs
+++ b/crates/agent_ui/src/thread_metadata_store.rs
@@ -25,7 +25,7 @@ pub use project::WorktreePaths;
use project::{AgentId, linked_worktree_short_name};
use remote::{RemoteConnectionOptions, same_remote_connection_identity};
use ui::{App, Context, SharedString, ThreadItemWorktreeInfo, WorktreeKind};
-use util::ResultExt as _;
+use util::{ResultExt as _, paths::PathStyle};
use workspace::{PathList, SerializedWorkspaceLocation, WorkspaceDb};
use crate::DEFAULT_THREAD_TITLE;
@@ -384,7 +384,8 @@ pub fn worktree_info_from_thread_paths<S: std::hash::BuildHasher>(
let is_linked = main_path != folder_path;
if is_linked {
- let short_name = linked_worktree_short_name(main_path, folder_path).unwrap_or_default();
+ let short_name = linked_worktree_short_name(main_path, folder_path, PathStyle::local())
+ .unwrap_or_default();
let project_name = main_path
.file_name()
.map(|n| SharedString::from(n.to_string_lossy().to_string()))
diff --git a/crates/git_ui_core/src/worktree_service.rs b/crates/git_ui_core/src/worktree_service.rs
index 63e9ad08aa5..cc1b230a6bb 100644
--- a/crates/git_ui_core/src/worktree_service.rs
+++ b/crates/git_ui_core/src/worktree_service.rs
@@ -456,7 +456,7 @@ async fn fetch_remote_for_worktree_base(
/// Multiple entries in `git_repos` can be linked worktrees of the *same*
/// underlying repository (e.g. a project that has both the main checkout and
/// one of its linked worktrees open as separate Zed worktrees). Those entries
-/// resolve to the same target path via [`Repository::path_for_new_linked_worktree`],
+/// resolve to the same target path via [`project::git_store::RepositorySnapshot::path_for_new_linked_worktree`],
/// so we create the new worktree only once and remap every contributing
/// work directory onto it. Without this dedup, the second `git worktree add`
/// fails with "already exists".
diff --git a/crates/project/src/git_store.rs b/crates/project/src/git_store.rs
index f795e8b2c98..6465fb196e7 100644
--- a/crates/project/src/git_store.rs
+++ b/crates/project/src/git_store.rs
@@ -6306,8 +6306,10 @@ impl RepositorySnapshot {
/// common Git directory is the main worktree's `.git` directory.
pub fn main_worktree_abs_path(&self) -> Option<&Path> {
if self.is_linked_worktree() {
- if self.common_dir_abs_path.file_name()? == std::ffi::OsStr::new(".git") {
- self.common_dir_abs_path.parent()
+ if self.path_style.file_name(self.common_dir_abs_path.as_ref())
+ == Some(std::ffi::OsStr::new(".git"))
+ {
+ self.path_style.parent(self.common_dir_abs_path.as_ref())
} else {
None
}
@@ -6316,6 +6318,36 @@ impl RepositorySnapshot {
}
}
+ /// The path that anchors a linked worktree's location: the main worktree
+ /// when one exists, otherwise the repository's identity path (relevant for
+ /// bare repositories), falling back to the common Git directory.
+ fn linked_worktree_anchor_path(&self) -> &Path {
+ self.main_worktree_abs_path()
+ .or_else(|| repo_identity_path_if_local(&self.common_dir_abs_path, self.path_style))
+ .unwrap_or(self.common_dir_abs_path.as_ref())
+ }
+
+ /// Computes the path where a new linked worktree for this repository
+ /// would be created, based on the `git.worktree_directory` setting.
+ pub fn path_for_new_linked_worktree(
+ &self,
+ worktree_name: &str,
+ worktree_directory_setting: &str,
+ ) -> Result<PathBuf> {
+ let repository_anchor = self.linked_worktree_anchor_path();
+ let project_name = self
+ .path_style
+ .file_name(repository_anchor)
+ .ok_or_else(|| anyhow!("git repo must have a directory name"))?;
+ let directory = worktrees_directory_for_repo(
+ repository_anchor,
+ worktree_directory_setting,
+ self.path_style,
+ )?;
+ let directory = self.path_style.join_path(&directory, worktree_name)?;
+ self.path_style.join_path(&directory, project_name)
+ }
+
/// The main worktree is the original checkout that other worktrees were
/// created from.
///
@@ -9211,32 +9243,6 @@ impl Repository {
.then_some(&self.work_directory_abs_path)
}
- fn linked_worktree_anchor_path(&self) -> &Path {
- self.snapshot
- .main_worktree_abs_path()
- .or_else(|| repo_identity_path_if_local(&self.common_dir_abs_path, self.path_style))
- .unwrap_or(self.common_dir_abs_path.as_ref())
- }
-
- pub fn path_for_new_linked_worktree(
- &self,
- branch_name: &str,
- worktree_directory_setting: &str,
- ) -> Result<PathBuf> {
- let repository_anchor = self.linked_worktree_anchor_path();
- let project_name = repository_anchor
- .file_name()
- .and_then(|name| name.to_str())
- .ok_or_else(|| anyhow!("git repo must have a directory name"))?;
- let directory = worktrees_directory_for_repo(
- repository_anchor,
- worktree_directory_setting,
- self.path_style,
- )?;
- let directory = self.path_style.join_path(&directory, branch_name)?;
- self.path_style.join_path(&directory, project_name)
- }
-
pub fn worktrees(&mut self) -> oneshot::Receiver<Result<Vec<GitWorktree>>> {
let id = self.id;
self.send_job("worktrees", None, move |repo, _| async move {
@@ -10866,10 +10872,10 @@ pub fn worktrees_directory_for_repo(
} else {
path::normalize_path(&joined)
};
- let resolved = if resolved.starts_with(repository_anchor_path) {
+ let resolved = if path_style.starts_with(resolved.as_path(), repository_anchor_path) {
resolved
- } else if let Some(repo_dir_name) = repository_anchor_path
- .file_name()
+ } else if let Some(repo_dir_name) = path_style
+ .file_name(repository_anchor_path)
.and_then(|name| name.to_str())
{
path_style.join_path(&resolved, repo_dir_name)?
@@ -10877,11 +10883,11 @@ pub fn worktrees_directory_for_repo(
resolved
};
- let parent = repository_anchor_path
- .parent()
+ let parent = path_style
+ .parent(repository_anchor_path)
.unwrap_or(repository_anchor_path);
- if !resolved.starts_with(parent) {
+ if !path_style.starts_with(resolved.as_path(), parent) {
anyhow::bail!(
"git.worktree_directory resolved to {resolved:?}, which is outside \
the project root and its parent directory. It must resolve to a \
@@ -10992,21 +10998,19 @@ pub fn is_submodule_git_dir(git_dir: &Path) -> bool {
pub fn linked_worktree_short_name(
main_worktree_path: &Path,
linked_worktree_path: &Path,
+ path_style: PathStyle,
) -> Option<SharedString> {
if main_worktree_path == linked_worktree_path {
return None;
}
- let project_name = main_worktree_path.file_name()?.to_str()?;
- let directory_name = linked_worktree_path.file_name()?.to_str()?;
+ let project_name = path_style.file_name(main_worktree_path)?.to_str()?;
+ let directory_name = path_style.file_name(linked_worktree_path)?.to_str()?;
let name = if directory_name != project_name {
directory_name.to_string()
} else {
- linked_worktree_path
- .parent()?
- .file_name()?
- .to_str()?
- .to_string()
+ let parent = path_style.parent(linked_worktree_path)?;
+ path_style.file_name(parent)?.to_str()?.to_string()
};
Some(name.into())
}
@@ -12348,6 +12352,53 @@ mod tests {
);
}
+ #[test]
+ fn test_new_worktree_path_uses_windows_style_for_remote_paths_on_unix_host() {
+ let snapshot = RepositorySnapshot::empty(
+ RepositoryId(0),
+ Path::new(r"C:\Users\user\dev\lsp-tests").into(),
+ None,
+ None,
+ None,
+ PathStyle::Windows,
+ );
+ let path = snapshot
+ .path_for_new_linked_worktree("nimble-sky", "../worktrees")
+ .unwrap();
+ assert_eq!(
+ path,
+ PathBuf::from(r"C:\Users\user\dev\worktrees\lsp-tests\nimble-sky\lsp-tests")
+ );
+ }
+
+ #[test]
+ fn test_new_worktree_path_from_windows_remote_linked_worktree_on_unix_host() {
+ let snapshot = RepositorySnapshot::empty(
+ RepositoryId(0),
+ Path::new(r"C:\Users\user\dev\worktrees\lsp-tests\existing-worktree\lsp-tests").into(),
+ Some(Path::new(r"C:\Users\user\dev\lsp-tests\.git\worktrees\existing-worktree").into()),
+ Some(
+ Path::new(
+ r"C:\Users\user\dev\worktrees\lsp-tests\existing-worktree\lsp-tests\.git",
+ )
+ .into(),
+ ),
+ Some(Path::new(r"C:\Users\user\dev\lsp-tests\.git").into()),
+ PathStyle::Windows,
+ );
+
+ assert_eq!(
+ snapshot.main_worktree_abs_path(),
+ Some(Path::new(r"C:\Users\user\dev\lsp-tests"))
+ );
+ assert_eq!(
+ snapshot
+ .path_for_new_linked_worktree("nimble-sky", "../worktrees")
+ .unwrap(),
+ PathBuf::from(r"C:\Users\user\dev\worktrees\lsp-tests\nimble-sky\lsp-tests")
+ );
+ }
+
fn verify_invariants(repository: &Repository) -> anyhow::Result<()> {
match &repository.commit_data_handler {
CommitDataHandlerState::Open(handler) => {
diff --git a/crates/project/tests/integration/git_store.rs b/crates/project/tests/integration/git_store.rs
index ac54aee118b..e02ca05838f 100644
--- a/crates/project/tests/integration/git_store.rs
+++ b/crates/project/tests/integration/git_store.rs
@@ -1290,15 +1290,27 @@ mod git_worktrees {
#[test]
fn test_worktree_directory_uses_remote_path_style() {
- let work_dir = Path::new("/home/user/dev/lsp-tests");
-
- let directory =
- worktrees_directory_for_repo(work_dir, "../worktrees", PathStyle::Unix).unwrap();
+ // Unix remote
+ let anchor = Path::new("/home/user/dev/lsp-tests");
assert_eq!(
- directory,
+ worktrees_directory_for_repo(anchor, "../worktrees", PathStyle::Unix).unwrap(),
PathBuf::from("/home/user/dev/worktrees/lsp-tests")
);
+
+ // Windows remote: the anchor intentionally mixes `/` and `\` separators.
+ let anchor = Path::new(r"C:\Users/user/dev\lsp-tests");
+
+ assert_eq!(
+ worktrees_directory_for_repo(anchor, "../worktrees", PathStyle::Windows).unwrap(),
+ PathBuf::from(r"C:\Users\user\dev\worktrees\lsp-tests")
+ );
+
+ assert_eq!(
+ worktrees_directory_for_repo(anchor, ".git\\zed-worktrees", PathStyle::Windows)
+ .unwrap(),
+ PathBuf::from(r"C:\Users\user\dev\lsp-tests\.git\zed-worktrees")
+ );
}
#[gpui::test]
@@ -1960,20 +1972,40 @@ mod resolve_worktree_tests {
(
"/home/bob/zed",
"/home/bob/worktrees/olivetti/zed",
+ PathStyle::Unix,
Some("olivetti".into()),
),
- ("/home/bob/zed", "/home/bob/zed2", Some("zed2".into())),
+ (
+ "/home/bob/zed",
+ "/home/bob/zed2",
+ PathStyle::Unix,
+ Some("zed2".into()),
+ ),
(
"/home/bob/zed",
"/home/bob/worktrees/zed/selectric",
+ PathStyle::Unix,
Some("selectric".into()),
),
- ("/home/bob/zed", "/home/bob/zed", None),
+ ("/home/bob/zed", "/home/bob/zed", PathStyle::Unix, None),
+ (
+ r"C:\Users\bob\dev\zed",
+ r"C:\Users\bob\dev\worktrees\zed\olivetti\zed",
+ PathStyle::Windows,
+ Some("olivetti".into()),
+ ),
+ (
+ r"C:\Users\bob\dev\zed",
+ r"C:\Users\bob\dev\zed2",
+ PathStyle::Windows,
+ Some("zed2".into()),
+ ),
];
- for (main_worktree_path, linked_worktree_path, expected) in examples {
+ for (main_worktree_path, linked_worktree_path, path_style, expected) in examples {
let short_name = linked_worktree_short_name(
Path::new(main_worktree_path),
Path::new(linked_worktree_path),
+ path_style,
);
assert_eq!(
short_name, expected,
diff --git a/crates/sidebar/src/sidebar.rs b/crates/sidebar/src/sidebar.rs
index 890248d674a..235e32980e1 100644
--- a/crates/sidebar/src/sidebar.rs
+++ b/crates/sidebar/src/sidebar.rs
@@ -634,20 +634,24 @@ fn workspace_menu_worktree_labels(
let root_paths = workspace.read(cx).root_paths(cx);
let show_folder_name = root_paths.len() > 1;
let project = workspace.read(cx).project().clone();
- let repository_snapshots: Vec<_> = project
- .read(cx)
- .repositories(cx)
- .values()
- .map(|repo| repo.read(cx).snapshot())
- .collect();
+ let (path_style, repository_snapshots) = {
+ let project = project.read(cx);
+ let path_style = project.path_style(cx);
+ let repository_snapshots = project
+ .repositories(cx)
+ .values()
+ .map(|repo| repo.read(cx).snapshot())
+ .collect::<Vec<_>>();
+ (path_style, repository_snapshots)
+ };
root_paths
.into_iter()
.map(|root_path| {
let root_path = root_path.as_ref();
- let folder_name = root_path
- .file_name()
- .map(|name| SharedString::from(name.to_string_lossy().to_string()))
+ let folder_name = path_style
+ .file_name(root_path)
+ .map(|name| SharedString::from(name.to_string_lossy().into_owned()))
.unwrap_or_default();
let repository_snapshot = repository_snapshots
.iter()
@@ -663,7 +667,11 @@ fn workspace_menu_worktree_labels(
.main_worktree_abs_path()
.or(identity_fallback)
.and_then(|name_anchor_path| {
- project::linked_worktree_short_name(name_anchor_path, root_path)
+ project::linked_worktree_short_name(
+ name_anchor_path,
+ root_path,
+ snapshot.path_style,
+ )
})
.unwrap_or_else(|| folder_name.clone())
} else {
diff --git a/crates/title_bar/src/title_bar.rs b/crates/title_bar/src/title_bar.rs
index 6fd8b7bacd8..3ffc153bbcf 100644
--- a/crates/title_bar/src/title_bar.rs
+++ b/crates/title_bar/src/title_bar.rs
@@ -270,6 +270,7 @@ impl Render for TitleBar {
linked_worktree_short_name(
name_anchor_path,
repo.work_directory_abs_path.as_ref(),
+ repo.path_style,
)
})
.or_else(|| {
@@ -1478,7 +1479,11 @@ mod tests {
assert_eq!(
name_anchor_path.and_then(|name_anchor_path| {
- linked_worktree_short_name(name_anchor_path, work_directory_path)
+ linked_worktree_short_name(
+ name_anchor_path,
+ work_directory_path,
+ PathStyle::local(),
+ )
}),
Some("plum-warbler".into())
);