From 2a39a7bf62b79cbd13b855611302a845461970ca Mon Sep 17 00:00:00 2001
From: Matt Toohey <contact@matttoohey.com>
Date: Tue, 24 Mar 2026 13:18:42 +1100
Subject: [PATCH 1/2] fix(diff): run remote diff git commands from repo root
 for monorepo subpaths

Remote diffs showed no highlighted changes for monorepo subpath branches
because `git diff -- <path>` resolved pathspecs relative to CWD (the
subpath), doubling the path prefix. Fix by deriving just the clone
directory for `resolve_branch_context` so all remote diff commands run
from the repo root, making path resolution consistent across
`diff --name-status`, `show <ref>:<path>`, and `diff -- <path>`.

Also removes legacy `repo:` / `repos/` subpath format handling.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---
 apps/staged/src-tauri/src/branches.rs      | 21 ++++-----------------
 apps/staged/src-tauri/src/diff_commands.rs | 15 ++++++++++++++-
 2 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/apps/staged/src-tauri/src/branches.rs b/apps/staged/src-tauri/src/branches.rs
index 5886cf3e..e04ae90e 100644
--- a/apps/staged/src-tauri/src/branches.rs
+++ b/apps/staged/src-tauri/src/branches.rs
@@ -144,7 +144,7 @@ pub(crate) fn validate_workspace_subpath(subpath: &str) -> Result<String, String
 ///
 /// The name is sanitised so it is safe to use as a directory name on the
 /// workspace filesystem.
