Terminate auto-created worktree paths with the original project name

Max Brunsfeld created

Change summary

crates/agent_ui/src/agent_panel.rs                                    |  8 
crates/collab/tests/integration/git_tests.rs                          |  4 
crates/collab/tests/integration/remote_editing_collaboration_tests.rs |  2 
crates/git/src/repository.rs                                          |  7 
crates/git_ui/src/worktree_picker.rs                                  |  3 
crates/project/src/git_store.rs                                       | 10 
crates/project/tests/integration/git_store.rs                         |  4 
7 files changed, 22 insertions(+), 16 deletions(-)

Detailed changes

crates/agent_ui/src/agent_panel.rs 🔗

@@ -2596,10 +2596,14 @@ impl AgentPanel {
         for repo in git_repos {
             let (work_dir, new_path, receiver) = repo.update(cx, |repo, _cx| {
                 let original_repo = repo.original_repo_abs_path.clone();
+                let project_name = original_repo
+                    .file_name()
+                    .ok_or_else(|| anyhow!("git repo must have a directory name"))?;
                 let directory =
                     validate_worktree_directory(&original_repo, worktree_directory_setting)?;
-                let new_path = directory.join(branch_name);
-                let receiver = repo.create_worktree(branch_name.to_string(), directory, None);
+                let new_path = directory.join(branch_name).join(project_name);
+                let receiver =
+                    repo.create_worktree(branch_name.to_string(), new_path.clone(), None);
                 let work_dir = repo.work_directory_abs_path.clone();
                 anyhow::Ok((work_dir, new_path, receiver))
             })?;

crates/collab/tests/integration/git_tests.rs 🔗

@@ -215,7 +215,7 @@ async fn test_remote_git_worktrees(
         repo_b.update(cx, |repository, _| {
             repository.create_worktree(
                 "feature-branch".to_string(),
-                worktree_directory.clone(),
+                worktree_directory.join("feature-branch"),
                 Some("abc123".to_string()),
             )
         })
@@ -266,7 +266,7 @@ async fn test_remote_git_worktrees(
         repo_b.update(cx, |repository, _| {
             repository.create_worktree(
                 "bugfix-branch".to_string(),
-                worktree_directory.clone(),
+                worktree_directory.join("bugfix-branch"),
                 None,
             )
         })

crates/git/src/repository.rs 🔗

@@ -769,7 +769,7 @@ pub trait GitRepository: Send + Sync {
 
     fn create_worktree(
         &self,
-        name: String,
+        branch_name: String,
         directory: PathBuf,
         from_commit: Option<String>,
     ) -> BoxFuture<'_, Result<()>>;
@@ -1717,7 +1717,6 @@ impl GitRepository for RealGitRepository {
         from_commit: Option<String>,
     ) -> BoxFuture<'_, Result<()>> {
         let git_binary = self.git_binary();
-        let final_path = directory.join(&name);
         let mut args = vec![
             OsString::from("--no-optional-locks"),
             OsString::from("worktree"),
@@ -1725,7 +1724,7 @@ impl GitRepository for RealGitRepository {
             OsString::from("-b"),
             OsString::from(name.as_str()),
             OsString::from("--"),
-            OsString::from(final_path.as_os_str()),
+            OsString::from(directory.as_os_str()),
         ];
         if let Some(from_commit) = from_commit {
             args.push(OsString::from(from_commit));
@@ -1735,7 +1734,7 @@ impl GitRepository for RealGitRepository {
 
         self.executor
             .spawn(async move {
-                std::fs::create_dir_all(final_path.parent().unwrap_or(&final_path))?;
+                std::fs::create_dir_all(directory.parent().unwrap_or(&directory))?;
                 let git = git_binary?;
                 let output = git.build_command(&args).output().await?;
                 if output.status.success() {

crates/git_ui/src/worktree_picker.rs 🔗

@@ -304,7 +304,8 @@ impl WorktreeListDelegate {
                 let directory =
                     validate_worktree_directory(&original_repo, &worktree_directory_setting)?;
                 let new_worktree_path = directory.join(&branch);
-                let receiver = repo.create_worktree(branch.clone(), directory, commit);
+                let receiver =
+                    repo.create_worktree(branch.clone(), new_worktree_path.clone(), commit);
                 anyhow::Ok((receiver, new_worktree_path))
             })?;
             receiver.await??;

crates/project/src/git_store.rs 🔗

@@ -5757,24 +5757,26 @@ impl Repository {
 
     pub fn create_worktree(
         &mut self,
-        name: String,
+        branch_name: String,
         directory: PathBuf,
         commit: Option<String>,
     ) -> oneshot::Receiver<Result<()>> {
         let id = self.id;
         self.send_job(
-            Some("git worktree add".into()),
+            Some(format!("git worktree add - {}", branch_name).into()),
             move |repo, _cx| async move {
                 match repo {
                     RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
-                        backend.create_worktree(name, directory, commit).await
+                        backend
+                            .create_worktree(branch_name, directory, commit)
+                            .await
                     }
                     RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
                         client
                             .request(proto::GitCreateWorktree {
                                 project_id: project_id.0,
                                 repository_id: id.to_proto(),
-                                name,
+                                name: branch_name,
                                 directory: directory.to_string_lossy().to_string(),
                                 commit,
                             })

crates/project/tests/integration/git_store.rs 🔗

@@ -1226,7 +1226,7 @@ mod git_worktrees {
             repository.update(cx, |repository, _| {
                 repository.create_worktree(
                     "feature-branch".to_string(),
-                    worktree_directory.clone(),
+                    worktree_directory.join("feature-branch"),
                     Some("abc123".to_string()),
                 )
             })
@@ -1252,7 +1252,7 @@ mod git_worktrees {
             repository.update(cx, |repository, _| {
                 repository.create_worktree(
                     "bugfix-branch".to_string(),
-                    worktree_directory.clone(),
+                    worktree_directory.join("bugfix-branch"),
                     None,
                 )
             })