voom 0.14.1

Fast, safe, parallel build-artifact pruning across every major language ecosystem
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! The two renderers over one result set.
//!
//! Split from the rest of the module for the same reason `src/report/` is a module of its own:
//! rendering knows how wide a column is and which word names a state, and knows nothing about
//! subprocesses. Both renderers are fed from one [`GitPruneResult`], so the JSON cannot drift
//! from what the human saw.
//!
//! The house style is the sweep's (ADR 0007): blocks, an aligned em dash before a reason, colour
//! as a second channel and never the only one, every state named in words, and a deterministic
//! order.
//!
//! There are two audiences. `voom git-prune` asked, so it is told about every repository. A
//! sweep did not, so [`write_sweep_block`] prints only the repositories where something happened
//! — and nothing at all, not even a heading, when nothing did.

use std::io;
use std::path::{Path, PathBuf};

use owo_colors::{OwoColorize, Style};
use serde_json::{Value, json};

use super::{GitPruneResult, Repository, SCHEMA_VERSION, Status, Step, StepKind, StepOutcome};

/// The widest a path column is padded out to before its explanation, as in the sweep's report.
const MAX_PATH_COLUMN: usize = 40;

/// Spaces between two columns.
const GUTTER: &str = "  ";

/// Introduces the explanation that follows a path.
const BECAUSE: &str = "— ";

/// The palette, matching the sweep's report: colour is a second channel and never the only one.
mod palette {
    use owo_colors::Style;

    pub fn pruned() -> Style {
        Style::new().green()
    }

    pub fn would_prune() -> Style {
        Style::new().yellow()
    }

    /// A rail doing its job, which the sweep also prints in cyan.
    pub fn skipped() -> Style {
        Style::new().cyan()
    }

    pub fn failed() -> Style {
        Style::new().red()
    }

    pub fn quiet() -> Style {
        Style::new().dimmed()
    }
}

/// One rendered line, with every column held as plain text so the block can be measured.
struct Row {
    label: &'static str,
    style: Style,
    path: String,
    detail: Option<String>,
}

/// The directory the report prints paths relative to: the deepest one containing every root.
fn base_path(roots: &[PathBuf]) -> Option<&Path> {
    let first = roots.first()?;
    let base = first
        .ancestors()
        .find(|candidate| roots.iter().all(|root| root.starts_with(candidate)))?;
    (base.is_absolute() && base.parent().is_some()).then_some(base)
}

fn shorten(path: &Path, base: Option<&Path>) -> String {
    let Some(base) = base else {
        return path.display().to_string();
    };
    match path.strip_prefix(base) {
        Ok(relative) if relative.as_os_str().is_empty() => ".".to_owned(),
        Ok(relative) => relative.display().to_string(),
        Err(_) => path.display().to_string(),
    }
}

/// Everything the repository's steps had to say, in one line.
fn detail_of(repository: &Repository) -> Option<String> {
    if let Some(skipped) = &repository.skipped {
        return Some(skipped.to_string());
    }
    let parts: Vec<String> = repository
        .steps
        .iter()
        .filter(|step| step.kind != StepKind::RemoteList)
        .filter_map(step_detail)
        .collect();
    if parts.is_empty() {
        // A `remote` listing that failed is the only step the filter above drops, so a failure
        // there is named rather than left as a bare "failed".
        return repository
            .steps
            .iter()
            .find(|step| !step.succeeded())
            .map(|step| format!("{} failed", step.label()));
    }
    Some(parts.join("; "))
}

fn step_detail(step: &Step) -> Option<String> {
    match &step.outcome {
        StepOutcome::Succeeded { summary } if summary.is_empty() => None,
        StepOutcome::Succeeded { summary } => Some(summary.clone()),
        StepOutcome::Failed {
            code: Some(code),
            message,
        } => Some(format!("{} exited {code}: {message}", step.label())),
        StepOutcome::Failed { code: None, message } => Some(format!("{}: {message}", step.label())),
        StepOutcome::TimedOut => Some(format!("{} timed out and was killed", step.label())),
    }
}

fn row(repository: &Repository, dry_run: bool, base: Option<&Path>) -> Row {
    let (label, style) = match repository.status() {
        Status::Pruned if dry_run && repository.has_pending_work() => ("would prune", palette::would_prune()),
        Status::Pruned if dry_run => ("nothing to prune", palette::quiet()),
        Status::Pruned => ("pruned", palette::pruned()),
        Status::Skipped => ("skipped", palette::skipped()),
        Status::Failed => ("failed", palette::failed()),
    };
    Row {
        label,
        style,
        path: shorten(&repository.path, base),
        detail: detail_of(repository),
    }
}

