Skip to main content

apply

Function apply 

Source
pub fn apply(
    root: &Path,
    plan: &Plan,
    pr_number: Option<u32>,
    bump: impl Fn(Version) -> Result<()>,
) -> Result<()>
Expand description

Apply plan to root: write ADR files + index rows, insert changelog rows, run bump for the final version, then delete the consumed pending files.

The whole thing is transactional. Every write is staged in memory first — the changelog and index edits are computed, ADR contents rendered, and any ADR-number collision detected — before a single byte hits disk, so a malformed table or a stale number fails with nothing written. During the commit phase the tree is mutated in a fixed order (ADR files, changelog, index, then the flaky bump); if any step fails, the tree is rolled back to its pre-apply state — the edited docs restored and the freshly written ADR files removed. Only once everything has succeeded are the consumed pending files deleted.

This is what makes a re-run safe: a failed run (a bump that errors, most likely) leaves the pending files intact and no partial changelog/ADR edits behind, so a retry recomputes the identical plan rather than duplicating rows or re-numbering ADRs past the files a previous attempt wrote. (bump’s own partial effects on the manifests it rewrites are outside this function’s reach; the CLI additionally refuses to --apply on a dirty worktree so that debris is surfaced too.)

bump is injected so tests exercise the whole flow on a fixture tree with a stub that just rewrites the fixture’s Cargo.toml. pr_number is the PR whose merge triggered this run, if it could be recovered from the triggering commit message (#1001) — None records a ledger row without one (e.g. a local stamp --apply run outside CI) rather than guessing. Only trusted for a single-increment plan: a multi-increment run (a prior run’s failed push left its pending file for this one to pick up) has no way to tell which increment the recovered number actually belongs to, so every row in that case gets a blank cell instead of one confidently wrong number applied to increments from different PRs.