oakum 0.12.1

A polyglot release tool that derives dependent version bumps from the dependency graph
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
mod add;
mod base_tree;
mod changelog;
mod check_report;
mod ci;
mod config;
mod consume;
mod coverage;
mod dependency_updates;
mod detect_tools;
mod fs;
mod generate;
mod git;
mod github;
mod handoff;
mod inherited;
mod init;
mod install_pin;
mod intent;
mod lockfiles;
mod markdown;
mod migrate;
mod migrate_config;
mod migrate_gates;
mod migrate_output;
mod migrate_source_plan;
mod owned_files;
mod preconditions;
mod printable;
mod reasons;
mod release;
mod release_state;
mod render;
mod repository;
mod retired;
mod root_files;
mod stale;
mod status;
mod tag_shape;
mod tags;
mod template;
mod update_release;
mod upgrade;
mod verdict;
mod version;
mod workflow;
mod workspace;
mod write_set;

use std::ffi::OsString;
use std::fmt;

use clap::{Parser, Subcommand};

/// Polyglot release tool: version math, changelogs, and graph-derived bumps.
#[derive(Debug, Parser)]
#[command(
    name = "oakum",
    version,
    about,
    long_about = None,
    after_help = "Typical flow: `oakum init`, `oakum workflow` saved under `.github/workflows/`, `oakum add` for each change, \
                  `oakum status` to see the plan, and `oakum check --strict` on pull requests. \
                  CI runs `oakum ci version-pr` and `oakum release`.\n\n\
                  Guides: https://github.com/oakoss/oakum/tree/main/docs/guide"
)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

macro_rules! merge_base {
    () => {
        "the merge-base with `origin/main`, else `main`, else `master`"
    };
}

macro_rules! since_tags {
    () => {
        "each package oakum tags starts at its highest reachable tag"
    };
}

/// `--from` on `generate` and `release`, which read commits since one base.
pub(super) const FROM_MERGE_BASE: &str = concat!(
    "Git ref to scan from (exclusive). Default: ",
    merge_base!(),
    "."
);

/// `--from` on the commands that read coverage as well as the plan.
pub(super) const FROM_PLAN: &str = concat!(
    "Git ref to scan from (exclusive). Default: ",
    merge_base!(),
    "; for intent read from conventional commits, ",
    since_tags!(),
    " instead."
);

/// `--from` on the commands that read only the plan, which bump files make
/// no use of a base for.
pub(super) const FROM_INTENT: &str = concat!(
    "Git ref to read conventional commits from (exclusive); unused with bump files. Default: ",
    since_tags!(),
    ", and every other package at ",
    merge_base!(),
    "."
);

/// Listed in the order a user meets them; clap shows declaration order.
#[derive(Debug, Subcommand)]
enum Commands {
    /// Write oakum's three files and print the workflow to paste.
    Init(init::InitArgs),
    /// Print the workflow for this repository on stdout, writing nothing.
    Workflow(workflow::WorkflowArgs),
    /// Write one `.changeset/*.md` bump file.
    Add(add::AddArgs),
    /// Derive one `.changeset/*.md` from commits on this branch.
    Generate(generate::GenerateArgs),
    /// Print the versioned release state: the `summary` render by default, or JSON.
    Status(status::StatusArgs),
    /// Report drift and name the fix.
    Check(preconditions::CheckArgs),
    /// Apply the plan: bump versions, update lockfiles, write changelogs, consume bump files.
    Version(version::VersionArgs),
    /// Push tags and create GitHub releases for untagged manifest versions.
    Release(release::ReleaseArgs),
    /// GitHub writes for CI.
    Ci(ci::CiArgs),
    /// Transform another tool's bump files and config into oakum's, and print remaining steps.
    Migrate(migrate::MigrateArgs),
    /// Print plan bump-file inputs as JSON (hidden plumbing for tests).
    #[command(name = "plan-intent", hide = true)]
    PlanIntent(intent::PlanIntentArgs),
    /// Print foreign release-tool markers (hidden plumbing for tests).
    #[command(name = "detect-release-tools", hide = true)]
    DetectReleaseTools,
    /// Print tags reachable from HEAD as `commit\\ttag`.
    #[command(name = "reachable-tags", hide = true)]
    ReachableTags,
    /// Tag-only readiness path (no coverage).
    #[command(name = "tag-drift", hide = true)]
    TagDrift,
    /// Migrate `.changeset/_config.toml` and `_schema.json` to this binary, and refresh a README an earlier oakum wrote.
    Upgrade,
}

/// Says a line on stderr without panicking when the write is refused. A broken
/// pipe makes `eprintln!` panic, which replaces ADR-0034's exit code with 101
/// and loses the refusal — measured: a run owing exit 2 exited 101 and printed
/// nothing when its stderr reader had gone away. A *closed* stderr does not,
/// for the reason `git::env::say` records. Escaped here so no message has to:
/// [`printable`].
pub(crate) fn say_err(line: &str) {
    let _ = printable::say(std::io::stderr(), line);
}