/// Renders the subcommand's human report, which names every repository it looked at.
///
/// # Errors
///
/// Propagates any write failure from `out`.
pub fn render_human(result: &GitPruneResult, out: &mut impl io::Write) -> io::Result<()> {
    let base = base_path(&result.roots);
    let rows: Vec<Row> = result
        .repositories
        .iter()
        .map(|repository| row(repository, result.dry_run, base))
        .collect();

    if let Some(base) = base.filter(|_| !rows.is_empty()) {
        writeln!(out, "{}", base.display().bold())?;
    }
    write_block(&rows, out)?;
    if !rows.is_empty() {
        writeln!(out)?;
    }
    write_footer(result, out)
}

/// Renders the git block for a *sweep's* report: the noteworthy repositories and nothing else.
///
/// `base` is the sweep's own base path, so the git rows line up with the artifact rows above
/// them rather than being shortened against a different directory.
///
/// Returns whether anything was written, so the caller can place its blank lines. Writes nothing
/// at all — not even a heading — when the sweep pruned nothing, which is the correct output for a
/// no-op.
///
/// # Errors
///
/// Propagates any write failure from `out`.
pub fn write_sweep_block(
    result: &GitPruneResult,
    base: Option<&Path>,
    verbose: bool,
    out: &mut impl io::Write,
) -> io::Result<bool> {
    let noteworthy = result.noteworthy(verbose);
    if noteworthy.is_empty() {
        return Ok(false);
    }
    let rows: Vec<Row> = noteworthy
        .iter()
        .map(|repository| row(repository, result.dry_run, base))
        .collect();
    write_block(&rows, out)?;
    Ok(true)
}

fn write_block(rows: &[Row], out: &mut impl io::Write) -> io::Result<()> {
    let label_width = rows.iter().map(|row| width_of(row.label)).max().unwrap_or(0);
    // Only the rows carrying an explanation are measured: they are the only ones the column
    // exists to line up.
    let path_width = rows
        .iter()
        .filter(|row| row.detail.is_some())
        .map(|row| width_of(&row.path))
        .max()
        .unwrap_or(0)
        .min(MAX_PATH_COLUMN);

    for row in rows {
        write!(out, "{GUTTER}{}", row.style.style(row.label))?;
        write_fill(out, label_width.saturating_sub(width_of(row.label)))?;
        write!(out, "{GUTTER}{}", row.path)?;
        match &row.detail {
            None => writeln!(out)?,
            Some(detail) => {
                write_fill(out, path_width.saturating_sub(width_of(&row.path)))?;
                writeln!(
                    out,
                    "{GUTTER}{}",
                    format_args!("{BECAUSE}{detail}").style(palette::quiet())
                )?;
            }
        }
    }
    Ok(())
}

fn write_footer(result: &GitPruneResult, out: &mut impl io::Write) -> io::Result<()> {
    let totals = result.totals();
    let (verb, count) = if result.dry_run {
        ("would be pruned", totals.would_prune)
    } else {
        ("pruned", totals.pruned)
    };
    writeln!(
        out,
        "{} {}, {} {verb} in {:.2?}",
        totals.repositories.bold(),
        repositories_word(totals.repositories),
        count.bold(),
        result.elapsed
    )?;
    // Totals of different kinds get their own lines, the way the sweep's footer separates them.
    if totals.skipped > 0 {
        let text = format!("{} skipped", totals.skipped);
        writeln!(out, "{GUTTER}{}", text.style(palette::skipped()))?;
    }
    if totals.failed > 0 {
        let text = format!("{} failed", totals.failed);
        writeln!(out, "{GUTTER}{}", text.style(palette::failed()))?;
    }
    Ok(())
}

fn repositories_word(count: usize) -> &'static str {
    if count == 1 { "repository" } else { "repositories" }
}

/// The column count `text` occupies. Characters rather than bytes, which is right for the
/// non-Latin path components a filesystem tool meets.
fn width_of(text: &str) -> usize {
    text.chars().count()
}

fn write_fill(out: &mut impl io::Write, spaces: usize) -> io::Result<()> {
    write!(out, "{:spaces$}", "")
}

/// Builds the JSON document.
///
/// Exposed so tests can assert on structure rather than on text, and so the sweep's renderer can
/// embed it whole under its own `git` key.
#[must_use]
pub fn document(result: &GitPruneResult) -> Value {
    let totals = result.totals();
    json!({
        "schema_version": SCHEMA_VERSION,
        "dry_run": result.dry_run,
        "roots": result.roots.iter().map(|root| root.display().to_string()).collect::<Vec<_>>(),
        "repositories": result
            .repositories
            .iter()
            .map(|repository| repository_value(repository, result.dry_run))
            .collect::<Vec<_>>(),
        "totals": {
            "repositories": totals.repositories,
            "pruned": totals.pruned,
            "would_prune": totals.would_prune,
            "skipped": totals.skipped,
            "failed": totals.failed,
        },
        "elapsed_ms": u64::try_from(result.elapsed.as_millis()).unwrap_or(u64::MAX),
    })
}

