1use std::path::{Path, PathBuf};
4
5use serde::Serialize;
6
7use crate::config::Config;
8use crate::error::MarsError;
9use crate::local_source;
10use crate::lock::ItemKind;
11use crate::sync::{ResolutionMode, SyncOptions, SyncRequest};
12use crate::types::{DestPath, MarsContext};
13
14use super::output;
15
16#[derive(Debug, clap::Args)]
17pub struct AdoptArgs {
18 pub path: PathBuf,
20
21 #[arg(long)]
23 pub dry_run: bool,
24}
25
26#[derive(Debug)]
27struct AdoptPlan {
28 kind: ItemKind,
29 name: String,
30 source_abs: PathBuf,
31 source_display: String,
32 dest_abs: PathBuf,
33 dest_display: String,
34}
35
36#[derive(Debug, Serialize)]
37struct AdoptJson<'a> {
38 ok: bool,
39 kind: &'a str,
40 name: &'a str,
41 source_path: &'a str,
42 dest_path: &'a str,
43 #[serde(skip_serializing_if = "Option::is_none")]
44 sync: Option<serde_json::Value>,
45}
46
47pub fn run(args: &AdoptArgs, ctx: &MarsContext, json: bool) -> Result<i32, MarsError> {
48 let config = crate::config::load(&ctx.project_root)?;
49
50 let lock = crate::lock::load(&ctx.project_root)?;
51 let source_abs = resolve_cli_path(&args.path)?;
52 let source_display = relative_display(&ctx.project_root, &source_abs);
53
54 if source_abs.symlink_metadata().is_err() {
55 return Err(MarsError::InvalidRequest {
56 message: format!("path not found: {source_display}"),
57 });
58 }
59
60 let (target_name, target_rel) = source_target_membership(ctx, &config, &source_abs)?;
61 let target_dest = DestPath::new(target_rel.to_string_lossy().as_ref()).map_err(|e| {
62 MarsError::InvalidRequest {
63 message: format!(
64 "{} resolves to invalid managed target item `{}`: {e}",
65 source_display,
66 target_rel.display()
67 ),
68 }
69 })?;
70 if lock.contains_output(&target_name, target_dest.as_str()) {
71 return Err(MarsError::InvalidRequest {
72 message: format!(
73 "{source_display} is already managed by Mars (target `{target_name}` item `{}`)",
74 target_rel.display()
75 ),
76 });
77 }
78
79 let plan = build_plan(ctx, &source_abs, &source_display)?;
80
81 if args.dry_run {
82 return print_dry_run(&plan, json);
83 }
84
85 move_item(&plan.source_abs, &plan.dest_abs)?;
86
87 let request = SyncRequest {
88 resolution: ResolutionMode::Normal,
89 mutation: None,
90 options: SyncOptions::default(),
91 lossiness_mode: crate::diagnostic::LossinessMode::Hidden,
92 };
93 let report = crate::sync::execute(ctx, &request)?;
94
95 if json {
96 output::print_json(&AdoptJson {
97 ok: true,
98 kind: kind_name(plan.kind),
99 name: &plan.name,
100 source_path: &plan.source_display,
101 dest_path: &plan.dest_display,
102 sync: Some(output::sync_report_json(&report)),
103 });
104 } else {
105 output::print_success(&format!(
106 "adopted {} `{}`: {} -> {}",
107 kind_name(plan.kind),
108 plan.name,
109 plan.source_display,
110 plan.dest_display
111 ));
112 output::print_sync_report(&report, false, true);
113 }
114
115 Ok(if report.has_conflicts() { 1 } else { 0 })
116}
117
118fn resolve_cli_path(path: &Path) -> Result<PathBuf, MarsError> {
119 let absolute = if path.is_absolute() {
120 path.to_path_buf()
121 } else {
122 std::env::current_dir()?.join(path)
123 };
124 Ok(absolute)
125}
126
127fn source_target_membership(
128 ctx: &MarsContext,
129 config: &Config,
130 source_abs: &Path,
131) -> Result<(String, PathBuf), MarsError> {
132 let source_canon = dunce::canonicalize(source_abs)?;
133 for target_name in config.settings.managed_targets() {
134 let target_root = ctx.project_root.join(&target_name);
135 let Ok(target_canon) = dunce::canonicalize(&target_root) else {
136 continue;
137 };
138 if let Ok(relative) = source_canon.strip_prefix(&target_canon) {
139 return Ok((target_name, relative.to_path_buf()));
140 }
141 }
142
143 Err(MarsError::InvalidRequest {
144 message: format!(
145 "{} is not inside a managed target directory",
146 relative_display(&ctx.project_root, source_abs)
147 ),
148 })
149}
150
151fn build_plan(
152 ctx: &MarsContext,
153 source_abs: &Path,
154 source_display: &str,
155) -> Result<AdoptPlan, MarsError> {
156 let metadata = source_abs.symlink_metadata()?;
157 let preferred_root = local_source::preferred_local_source_root(&ctx.project_root);
158
159 let (kind, name, dest_abs) = if metadata.is_dir() {
160 if !source_abs.join("SKILL.md").is_file() {
161 return Err(MarsError::InvalidRequest {
162 message: format!(
163 "{source_display} is not a valid skill directory (expected a directory containing SKILL.md)"
164 ),
165 });
166 }
167 let name = source_abs
168 .file_name()
169 .and_then(|name| name.to_str())
170 .ok_or_else(|| MarsError::InvalidRequest {
171 message: format!("could not derive skill name from {source_display}"),
172 })?
173 .to_string();
174 (
175 ItemKind::Skill,
176 name.clone(),
177 preferred_root.join("skills").join(&name),
178 )
179 } else if metadata.is_file() {
180 let is_agent = source_abs.extension().and_then(|ext| ext.to_str()) == Some("md")
181 && source_abs
182 .parent()
183 .and_then(|path| path.file_name())
184 .and_then(|name| name.to_str())
185 == Some("agents");
186 if !is_agent {
187 return Err(MarsError::InvalidRequest {
188 message: format!(
189 "{source_display} is not a valid agent file (expected a .md file inside agents/)"
190 ),
191 });
192 }
193 let name = source_abs
194 .file_stem()
195 .and_then(|name| name.to_str())
196 .ok_or_else(|| MarsError::InvalidRequest {
197 message: format!("could not derive agent name from {source_display}"),
198 })?
199 .to_string();
200 (
201 ItemKind::Agent,
202 name.clone(),
203 preferred_root.join("agents").join(format!("{name}.md")),
204 )
205 } else {
206 return Err(MarsError::InvalidRequest {
207 message: format!(
208 "{source_display} is not a valid item (expected a skill directory or agent markdown file)"
209 ),
210 });
211 };
212
213 if dest_abs.symlink_metadata().is_ok() {
214 return Err(MarsError::InvalidRequest {
215 message: format!(
216 "{} already exists; refusing to overwrite local source content",
217 relative_display(&ctx.project_root, &dest_abs)
218 ),
219 });
220 }
221
222 Ok(AdoptPlan {
223 kind,
224 name,
225 source_abs: source_abs.to_path_buf(),
226 source_display: source_display.to_string(),
227 dest_display: relative_display(&ctx.project_root, &dest_abs),
228 dest_abs,
229 })
230}
231
232fn print_dry_run(plan: &AdoptPlan, json: bool) -> Result<i32, MarsError> {
233 if json {
234 output::print_json(&serde_json::json!({
235 "ok": true,
236 "dry_run": true,
237 "kind": kind_name(plan.kind),
238 "name": plan.name,
239 "source_path": plan.source_display,
240 "dest_path": plan.dest_display,
241 "sync": serde_json::Value::Null,
242 }));
243 } else {
244 output::print_info(&format!(
245 "would adopt {} `{}`: {} -> {}",
246 kind_name(plan.kind),
247 plan.name,
248 plan.source_display,
249 plan.dest_display
250 ));
251 }
252 Ok(0)
253}
254
255fn move_item(source: &Path, dest: &Path) -> Result<(), MarsError> {
256 if let Some(parent) = dest.parent() {
257 std::fs::create_dir_all(parent)?;
258 }
259
260 match std::fs::rename(source, dest) {
261 Ok(()) => Ok(()),
262 Err(err) if is_cross_device_rename(&err) => Err(MarsError::InvalidRequest {
263 message: format!(
264 "cannot adopt {} across filesystems in MVP; move it onto the same filesystem as the repo first",
265 source.display()
266 ),
267 }),
268 Err(err) => Err(err.into()),
269 }
270}
271
272fn kind_name(kind: ItemKind) -> &'static str {
273 match kind {
274 ItemKind::Agent => "agent",
275 ItemKind::Skill => "skill",
276 ItemKind::Hook => "hook",
277 ItemKind::McpServer => "mcp-server",
278 ItemKind::BootstrapDoc => "bootstrap-doc",
279 }
280}
281
282fn relative_display(project_root: &Path, path: &Path) -> String {
283 path.strip_prefix(project_root)
284 .unwrap_or(path)
285 .display()
286 .to_string()
287}
288
289#[cfg(unix)]
290fn is_cross_device_rename(err: &std::io::Error) -> bool {
291 err.raw_os_error() == Some(libc::EXDEV)
292}
293
294#[cfg(windows)]
295fn is_cross_device_rename(err: &std::io::Error) -> bool {
296 const ERROR_NOT_SAME_DEVICE: i32 = 17;
297 err.raw_os_error() == Some(ERROR_NOT_SAME_DEVICE)
298}
299
300#[cfg(not(any(unix, windows)))]
301fn is_cross_device_rename(_err: &std::io::Error) -> bool {
302 false
303}