-fn repo_name_from_github_repo(github_repo: &str) -> String {
+pub(crate) fn repo_name_from_github_repo(github_repo: &str) -> String {
     let raw = github_repo.rsplit('/').next().unwrap_or(github_repo);
     let collapsed = raw
         .chars()
@@ -283,22 +283,9 @@ pub(crate) fn resolve_branch_workspace_subpath(
         return Ok(None);
     };
 
-    // Backward compatibility: existing DB rows may still have the old
-    // `repo:` prefix or `repos/` path format.
-    if let Some(ref subpath) = repo.subpath {
-        let trimmed = subpath.trim().trim_matches('/');
-        if let Some(repo_dir) = trimmed.strip_prefix("repo:") {
-            let dir = validate_workspace_subpath(repo_dir)?;
-            return Ok(Some(format!("home:{dir}")));
-        }
-        if trimmed.starts_with("repos/") {
-            return Ok(Some(trimmed.to_string()));
-        }
-    }
-
-    // Current format: repo.subpath is None or a plain path relative to the
-    // repo root (e.g. "apps/staged"). Derive the workspace clone directory
-    // from github_repo.
+    // repo.subpath is None or a plain path relative to the repo root
+    // (e.g. "apps/staged"). Derive the workspace clone directory from
+    // github_repo.
     let clone_dir = repo_name_from_github_repo(&repo.github_repo);
     let workspace_path = match &repo.subpath {
         Some(subpath) => {
diff --git a/apps/staged/src-tauri/src/diff_commands.rs b/apps/staged/src-tauri/src/diff_commands.rs
index 8664d8a5..c1d8eecd 100644
--- a/apps/staged/src-tauri/src/diff_commands.rs
+++ b/apps/staged/src-tauri/src/diff_commands.rs
@@ -30,7 +30,20 @@ pub(crate) fn resolve_branch_context(
             .workspace_name
             .clone()
             .ok_or_else(|| format!("Branch has no workspace name: {branch_id}"))?;
-        let repo_subpath = branches::resolve_branch_workspace_subpath(store, &branch)?;
+
+        // Use the repo root (clone dir only) so all paths are consistently
+        // repo-root-relative — git diff pathspecs, git show tree paths, etc.
+        let repo_subpath = if let Some(repo_id) = branch.project_repo_id.as_deref() {
+            if let Some(repo) = store.get_project_repo(repo_id).map_err(|e| e.to_string())? {
+                let clone_dir = branches::repo_name_from_github_repo(&repo.github_repo);
+                Some(format!("home:{clone_dir}"))
+            } else {
+                None
+            }
+        } else {
+            None
+        };
+
         return Ok(BranchDiffContext {
             base_branch: branch.base_branch,
             worktree_path: None,

From 21d2e2225719fb04bbb33fc767bac54c8d8b4fe4 Mon Sep 17 00:00:00 2001
From: Matt Toohey <contact@matttoohey.com>
Date: Tue, 24 Mar 2026 13:32:08 +1100
Subject: [PATCH 2/2] refactor(diff): extract resolve_branch_clone_dir helper
 to deduplicate repo-root logic

The clone-dir derivation was duplicated between resolve_branch_context
in diff_commands.rs and resolve_branch_workspace_subpath in branches.rs.
Extract a shared resolve_branch_clone_dir helper so both call sites
use the same code path and stay in sync if the logic ever changes.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---
 apps/staged/src-tauri/src/branches.rs      | 22 +++++++++++++++++++---
 apps/staged/src-tauri/src/diff_commands.rs | 11 +----------
 2 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/apps/staged/src-tauri/src/branches.rs b/apps/staged/src-tauri/src/branches.rs
index e04ae90e..34f8373c 100644
--- a/apps/staged/src-tauri/src/branches.rs
+++ b/apps/staged/src-tauri/src/branches.rs
@@ -272,6 +272,25 @@ pub(crate) fn resolve_workspace_repo_path(
 ///
 /// Returns a `home:<dir>` string suitable for `resolve_workspace_repo_path`,
 /// or `None` when the branch has no associated project repo.
+/// Return just the repo-root clone directory as `home:<clone_dir>`.
+///
+/// This is the base path on the workspace filesystem where the repo is
+/// cloned. Use this when you need commands to run from the repo root
+/// (e.g. `git diff` in `diff_commands`).
+pub(crate) fn resolve_branch_clone_dir(
+    store: &Arc<Store>,
+    branch: &store::Branch,
+) -> Result<Option<String>, String> {
+    let Some(repo_id) = branch.project_repo_id.as_deref() else {
+        return Ok(None);
+    };
+    let Some(repo) = store.get_project_repo(repo_id).map_err(|e| e.to_string())? else {
+        return Ok(None);
+    };
+    let clone_dir = repo_name_from_github_repo(&repo.github_repo);
+    Ok(Some(format!("home:{clone_dir}")))
+}
+
 pub(crate) fn resolve_branch_workspace_subpath(
     store: &Arc<Store>,
     branch: &store::Branch,
@@ -283,9 +302,6 @@ pub(crate) fn resolve_branch_workspace_subpath(
         return Ok(None);
     };
 
-    // repo.subpath is None or a plain path relative to the repo root
-    // (e.g. "apps/staged"). Derive the workspace clone directory from
-    // github_repo.
     let clone_dir = repo_name_from_github_repo(&repo.github_repo);
     let workspace_path = match &repo.subpath {
         Some(subpath) => {
diff --git a/apps/staged/src-tauri/src/diff_commands.rs b/apps/staged/src-tauri/src/diff_commands.rs
index c1d8eecd..aa7c30e4 100644
--- a/apps/staged/src-tauri/src/diff_commands.rs
+++ b/apps/staged/src-tauri/src/diff_commands.rs
@@ -33,16 +33,7 @@ pub(crate) fn resolve_branch_context(
 
         // Use the repo root (clone dir only) so all paths are consistently
         // repo-root-relative — git diff pathspecs, git show tree paths, etc.
-        let repo_subpath = if let Some(repo_id) = branch.project_repo_id.as_deref() {
-            if let Some(repo) = store.get_project_repo(repo_id).map_err(|e| e.to_string())? {
-                let clone_dir = branches::repo_name_from_github_repo(&repo.github_repo);
-                Some(format!("home:{clone_dir}"))
-            } else {
-                None
-            }
-        } else {
-            None
-        };
+        let repo_subpath = branches::resolve_branch_clone_dir(store, &branch)?;
 
         return Ok(BranchDiffContext {
             base_branch: branch.base_branch,