fn repository_value(repository: &Repository, dry_run: bool) -> Value {
    let status = match repository.status() {
        Status::Pruned if dry_run && repository.has_pending_work() => "would_prune",
        Status::Pruned if dry_run => "nothing_to_prune",
        Status::Pruned => "pruned",
        Status::Skipped => "skipped",
        Status::Failed => "failed",
    };
    json!({
        "path": repository.path.display().to_string(),
        "status": status,
        "skipped": repository.skipped.as_ref().map(|skipped| json!({
            "reason": skipped.code(),
            "detail": skipped.to_string(),
        })),
        "steps": repository
            .steps
            .iter()
            .filter(|step| step.kind != StepKind::RemoteList || !step.succeeded())
            .map(step_value)
            .collect::<Vec<_>>(),
    })
}

fn step_value(step: &Step) -> Value {
    let outcome = match &step.outcome {
        StepOutcome::Succeeded { summary } => json!({ "status": "succeeded", "detail": summary }),
        StepOutcome::Failed { code, message } => json!({
            "status": "failed",
            "exit_code": code,
            "detail": message,
        }),
        StepOutcome::TimedOut => json!({
            "status": "timed_out",
            "detail": "killed after the repository timeout",
        }),
    };
    json!({
        "step": step.kind.code(),
        "remote": step.remote,
        // The argv voom actually built. A reader checking that a sweep made no network call reads
        // this rather than trusting the documentation.
        "command": step.command,
        "outcome": outcome,
    })
}