/// The same for stdout. A report is not worth a panic: `println!` aborts on a
/// refused write, so `oakum check | head -1` exited 101 with nothing on either
/// stream, discarding an outcome the run had already established.
pub(crate) fn say_out(line: &str) {
    let _ = printable::say(std::io::stdout(), line);
}

/// A line that is the command's result (a path, a URL, a document) rather than
/// a report about it. `say_out` discards a refusal because a reader who went
/// away chose not to read; a result nobody received is a command that did not
/// finish. The caller maps the error with the side effect that already
/// happened, so exit 2 (unverified) and stderr name what exists.
/// A closed descriptor (`>&-`) is not a refusal: std reports `Ok` on EBADF
/// for stdout, so a caller who closed it gets exit 0, as for `ask`'s stderr.
pub(crate) fn deliver_out(line: &str) -> std::io::Result<()> {
    use std::io::Write;
    writeln!(std::io::stdout(), "{line}")
}

/// [`deliver_out`] for a pre-formatted block, written as-is: it carries its own
/// newlines, and a body that ends without one must stay that way.
pub(crate) fn deliver_block(text: &str) -> std::io::Result<()> {
    use std::io::Write;
    let mut stdout = std::io::stdout();
    stdout.write_all(text.as_bytes())?;
    // Stdout is line-buffered: a body with no newline would sit until the
    // exit flush, whose refusal nobody reads.
    stdout.flush()
}

/// [`deliver_block`] for a report a person reads rather than a result a script
/// takes, so it is escaped as `say_out` escapes.
pub(crate) fn deliver_report(text: &str) -> std::io::Result<()> {
    deliver_block(&printable::for_terminal(text))
}

/// A prompt on stderr, no newline, flushed so it reaches the terminal before
/// the read that follows. A refused write propagates: a prompt that cannot be
/// shown cannot be answered. A *closed* stderr does not — std reports `Ok` on
/// EBADF, as `say_err` records — so the wizard then runs on an empty read.
pub(crate) fn ask(prompt: &str) -> std::io::Result<()> {
    printable::prompt(std::io::stderr(), prompt)
}

pub fn run() -> Result<(), CliError> {
    run_with(std::env::args_os())
}

fn run_with<I, T>(args: I) -> Result<(), CliError>
where
    I: IntoIterator<Item = T>,
    T: Into<OsString> + Clone,
{
    let cli = Cli::parse_from(args);
    match cli.command {
        Commands::Add(args) => add::run(args).map_err(CliError::from_boxed),
        Commands::Check(args) => preconditions::run(&args),
        Commands::TagDrift => preconditions::run_tags_only(),
        Commands::Generate(args) => generate::run(&args).map_err(CliError::from_boxed),
        Commands::Init(args) => init::run(&args).map_err(CliError::from_boxed),
        Commands::Workflow(args) => workflow::run(&args).map_err(CliError::from_boxed),
        Commands::Migrate(args) => migrate::run(&args).map_err(CliError::from_boxed),
        Commands::PlanIntent(args) => intent::run(&args).map_err(CliError::from_boxed),
        Commands::DetectReleaseTools => detect_tools::run(),
        Commands::Status(args) => status::run(&args).map_err(CliError::from_boxed),
        Commands::ReachableTags => tags::run(),
        Commands::Upgrade => upgrade::run().map_err(CliError::from_boxed),
        Commands::Version(args) => version::run(&args).map_err(CliError::from_boxed),
        Commands::Release(args) => release::run(&args),
        Commands::Ci(args) => ci::run(&args),
    }
}

pub(super) fn binary_version() -> Result<semver::Version, Box<dyn std::error::Error>> {
    env!("CARGO_PKG_VERSION")
        .parse::<semver::Version>()
        .map_err(|err| {
            Box::new(CliError::new(format!(
                "this binary reports a non-semver version: {err}"
            )))
            .into()
        })
}

/// The one implementation lives in the library, which builds the same sentence
/// for a render that failed on an undefined variable.
pub(super) use oakum::template::quoted;

/// Asserts the schema description for a template surface names every variable
/// that surface actually renders with. The lists are written by hand where a
/// reader configuring the key will find them; this is what stops them rotting
/// away from the contexts, which is how `tag-format` came to document nothing
/// about `package` at all.
#[cfg(test)]
pub(crate) fn schema_names_every_variable(surface: &str, context: impl serde::Serialize) {
    let schema = oakum::config::schema();
    let described = schema["properties"][surface]["description"]
        .as_str()
        .unwrap_or_else(|| panic!("`{surface}` has a schema description"))
        .to_owned();
    let rendered = serde_json::to_value(context).expect("the context serializes");
    let keys = rendered
        .as_object()
        .unwrap_or_else(|| panic!("`{surface}` renders with a map context"));
    assert!(!keys.is_empty(), "`{surface}` renders with no variables");
    for key in keys.keys() {
        assert!(
            described.contains(&format!("`{key}`")),
            "`{surface}` receives `{key}`, which its schema description does not name: {described}"
        );
    }
}

