Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix(statusline): scope branch diff to sparse checkout cone (#1024)

Ben Druckerb10c92b85fb9
diff --git a/src/git/repository/diff.rs b/src/git/repository/diff.rs
index a4994110a..c1ee836f4 100644
--- a/src/git/repository/diff.rs
+++ b/src/git/repository/diff.rs
@@ -296,7 +296,15 @@ impl Repository {
// Use two-dot syntax with the cached merge-base
let range = format!("{}..{}", merge_base, head);
- let stdout = self.run_command(&["diff", "--numstat", &range])?;
+ let mut args = vec!["diff", "--numstat", &range];
+
+ let sparse_paths = self.sparse_checkout_paths();
+ if !sparse_paths.is_empty() {
+ args.push("--");
+ args.extend(sparse_paths.iter().map(|s| s.as_str()));
+ }
+
+ let stdout = self.run_command(&args)?;
LineDiff::from_numstat(&stdout)
}
diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rs
index 2583406cf..ab891cc59 100644
--- a/src/git/repository/mod.rs
+++ b/src/git/repository/mod.rs
@@ -114,6 +114,8 @@ pub(super) struct RepoCache {
pub(super) project_identifier: OnceCell<String>,
/// Project config (loaded from .config/wt.toml in main worktree)
pub(super) project_config: OnceCell<Option<ProjectConfig>>,
+ /// Sparse checkout paths (empty if not a sparse checkout)
+ pub(super) sparse_checkout_paths: OnceCell<Vec<String>>,
/// Merge-base cache: (commit1, commit2) -> merge_base_sha (None = no common ancestor)
pub(super) merge_base: DashMap<(String, String), Option<String>>,
/// Batch ahead/behind cache: (base_ref, branch_name) -> (ahead, behind)
@@ -408,6 +410,30 @@ impl Repository {
})
}
+ /// Get the sparse checkout paths for this repository.
+ ///
+ /// Returns the list of paths from `git sparse-checkout list`. For non-sparse
+ /// repos, returns an empty slice (the command exits with code 128).
+ ///
+ /// Assumes cone mode (the git default). Cached using `discovery_path` —
+ /// scoped to the worktree the user is running from, not per-listed-worktree.
+ pub fn sparse_checkout_paths(&self) -> &[String] {
+ self.cache.sparse_checkout_paths.get_or_init(|| {
+ let output = match self.run_command_output(&["sparse-checkout", "list"]) {
+ Ok(out) => out,
+ Err(_) => return Vec::new(),
+ };
+
+ if output.status.success() {
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ stdout.lines().map(String::from).collect()
+ } else {
+ // Exit 128 = not a sparse checkout (expected, not an error)
+ Vec::new()
+ }
+ })
+ }
+
/// Check if git's builtin fsmonitor daemon is enabled.
///
/// Returns true only for `core.fsmonitor=true` (the builtin daemon).
diff --git a/tests/integration_tests/repository.rs b/tests/integration_tests/repository.rs
index 091a2b562..e3c06df5c 100644
--- a/tests/integration_tests/repository.rs
+++ b/tests/integration_tests/repository.rs
@@ -763,3 +763,111 @@ fn test_is_dirty_does_not_detect_skip_worktree_changes() {
"is_dirty() does not detect skip-worktree changes by design"
);
}
+
+// =============================================================================
+// sparse_checkout_paths() tests
+// =============================================================================
+
+#[test]
+fn test_sparse_checkout_paths_empty_for_normal_repo() {
+ let repo = TestRepo::new();
+ let repository = Repository::at(repo.root_path().to_path_buf()).unwrap();
+
+ let paths = repository.sparse_checkout_paths();
+ assert!(
+ paths.is_empty(),
+ "normal repo should have no sparse checkout paths"
+ );
+}
+
+#[test]
+fn test_sparse_checkout_paths_returns_cone_paths() {
+ let repo = TestRepo::new();
+
+ // Create directories with files and commit them
+ let dir1 = repo.root_path().join("dir1");
+ let dir2 = repo.root_path().join("dir2");
+ fs::create_dir_all(&dir1).unwrap();
+ fs::create_dir_all(&dir2).unwrap();
+ fs::write(dir1.join("file.txt"), "content1").unwrap();
+ fs::write(dir2.join("file.txt"), "content2").unwrap();
+ repo.run_git(&["add", "."]);
+ repo.run_git(&["commit", "-m", "add directories"]);
+
+ // Set up sparse checkout in cone mode
+ repo.run_git(&["sparse-checkout", "init", "--cone"]);
+ repo.run_git(&["sparse-checkout", "set", "dir1", "dir2"]);
+
+ let repository = Repository::at(repo.root_path().to_path_buf()).unwrap();
+ let paths = repository.sparse_checkout_paths();
+
+ assert_eq!(paths, &["dir1".to_string(), "dir2".to_string()]);
+}
+
+#[test]
+fn test_sparse_checkout_paths_cached() {
+ let repo = TestRepo::new();
+
+ let dir1 = repo.root_path().join("dir1");
+ fs::create_dir_all(&dir1).unwrap();
+ fs::write(dir1.join("file.txt"), "content").unwrap();
+ repo.run_git(&["add", "."]);
+ repo.run_git(&["commit", "-m", "add dir1"]);
+
+ repo.run_git(&["sparse-checkout", "init", "--cone"]);
+ repo.run_git(&["sparse-checkout", "set", "dir1"]);
+
+ let repository = Repository::at(repo.root_path().to_path_buf()).unwrap();
+
+ let first = repository.sparse_checkout_paths();
+ let second = repository.sparse_checkout_paths();
+
+ assert_eq!(first, second);
+ assert_eq!(first, &["dir1".to_string()]);
+}
+
+#[test]
+fn test_branch_diff_stats_scoped_to_sparse_checkout() {
+ let repo = TestRepo::new();
+
+ // Create two directories with files on main
+ let inside = repo.root_path().join("inside");
+ let outside = repo.root_path().join("outside");
+ fs::create_dir_all(&inside).unwrap();
+ fs::create_dir_all(&outside).unwrap();
+ fs::write(inside.join("file.txt"), "base content\n").unwrap();
+ fs::write(outside.join("file.txt"), "base content\n").unwrap();
+ repo.run_git(&["add", "."]);
+ repo.run_git(&["commit", "-m", "add directories"]);
+
+ // Create feature branch and modify files in both directories
+ repo.run_git(&["checkout", "-b", "feature"]);
+ fs::write(inside.join("file.txt"), "modified inside\nadded line\n").unwrap();
+ fs::write(outside.join("file.txt"), "modified outside\nadded line\n").unwrap();
+ repo.run_git(&["add", "."]);
+ repo.run_git(&["commit", "-m", "modify both dirs"]);
+
+ // Go back to main and set up sparse checkout
+ repo.run_git(&["checkout", "main"]);
+ repo.run_git(&["sparse-checkout", "init", "--cone"]);
+ repo.run_git(&["sparse-checkout", "set", "inside"]);
+
+ let repository = Repository::at(repo.root_path().to_path_buf()).unwrap();
+ let stats = repository.branch_diff_stats("main", "feature").unwrap();
+
+ // Only changes in inside/ should be counted
+ // inside/file.txt: "base content\n" → "modified inside\nadded line\n" = 2 added, 1 deleted
+ assert_eq!(stats.added, 2, "sparse: only inside/ additions");
+ assert_eq!(stats.deleted, 1, "sparse: only inside/ deletions");
+
+ // Disable sparse checkout — full stats include both inside/ and outside/
+ repo.run_git(&["sparse-checkout", "disable"]);
+ let full_repository = Repository::at(repo.root_path().to_path_buf()).unwrap();
+ let full_stats = full_repository
+ .branch_diff_stats("main", "feature")
+ .unwrap();
+
+ // Both files have identical diffs, so full = 2x sparse
+ assert_eq!(full_stats.added, 4, "full: inside/ + outside/ additions");
+ assert_eq!(full_stats.deleted, 2, "full: inside/ + outside/ deletions");
+}