Skip to main content

release_kit/
landing.rs

1//! The target-side landing model: file kinds, parameter rendering, and
2//! the routing block.
3//!
4//! Every landable file has a declared kind — `rendered` files release-kit
5//! owns and may rewrite, `seeded` files the target tunes, `state` files
6//! the release automation maintains — and a `rendered` file's bytes are a
7//! deterministic function of the payload plus the landing parameters, so
8//! a later command can compare what is on disk against what would be
9//! written. The kinds are declared here, beside the payload, never
10//! inferred at runtime; a test holds the table closed over every snippet.
11
12pub mod invariants;
13pub mod manifest;
14
15use camino::Utf8Path;
16use serde::{Deserialize, Serialize};
17
18pub use manifest::{Style, Workflow};
19
20use crate::diagnostic::{Diagnostic, Reason};
21use crate::error::RkError;
22use crate::{atomic, embedded};
23
24/// Who owns a landed file's bytes after landing.
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
26#[serde(rename_all = "lowercase")]
27pub enum Kind {
28    /// release-kit owns it: a newer payload re-renders it, and a target
29    /// edit is a conflict.
30    Rendered,
31    /// The target owns it: a starting point the project tunes, reported
32    /// and never rewritten.
33    Seeded,
34    /// The release automation owns it: never written after the first
35    /// landing, never compared.
36    State,
37}
38
39impl Kind {
40    /// The wire and report form.
41    #[must_use]
42    pub const fn as_str(self) -> &'static str {
43        match self {
44            Self::Rendered => "rendered",
45            Self::Seeded => "seeded",
46            Self::State => "state",
47        }
48    }
49}
50
51/// The declared classification: every landable destination and its kind.
52/// The workflow and pipeline files carry the release automation and the
53/// OIDC permission, so release-kit owns them; the tool configurations are
54/// per-project judgment; the two state files are rewritten by the release
55/// automation itself.
56const KINDS: [(&str, Kind); 16] = [
57    (".github/workflows/release-plz.yml", Kind::Rendered),
58    (".github/workflows/release-please.yml", Kind::Rendered),
59    (".github/workflows/release.yml", Kind::Rendered),
60    (".github/workflows/pr-title.yml", Kind::Rendered),
61    (".github/workflows/nix.yml", Kind::Rendered),
62    (".gitlab-ci.yml", Kind::Rendered),
63    (".gitlab/ci/mr-title.yml", Kind::Rendered),
64    ("release-plz.toml", Kind::Seeded),
65    ("dist-workspace.toml", Kind::Seeded),
66    ("release-please-config.json", Kind::Seeded),
67    ("cliff.toml", Kind::Seeded),
68    ("nix/package.nix", Kind::Seeded),
69    ("flake.nix", Kind::Seeded),
70    (".release-please-manifest.json", Kind::State),
71    ("VERSION", Kind::State),
72    ("flake.lock", Kind::State),
73];
74
75/// The destinations of the opt-in Nix capability, present in a projection
76/// only where the landing's `nix` parameter is on.
77///
78/// The parameter is recorded, so `status`, `upgrade`, and `adopt` can
79/// reconstruct whether these files are supposed to exist: an absent file
80/// under `nix = false` is not wanted, never drifted.
81pub const NIX_DESTINATIONS: [&str; 4] = [
82    "nix/package.nix",
83    "flake.nix",
84    "flake.lock",
85    ".github/workflows/nix.yml",
86];
87
88/// The subset a target with a flake of its own keeps out: the seed pair,
89/// and the workflow whose check would run against a flake release-kit did
90/// not author.
91///
92/// The seeded package expression is not in it — it lands either way, as
93/// the starting point the target integrates by hand.
94pub const NIX_WITHHOLDABLE: [&str; 3] = ["flake.nix", "flake.lock", ".github/workflows/nix.yml"];
95
96/// The declared kind of a destination, or `None` for a file the payload
97/// does not classify.
98#[must_use]
99pub fn kind_of(destination: &str) -> Option<Kind> {
100    if destination == AGENTS_DESTINATION || destination == HOOKS_DESTINATION {
101        return Some(Kind::Rendered);
102    }
103    KINDS
104        .iter()
105        .find(|(name, _)| *name == destination)
106        .map(|(_, kind)| *kind)
107}
108
109/// The mechanical substitution sites in `rendered` files.
110///
111/// Known values, substituted identically everywhere each appears. The
112/// owner is derived from the landing's `repo` parameter and the two scope
113/// forms from its `scopes` list, so the landed bytes stay a deterministic
114/// function of payload plus parameters.
115pub const OWNER_TOKEN: &[u8] = b"OWNER";
116
117/// The scope list, comma-joined: hook arguments and prose.
118pub const SCOPES_CSV_TOKEN: &[u8] = b"RK_SCOPES_CSV";
119
120/// The scope list, pipe-joined: the title checks' regular expression.
121pub const SCOPES_PIPE_TOKEN: &[u8] = b"RK_SCOPES_PIPE";
122
123/// The recorded release style: `trunk` arms the bot's request in the
124/// landed release workflow, `lines` leaves every request unarmed.
125pub const STYLE_TOKEN: &[u8] = b"RK_STYLE";
126
127/// Substitute the landing parameters into a `rendered` file's bytes.
128///
129/// The repository's owner — the project path's first segment — replaces
130/// every `OWNER` occurrence, the scope list replaces the two scope
131/// tokens, and the recorded style replaces the style token. An empty
132/// scope list — or an unresolved style — leaves its tokens standing,
133/// which only a preview renders under; an apply refuses before reaching
134/// here.
135#[must_use]
136pub fn render(baseline: &[u8], repo: &str, scopes: &[String], style: Option<Style>) -> Vec<u8> {
137    let owner = repo.split('/').next().unwrap_or(repo);
138    let mut out = substitute(baseline, OWNER_TOKEN, owner.as_bytes());
139    if let Some(style) = style {
140        out = substitute(&out, STYLE_TOKEN, style.as_str().as_bytes());
141    }
142    if !scopes.is_empty() {
143        out = substitute(&out, SCOPES_CSV_TOKEN, scopes.join(",").as_bytes());
144        // The pipe form drops into an extended regular expression, where a
145        // dot matches any character; among the characters `parse_scopes`
146        // admits, the dot is the only special one, so `api.v1` escapes to
147        // match itself alone.
148        let pipe: Vec<String> = scopes.iter().map(|s| s.replace('.', "\\.")).collect();
149        out = substitute(&out, SCOPES_PIPE_TOKEN, pipe.join("|").as_bytes());
150    }
151    out
152}
153
154/// Every `token` occurrence replaced with `value`.
155fn substitute(baseline: &[u8], token: &[u8], value: &[u8]) -> Vec<u8> {
156    let mut out = Vec::with_capacity(baseline.len());
157    let mut rest = baseline;
158    while let Some(at) = find(rest, token) {
159        out.extend_from_slice(&rest[..at]);
160        out.extend_from_slice(value);
161        rest = &rest[at + token.len()..];
162    }
163    out.extend_from_slice(rest);
164    out
165}
166
167/// The `--scopes` argument parsed into the recorded list.
168///
169/// Comma-separated, each scope non-empty and made of letters, digits, and
170/// `_ . / -` — a set safe for the title checks' regular expression once
171/// the renderer escapes the dot, the one special character among them.
172///
173/// # Errors
174///
175/// Returns [`RkError::Usage`] naming the offending scope, or the empty
176/// list.
177pub fn parse_scopes(raw: &str) -> Result<Vec<String>, RkError> {
178    let scopes: Vec<String> = raw
179        .split(',')
180        .map(str::trim)
181        .filter(|scope| !scope.is_empty())
182        .map(str::to_owned)
183        .collect();
184    if scopes.is_empty() {
185        return Err(RkError::Usage(
186            "--scopes names no scope; pass a comma-separated list, e.g. --scopes api,cli".into(),
187        ));
188    }
189    for scope in &scopes {
190        let clean = scope
191            .chars()
192            .all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | '/' | '-'));
193        if !clean {
194            return Err(RkError::Usage(format!(
195                "the scope '{scope}' carries a character outside letters, digits, and _ . / -"
196            )));
197        }
198    }
199    Ok(scopes)
200}
201
202/// First occurrence of `needle` in `haystack`.
203fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
204    haystack
205        .windows(needle.len())
206        .position(|window| window == needle)
207}
208
209/// The destination the routing block splices into.
210pub const AGENTS_DESTINATION: &str = "AGENTS.md";
211
212/// The block's opening marker.
213pub const BLOCK_BEGIN: &str = "<!-- BEGIN release-kit -->";
214
215/// The block's closing marker.
216pub const BLOCK_END: &str = "<!-- END release-kit -->";
217
218/// The destination the hook block splices into.
219pub const HOOKS_DESTINATION: &str = ".pre-commit-config.yaml";
220
221/// The hook block's opening marker, a YAML comment at column zero.
222pub const HOOKS_BEGIN: &str = "# BEGIN release-kit";
223
224/// The hook block's closing marker.
225pub const HOOKS_END: &str = "# END release-kit";
226
227/// The top-level key the fresh hook file carries and the skills verify on
228/// an existing one: the commit-msg and pre-push hooks run only where their
229/// hook types are installed.
230pub const HOOK_TYPES_LINE: &str = "default_install_hook_types: [pre-commit, commit-msg, pre-push]";
231
232/// The authored routing-block template, `blocks/agents-block.md.in`.
233static AGENTS_BLOCK: &str = include_str!("../blocks/agents-block.md.in");
234
235/// The routing block's mode line, worktree form.
236static AGENTS_LINE_WORKTREE: &str = include_str!("../blocks/agents-line-worktree.md.in");
237
238/// The routing block's mode line, branches form.
239static AGENTS_LINE_BRANCHES: &str = include_str!("../blocks/agents-line-branches.md.in");
240
241/// The authored hook-block template, `blocks/pre-commit-block.yaml.in`.
242static PRE_COMMIT_BLOCK: &str = include_str!("../blocks/pre-commit-block.yaml.in");
243
244/// The worktree mode's guard entry, `blocks/pre-commit-worktree-guard.yaml.in`.
245static PRE_COMMIT_WORKTREE_GUARD: &str =
246    include_str!("../blocks/pre-commit-worktree-guard.yaml.in");
247
248/// An authored block without the one final newline the repository's
249/// hooks enforce on every file under `blocks/`; a test in
250/// `src/embedded.rs` holds each file to exactly one.
251fn authored(text: &str) -> &str {
252    text.strip_suffix('\n').unwrap_or(text)
253}
254
255/// The one branch grammar.
256///
257/// The extended regular expression the landed
258/// `rk-branch-name` hook tests, and the same anchored language
259/// `rk worktree add` validates before creating anything. One owner by
260/// token — `concat!` cannot interpolate a const, so [`hooks_block`]
261/// substitutes it for the template's `RK_BRANCH_GRAMMAR` token.
262pub const BRANCH_GRAMMAR: &str = r"^((build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)/[A-Za-z0-9._/-]+|([0-9]+|[A-Z][A-Z0-9]+-[0-9]+)-[A-Za-z0-9._-]+|release[-/].+)$";
263
264/// The routing block for one workflow mode: the whole of target-side
265/// governance, authored as `blocks/agents-block.md.in` and never grown
266/// into a method chapter.
267///
268/// Markers included, without a
269/// trailing newline and with its scope token unrendered: the template
270/// with the mode's one orientation line substituted, everything else —
271/// the agent-boundary line included — byte-identical across modes.
272#[must_use]
273pub fn routing_block(workflow: Workflow) -> String {
274    let line = match workflow {
275        Workflow::Worktree => authored(AGENTS_LINE_WORKTREE),
276        Workflow::Branches => authored(AGENTS_LINE_BRANCHES),
277    };
278    authored(AGENTS_BLOCK).replacen("RK_WORKFLOW_LINE", line, 1)
279}
280
281/// The hook block for one workflow mode, authored as
282/// `blocks/pre-commit-block.yaml.in` with the worktree mode's guard entry
283/// beside it in `blocks/pre-commit-worktree-guard.yaml.in`.
284///
285/// Markers included, without a
286/// trailing newline and with its scope token unrendered. What is landed
287/// is what runs: the worktree mode's block carries the location guard and
288/// names the sweep-skip pair, and the branches mode's block carries no
289/// guard entry at all — never an entry that reads local state to decide
290/// whether to enforce. The one branch grammar substitutes here from
291/// [`BRANCH_GRAMMAR`].
292#[must_use]
293pub fn hooks_block(workflow: Workflow) -> String {
294    let (guard, skip) = match workflow {
295        Workflow::Worktree => (
296            format!("{}\n", authored(PRE_COMMIT_WORKTREE_GUARD)),
297            "no-commit-to-branch,rk-worktree-location",
298        ),
299        Workflow::Branches => (String::new(), "no-commit-to-branch"),
300    };
301    authored(PRE_COMMIT_BLOCK)
302        .replacen("RK_BRANCH_GRAMMAR", BRANCH_GRAMMAR, 1)
303        .replacen("RK_SWEEP_SKIP", skip, 1)
304        .replacen("RK_WORKTREE_GUARD", &guard, 1)
305}
306
307/// The markers of a block destination, or `None` for a whole-file one.
308#[must_use]
309pub fn block_markers(destination: &str) -> Option<(&'static str, &'static str)> {
310    match destination {
311        AGENTS_DESTINATION => Some((BLOCK_BEGIN, BLOCK_END)),
312        HOOKS_DESTINATION => Some((HOOKS_BEGIN, HOOKS_END)),
313        _ => None,
314    }
315}
316
317/// The marked block inside a document, markers included, or `None` where
318/// the text carries no complete block.
319#[must_use]
320pub fn extract_block<'a>(text: &'a str, begin: &str, end: &str) -> Option<&'a str> {
321    let start = text.find(begin)?;
322    let stop = text[start..].find(end)? + start + end.len();
323    Some(&text[start..stop])
324}
325
326/// The whole `AGENTS.md` content after splicing the rendered block.
327///
328/// A fresh file where none exists, the block replaced in place where one
329/// is marked, appended after the target's own content otherwise —
330/// release-kit owns the lines inside the markers, not the document.
331#[must_use]
332pub fn splice_agents_block(existing: Option<&str>, block: &str) -> String {
333    existing.map_or_else(
334        || format!("{block}\n"),
335        |text| {
336            extract_block(text, BLOCK_BEGIN, BLOCK_END).map_or_else(
337                || format!("{}\n\n{block}\n", text.trim_end()),
338                |found| text.replacen(found, block, 1),
339            )
340        },
341    )
342}
343
344/// The whole `.pre-commit-config.yaml` content after splicing the
345/// rendered hook block.
346///
347/// A fresh file carries the hook-types key, the `repos:` key, and the
348/// block; a marked file takes the block in place; an unmarked file takes
349/// it directly under its `repos:` line, above the target's own hooks. An
350/// unmarked file with no `repos:` line is refused by name — the block's
351/// entries are list items and have nowhere honest to go.
352///
353/// # Errors
354///
355/// The reason the block has no place, for the caller's refusal to carry.
356pub fn splice_hooks_block(existing: Option<&str>, block: &str) -> Result<String, String> {
357    let Some(text) = existing else {
358        return Ok(format!("{HOOK_TYPES_LINE}\n\nrepos:\n{block}\n"));
359    };
360    if let Some(defect) = hooks_marker_defect(text) {
361        return Err(defect);
362    }
363    if let Some(found) = extract_block(text, HOOKS_BEGIN, HOOKS_END) {
364        return Ok(text.replacen(found, block, 1));
365    }
366    let mut out = String::with_capacity(text.len() + block.len() + 1);
367    let mut placed = false;
368    for line in text.split_inclusive('\n') {
369        out.push_str(line);
370        if !placed && line.trim_end() == "repos:" {
371            if !out.ends_with('\n') {
372                out.push('\n');
373            }
374            out.push_str(block);
375            out.push('\n');
376            placed = true;
377        }
378    }
379    if placed {
380        Ok(out)
381    } else {
382        Err(format!(
383            "{HOOKS_DESTINATION} exists with no repos: line, so the hook block has nowhere to land"
384        ))
385    }
386}
387
388/// The one definition of an ill-formed hook file, shared by the splice
389/// and every reader that judges one.
390///
391/// The hooks between the markers execute, so ownership must be
392/// unambiguous: exactly one begin marker paired with exactly one end
393/// marker after it, or none of either. A second begin is a second block
394/// pre-commit would still run, and a marker without its pair — or an end
395/// before its begin — is a block whose extent nothing can state.
396#[must_use]
397pub fn hooks_marker_defect(text: &str) -> Option<String> {
398    let begins = text.matches(HOOKS_BEGIN).count();
399    let ends = text.matches(HOOKS_END).count();
400    if begins > 1 || ends > 1 {
401        return Some(format!(
402            "{HOOKS_DESTINATION} carries more than one release-kit marker pair; release-kit owns exactly one block"
403        ));
404    }
405    match (text.find(HOOKS_BEGIN), text.find(HOOKS_END)) {
406        (Some(begin), Some(end)) if end > begin => None,
407        (None, None) => None,
408        _ => Some(format!(
409            "{HOOKS_DESTINATION} carries an unmatched or misordered release-kit marker, so the block's extent is ambiguous"
410        )),
411    }
412}
413
414/// How a projected artifact occupies its destination.
415#[derive(Debug, Clone, Copy, PartialEq, Eq)]
416pub enum Placement {
417    /// The artifact is the whole file.
418    Whole,
419    /// The artifact is the marked block inside the target's `AGENTS.md`.
420    Block,
421}
422
423/// One artifact of the payload projection: what would land at one
424/// destination, with the payload bytes it was rendered from.
425#[derive(Debug)]
426pub struct Entry {
427    /// The destination, relative to the target root.
428    pub destination: String,
429    /// The declared kind.
430    pub kind: Kind,
431    /// Whole file, or the marked block.
432    pub placement: Placement,
433    /// The payload bytes before substitution — what `baseline_sha256`
434    /// digests.
435    pub baseline: Vec<u8>,
436    /// The bytes a landing writes: substituted for `rendered` files,
437    /// identical to the baseline otherwise.
438    pub rendered: Vec<u8>,
439}
440
441/// The landable files of one `(technology, forge)` pair, as
442/// `(destination, payload bytes)`.
443///
444/// # Errors
445///
446/// Returns [`RkError::Usage`] naming the known bindings for an unknown
447/// technology, and the supported pairs for a pair with no files.
448pub fn pair_files(tech: &str, forge: &str) -> Result<Vec<(String, &'static [u8])>, RkError> {
449    // The shared zone is not a technology: `_shared/<forge>` composes into
450    // every pair and never names one.
451    if tech.starts_with('_') || embedded::SNIPPETS.get_dir(tech).is_none() {
452        let known: Vec<String> = embedded::SNIPPETS
453            .dirs()
454            .map(|dir| dir.path().to_string_lossy().into_owned())
455            .filter(|name| !name.starts_with('_'))
456            .collect();
457        return Err(RkError::Usage(format!(
458            "unknown tech '{tech}'; the bindings are: {}",
459            known.join(", ")
460        )));
461    }
462    let pair = format!("{tech}/{forge}");
463    let pair_dir = embedded::SNIPPETS.get_dir(&pair).ok_or_else(|| {
464        let known: Vec<String> = embedded::SNIPPETS
465            .dirs()
466            .filter(|dir| !dir.path().to_string_lossy().starts_with('_'))
467            .flat_map(include_dir::Dir::dirs)
468            .map(|dir| dir.path().to_string_lossy().replace('/', ", "))
469            .collect();
470        RkError::Usage(format!(
471            "the pair ({tech}, {forge}) has no landable files; the supported pairs are: {}",
472            known.join("; ")
473        ))
474    })?;
475    // Payload paths carry their zone prefix; destinations do not. The
476    // shared zone lands first, and a destination both zones ship is a
477    // payload defect refused by name, never one zone silently winning.
478    let mut files: Vec<(String, &'static [u8])> = Vec::new();
479    let shared = format!("_shared/{forge}");
480    if let Some(shared_dir) = embedded::SNIPPETS.get_dir(&shared) {
481        for (path, contents) in embedded::walk(shared_dir) {
482            let rel = path
483                .strip_prefix(&format!("{shared}/"))
484                .map_or(path.as_str(), |rel| rel)
485                .to_owned();
486            files.push((rel, contents));
487        }
488    }
489    for (path, contents) in embedded::walk(pair_dir) {
490        let rel = path
491            .strip_prefix(&format!("{pair}/"))
492            .map_or(path.as_str(), |rel| rel)
493            .to_owned();
494        if files.iter().any(|(existing, _)| *existing == rel) {
495            return Err(anyhow::anyhow!(
496                "the shared zone and the pair ({tech}, {forge}) both ship {rel}; the payload is defective"
497            )
498            .into());
499        }
500        files.push((rel, contents));
501    }
502    Ok(files)
503}
504
505/// The whole payload projection for one pair.
506///
507/// Under the `repo`, `scopes`, `workflow`,
508/// `style`, and `nix` parameters: every snippet with its kind and
509/// rendered bytes, plus the routing block and the hook block — each a
510/// pure function of the recorded mode — sorted by destination. The Nix
511/// destinations project only where `nix` is on; a pair that ships none of
512/// them honestly projects the smaller product.
513///
514/// # Errors
515///
516/// Returns the [`pair_files`] errors, and [`RkError::Other`] for a
517/// snippet destination the kind table does not classify, which is a
518/// defect in this binary.
519pub fn projection(
520    tech: &str,
521    forge: &str,
522    repo: &str,
523    scopes: &[String],
524    workflow: Workflow,
525    style: Option<Style>,
526    nix: bool,
527) -> Result<Vec<Entry>, RkError> {
528    let mut entries = Vec::new();
529    for (destination, baseline) in pair_files(tech, forge)? {
530        if !nix && NIX_DESTINATIONS.contains(&destination.as_str()) {
531            continue;
532        }
533        let kind = kind_of(&destination).ok_or_else(|| {
534            anyhow::anyhow!("the payload does not classify {destination}; the kind table is stale")
535        })?;
536        let rendered = match kind {
537            Kind::Rendered => render(baseline, repo, scopes, style),
538            Kind::Seeded | Kind::State => baseline.to_vec(),
539        };
540        entries.push(Entry {
541            destination,
542            kind,
543            placement: Placement::Whole,
544            baseline: baseline.to_vec(),
545            rendered,
546        });
547    }
548    for (destination, template) in [
549        (AGENTS_DESTINATION, routing_block(workflow)),
550        (HOOKS_DESTINATION, hooks_block(workflow)),
551    ] {
552        entries.push(Entry {
553            destination: destination.to_owned(),
554            kind: Kind::Rendered,
555            placement: Placement::Block,
556            baseline: template.as_bytes().to_vec(),
557            rendered: render(template.as_bytes(), repo, scopes, style),
558        });
559    }
560    entries.sort_by(|a, b| a.destination.cmp(&b.destination));
561    Ok(entries)
562}
563
564/// Why the whole Nix capability stays out of a landing, or `None` where
565/// the target's crate shape supports the seed.
566///
567/// The gate holds every structural prerequisite the seed relies on, not
568/// only evaluation: the package expression reads `Cargo.toml` through
569/// `importTOML` and throws without `../Cargo.lock`, and the seed flake's
570/// smoke check runs the crate's binary, which only an implicit
571/// `src/main.rs` or an explicit `[[bin]]` entry produces. A shape
572/// missing any of these would land files that fail on their first
573/// evaluation or first check, so the landing reports the smaller product
574/// with the missing piece named instead.
575#[must_use]
576pub fn nix_unsupported_shape(target: &Utf8Path) -> Option<String> {
577    let Ok(text) = std::fs::read_to_string(target.join("Cargo.toml")) else {
578        return Some(
579            "the target has no readable Cargo.toml, which the seeded package expression reads; no Nix file lands".to_owned(),
580        );
581    };
582    let Ok(table) = text.parse::<toml::Table>() else {
583        return Some(
584            "the target's Cargo.toml does not parse, and the seeded package expression reads it; no Nix file lands".to_owned(),
585        );
586    };
587    if !table.contains_key("package") {
588        return Some(
589            "the target's Cargo.toml has no [package] table; the seed supports a single crate, so no Nix file lands".to_owned(),
590        );
591    }
592    if !target.join("Cargo.lock").is_file() {
593        return Some(
594            "the target has no Cargo.lock, which the seeded package expression builds from; commit one, then opt in".to_owned(),
595        );
596    }
597    let implicit_bin = target.join("src/main.rs").is_file()
598        && table
599            .get("package")
600            .and_then(toml::Value::as_table)
601            .and_then(|package| package.get("autobins"))
602            .and_then(toml::Value::as_bool)
603            != Some(false);
604    let explicit_bins = table.get("bin").and_then(toml::Value::as_array);
605    if explicit_bins.is_none() && !implicit_bin {
606        return Some(
607            "the target declares no binary — no effective src/main.rs and no [[bin]] entry — and the seed flake's smoke check runs one; no Nix file lands".to_owned(),
608        );
609    }
610    // The seed's mainProgram is the first [[bin]] entry; one whose
611    // required-features a default build does not enable produces no
612    // executable, so the smoke check would fail on a green landing. A
613    // requirement the default feature set covers builds normally and
614    // passes.
615    if let Some(bins) = explicit_bins {
616        let required = bins
617            .first()
618            .and_then(toml::Value::as_table)
619            .and_then(|bin| bin.get("required-features"))
620            .and_then(toml::Value::as_array);
621        if let Some(required) = required {
622            let enabled = default_features(&table);
623            let missing = required
624                .iter()
625                .filter_map(toml::Value::as_str)
626                .any(|feature| !enabled.contains(feature));
627            if missing {
628                return Some(
629                    "the target's first [[bin]] entry requires features a default build does not enable; no Nix file lands".to_owned(),
630                );
631            }
632        }
633    }
634    None
635}
636
637/// Whether any feature's list carries a `dep:name` edge, which is what
638/// suppresses the optional dependency's implicit same-named feature.
639fn dep_edge_suppresses(features: &toml::Table, name: &str) -> bool {
640    let edge = format!("dep:{name}");
641    features.values().any(|list| {
642        list.as_array().is_some_and(|entries| {
643            entries
644                .iter()
645                .filter_map(toml::Value::as_str)
646                .any(|entry| entry == edge)
647        })
648    })
649}
650
651/// Whether `name` is declared an optional dependency, in any of the
652/// dependency tables a binary's build reads.
653fn is_optional_dependency(table: &toml::Table, name: &str) -> bool {
654    ["dependencies", "build-dependencies"]
655        .iter()
656        .any(|section| {
657            table
658                .get(*section)
659                .and_then(toml::Value::as_table)
660                .and_then(|dependencies| dependencies.get(name))
661                .and_then(toml::Value::as_table)
662                .and_then(|dependency| dependency.get("optional"))
663                .and_then(toml::Value::as_bool)
664                == Some(true)
665        })
666}
667
668/// The features a default build enables: the `default` feature resolved
669/// through the `[features]` table's own enables — an approximation of
670/// cargo's default resolution for the documented supported shapes, erring
671/// toward withholding where the semantics run deeper. Dependency forms —
672/// `dep:name`, weak `name?/feature` — are not feature names here and are
673/// skipped; the closure is bounded by the table's size.
674fn default_features(table: &toml::Table) -> std::collections::BTreeSet<String> {
675    let Some(features) = table.get("features").and_then(toml::Value::as_table) else {
676        return std::collections::BTreeSet::new();
677    };
678    let mut enabled = std::collections::BTreeSet::new();
679    let mut queue = vec!["default".to_owned()];
680    while let Some(name) = queue.pop() {
681        if !enabled.insert(name.clone()) {
682            continue;
683        }
684        if let Some(implies) = features.get(&name).and_then(toml::Value::as_array) {
685            for implied in implies.iter().filter_map(toml::Value::as_str) {
686                if implied.starts_with("dep:") || implied.contains("?/") {
687                    // `dep:name` enables the dependency without a feature
688                    // of this crate; a weak `name?/feature` edge enables
689                    // nothing by itself.
690                    continue;
691                }
692                if let Some((package, _)) = implied.split_once('/') {
693                    // A strong `name/feature` edge activates this crate's
694                    // same-named feature only for an optional dependency,
695                    // and only where that feature exists: declared
696                    // explicitly, or implicit and not suppressed by a
697                    // `dep:` edge anywhere in the table. A non-optional
698                    // dependency's edge enables a feature of the
699                    // dependency and nothing of this crate.
700                    let feature_exists =
701                        features.contains_key(package) || !dep_edge_suppresses(features, package);
702                    if is_optional_dependency(table, package) && feature_exists {
703                        queue.push(package.to_owned());
704                    }
705                } else {
706                    queue.push(implied.to_owned());
707                }
708            }
709        }
710    }
711    enabled
712}
713
714/// Why the flake half of the Nix capability stays out of this landing, or
715/// `None` where the pair lands whole.
716///
717/// The pair is all-or-nothing: a target that already carries a
718/// `flake.nix` or `flake.lock` of its own keeps its pair — a seed lock
719/// beside a foreign flake describes the wrong input graph — and the
720/// rendered workflow is withheld with it, because a green check that
721/// never builds the landed expression proves nothing. A pair the record
722/// names is release-kit's own landing and is never withheld.
723///
724/// # Errors
725///
726/// Any read failure other than the files being absent.
727pub fn nix_withheld(
728    target: &Utf8Path,
729    recorded: Option<&manifest::Manifest>,
730) -> std::io::Result<Option<String>> {
731    if recorded.is_some_and(|record| record.file("flake.nix").is_some()) {
732        return Ok(None);
733    }
734    let mut present = Vec::new();
735    for name in ["flake.nix", "flake.lock"] {
736        match std::fs::symlink_metadata(target.join(name).as_std_path()) {
737            Ok(_) => present.push(name),
738            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
739            Err(e) => return Err(e),
740        }
741    }
742    if present.is_empty() {
743        return Ok(None);
744    }
745    Ok(Some(format!(
746        "the target already carries {}; its flake pair stays its own, and the nix workflow is withheld with it",
747        present.join(" and ")
748    )))
749}
750
751/// One destination a landing withholds, with why.
752#[derive(Debug, Serialize)]
753pub struct Withheld {
754    /// The destination that stays out.
755    pub path: String,
756    /// The reason, stated once per destination so a machine reader needs
757    /// no join.
758    pub reason: String,
759}
760
761/// Drop the Nix destinations this target cannot take from a projection,
762/// naming each with its reason.
763///
764/// The one judgment every landing verb shares, so a preview, an apply, an
765/// upgrade, and an adoption all withhold identically: an unsupported
766/// crate shape withholds the whole capability, and a flake pair of the
767/// target's own withholds the pair and the workflow while the seeded
768/// package expression still lands.
769///
770/// # Errors
771///
772/// Any read failure from the pair check other than absence.
773pub fn withhold_nix(
774    target: &Utf8Path,
775    nix: bool,
776    recorded: Option<&manifest::Manifest>,
777    entries: &mut Vec<Entry>,
778) -> Result<Vec<Withheld>, RkError> {
779    if !nix {
780        return Ok(Vec::new());
781    }
782    let (set, reason): (&[&str], String) = if let Some(reason) = nix_unsupported_shape(target) {
783        (&NIX_DESTINATIONS[..], reason)
784    } else if let Some(reason) = nix_withheld(target, recorded)? {
785        (&NIX_WITHHOLDABLE[..], reason)
786    } else {
787        return Ok(Vec::new());
788    };
789    let mut withheld = Vec::new();
790    entries.retain(|entry| {
791        if set.contains(&entry.destination.as_str()) {
792            withheld.push(Withheld {
793                path: entry.destination.clone(),
794                reason: reason.clone(),
795            });
796            false
797        } else {
798            true
799        }
800    });
801    Ok(withheld)
802}
803
804/// The bytes an entry's destination currently holds: the whole file, or
805/// the marked block extracted from the target's `AGENTS.md`. `None` means
806/// the file — or the block — is absent.
807///
808/// # Errors
809///
810/// Any read failure other than the file being absent.
811pub fn read_destination(target: &Utf8Path, entry: &Entry) -> std::io::Result<Option<Vec<u8>>> {
812    read_recorded(target, &entry.destination)
813}
814
815/// The bytes a recorded destination currently holds, by the placement
816/// its name implies.
817///
818/// The marked block for `AGENTS.md` and `.pre-commit-config.yaml`, the
819/// whole file otherwise. `None` means the file — or the block — is
820/// absent.
821///
822/// # Errors
823///
824/// Any read failure other than the file being absent.
825pub fn read_recorded(target: &Utf8Path, destination: &str) -> std::io::Result<Option<Vec<u8>>> {
826    let path = target.join(destination);
827    let bytes = match std::fs::read(&path) {
828        Ok(bytes) => bytes,
829        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
830        Err(e) => return Err(e),
831    };
832    if let Some((begin, end)) = block_markers(destination) {
833        let text = String::from_utf8_lossy(&bytes);
834        Ok(extract_block(&text, begin, end).map(|block| block.as_bytes().to_vec()))
835    } else {
836        Ok(Some(bytes))
837    }
838}
839
840/// What one detection pass resolved for a target-side verb, with the
841/// override flags applied.
842#[derive(Debug)]
843pub struct Resolved {
844    /// The forge whose payload applies.
845    pub forge: String,
846    /// The project path, where a flag or the remote names one.
847    pub repo: Option<String>,
848}
849
850/// Resolve forge and repository in one pass: the flags override, the
851/// `origin` remote answers otherwise.
852///
853/// An unrecognized host refuses rather than defaulting — landing one
854/// forge's files into the other forge's project is a half-configured
855/// repository that looks done.
856///
857/// # Errors
858///
859/// Returns [`RkError::Usage`] for an unknown `--forge` value, and a
860/// refusal naming the override when no forge resolves.
861pub fn resolve(
862    target: &Utf8Path,
863    forge_flag: Option<&str>,
864    repo_flag: Option<&str>,
865) -> Result<Resolved, RkError> {
866    let forge_flag = forge_flag
867        .map(|name| {
868            crate::detect::Forge::parse(name).ok_or_else(|| {
869                RkError::Usage(format!(
870                    "unknown forge '{name}'; the forges are: github, gitlab"
871                ))
872            })
873        })
874        .transpose()?;
875    let detected = crate::detect::detect(target.as_std_path());
876    let forge = forge_flag
877        .or(detected.forge)
878        .map(|forge| forge.as_str().to_owned())
879        .ok_or_else(|| {
880            let message = detected.host.map_or_else(
881                || "no forge detected: the target has no origin remote".to_owned(),
882                |host| format!("no forge detected: the host {host} is not recognized"),
883            );
884            RkError::refusal(
885                Diagnostic::new(Reason::ForgeUndetected, message)
886                    .expected("a github.com or gitlab remote, or --forge")
887                    .action("pass --forge <github|gitlab>"),
888            )
889        })?;
890    Ok(Resolved {
891        forge,
892        repo: repo_flag.map(str::to_owned).or(detected.repo),
893    })
894}
895
896/// The refusal a verb answers when it needs the `repo` parameter and
897/// neither a flag nor the remote supplies one.
898#[must_use]
899pub fn repo_unresolved() -> RkError {
900    RkError::missing(
901        Diagnostic::new(
902            Reason::ForgeUndetected,
903            "no repository detected: the target has no origin remote",
904        )
905        .expected("an origin remote naming the project")
906        .action("pass --repo <path>"),
907    )
908}
909
910/// Land one entry: the whole file through the temp-plus-rename writer, or
911/// the block spliced into its document and the whole document rewritten
912/// the same way.
913///
914/// # Errors
915///
916/// Any write failure; the destination then holds what it held. An
917/// unspliceable hook file surfaces as an error here only as a backstop —
918/// [`hooks_splice_refusal`] is the check a verb runs before any write.
919pub fn write_destination(target: &Utf8Path, entry: &Entry) -> std::io::Result<()> {
920    let path = target.join(&entry.destination);
921    match entry.placement {
922        Placement::Whole => atomic::write(path.as_std_path(), &entry.rendered),
923        Placement::Block => {
924            let existing = match std::fs::read(&path) {
925                Ok(bytes) => Some(String::from_utf8_lossy(&bytes).into_owned()),
926                Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
927                Err(e) => return Err(e),
928            };
929            let block = String::from_utf8_lossy(&entry.rendered).into_owned();
930            let spliced = if entry.destination == HOOKS_DESTINATION {
931                splice_hooks_block(existing.as_deref(), &block).map_err(std::io::Error::other)?
932            } else {
933                splice_agents_block(existing.as_deref(), &block)
934            };
935            atomic::write(path.as_std_path(), spliced.as_bytes())
936        }
937    }
938}
939
940/// The hook file's defect, read from the target: `None` for a missing
941/// file or one the block can land in.
942///
943/// The one judgment every verb shares, covering every splice refusal —
944/// ill-formed markers, and an unmarked file offering the block no
945/// `repos:` line. Status reports it as rendered drift, upgrade collects
946/// it as a conflict in preview and apply alike so no landing dies
947/// half-written, and adopt lists it with its mismatches.
948///
949/// # Errors
950///
951/// Any read failure other than the file being absent.
952pub fn hooks_file_defect(target: &Utf8Path) -> std::io::Result<Option<String>> {
953    let path = target.join(HOOKS_DESTINATION);
954    match std::fs::read(&path) {
955        Ok(bytes) => {
956            let text = String::from_utf8_lossy(&bytes);
957            Ok(splice_hooks_block(Some(&text), authored(PRE_COMMIT_BLOCK)).err())
958        }
959        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
960        Err(e) => Err(e),
961    }
962}
963
964/// The refusal a landing verb answers before writing anything, where
965/// the target's hook file offers the block no place.
966///
967/// Checked ahead of every write so the all-or-nothing property holds and
968/// no landing dies half-written into `.pre-commit-config.yaml`.
969///
970/// # Errors
971///
972/// [`RkError::Refusal`] naming the file, and any read failure.
973pub fn hooks_splice_refusal(target: &Utf8Path) -> Result<(), RkError> {
974    hooks_file_defect(target)?.map_or(Ok(()), |reason| {
975        Err(RkError::refusal(
976            Diagnostic::new(
977                Reason::StateDrift,
978                format!("{reason}, and nothing was written"),
979            )
980            .expected("a .pre-commit-config.yaml the block can land in, or none")
981            .action(format!(
982                "resolve it in {}, then re-run",
983                target.join(HOOKS_DESTINATION)
984            ))
985            .target_state("unchanged"),
986        ))
987    })
988}
989
990#[cfg(test)]
991mod tests {
992    #![allow(clippy::expect_used)]
993
994    use super::{
995        AGENTS_DESTINATION, BLOCK_BEGIN, BLOCK_END, BRANCH_GRAMMAR, HOOK_TYPES_LINE, HOOKS_BEGIN,
996        HOOKS_DESTINATION, HOOKS_END, Kind, Style, Workflow, extract_block, hooks_block, kind_of,
997        pair_files, parse_scopes, projection, render, routing_block, splice_agents_block,
998        splice_hooks_block,
999    };
1000    use crate::embedded;
1001
1002    fn scopes(list: &[&str]) -> Vec<String> {
1003        list.iter().map(|s| (*s).to_owned()).collect()
1004    }
1005
1006    /// Every snippet destination has a declared kind: a new landable file
1007    /// without a classification fails here, not at a landing. The shared
1008    /// zone's files are enumerated the same way.
1009    #[test]
1010    fn the_kind_table_closes_over_every_snippet() {
1011        for tech_dir in embedded::SNIPPETS.dirs() {
1012            for pair_dir in tech_dir.dirs() {
1013                let prefix = format!("{}/", pair_dir.path().to_string_lossy());
1014                for (path, _) in embedded::walk(pair_dir) {
1015                    let destination = path.strip_prefix(&prefix).unwrap_or(&path);
1016                    assert!(
1017                        kind_of(destination).is_some(),
1018                        "{destination}: no declared kind"
1019                    );
1020                }
1021            }
1022        }
1023        assert_eq!(kind_of(AGENTS_DESTINATION), Some(Kind::Rendered));
1024        assert_eq!(kind_of(HOOKS_DESTINATION), Some(Kind::Rendered));
1025        assert_eq!(kind_of("something-else.txt"), None);
1026    }
1027
1028    /// Substitution is total and derives from the repo parameter's first
1029    /// segment, so a nested GitLab project path still yields its root
1030    /// namespace; the scope list renders in both joined forms.
1031    #[test]
1032    fn rendering_substitutes_every_owner_occurrence() {
1033        let baseline = b"if: repository_owner == 'OWNER'\n# OWNER again: OWNER\n";
1034        let rendered = render(baseline, "acme/sub/widget", &[], None);
1035        let text = String::from_utf8(rendered).expect("rendered bytes stay text");
1036        assert_eq!(text, "if: repository_owner == 'acme'\n# acme again: acme\n");
1037
1038        let baseline = b"scopes 'RK_SCOPES_CSV' match (RK_SCOPES_PIPE)\n";
1039        let rendered = render(baseline, "acme/widget", &scopes(&["api", "cli"]), None);
1040        let text = String::from_utf8(rendered).expect("rendered bytes stay text");
1041        assert_eq!(text, "scopes 'api,cli' match (api|cli)\n");
1042
1043        // A dot is the one admitted character that is special in the
1044        // regular expression: it escapes, so `api.v1` matches only itself.
1045        let rendered = render(baseline, "acme/widget", &scopes(&["api.v1"]), None);
1046        let text = String::from_utf8(rendered).expect("rendered bytes stay text");
1047        assert_eq!(text, "scopes 'api.v1' match (api\\.v1)\n");
1048    }
1049
1050    /// The scope argument parses to the recorded list, refusing the empty
1051    /// list and any scope that would not drop into the title regex.
1052    #[test]
1053    fn scope_parsing_refuses_the_unusable() {
1054        assert_eq!(
1055            parse_scopes("api, cli,guides/release").expect("a clean list parses"),
1056            scopes(&["api", "cli", "guides/release"])
1057        );
1058        assert!(parse_scopes("").is_err());
1059        assert!(parse_scopes(" , ").is_err());
1060        assert!(parse_scopes("api|cli").is_err());
1061        assert!(parse_scopes("a b").is_err());
1062    }
1063
1064    /// The shared zone composes into every pair, lands first, and is
1065    /// absent from the technology listing an unknown tech names.
1066    #[test]
1067    fn the_shared_zone_composes_into_the_pair() {
1068        let files = pair_files("rust", "github").expect("the pair lists");
1069        assert!(
1070            files
1071                .iter()
1072                .any(|(dest, _)| dest == ".github/workflows/pr-title.yml"),
1073            "the shared title check lands with the pair"
1074        );
1075        let files = pair_files("rust", "gitlab").expect("the pair lists");
1076        assert!(
1077            files
1078                .iter()
1079                .any(|(dest, _)| dest == ".gitlab/ci/mr-title.yml"),
1080            "the shared title job lands with the pair"
1081        );
1082        let err = pair_files("_shared", "github").expect_err("the shared zone is no tech");
1083        let listing = err.to_string();
1084        let bindings = listing
1085            .split("the bindings are:")
1086            .nth(1)
1087            .expect("the refusal lists the bindings");
1088        assert!(!bindings.contains("_shared"), "{listing}");
1089    }
1090
1091    /// A rendered projection carries no unsubstituted token and no
1092    /// mechanical sentinel; the one judgment sentinel stays in its seeded
1093    /// file.
1094    #[test]
1095    fn a_projection_renders_owned_files_and_keeps_seeded_judgment() {
1096        let entries = projection(
1097            "rust",
1098            "github",
1099            "acme/widget",
1100            &scopes(&["api", "cli"]),
1101            Workflow::Branches,
1102            Some(Style::Trunk),
1103            false,
1104        )
1105        .expect("the pair projects");
1106        let workflow = entries
1107            .iter()
1108            .find(|entry| entry.destination.ends_with("release-plz.yml"))
1109            .expect("the workflow projects");
1110        assert_eq!(workflow.kind, Kind::Rendered);
1111        let text = String::from_utf8_lossy(&workflow.rendered);
1112        assert!(!text.contains("OWNER"), "an owner token survived rendering");
1113        assert!(text.contains("'acme'"));
1114        assert!(!text.contains("TODO(release-kit)"));
1115        let title = entries
1116            .iter()
1117            .find(|entry| entry.destination.ends_with("pr-title.yml"))
1118            .expect("the title check projects");
1119        let text = String::from_utf8_lossy(&title.rendered);
1120        assert!(text.contains("api|cli"), "{text}");
1121        assert!(
1122            !text.contains("RK_SCOPES"),
1123            "a scope token survived: {text}"
1124        );
1125        let seeded = entries
1126            .iter()
1127            .find(|entry| entry.destination == "release-plz.toml")
1128            .expect("the seeded file projects");
1129        assert_eq!(seeded.kind, Kind::Seeded);
1130        assert_eq!(seeded.rendered, seeded.baseline);
1131        assert!(String::from_utf8_lossy(&seeded.rendered).contains("TODO(release-kit)"));
1132        for block in [AGENTS_DESTINATION, HOOKS_DESTINATION] {
1133            let entry = entries
1134                .iter()
1135                .find(|entry| entry.destination == block)
1136                .expect("both blocks are part of the projection");
1137            let text = String::from_utf8_lossy(&entry.rendered);
1138            assert!(!text.contains("RK_SCOPES"), "{block} kept a token: {text}");
1139            assert!(text.contains("api,cli"), "{block} lost the scopes: {text}");
1140        }
1141    }
1142
1143    /// The Nix destinations project only under the opt-in: off, none of
1144    /// them appears; on, the rust pairs carry them — the gitlab pair too,
1145    /// minus the workflow, which is a forge file the gitlab payload does
1146    /// not ship — and a pair without them projects the smaller product.
1147    #[test]
1148    fn the_nix_destinations_project_only_under_the_opt_in() {
1149        use super::NIX_DESTINATIONS;
1150        let paths = |nix: bool, forge: &str| -> Vec<String> {
1151            projection(
1152                "rust",
1153                forge,
1154                "acme/widget",
1155                &scopes(&["api"]),
1156                Workflow::Worktree,
1157                Some(Style::Trunk),
1158                nix,
1159            )
1160            .expect("the pair projects")
1161            .into_iter()
1162            .map(|entry| entry.destination)
1163            .collect()
1164        };
1165        let off = paths(false, "github");
1166        for destination in NIX_DESTINATIONS {
1167            assert!(!off.contains(&destination.to_owned()), "{destination}");
1168        }
1169        let on = paths(true, "github");
1170        for destination in ["nix/package.nix", "flake.nix", "flake.lock"] {
1171            assert!(on.contains(&destination.to_owned()), "{destination}");
1172        }
1173        let gitlab = paths(true, "gitlab");
1174        assert!(gitlab.contains(&"nix/package.nix".to_owned()));
1175        assert!(!gitlab.contains(&".github/workflows/nix.yml".to_owned()));
1176        let bash = projection(
1177            "bash",
1178            "github",
1179            "acme/widget",
1180            &scopes(&["api"]),
1181            Workflow::Worktree,
1182            Some(Style::Trunk),
1183            true,
1184        )
1185        .expect("an out-of-matrix pair projects the smaller product");
1186        assert!(
1187            bash.iter()
1188                .all(|entry| !NIX_DESTINATIONS.contains(&entry.destination.as_str()))
1189        );
1190    }
1191
1192    /// The github and gitlab copies of the forge-independent Nix payload
1193    /// stay byte-identical: the loader composes exactly two layers and has
1194    /// no technology-wide zone, so the duplication is deliberate and this
1195    /// parity test is what keeps it honest.
1196    #[test]
1197    fn the_nix_seeds_are_identical_across_forge_pairs() {
1198        for name in ["nix/package.nix", "flake.nix", "flake.lock"] {
1199            let github = embedded::SNIPPETS
1200                .get_file(format!("rust/github/{name}"))
1201                .expect("the github copy ships")
1202                .contents();
1203            let gitlab = embedded::SNIPPETS
1204                .get_file(format!("rust/gitlab/{name}"))
1205                .expect("the gitlab copy ships")
1206                .contents();
1207            assert_eq!(github, gitlab, "{name} diverged between the pairs");
1208        }
1209    }
1210
1211    /// The withhold judgment: a flake pair of the target's own withholds
1212    /// the pair and the workflow while the package expression lands, a
1213    /// crate shape the seed does not support withholds everything, and a
1214    /// clean single-crate target withholds nothing.
1215    #[test]
1216    fn the_nix_withhold_judgment_covers_the_three_shapes() {
1217        use super::{NIX_DESTINATIONS, withhold_nix};
1218        let dir = tempfile::tempdir().expect("a scratch target exists");
1219        let target = camino::Utf8Path::from_path(dir.path()).expect("utf-8 path");
1220        let entries = || {
1221            projection(
1222                "rust",
1223                "github",
1224                "acme/widget",
1225                &scopes(&["api"]),
1226                Workflow::Worktree,
1227                Some(Style::Trunk),
1228                true,
1229            )
1230            .expect("the pair projects")
1231        };
1232
1233        // No Cargo.toml: the whole capability is withheld by name.
1234        let mut all = entries();
1235        let withheld = withhold_nix(target, true, None, &mut all).expect("the judgment runs");
1236        let paths: Vec<&str> = withheld.iter().map(|w| w.path.as_str()).collect();
1237        assert_eq!(
1238            paths,
1239            [
1240                ".github/workflows/nix.yml",
1241                "flake.lock",
1242                "flake.nix",
1243                "nix/package.nix"
1244            ]
1245        );
1246        assert!(
1247            all.iter()
1248                .all(|entry| !NIX_DESTINATIONS.contains(&entry.destination.as_str()))
1249        );
1250
1251        // A single crate with its own flake: the pair and the workflow are
1252        // withheld, and the package expression still lands.
1253        std::fs::write(
1254            target.join("Cargo.toml"),
1255            "[package]\nname = \"widget\"\nversion = \"0.1.0\"\n",
1256        )
1257        .expect("the crate manifest writes");
1258        std::fs::write(target.join("Cargo.lock"), "version = 4\n").expect("the lock writes");
1259        std::fs::create_dir_all(target.join("src")).expect("the src dir exists");
1260        std::fs::write(target.join("src/main.rs"), "fn main() {}\n").expect("the main writes");
1261        std::fs::write(target.join("flake.nix"), "{ }\n").expect("the flake writes");
1262        let mut all = entries();
1263        let withheld = withhold_nix(target, true, None, &mut all).expect("the judgment runs");
1264        let paths: Vec<&str> = withheld.iter().map(|w| w.path.as_str()).collect();
1265        assert_eq!(
1266            paths,
1267            [".github/workflows/nix.yml", "flake.lock", "flake.nix"]
1268        );
1269        assert!(
1270            all.iter()
1271                .any(|entry| entry.destination == "nix/package.nix")
1272        );
1273
1274        // A clean single crate: nothing is withheld.
1275        std::fs::remove_file(target.join("flake.nix")).expect("the flake removes");
1276        let mut all = entries();
1277        let withheld = withhold_nix(target, true, None, &mut all).expect("the judgment runs");
1278        assert!(withheld.is_empty());
1279        assert!(all.iter().any(|entry| entry.destination == "flake.nix"));
1280
1281        // Off, the judgment does not even look.
1282        let mut all = entries();
1283        let withheld = withhold_nix(target, false, None, &mut all).expect("the judgment runs");
1284        assert!(withheld.is_empty());
1285    }
1286
1287    #[test]
1288    fn the_block_splices_into_every_agents_shape() {
1289        let owned = routing_block(Workflow::Branches);
1290        let block = owned.as_str();
1291        let fresh = splice_agents_block(None, block);
1292        assert_eq!(fresh, format!("{block}\n"));
1293        assert_eq!(extract_block(&fresh, BLOCK_BEGIN, BLOCK_END), Some(block));
1294
1295        let appended = splice_agents_block(Some("# My project\n\nOwn rules.\n"), block);
1296        assert!(appended.starts_with("# My project\n\nOwn rules.\n\n<!-- BEGIN release-kit -->"));
1297        assert_eq!(
1298            extract_block(&appended, BLOCK_BEGIN, BLOCK_END),
1299            Some(block)
1300        );
1301
1302        let stale = appended.replace("Never author a tag", "Do author a tag");
1303        let refreshed = splice_agents_block(Some(&stale), block);
1304        assert_eq!(
1305            extract_block(&refreshed, BLOCK_BEGIN, BLOCK_END),
1306            Some(block)
1307        );
1308        assert!(refreshed.starts_with("# My project"));
1309        assert_eq!(
1310            refreshed.matches("BEGIN release-kit").count(),
1311            1,
1312            "a re-splice must replace, not accumulate"
1313        );
1314    }
1315
1316    /// The hook block lands under `repos:` in every honest shape and
1317    /// refuses the one dishonest shape by name.
1318    #[test]
1319    fn the_hook_block_splices_under_repos() {
1320        let owned = hooks_block(Workflow::Branches);
1321        let block = owned.as_str();
1322        let fresh = splice_hooks_block(None, block).expect("a fresh file splices");
1323        assert!(fresh.starts_with(HOOK_TYPES_LINE));
1324        assert!(fresh.contains("\nrepos:\n# BEGIN release-kit\n"));
1325        assert_eq!(extract_block(&fresh, HOOKS_BEGIN, HOOKS_END), Some(block));
1326
1327        let own =
1328            "repos:\n  - repo: https://example.com/own\n    rev: v1\n    hooks:\n      - id: own\n";
1329        let spliced = splice_hooks_block(Some(own), block).expect("an unmarked file splices");
1330        assert!(spliced.starts_with("repos:\n# BEGIN release-kit\n"));
1331        assert!(spliced.contains("- id: own"), "the target's hooks survive");
1332        assert!(
1333            !spliced.contains(HOOK_TYPES_LINE),
1334            "an existing file's top level is the skills' duty, not the splice's"
1335        );
1336
1337        let stale = spliced.replace("--force-scope", "--no-scope");
1338        let refreshed = splice_hooks_block(Some(&stale), block).expect("a marked file re-splices");
1339        assert_eq!(
1340            extract_block(&refreshed, HOOKS_BEGIN, HOOKS_END),
1341            Some(block)
1342        );
1343        assert_eq!(refreshed.matches(HOOKS_BEGIN).count(), 1);
1344
1345        let err = splice_hooks_block(Some("minimum_pre_commit_version: '3.2.0'\n"), block)
1346            .expect_err("no repos: line refuses");
1347        assert!(err.contains("repos:"), "{err}");
1348
1349        // The hooks between the markers execute, so ownership is exactly
1350        // one well-formed block: a duplicate or an unmatched marker
1351        // refuses rather than leaving a stale block active.
1352        let doubled = format!("repos:\n{block}\n{block}\n");
1353        let err = splice_hooks_block(Some(&doubled), block).expect_err("a second block refuses");
1354        assert!(err.contains("one block"), "{err}");
1355        let unmatched = "repos:\n# BEGIN release-kit\n  - repo: local\n";
1356        let err =
1357            splice_hooks_block(Some(unmatched), block).expect_err("an unmatched marker refuses");
1358        assert!(err.contains("unmatched"), "{err}");
1359    }
1360
1361    /// Both modes of both blocks: the guard entry and the skip pair exist
1362    /// exactly in the worktree mode, one orientation line differs in the
1363    /// routing block, the rest is byte-identical, no mode token survives
1364    /// substitution, and the rendered grammar is [`BRANCH_GRAMMAR`], the
1365    /// one owner.
1366    #[test]
1367    fn the_blocks_render_per_mode_and_carry_the_one_grammar() {
1368        let worktree_hooks = hooks_block(Workflow::Worktree);
1369        let branches_hooks = hooks_block(Workflow::Branches);
1370        assert!(worktree_hooks.contains("- id: rk-worktree-location"));
1371        assert!(
1372            worktree_hooks.contains("SKIP=no-commit-to-branch,rk-worktree-location"),
1373            "{worktree_hooks}"
1374        );
1375        assert!(!branches_hooks.contains("rk-worktree-location"));
1376        assert!(branches_hooks.contains("SKIP=no-commit-to-branch in"));
1377        for block in [&worktree_hooks, &branches_hooks] {
1378            assert!(block.contains(BRANCH_GRAMMAR), "the grammar has one owner");
1379            for token in ["RK_BRANCH_GRAMMAR", "RK_SWEEP_SKIP", "RK_WORKTREE_GUARD"] {
1380                assert!(!block.contains(token), "{token} survived: {block}");
1381            }
1382        }
1383        // A hook entry renders as a YAML plain scalar, where a colon
1384        // followed by a space ends the scalar and breaks the whole file
1385        // — the defect dogfood caught in the guard's refusal messages —
1386        // so no entry value may carry one.
1387        for block in [&worktree_hooks, &branches_hooks] {
1388            for line in block.lines() {
1389                if let Some(value) = line.trim_start().strip_prefix("entry: ") {
1390                    assert!(
1391                        !value.contains(": "),
1392                        "an entry value breaks the YAML plain scalar: {line}"
1393                    );
1394                }
1395            }
1396        }
1397        let guard_line = worktree_hooks
1398            .lines()
1399            .position(|line| line.contains("id: rk-worktree-location"))
1400            .expect("the guard entry exists");
1401        let name_line = worktree_hooks
1402            .lines()
1403            .position(|line| line.contains("id: rk-branch-name"))
1404            .expect("the name hook exists");
1405        assert!(
1406            guard_line > name_line,
1407            "the guard lands directly after rk-branch-name"
1408        );
1409
1410        let worktree_routing = routing_block(Workflow::Worktree);
1411        let branches_routing = routing_block(Workflow::Branches);
1412        assert!(worktree_routing.contains("This project works in worktrees"));
1413        assert!(branches_routing.contains("Branches are worked in the main checkout"));
1414        for block in [&worktree_routing, &branches_routing] {
1415            assert!(block.contains("creating or removing a worktree"));
1416            assert!(block.contains("`rk worktree add <branch>`"));
1417            assert!(!block.contains("RK_WORKFLOW_LINE"), "{block}");
1418        }
1419        let differing: Vec<(&str, &str)> = worktree_routing
1420            .lines()
1421            .zip(branches_routing.lines())
1422            .filter(|(a, b)| a != b)
1423            .collect();
1424        assert_eq!(
1425            differing.len(),
1426            1,
1427            "exactly one routing line differs per mode: {differing:?}"
1428        );
1429    }
1430
1431    /// One definition of an ill-formed hook file, for every reader: the
1432    /// well-formed shapes pass and each ambiguous shape names a defect.
1433    #[test]
1434    fn the_hook_marker_defects_are_named() {
1435        use super::hooks_marker_defect;
1436        let owned = hooks_block(Workflow::Branches);
1437        let block = owned.as_str();
1438        assert_eq!(hooks_marker_defect(""), None);
1439        assert_eq!(hooks_marker_defect(&format!("repos:\n{block}\n")), None);
1440        for (case, text) in [
1441            (
1442                "a second begin",
1443                format!("repos:\n{block}\n# BEGIN release-kit\n"),
1444            ),
1445            (
1446                "a second end",
1447                format!("repos:\n{block}\n# END release-kit\n"),
1448            ),
1449            (
1450                "an unpaired begin",
1451                "repos:\n# BEGIN release-kit\n".to_owned(),
1452            ),
1453            ("an unpaired end", "repos:\n# END release-kit\n".to_owned()),
1454            (
1455                "an end before its begin",
1456                "repos:\n# END release-kit\n# BEGIN release-kit\n".to_owned(),
1457            ),
1458        ] {
1459            assert!(
1460                hooks_marker_defect(&text).is_some(),
1461                "{case} must be a defect"
1462            );
1463        }
1464    }
1465}