use camino::Utf8Path;
use serde::Serialize;
use crate::branches::{Branch, Class, FOR_EACH_REF_FORMAT, classify, merged_request_for};
use crate::cli::branches::{BranchesAction, BranchesArgs};
use crate::detect::Forge;
use crate::diagnostic::{Diagnostic, Reason};
use crate::error::RkError;
use crate::output::Output;
use crate::setup::context::{TRUNK_BRANCH, resolve_cli};
const OPERATOR_LINE: &str = "Deleting a branch is the operator's action: an agent reading this states the command and waits to be asked.";
#[derive(Debug, Serialize)]
struct Report {
schema: &'static str,
mode: &'static str,
branches: Vec<Row>,
next: Vec<String>,
}
#[derive(Debug, Serialize)]
struct Row {
name: String,
tip: String,
status: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
request: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
detail: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
worktree: Option<String>,
}
impl Row {
fn from(branch: &Branch, class: Class) -> Self {
let (status, request, detail, worktree) = match class {
Class::Kept { reason } => ("kept", None, Some(reason), None),
Class::Candidate => ("candidate", None, None, None),
Class::WorktreeBound { path } => ("worktree-bound", None, None, Some(path)),
Class::Confirmed { request } => ("confirmed", Some(request), None, None),
Class::Unconfirmed { detail } => ("unconfirmed", None, Some(detail), None),
Class::Unknown { detail } => ("unknown", None, Some(detail), None),
};
Self {
name: branch.name.clone(),
tip: branch.tip.clone(),
status,
request,
detail,
worktree,
}
}
fn describe(&self) -> String {
match self.status {
"kept" => format!("kept: {}", self.detail.as_deref().unwrap_or("")),
"worktree-bound" => format!(
"worktree-bound: checked out at {}; its worktree owns the cleanup",
self.worktree.as_deref().unwrap_or("")
),
"confirmed" => format!(
"confirmed: merged request {} matches this tip",
self.request.as_deref().unwrap_or("")
),
"unconfirmed" => format!("unconfirmed: {}", self.detail.as_deref().unwrap_or("")),
"unknown" => format!("unknown: {}", self.detail.as_deref().unwrap_or("")),
"deleted" => {
let mut line = format!(
"deleted (merged request {})",
self.request.as_deref().unwrap_or("")
);
if let Some(detail) = &self.detail {
line.push_str("; ");
line.push_str(detail);
}
line
}
"delete-failed" => format!("delete failed: {}", self.detail.as_deref().unwrap_or("")),
_ => "candidate".to_owned(),
}
}
}
pub fn run(args: &BranchesArgs) -> Result<(), RkError> {
match &args.action {
BranchesAction::Prune {
target,
repo,
forge,
verify,
apply,
quiet,
json,
} => prune(
target,
repo.as_deref(),
forge.as_deref(),
*verify,
*apply,
*quiet,
Output::new(*json),
),
}
}
fn prune(
target: &Utf8Path,
repo_flag: Option<&str>,
forge_flag: Option<&str>,
verify: bool,
apply: bool,
quiet: bool,
out: Output,
) -> Result<(), RkError> {
if !target.is_dir() {
return Err(RkError::missing(
Diagnostic::new(
Reason::TargetNotFound,
format!("target {target} is not a directory"),
)
.expected("an existing repository to read"),
));
}
let listed = git(
target,
&[
"for-each-ref",
"refs/heads",
"--format",
FOR_EACH_REF_FORMAT,
],
)?;
if !listed.status.success() {
return Err(RkError::refusal(
Diagnostic::new(
Reason::PrerequisiteUnmet,
format!("target {target} is not a git repository"),
)
.expected("a repository whose branches git can list"),
));
}
let branches = crate::branches::parse_branches(&String::from_utf8_lossy(&listed.stdout));
let current = git(target, &["symbolic-ref", "--quiet", "--short", "HEAD"])
.ok()
.filter(|answer| answer.status.success())
.map(|answer| String::from_utf8_lossy(&answer.stdout).trim().to_owned())
.filter(|name| !name.is_empty());
let mut judged: Vec<(&Branch, Class)> = branches
.iter()
.filter_map(|branch| {
classify(branch, current.as_deref(), TRUNK_BRANCH).map(|class| (branch, class))
})
.collect();
if (verify || apply)
&& judged
.iter()
.any(|(_, class)| matches!(class, Class::Candidate))
{
confirm_candidates(target, forge_flag, repo_flag, &mut judged)?;
}
let mut rows: Vec<Row> = judged
.iter()
.map(|(branch, class)| Row::from(branch, class.clone()))
.collect();
let mut failed_deletes = 0usize;
if apply {
for row in &mut rows {
if row.status != "confirmed" {
continue;
}
if let Err(count) = delete_branch(target, row) {
failed_deletes += count;
}
}
}
let mode = if apply {
"apply"
} else if verify {
"verify"
} else {
"preview"
};
let next = next_lines(mode);
render(out, &rows, &next, quiet);
out.emit(&Report {
schema: "rk.branches-prune/1",
mode,
branches: rows,
next,
})?;
if failed_deletes > 0 {
return Err(RkError::subprocess(
Diagnostic::new(
Reason::SubprocessFailed,
format!("git refused to delete {failed_deletes} confirmed branches"),
)
.expected("every confirmed branch deleted; the report names each outcome"),
));
}
Ok(())
}
fn confirm_candidates(
target: &Utf8Path,
forge_flag: Option<&str>,
repo_flag: Option<&str>,
judged: &mut [(&Branch, Class)],
) -> Result<(), RkError> {
let resolved = crate::landing::resolve(target, forge_flag, repo_flag)?;
let forge = Forge::parse(&resolved.forge)
.ok_or_else(|| RkError::Usage(format!("unknown forge '{}'", resolved.forge)))?;
let repo = resolved.repo.ok_or_else(crate::landing::repo_unresolved)?;
let cli = resolve_cli(forge)?;
for (branch, class) in judged {
if matches!(class, Class::Candidate) {
*class = merged_request_for(&cli, target.as_std_path(), forge, &repo, &branch.tip);
}
}
Ok(())
}
fn delete_branch(target: &Utf8Path, row: &mut Row) -> Result<(), usize> {
let ref_name = format!("refs/heads/{}", row.name);
let rechecked = git(
target,
&[
"for-each-ref",
&ref_name,
"--format",
"%(objectname)%09%(worktreepath)",
],
)
.map_err(|_| 1usize)?;
match recheck_verdict(&rechecked) {
Err(detail) => {
row.status = "delete-failed";
row.detail = Some(detail);
return Err(1);
}
Ok(Some(worktree)) => {
row.status = "worktree-bound";
row.worktree = Some(worktree);
return Ok(());
}
Ok(None) => {}
}
let deleted = git(target, &["update-ref", "-d", &ref_name, &row.tip]).map_err(|_| 1usize)?;
if !deleted.status.success() {
row.status = "delete-failed";
row.detail = Some(last_line(&deleted.stderr));
return Err(1);
}
row.status = "deleted";
let section = format!("branch.{}", row.name);
let removed = git(target, &["config", "--remove-section", §ion]).map_err(|_| 1usize)?;
if !removed.status.success() {
let leftover =
git(target, &["config", "--get-regexp", "^branch\\."]).map_err(|_| 1usize)?;
let prefix = format!("branch.{}.", row.name);
let survives = leftover.status.success()
&& String::from_utf8_lossy(&leftover.stdout)
.lines()
.any(|line| line.starts_with(&prefix));
if survives {
row.detail = Some("the branch configuration could not be removed".to_owned());
}
}
Ok(())
}
fn recheck_verdict(probe: &std::process::Output) -> Result<Option<String>, String> {
if !probe.status.success() {
return Err(format!(
"the checkout recheck failed: {}",
last_line(&probe.stderr)
));
}
let answer = String::from_utf8_lossy(&probe.stdout);
let worktree = answer
.trim_end()
.split_once('\t')
.map(|(_, worktree)| worktree.to_owned())
.unwrap_or_default();
Ok((!worktree.is_empty()).then_some(worktree))
}
fn next_lines(mode: &str) -> Vec<String> {
let verify = "rk branches prune --verify confirms each candidate against the forge";
let apply = "rk branches prune --apply verifies, then deletes the confirmed branches";
match mode {
"preview" => vec![verify.to_owned(), apply.to_owned()],
"verify" => vec![apply.to_owned()],
_ => Vec::new(),
}
}
fn render(out: Output, rows: &[Row], next: &[String], quiet: bool) {
if quiet && rows.is_empty() {
return;
}
if rows.is_empty() {
out.result_line("no local branch tracks a gone remote branch");
} else {
out.result_line(header(rows.len()));
let width = rows.iter().map(|row| row.name.len()).max().unwrap_or(0);
for row in rows {
let tip = row.tip.get(..8).unwrap_or(&row.tip);
out.result_line(format!(" {:width$} {tip} {}", row.name, row.describe()));
}
}
out.next(next);
out.result_line(OPERATOR_LINE);
}
fn header(count: usize) -> String {
if count == 1 {
"1 local branch tracks a remote branch that is gone (a candidate, not proof):".to_owned()
} else {
format!(
"{count} local branches track a remote branch that is gone (a candidate, not proof):"
)
}
}
fn git(target: &Utf8Path, args: &[&str]) -> Result<std::process::Output, RkError> {
std::process::Command::new("git")
.arg("-C")
.arg(target.as_std_path())
.args(args)
.output()
.map_err(|source| {
RkError::subprocess(
Diagnostic::new(
Reason::SubprocessSpawn,
format!("git did not run: {source}"),
)
.expected("git installed and on PATH"),
)
})
}
fn last_line(bytes: &[u8]) -> String {
String::from_utf8_lossy(bytes)
.lines()
.rev()
.find(|line| !line.trim().is_empty())
.unwrap_or("no output")
.to_owned()
}
#[cfg(test)]
mod tests {
#![allow(clippy::expect_used)]
use super::{Report, Row, recheck_verdict};
#[cfg(unix)]
#[test]
fn the_recheck_verdict_fails_closed() {
use std::os::unix::process::ExitStatusExt as _;
let output = |code: i32, stdout: &str, stderr: &str| std::process::Output {
status: std::process::ExitStatus::from_raw(code << 8),
stdout: stdout.as_bytes().to_vec(),
stderr: stderr.as_bytes().to_vec(),
};
let failed = recheck_verdict(&output(128, "", "fatal: not a git repository"));
assert!(
failed.is_err_and(|detail| detail.contains("not a git repository")),
"a probe that cannot answer proves nothing"
);
assert_eq!(
recheck_verdict(&output(
0,
"aaaa /srv/checkouts/wt
",
""
)),
Ok(Some("/srv/checkouts/wt".to_owned()))
);
assert_eq!(
recheck_verdict(&output(
0, "aaaa
", ""
)),
Ok(None)
);
}
#[test]
fn the_branches_prune_schema_snapshot_holds() {
let populated = Report {
schema: "rk.branches-prune/1",
mode: "verify",
branches: vec![
Row {
name: "feat/x".into(),
tip: "aaaabbbbccccddddaaaabbbbccccddddaaaabbbb".into(),
status: "confirmed",
request: Some("#8".into()),
detail: None,
worktree: None,
},
Row {
name: "fix/y".into(),
tip: "bbbbccccddddaaaabbbbccccddddaaaabbbbcccc".into(),
status: "kept",
request: None,
detail: Some("the current branch".into()),
worktree: None,
},
Row {
name: "fix/z".into(),
tip: "ccccddddaaaabbbbccccddddaaaabbbbccccdddd".into(),
status: "worktree-bound",
request: None,
detail: None,
worktree: Some("/srv/checkouts/wt".into()),
},
],
next: vec![
"rk branches prune --apply verifies, then deletes the confirmed branches".into(),
],
};
assert_eq!(
serde_json::to_string(&populated).expect("a report serializes"),
r##"{"schema":"rk.branches-prune/1","mode":"verify","branches":[{"name":"feat/x","tip":"aaaabbbbccccddddaaaabbbbccccddddaaaabbbb","status":"confirmed","request":"#8"},{"name":"fix/y","tip":"bbbbccccddddaaaabbbbccccddddaaaabbbbcccc","status":"kept","detail":"the current branch"},{"name":"fix/z","tip":"ccccddddaaaabbbbccccddddaaaabbbbccccdddd","status":"worktree-bound","worktree":"/srv/checkouts/wt"}],"next":["rk branches prune --apply verifies, then deletes the confirmed branches"]}"##
);
let clean = Report {
schema: "rk.branches-prune/1",
mode: "preview",
branches: vec![],
next: vec![
"rk branches prune --verify confirms each candidate against the forge".into(),
"rk branches prune --apply verifies, then deletes the confirmed branches".into(),
],
};
assert_eq!(
serde_json::to_string(&clean).expect("a report serializes"),
r#"{"schema":"rk.branches-prune/1","mode":"preview","branches":[],"next":["rk branches prune --verify confirms each candidate against the forge","rk branches prune --apply verifies, then deletes the confirmed branches"]}"#,
"a clean clone reports one empty list a caller can branch on"
);
}
}