Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Refactor: Clarify worktree root functions
Maximilian Roosfebe21be64f9
diff --git a/src/commands/merge.rs b/src/commands/merge.rsindex adea9cfa8..ae35a5d46 100644--- a/src/commands/merge.rs+++ b/src/commands/merge.rs@@ -72,7 +72,7 @@ pub fn handle_merge(println!("🔄 {cyan}Cleaning up worktree...{cyan:#}");// Get primary worktree path before finishing (while we can still run git commands)- let primary_worktree_dir = repo.repo_root()?;+ let primary_worktree_dir = repo.main_worktree_root()?;let result = handle_remove()?;diff --git a/src/commands/worktree.rs b/src/commands/worktree.rsindex 3175e668a..7ef660e23 100644--- a/src/commands/worktree.rs+++ b/src/commands/worktree.rs@@ -188,7 +188,7 @@ pub fn handle_switch(}// No existing worktree, create one- let repo_root = repo.repo_root()?;+ let repo_root = repo.main_worktree_root()?;let repo_name = repo_root.file_name()@@ -254,7 +254,7 @@ pub fn handle_remove() -> Result<RemoveResult, GitError> {if in_worktree {// In worktree: navigate to primary worktree and remove this onelet worktree_root = repo.worktree_root()?;- let primary_worktree_dir = repo.repo_root()?;+ let primary_worktree_dir = repo.main_worktree_root()?;// Remove the worktreeif let Err(e) = repo.remove_worktree(&worktree_root) {@@ -391,7 +391,7 @@ fn prompt_for_approval(command: &str, project_id: &str) -> io::Result<bool> {/// Helper to load project config with error handlingfn load_project_config(repo: &Repository) -> Result<Option<ProjectConfig>, GitError> {- let repo_root = repo.repo_root()?;+ let repo_root = repo.worktree_root()?;let config_path = repo_root.join(".config").join("wt.toml");match ProjectConfig::load(&repo_root) {Ok(cfg) => Ok(cfg),@@ -452,7 +452,7 @@ fn execute_post_create_commands(}let project_id = repo.project_identifier()?;- let repo_root = repo.repo_root()?;+ let repo_root = repo.main_worktree_root()?;// Execute each command sequentiallyfor (name, command) in commands {@@ -508,7 +508,7 @@ fn spawn_post_start_commands(}let project_id = repo.project_identifier()?;- let repo_root = repo.repo_root()?;+ let repo_root = repo.main_worktree_root()?;// Spawn each command as a detached background processfor (name, command) in commands {diff --git a/src/git.rs b/src/git.rsindex 64098afb0..fc42de55b 100644--- a/src/git.rs+++ b/src/git.rs@@ -156,11 +156,12 @@ impl Repository {Ok(PathBuf::from(stdout.trim()))}- /// Get the canonicalized repository root directory (parent of .git).+ /// Get the main worktree root (top-level directory of the main worktree).///- /// The canonicalization resolves symlinks and relative paths, which is important- /// for worktree operations to ensure consistent path handling.- pub fn repo_root(&self) -> Result<PathBuf, GitError> {+ /// This returns the main worktree's root, even when called from a linked worktree.+ /// The main worktree is the original repository created by git-init or git-clone.+ /// Use this when you need to reference the primary worktree location.+ pub fn main_worktree_root(&self) -> Result<PathBuf, GitError> {let git_common_dir = self.git_common_dir()?.canonicalize()@@ -193,9 +194,15 @@ impl Repository {}/// Get the worktree root directory (top-level of the working tree).+ ///+ /// Returns the canonicalized absolute path to the top-level directory of the+ /// current working tree. This could be the main worktree or a linked worktree.pub fn worktree_root(&self) -> Result<PathBuf, GitError> {let stdout = self.run_command(&["rev-parse", "--show-toplevel"])?;- Ok(PathBuf::from(stdout.trim()))+ let path = PathBuf::from(stdout.trim());+ path.canonicalize().map_err(|e| {+ GitError::CommandFailed(format!("Failed to canonicalize worktree root: {}", e))+ })}/// Check if the path is in a worktree (vs the main repository).@@ -492,8 +499,8 @@ impl Repository {return Ok(url.to_string());}- // Fall back to repository name- let repo_root = self.repo_root()?;+ // Fall back to repository name (use main worktree for consistency across all worktrees)+ let repo_root = self.main_worktree_root()?;let repo_name = repo_root.file_name().and_then(|name| name.to_str())diff --git a/tests/snapshots/integration__integration_tests__merge__merge_squash_with_llm.snap b/tests/snapshots/integration__integration_tests__merge__merge_squash_with_llm.snapindex 9cc6e8256..6f54e0f20 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_squash_with_llm.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_squash_with_llm.snap@@ -16,8 +16,9 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1704067200"- WORKTRUNK_LLM__ARGS: "feat: implement user authentication system"- WORKTRUNK_LLM__COMMAND: echo+ WORKTRUNK_LLM__ARGS__0: "-c"+ WORKTRUNK_LLM__ARGS__1: "cat >/dev/null && echo 'feat: implement user authentication system'"+ WORKTRUNK_LLM__COMMAND: sh---success: trueexit_code: 0