feat: add ecc2 worktree file previews

This commit is contained in:
Affaan Mustafa
2026-04-08 13:45:32 -07:00
parent afb97961e3
commit 5070b2d785
2 changed files with 185 additions and 15 deletions

View File

@@ -107,6 +107,35 @@ pub fn diff_summary(worktree: &WorktreeInfo) -> Result<Option<String>> {
}
}
pub fn diff_file_preview(worktree: &WorktreeInfo, limit: usize) -> Result<Vec<String>> {
let mut preview = Vec::new();
let base_ref = format!("{}...HEAD", worktree.base_branch);
let committed = git_diff_name_status(&worktree.path, &[&base_ref])?;
if !committed.is_empty() {
preview.extend(
committed
.into_iter()
.map(|entry| format!("Branch {entry}"))
.take(limit.saturating_sub(preview.len())),
);
}
if preview.len() < limit {
let working = git_status_short(&worktree.path)?;
if !working.is_empty() {
preview.extend(
working
.into_iter()
.map(|entry| format!("Working {entry}"))
.take(limit.saturating_sub(preview.len())),
);
}
}
Ok(preview)
}
fn git_diff_shortstat(worktree_path: &Path, extra_args: &[&str]) -> Result<Option<String>> {
let mut command = Command::new("git");
command
@@ -137,6 +166,60 @@ fn git_diff_shortstat(worktree_path: &Path, extra_args: &[&str]) -> Result<Optio
}
}
fn git_diff_name_status(worktree_path: &Path, extra_args: &[&str]) -> Result<Vec<String>> {
let mut command = Command::new("git");
command
.arg("-C")
.arg(worktree_path)
.arg("diff")
.arg("--name-status");
command.args(extra_args);
let output = command
.output()
.context("Failed to generate worktree diff file preview")?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
tracing::warn!(
"Worktree diff file preview warning for {}: {stderr}",
worktree_path.display()
);
return Ok(Vec::new());
}
Ok(parse_nonempty_lines(&output.stdout))
}
fn git_status_short(worktree_path: &Path) -> Result<Vec<String>> {
let output = Command::new("git")
.arg("-C")
.arg(worktree_path)
.args(["status", "--short"])
.output()
.context("Failed to generate worktree status preview")?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
tracing::warn!(
"Worktree status preview warning for {}: {stderr}",
worktree_path.display()
);
return Ok(Vec::new());
}
Ok(parse_nonempty_lines(&output.stdout))
}
fn parse_nonempty_lines(stdout: &[u8]) -> Vec<String> {
String::from_utf8_lossy(stdout)
.lines()
.map(str::trim)
.filter(|line| !line.is_empty())
.map(ToOwned::to_owned)
.collect()
}
fn get_current_branch(repo_root: &Path) -> Result<String> {
let output = Command::new("git")
.arg("-C")
@@ -157,7 +240,11 @@ mod tests {
use uuid::Uuid;
fn run_git(repo: &Path, args: &[&str]) -> Result<()> {
let output = Command::new("git").arg("-C").arg(repo).args(args).output()?;
let output = Command::new("git")
.arg("-C")
.arg(repo)
.args(args)
.output()?;
if !output.status.success() {
anyhow::bail!("{}", String::from_utf8_lossy(&output.stderr));
}
@@ -196,7 +283,10 @@ mod tests {
base_branch: "main".to_string(),
};
assert_eq!(diff_summary(&info)?, Some("Clean relative to main".to_string()));
assert_eq!(
diff_summary(&info)?,
Some("Clean relative to main".to_string())
);
fs::write(worktree_dir.join("README.md"), "hello\nmore\n")?;
let dirty = diff_summary(&info)?.expect("dirty summary");
@@ -212,4 +302,63 @@ mod tests {
let _ = fs::remove_dir_all(root);
Ok(())
}
#[test]
fn diff_file_preview_reports_branch_and_working_tree_files() -> Result<()> {
let root = std::env::temp_dir().join(format!("ecc2-worktree-preview-{}", Uuid::new_v4()));
let repo = root.join("repo");
fs::create_dir_all(&repo)?;
run_git(&repo, &["init", "-b", "main"])?;
run_git(&repo, &["config", "user.email", "ecc@example.com"])?;
run_git(&repo, &["config", "user.name", "ECC"])?;
fs::write(repo.join("README.md"), "hello\n")?;
run_git(&repo, &["add", "README.md"])?;
run_git(&repo, &["commit", "-m", "init"])?;
let worktree_dir = root.join("wt-1");
run_git(
&repo,
&[
"worktree",
"add",
"-b",
"ecc/test",
worktree_dir.to_str().expect("utf8 path"),
"HEAD",
],
)?;
fs::write(worktree_dir.join("src.txt"), "branch\n")?;
run_git(&worktree_dir, &["add", "src.txt"])?;
run_git(&worktree_dir, &["commit", "-m", "branch file"])?;
fs::write(worktree_dir.join("README.md"), "hello\nworking\n")?;
let info = WorktreeInfo {
path: worktree_dir.clone(),
branch: "ecc/test".to_string(),
base_branch: "main".to_string(),
};
let preview = diff_file_preview(&info, 6)?;
assert!(
preview
.iter()
.any(|line| line.contains("Branch A") && line.contains("src.txt"))
);
assert!(
preview
.iter()
.any(|line| line.contains("Working M") && line.contains("README.md"))
);
let _ = Command::new("git")
.arg("-C")
.arg(&repo)
.args(["worktree", "remove", "--force"])
.arg(&worktree_dir)
.output();
let _ = fs::remove_dir_all(root);
Ok(())
}
}