release-kit 0.2.3

A canonical release workflow: a technology-agnostic method, per-technology bindings, and the rk CLI that lands and serves them.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//! `rk branches prune`: report the branches a squash merge retired.
//!
//! Preview by default and fully offline: the post-merge hook runs it on
//! every pull, so the read path costs no network and no forge CLI. Only
//! `--verify` and `--apply` resolve the forge, and only `--apply` deletes
//! — each branch on the strength of a merged request whose recorded head
//! equals the local tip, never on `[gone]` alone.

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};

/// The closing line every non-quiet report ends with; it states who owns
/// the deletion, in the same voice as the landed routing block.
const OPERATOR_LINE: &str = "Deleting a branch is the operator's action: an agent reading this states the command and waits to be asked.";

/// The machine form of a prune report.
#[derive(Debug, Serialize)]
struct Report {
    /// The shape version of this document.
    schema: &'static str,
    /// Which mode produced it: preview, verify, or apply.
    mode: &'static str,
    /// Every gone branch, judged; empty when the clone is clean.
    branches: Vec<Row>,
    /// What plausibly follows.
    next: Vec<String>,
}

/// One gone branch in the report.
#[derive(Debug, Serialize)]
struct Row {
    /// The branch name.
    name: String,
    /// The full object name at the tip.
    tip: String,
    /// The judgment: candidate, kept, worktree-bound, confirmed,
    /// unconfirmed, unknown, deleted, or delete-failed.
    status: &'static str,
    /// The merged request that proved the tip, where one did.
    #[serde(skip_serializing_if = "Option::is_none")]
    request: Option<String>,
    /// Why the branch was kept or the answer is missing.
    #[serde(skip_serializing_if = "Option::is_none")]
    detail: Option<String>,
    /// The worktree the branch is checked out in, for the worktree-bound
    /// rows.
    #[serde(skip_serializing_if = "Option::is_none")]
    worktree: Option<String>,
}

impl Row {
    /// Map one judgment onto its wire form.
    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,
        }
    }

    /// The human tail of a row line.
    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(),
        }
    }
}

/// Dispatch the branches surface.
///
/// # Errors
///
/// Refuses when the target is not a git repository, propagates a git or
/// forge-CLI resolution failure, and — under `--apply` — returns the
/// subprocess failure of a deletion git itself refused, after the report
/// has named every branch's outcome.
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),
        ),
    }
}

/// The whole verb: enumerate, guard, optionally confirm, optionally
/// delete, and report.
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();

    // The forge is asked only where a candidate exists to confirm: the
    // clean path stays offline in every mode.
    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(())
}

/// Resolve the forge once and ask it about every candidate, in place.
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(())
}

/// Delete one confirmed branch, updating its row; `Err(1)` counts a
/// failed deletion toward the run's typed failure.
///
/// Two guards close the window between verification and deletion. The
/// checkout state is re-read at the last instant — a branch someone
/// checked out mid-run becomes worktree-bound, because `update-ref`,
/// unlike `branch -D`, never looks at worktree HEADs. Then the deletion
/// itself is a compare-and-delete: the verified tip travels with it, so
/// a ref the forge CLI raced past the verification is refused, not lost.
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) => {
            // Fail closed: a probe that cannot answer proves nothing,
            // and only a probe that answered "free" clears the delete.
            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";
    // What `git branch -d` would have removed beside the ref: the
    // branch's own configuration section, so a later branch under the
    // reused name inherits nothing stale. A section that was never
    // written makes the removal fail, which is the common clean case;
    // entries that survive the attempt are the reportable failure.
    let section = format!("branch.{}", row.name);
    let removed = git(target, &["config", "--remove-section", &section]).map_err(|_| 1usize)?;
    if !removed.status.success() {
        // Enumerate rather than pattern-match: a branch name can carry
        // regex metacharacters, so the filter is an exact prefix test
        // over the fixed-pattern listing.
        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(())
}

/// Judge the last-instant probe: `Err` when it did not answer, the
/// worktree path when the branch is checked out, `None` when it is free.
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))
}

/// What plausibly follows each mode; an apply is its own conclusion.
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(),
    }
}

/// The human report: silent under `--quiet` when nothing is reportable,
/// one judged line per gone branch otherwise, closed by who owns the
/// deletion.
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);
}

/// The count-bearing first 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):"
        )
    }
}

/// Run one git command against the target, spawn failure typed.
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"),
            )
        })
}

/// The last non-empty stderr line, for a one-line detail.
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};

    /// The last-instant probe fails closed: no answer is an error, a
    /// worktree path spares the branch, and only "free" clears the way.
    #[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)
        );
    }

    /// The complete `rk.branches-prune/1` shape, held by snapshot in both
    /// the populated and the clean forms.
    #[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"
        );
    }
}