/// What a failure is, once: the exit code and the stderr token are both read
/// off it.
///
/// Ordered by which one a run carrying several refusals should carry out:
/// a finding outranks a look that did not happen, so `Error` is declared first.
/// ADR-0034 exists so a caller can tell the two apart, and an ordering held
/// anywhere but on this type is a second place for them to disagree.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum Outcome {
    Error,
    Unverified,
}

impl Outcome {
    fn code(self) -> i32 {
        match self {
            Self::Unverified => 2,
            Self::Error => 1,
        }
    }

    fn token(self) -> &'static str {
        match self {
            Self::Unverified => "unverified",
            Self::Error => "error",
        }
    }
}

/// A run that did not succeed: its [`Outcome`] and the message stderr carries.
///
/// `Clone` so a failure can be cached and handed to more than one caller.
#[derive(Clone, Debug)]
pub(crate) struct CliError {
    class: Outcome,
    message: String,
    /// A git child that gave no answer, so a caller that turns an answered
    /// look into a plain refusal can still keep this one unverified.
    unanswered: bool,
}

impl CliError {
    pub(crate) fn new(message: impl Into<String>) -> Self {
        Self {
            class: Outcome::Error,
            message: message.into(),
            unanswered: false,
        }
    }

    pub(crate) fn unverified(detail: impl Into<String>) -> Self {
        Self {
            class: Outcome::Unverified,
            message: detail.into(),
            unanswered: false,
        }
    }

    pub(crate) fn unanswered(detail: impl Into<String>) -> Self {
        Self {
            unanswered: true,
            ..Self::unverified(detail)
        }
    }

    pub(crate) const fn is_unanswered(&self) -> bool {
        self.unanswered
    }

    #[cfg(test)]
    pub(crate) const fn is_unverified(&self) -> bool {
        matches!(self.class, Outcome::Unverified)
    }

    /// A new detail under this verdict's class. A wrapper that adds what
    /// landed must not reclassify why the run stopped.
    pub(crate) fn recast(&self, detail: impl Into<String>) -> Self {
        Self {
            message: detail.into(),
            ..self.clone()
        }
    }

    /// A result nobody received. `what` names the side effect that already
    /// happened and the thing that did not arrive ("wrote x, but its path"),
    /// so the shell learns both from one line.
    pub(crate) fn undelivered(what: impl std::fmt::Display, err: &std::io::Error) -> Self {
        Self::unverified(format!("{what} could not be delivered to stdout: {err}"))
    }

    pub(crate) fn tag_drift(count: usize) -> Self {
        Self::new(format!("{count} package(s) bumped without a tag"))
    }

    pub(crate) fn uncovered(count: usize) -> Self {
        Self::new(format!(
            "{count} package(s) changed with no covering intent"
        ))
    }

    pub(crate) fn from_boxed(err: Box<dyn std::error::Error>) -> Self {
        match err.downcast::<Self>() {
            Ok(typed) => *typed,
            Err(other) => Self::new(other.to_string()),
        }
    }

    /// Both channels a caller reads, the number and the word, come from here.
    pub(crate) const fn class(&self) -> Outcome {
        self.class
    }

    /// The three outcomes `AGENTS.md` requires, as the only channel a caller
    /// that is not reading stderr can see: `0` ok, `2` unverified, `1` error.
    ///
    /// Every exit stays non-zero, so a shell testing success is unaffected;
    /// what changes is that "we could not look" no longer arrives wearing the
    /// same number as "this failed".
    pub(crate) fn exit_code(&self) -> i32 {
        self.class().code()
    }

    /// The token stderr leads with. Printing a fixed `error: ` put two outcome
    /// tokens on one line — `error: unverified: …` — saying different things
    /// about one result.
    pub(crate) fn outcome(&self) -> &'static str {
        self.class().token()
    }

    /// The message without a leading `unverified: ` outcome token.
    pub(crate) fn detail(&self) -> String {
        let message = self.to_string();
        message
            .strip_prefix("unverified: ")
            .map(str::to_owned)
            .unwrap_or(message)
    }
}

impl fmt::Display for CliError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.message)
    }
}

impl std::error::Error for CliError {}

impl From<Box<dyn std::error::Error>> for CliError {
    fn from(err: Box<dyn std::error::Error>) -> Self {
        Self::from_boxed(err)
    }
}

