Skip to main content

release_kit/commands/
adopt.rs

1//! `rk adopt`: a pre-record target becomes a recorded one.
2//!
3//! A front over the engine: the plan is computed with the adopt intent,
4//! which verifies every destination and writes none, and on `--apply`
5//! the engine writes the configuration and the record, last, through
6//! one staged transaction. The candidate payload is rendered first,
7//! exactly as `rk init` would produce it; every `rendered` destination
8//! must match it byte for byte, and one mismatch refuses the whole
9//! adoption listing every mismatch in one run. Blessing whatever is on
10//! disk would launder arbitrary drift into release-kit ownership, so
11//! nothing here ever takes the disk as the baseline — and no target file
12//! is ever changed: not a byte, not a mode, not a sentinel.
13
14use serde::Serialize;
15
16use crate::cli::adopt::AdoptArgs;
17use crate::commands::reconcile::{self, FrontApplied, Trace};
18use crate::diagnostic::{Diagnostic, Reason};
19use crate::error::RkError;
20use crate::landing::manifest::{self, Style, Workflow};
21use crate::landing::{self, Kind};
22use crate::output::Output;
23use crate::plan::gather::Flags;
24use crate::plan::{Disposition, Intent, PlanRequest, Planned};
25use crate::release::EmbeddedReleaseSource;
26
27/// One verified destination.
28#[derive(Debug, Serialize)]
29struct FileEntry {
30    /// The destination, relative to the target.
31    path: String,
32    /// The declared ownership kind.
33    kind: &'static str,
34    /// `matches`, `differs` for a seeded file, or `state`.
35    action: &'static str,
36}
37
38/// The machine form of an adoption report.
39#[derive(Debug, Serialize)]
40struct Report {
41    /// The shape version of this document.
42    schema: &'static str,
43    /// `preview` or `apply`.
44    mode: &'static str,
45    /// The target directory.
46    target: String,
47    /// The technology whose payload was verified.
48    tech: String,
49    /// The forge whose payload was verified.
50    forge: String,
51    /// The parameter the candidate was rendered under.
52    repo: String,
53    /// The working-copy mode the candidate was rendered under and the
54    /// record carries.
55    workflow: &'static str,
56    style: &'static str,
57    /// Whether the record carries the Nix capability.
58    nix: bool,
59    /// The Nix destinations excluded from the candidate, each with why;
60    /// absent where nothing was withheld.
61    #[serde(skip_serializing_if = "Option::is_none")]
62    withheld: Option<Vec<landing::Withheld>>,
63    config: crate::config::Plan,
64    /// Every destination, with its verification result.
65    files: Vec<FileEntry>,
66    /// The plan the apply executed; absent in a preview.
67    #[serde(skip_serializing_if = "Option::is_none")]
68    plan: Option<Trace>,
69    /// What plausibly follows.
70    next: Vec<String>,
71}
72
73/// Verify the target against the rendered candidate and, on `--apply`,
74/// write the config and record inside `.release-kit/`.
75///
76/// # Errors
77///
78/// Returns a refusal for a target already carrying a record, for any
79/// `rendered` mismatch or missing expected file — listing every one in
80/// one run — and [`RkError::Missing`] where detection resolves no
81/// technology, forge, or repository and no flag covers the gap.
82#[allow(
83    clippy::too_many_lines,
84    reason = "one adopt run is one linear sequence of checks against one target, and cutting it would separate a refusal from the order it is reported in"
85)]
86pub fn run(args: &AdoptArgs) -> Result<(), RkError> {
87    let out = Output::new(args.json);
88    if !args.target.is_dir() {
89        return Err(RkError::missing(
90            Diagnostic::new(
91                Reason::TargetNotFound,
92                format!("target {} is not a directory", args.target),
93            )
94            .expected("an existing repository to adopt"),
95        ));
96    }
97    if landing::manifest::load(&args.target)?.is_some() {
98        return Err(RkError::refusal(
99            Diagnostic::new(
100                Reason::StateDrift,
101                format!(
102                    "{} already carries {}; it needs no adoption",
103                    args.target,
104                    manifest::MANIFEST_PATH
105                ),
106            )
107            .expected("a target without a landing record")
108            .action(format!(
109                "rk upgrade --target {} takes it to this binary's payload",
110                args.target
111            ))
112            .target_state("unchanged"),
113        ));
114    }
115    let config = crate::config::load(args.target.as_std_path())?;
116    let source = EmbeddedReleaseSource;
117    let params = landing::Params::resolve(
118        &source,
119        &args.target,
120        &landing::Inputs {
121            tech: args.tech.as_deref(),
122            forge: args.forge.as_deref(),
123            repo: args.repo.as_deref(),
124            workflow: args.workflow.as_deref().map(Workflow::parse).transpose()?,
125            style: args.style.as_deref().map(Style::parse).transpose()?,
126            nix: args.nix.then_some(true),
127        },
128        config.as_ref(),
129        None,
130        landing::Purpose::Adopt,
131    )?;
132    let tech = params.tech().to_owned();
133    let repo = params.repo().to_owned();
134    let workflow = params.workflow();
135    let style = params
136        .style()
137        .ok_or_else(|| RkError::Usage("landing style is unresolved".into()))?;
138    let request = PlanRequest {
139        target: args.target.clone(),
140        intent: Intent::Adopt,
141        selector: "embedded".into(),
142        fetch: false,
143        observe_forge: false,
144        flags: Flags {
145            tech: Some(tech.clone()),
146            forge: Some(params.forge().to_owned()),
147            repo: Some(repo.clone()),
148            workflow: Some(workflow.as_str().to_owned()),
149            style: Some(style.as_str().to_owned()),
150            nix: Some(params.nix()),
151        },
152        decisions: std::collections::BTreeMap::new(),
153    };
154    let planned = reconcile::compute(&request, &manifest::now())?;
155    let config = planned
156        .config
157        .clone()
158        .ok_or_else(|| RkError::Usage("landing parameters are unresolved".into()))?;
159    let files = verify(args, workflow, &planned)?;
160
161    for file in &files {
162        out.result_line(match file.action {
163            "differs" => format!("differs {} (seeded, target-owned)", file.path),
164            action => format!("{action} {}", file.path),
165        });
166    }
167    for entry in &planned.withheld {
168        out.result_line(format!("withheld {}: {}", entry.path, entry.reason));
169    }
170
171    let applied = if args.apply {
172        let applied = reconcile::apply_in_process(&planned, &request, "adopt")?;
173        out.result_line(format!("wrote {}", manifest::MANIFEST_PATH));
174        out.result_line(applied.line());
175        Some(applied)
176    } else {
177        None
178    };
179
180    let next = if args.apply {
181        vec![
182            "commit the config and the record".to_owned(),
183            format!("rk status --target {} reports this landing", args.target),
184        ]
185    } else {
186        vec![format!(
187            "rk adopt --tech {tech} --forge {} --repo {repo} --workflow {} --style {}{} --target {} --apply writes the config and the record inside .release-kit/",
188            params.forge().to_owned(),
189            workflow.as_str(),
190            style.as_str(),
191            if params.nix() { " --nix" } else { "" },
192            args.target
193        )]
194    };
195    out.result_line(format!(
196        "{} {}\n{}",
197        config.action,
198        crate::config::CONFIG_PATH,
199        config.content
200    ));
201    out.next(&next);
202    out.emit(&Report {
203        schema: "rk.adopt/6",
204        config,
205        mode: if args.apply { "apply" } else { "preview" },
206        target: args.target.to_string(),
207        tech,
208        forge: params.forge().to_owned(),
209        repo,
210        workflow: workflow.as_str(),
211        style: style.as_str(),
212        nix: params.nix(),
213        withheld: (!planned.withheld.is_empty()).then(|| planned.withheld.clone()),
214        files,
215        plan: applied.as_ref().map(FrontApplied::trace),
216        next,
217    })?;
218    applied
219        .and_then(|applied| applied.applied.failure())
220        .map_or(Ok(()), Err)
221}
222
223/// The verification pass, read off the plan: every destination checked
224/// against the rendered candidate, every failure collected before the one
225/// refusal, so an operator resolves everything and re-runs once.
226fn verify(
227    args: &AdoptArgs,
228    workflow: Workflow,
229    planned: &Planned,
230) -> Result<Vec<FileEntry>, RkError> {
231    let mut mismatches: Vec<String> = Vec::new();
232    let mut missing: Vec<String> = Vec::new();
233    let mut files = Vec::new();
234    // An ill-formed hook file lists beside the mismatches rather than
235    // refusing alone, so one run still names everything unadoptable.
236    let defects: Vec<String> = planned
237        .plan
238        .preconditions
239        .iter()
240        .filter(|p| p.id == "hooks-file-spliceable")
241        .filter_map(|p| match &p.evaluation {
242            crate::plan::Evaluation::Unsatisfied { reason } => Some(reason.clone()),
243            _ => None,
244        })
245        .collect();
246    for outcome in &planned.outcomes {
247        if outcome.disposition == Disposition::Write {
248            // A block-placed artifact reads as absent from a file that
249            // exists; the operator's remedy differs, so the label must.
250            let label = if args.target.join(&outcome.path).exists() {
251                format!("{} (carries no release-kit block)", outcome.path)
252            } else {
253                format!("{} (expected and missing)", outcome.path)
254            };
255            missing.push(label);
256            continue;
257        }
258        let action = match (outcome.kind, outcome.disposition) {
259            (Kind::Rendered | Kind::Seeded, Disposition::Unchanged) => "matches",
260            (Kind::Rendered, _) => {
261                mismatches.push(outcome.path.clone());
262                "differs"
263            }
264            (Kind::Seeded, _) => "differs",
265            (Kind::State, _) => "state",
266        };
267        files.push(FileEntry {
268            path: outcome.path.clone(),
269            kind: outcome.kind.as_str(),
270            action,
271        });
272    }
273    if mismatches.is_empty() && missing.is_empty() && defects.is_empty() {
274        return Ok(files);
275    }
276    let listed: Vec<String> = mismatches
277        .iter()
278        .map(|path| format!("{path} (differs from the rendered candidate)"))
279        .chain(missing.iter().cloned())
280        .chain(defects.iter().cloned())
281        .collect();
282    Err(RkError::refusal(
283        Diagnostic::new(
284            Reason::StateDrift,
285            format!(
286                "this target is not adoptable as-is, and no record was written: {}",
287                listed.join(", ")
288            ),
289        )
290        .expected(format!(
291            "every rendered destination matching the {} candidate, byte for byte",
292            workflow.as_str()
293        ))
294        .action(format!(
295            "align first: rk adopt without --apply lists every differing destination; bring each to the selected candidate's bytes — rk snippet and rk payload print them — then re-run, or select the other candidate with --workflow or --style{}",
296            // A policy the target wrote its own contact into is the one
297            // mismatch a committed answer resolves rather than an edit:
298            // naming the keys turns a dead end into the next step.
299            if mismatches.iter().any(|path| path == "SECURITY.md") {
300                ". SECURITY.md states two facts a target owns: set security.contact and security.response in .release-kit/config.toml to the wording this policy already carries, and the candidate matches"
301            } else {
302                ""
303            }
304        ))
305        .target_state("unchanged"),
306    ))
307}
308
309#[cfg(test)]
310mod tests {
311    use super::{FileEntry, Report};
312
313    /// The complete `rk.adopt/6` shape, held by snapshot.
314    #[test]
315    fn the_adopt_report_schema_snapshot_holds() {
316        let report = Report {
317            schema: "rk.adopt/6",
318            config: crate::config::Plan {
319                action: "added",
320                changes: vec![],
321                content: "schema_version = 1\n".into(),
322            },
323            mode: "apply",
324            target: "/tmp/t".into(),
325            tech: "rust".into(),
326            forge: "github".into(),
327            repo: "acme/widget".into(),
328            workflow: "branches",
329            style: "trunk",
330            nix: false,
331            withheld: None,
332            files: vec![FileEntry {
333                path: "release-plz.toml".into(),
334                kind: "seeded",
335                action: "differs",
336            }],
337            plan: None,
338            next: vec!["commit the config and the record".into()],
339        };
340        assert_eq!(
341            serde_json::to_string(&report).expect("a report serializes"),
342            r#"{"schema":"rk.adopt/6","mode":"apply","target":"/tmp/t","tech":"rust","forge":"github","repo":"acme/widget","workflow":"branches","style":"trunk","nix":false,"config":{"action":"added","changes":[],"content":"schema_version = 1\n"},"files":[{"path":"release-plz.toml","kind":"seeded","action":"differs"}],"next":["commit the config and the record"]}"#
343        );
344    }
345}