/// Renders the document as pretty-printed JSON.
///
/// # Errors
///
/// Propagates write and serialization failures from `out`.
pub fn render_json(result: &GitPruneResult, out: &mut impl io::Write) -> io::Result<()> {
    serde_json::to_writer_pretty(&mut *out, &document(result))?;
    writeln!(out)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::git::Skipped;
    use std::time::Duration;

    fn step(kind: StepKind, outcome: StepOutcome) -> Step {
        Step {
            kind,
            remote: None,
            command: vec!["git".to_owned(), kind.code().to_owned()],
            outcome,
        }
    }

    fn succeeded(kind: StepKind, summary: &str) -> Step {
        step(
            kind,
            StepOutcome::Succeeded {
                summary: summary.to_owned(),
            },
        )
    }

    fn result(repositories: Vec<Repository>, dry_run: bool) -> GitPruneResult {
        GitPruneResult {
            roots: vec![PathBuf::from("/projects")],
            repositories,
            dry_run,
            elapsed: Duration::from_millis(120),
        }
    }

    fn repository(path: &str, steps: Vec<Step>) -> Repository {
        Repository {
            path: PathBuf::from(path),
            skipped: None,
            steps,
        }
    }

    fn rendered(result: &GitPruneResult) -> String {
        let mut buffer = Vec::new();
        render_human(result, &mut buffer).expect("writing to a Vec cannot fail");
        String::from_utf8(buffer).expect("the report is UTF-8")
    }

    /// The rule the sweep's report depends on: a run where `gc --auto` decided against repacking
    /// and no worktree was stale says nothing at all.
    #[test]
    fn should_write_nothing_into_a_sweep_when_nothing_happened() {
        let quiet = result(
            vec![repository(
                "/projects/api",
                vec![succeeded(StepKind::WorktreePrune, ""), succeeded(StepKind::Gc, "")],
            )],
            false,
        );

        let mut buffer = Vec::new();
        let wrote = write_sweep_block(&quiet, None, false, &mut buffer).expect("writing to a Vec cannot fail");

        assert!(!wrote, "a no-op is silent");
        assert!(buffer.is_empty(), "{}", String::from_utf8_lossy(&buffer));
    }

    #[test]
    fn should_write_a_sweep_line_for_a_repository_where_something_happened() {
        let pruned = result(
            vec![
                repository(
                    "/projects/api",
                    vec![
                        succeeded(StepKind::WorktreePrune, "1 stale worktree pruned"),
                        succeeded(StepKind::Gc, ""),
                    ],
                ),
                repository(
                    "/projects/quiet",
                    vec![succeeded(StepKind::WorktreePrune, ""), succeeded(StepKind::Gc, "")],
                ),
            ],
            false,
        );

        let mut buffer = Vec::new();
        let wrote = write_sweep_block(&pruned, Some(Path::new("/projects")), false, &mut buffer).expect("a write");
        let text = String::from_utf8(buffer).expect("the report is UTF-8");

        assert!(wrote);
        assert!(text.contains("api"), "{text}");
        assert!(text.contains("1 stale worktree pruned"), "{text}");
        assert!(
            !text.contains("quiet"),
            "a repository with nothing to say gets no line: {text}"
        );
    }

    /// The explicit subcommand reports every repository, including the quiet ones — there the
    /// user asked.
    #[test]
    fn should_name_every_repository_in_the_subcommands_report() {
        let text = rendered(&result(
            vec![repository(
                "/projects/quiet",
                vec![succeeded(StepKind::WorktreePrune, ""), succeeded(StepKind::Gc, "")],
            )],
            false,
        ));

        assert!(text.contains("quiet"), "{text}");
        // The counts are bold, so the escapes sit between them and the words they qualify —
        // asserting on the words is what survives that without hard-coding the palette.
        assert!(text.contains("repository, "), "{text}");
        assert!(text.contains(" pruned in "), "{text}");
    }

    /// Every state is named in words, so the report survives `NO_COLOR` and a pipe to a file.
    #[test]
    fn should_name_a_skip_and_its_reason_in_words() {
        let mut skipped = repository("/projects/halfway", Vec::new());
        skipped.skipped = Some(Skipped::InProgress {
            marker: ".git/rebase-merge".to_owned(),
        });

        let text = rendered(&result(vec![skipped], false));

        assert!(text.contains("skipped"), "{text}");
        assert!(
            text.contains("a git operation is in progress (.git/rebase-merge)"),
            "{text}"
        );
        assert!(text.contains("1 skipped"), "the footer counts it: {text}");
    }

    #[test]
    fn should_name_a_failure_and_what_git_said() {
        let failed = repository(
            "/projects/broken",
            vec![step(
                StepKind::WorktreePrune,
                StepOutcome::Failed {
                    code: Some(128),
                    message: "fatal: bad config line 1".to_owned(),
                },
            )],
        );

        let text = rendered(&result(vec![failed], false));

        assert!(text.contains("failed"), "{text}");
        assert!(
            text.contains("worktree prune exited 128: fatal: bad config line 1"),
            "{text}"
        );
    }

    #[test]
    fn should_carry_the_schema_version_and_the_argv_it_ran() {
        let document = document(&result(
            vec![repository(
                "/projects/api",
                vec![succeeded(StepKind::WorktreePrune, "1 stale worktree pruned")],
            )],
            false,
        ));

        assert_eq!(document["schema_version"], SCHEMA_VERSION);
        assert_eq!(document["repositories"][0]["status"], "pruned");
        assert_eq!(document["repositories"][0]["steps"][0]["step"], "worktree_prune");
        assert_eq!(document["totals"]["pruned"], 1);
    }

    #[test]
    fn should_mark_a_dry_run_as_would_prune_only_when_git_has_something_to_remove() {
        let document = document(&result(
            vec![
                repository(
                    "/projects/api",
                    vec![succeeded(StepKind::WorktreePrune, "1 stale worktree would be pruned")],
                ),
                repository(
                    "/projects/quiet",
                    vec![
                        succeeded(StepKind::WorktreePrune, ""),
                        succeeded(StepKind::CountObjects, "3 loose objects"),
                    ],
                ),
            ],
            true,
        ));
        assert_eq!(document["repositories"][0]["status"], "would_prune");
        assert_eq!(document["repositories"][1]["status"], "nothing_to_prune");
        assert_eq!(document["totals"]["would_prune"], 1);
        assert_eq!(document["dry_run"], true);
    }

    #[test]
    fn should_not_label_a_dry_run_repository_would_prune_when_nothing_would_be_pruned() {
        let text = rendered(&result(
            vec![
                repository(
                    "/projects/api",
                    vec![succeeded(StepKind::WorktreePrune, "1 stale worktree would be pruned")],
                ),
                repository(
                    "/projects/quiet",
                    vec![
                        succeeded(StepKind::WorktreePrune, ""),
                        succeeded(StepKind::CountObjects, ""),
                    ],
                ),
            ],
            true,
        ));
        let text = anstream::adapter::strip_str(&text).to_string();
        let line = |name: &str| text.lines().find(|line| line.contains(name)).expect("a row").to_owned();

        assert!(line("api").contains("would prune"), "{text}");
        assert!(line("quiet").contains("nothing to prune"), "{text}");
        assert!(text.contains("1 would be pruned"), "{text}");
    }

    #[test]
    fn should_write_valid_json() {
        let mut buffer = Vec::new();
        render_json(&result(Vec::new(), true), &mut buffer).expect("writing to a Vec cannot fail");
        let parsed: Value = serde_json::from_slice(&buffer).expect("the output parses");
        assert_eq!(parsed["totals"]["repositories"], 0);
    }
}