impl From<github::Error> for CliError {
    fn from(err: github::Error) -> Self {
        match err {
            github::Error::Unverified { detail } => Self::unverified(detail),
            github::Error::Forbidden { path } => Self::new(format!("GitHub {path} returned 403")),
            unauthorized @ github::Error::Unauthorized { .. } => {
                Self::new(unauthorized.to_string())
            }
            github::Error::Other(message) => Self::new(message),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{CliError, Outcome};

    #[test]
    fn detail_strips_a_leading_unverified_token() {
        let nested = CliError::unverified("unverified: no remotes");
        assert_eq!(nested.detail(), "no remotes");
        let plain = CliError::new("boom");
        assert_eq!(plain.detail(), "boom");
    }

    /// A wrapper that adds context must not drop the mark a caller reads to
    /// keep a no-answer unverified.
    #[test]
    fn recast_keeps_an_unanswered_mark() {
        let wrapped = CliError::unanswered("unverified: git log failed").recast("context");
        assert!(wrapped.is_unanswered());
        assert_eq!(wrapped.exit_code(), 2);
        assert!(!CliError::unverified("looked")
            .recast("context")
            .is_unanswered());
    }

    /// Each constructor beside the code it must answer.
    fn exit_codes() -> [(CliError, i32); 6] {
        [
            (CliError::unverified("unverified: no remotes"), 2),
            (CliError::unanswered("unverified: git log failed"), 2),
            (CliError::tag_drift(1), 1),
            (CliError::uncovered(2), 1),
            (
                CliError::from(super::github::Error::Forbidden {
                    path: String::from("/comments"),
                }),
                1,
            ),
            (CliError::new("boom"), 1),
        ]
    }

    #[test]
    fn each_outcome_has_one_code_and_one_token() {
        assert_eq!(
            (Outcome::Unverified.code(), Outcome::Unverified.token()),
            (2, "unverified")
        );
        assert_eq!(
            (Outcome::Error.code(), Outcome::Error.token()),
            (1, "error")
        );
    }

    #[test]
    fn only_unverified_exits_two() {
        for (err, expected) in exit_codes() {
            assert_eq!(err.exit_code(), expected, "{err}");
            assert_eq!(expected == 2, err.is_unverified(), "{err}");
        }
    }

    /// The number and the word a caller reads come off one outcome, so a
    /// failure cannot exit 2 while stderr calls it an error.
    #[test]
    fn the_printed_token_agrees_with_the_exit_code() {
        for (err, expected) in exit_codes() {
            assert_eq!(
                err.outcome() == "unverified",
                expected == 2,
                "`{}` beside exit {expected}: {err}",
                err.outcome()
            );
        }
    }

    /// main prints `<outcome>: <detail>`; a detail that still carries its own
    /// `unverified: ` would put the token on the line twice, which is the
    /// defect this pairing replaced.
    #[test]
    fn the_printed_line_carries_one_outcome_token() {
        let line = {
            let err = CliError::unverified("unverified: no remotes");
            format!("{}: {}", err.outcome(), err.detail())
        };
        assert_eq!(line, "unverified: no remotes");
        assert_eq!(line.matches("unverified").count(), 1, "{line}");
    }

    #[test]
    fn from_boxed_keeps_unverified() {
        let boxed: Box<dyn std::error::Error> =
            Box::new(CliError::unverified("unverified: no remotes"));
        assert!(CliError::from_boxed(boxed).is_unverified());
    }

    #[test]
    fn from_boxed_does_not_treat_io_as_unverified() {
        let boxed: Box<dyn std::error::Error> = Box::new(std::io::Error::other("boom"));
        let err = CliError::from_boxed(boxed);
        assert!(!err.is_unverified());
        assert_eq!(err.to_string(), "boom");
    }

    #[test]
    fn github_unverified_maps_to_cli_unverified() {
        let err = CliError::from(super::github::Error::Unverified {
            detail: String::from("unverified: GitHub /repos/oakoss/oakum returned 502"),
        });
        assert!(err.is_unverified());
    }

    #[test]
    fn github_forbidden_is_an_error_naming_the_path() {
        let err = CliError::from(super::github::Error::Forbidden {
            path: String::from("/repos/oakoss/oakum/issues/4/comments"),
        });
        assert!(!err.is_unverified());
        assert_eq!(
            err.to_string(),
            "GitHub /repos/oakoss/oakum/issues/4/comments returned 403"
        );
    }

    #[test]
    fn tag_drift_and_uncovered_display_match_the_check_summaries() {
        assert_eq!(
            CliError::tag_drift(1).to_string(),
            "1 package(s) bumped without a tag"
        );
        assert_eq!(
            CliError::uncovered(2).to_string(),
            "2 package(s) changed with no covering intent"
        );
    }
}