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 {
91 force: false,
92 dry_run: false,
93 frozen: false,
94 refresh_models: false,
95 no_refresh_models: false,
96 },
97 };
98 let report = crate::sync::execute(ctx, &request)?;
99
100 if json {
101 output::print_json(&AdoptJson {
102 ok: true,
103 kind: kind_name(plan.kind),
104 name: &plan.name,
105 source_path: &plan.source_display,
106 dest_path: &plan.dest_display,
107 sync: Some(output::sync_report_json(&report)),
108 });
109 } else {
110 output::print_success(&format!(
111 "adopted {} `{}`: {} -> {}",
112 kind_name(plan.kind),
113 plan.name,
114 plan.source_display,
115 plan.dest_display
116 ));
117 output::print_sync_report(&report, false, true);
118 }
119
120 Ok(if report.has_conflicts() { 1 } else { 0 })
121}
122
123fn resolve_cli_path(path: &Path) -> Result<PathBuf, MarsError> {
124 let absolute = if path.is_absolute() {
125 path.to_path_buf()
126 } else {
127 std::env::current_dir()?.join(path)
128 };
129 Ok(absolute)
130}
131
132fn source_target_membership(
133 ctx: &MarsContext,
134 config: &Config,
135 source_abs: &Path,
136) -> Result<(String, PathBuf), MarsError> {
137 let source_canon = dunce::canonicalize(source_abs)?;
138 for target_name in config.settings.managed_targets() {
139 let target_root = ctx.project_root.join(&target_name);
140 let Ok(target_canon) = dunce::canonicalize(&target_root) else {
141 continue;
142 };
143 if let Ok(relative) = source_canon.strip_prefix(&target_canon) {
144 return Ok((target_name, relative.to_path_buf()));
145 }
146 }
147
148 Err(MarsError::InvalidRequest {
149 message: format!(
150 "{} is not inside a managed target directory",
151 relative_display(&ctx.project_root, source_abs)
152 ),
153 })
154}
155
156fn build_plan(
157 ctx: &MarsContext,
158 source_abs: &Path,
159 source_display: &str,
160) -> Result<AdoptPlan, MarsError> {
161 let metadata = source_abs.symlink_metadata()?;
162 let preferred_root = local_source::preferred_local_source_root(&ctx.project_root);
163
164 let (kind, name, dest_abs) = if metadata.is_dir() {
165 if !source_abs.join("SKILL.md").is_file() {
166 return Err(MarsError::InvalidRequest {
167 message: format!(
168 "{source_display} is not a valid skill directory (expected a directory containing SKILL.md)"
169 ),
170 });
171 }
172 let name = source_abs
173 .file_name()
174 .and_then(|name| name.to_str())
175 .ok_or_else(|| MarsError::InvalidRequest {
176 message: format!("could not derive skill name from {source_display}"),
177 })?
178 .to_string();
179 (
180 ItemKind::Skill,
181 name.clone(),
182 preferred_root.join("skills").join(&name),
183 )
184 } else if metadata.is_file() {
185 let is_agent = source_abs.extension().and_then(|ext| ext.to_str()) == Some("md")
186 && source_abs
187 .parent()
188 .and_then(|path| path.file_name())
189 .and_then(|name| name.to_str())
190 == Some("agents");
191 if !is_agent {
192 return Err(MarsError::InvalidRequest {
193 message: format!(
194 "{source_display} is not a valid agent file (expected a .md file inside agents/)"
195 ),
196 });
197 }
198 let name = source_abs
199 .file_stem()
200 .and_then(|name| name.to_str())
201 .ok_or_else(|| MarsError::InvalidRequest {
202 message: format!("could not derive agent name from {source_display}"),
203 })?
204 .to_string();
205 (
206 ItemKind::Agent,
207 name.clone(),
208 preferred_root.join("agents").join(format!("{name}.md")),
209 )
210 } else {
211 return Err(MarsError::InvalidRequest {
212 message: format!(
213 "{source_display} is not a valid item (expected a skill directory or agent markdown file)"
214 ),
215 });
216 };
217
218 if dest_abs.symlink_metadata().is_ok() {
219 return Err(MarsError::InvalidRequest {
220 message: format!(
221 "{} already exists; refusing to overwrite local source content",
222 relative_display(&ctx.project_root, &dest_abs)
223 ),
224 });
225 }
226
227 Ok(AdoptPlan {
228 kind,
229 name,
230 source_abs: source_abs.to_path_buf(),
231 source_display: source_display.to_string(),
232 dest_display: relative_display(&ctx.project_root, &dest_abs),
233 dest_abs,
234 })
235}
236
237fn print_dry_run(plan: &AdoptPlan, json: bool) -> Result<i32, MarsError> {
238 if json {
239 output::print_json(&serde_json::json!({
240 "ok": true,
241 "dry_run": true,
242 "kind": kind_name(plan.kind),
243 "name": plan.name,
244 "source_path": plan.source_display,
245 "dest_path": plan.dest_display,
246 "sync": serde_json::Value::Null,
247 }));
248 } else {
249 output::print_info(&format!(
250 "would adopt {} `{}`: {} -> {}",
251 kind_name(plan.kind),
252 plan.name,
253 plan.source_display,
254 plan.dest_display
255 ));
256 }
257 Ok(0)
258}
259
260fn move_item(source: &Path, dest: &Path) -> Result<(), MarsError> {
261 if let Some(parent) = dest.parent() {
262 std::fs::create_dir_all(parent)?;
263 }
264
265 match std::fs::rename(source, dest) {
266 Ok(()) => Ok(()),
267 Err(err) if is_cross_device_rename(&err) => Err(MarsError::InvalidRequest {
268 message: format!(
269 "cannot adopt {} across filesystems in MVP; move it onto the same filesystem as the repo first",
270 source.display()
271 ),
272 }),
273 Err(err) => Err(err.into()),
274 }
275}
276
277fn kind_name(kind: ItemKind) -> &'static str {
278 match kind {
279 ItemKind::Agent => "agent",
280 ItemKind::Skill => "skill",
281 ItemKind::Hook => "hook",
282 ItemKind::McpServer => "mcp-server",
283 ItemKind::BootstrapDoc => "bootstrap-doc",
284 }
285}
286
287fn relative_display(project_root: &Path, path: &Path) -> String {
288 path.strip_prefix(project_root)
289 .unwrap_or(path)
290 .display()
291 .to_string()
292}
293
294#[cfg(unix)]
295fn is_cross_device_rename(err: &std::io::Error) -> bool {
296 err.raw_os_error() == Some(libc::EXDEV)
297}
298
299#[cfg(windows)]
300fn is_cross_device_rename(err: &std::io::Error) -> bool {
301 const ERROR_NOT_SAME_DEVICE: i32 = 17;
302 err.raw_os_error() == Some(ERROR_NOT_SAME_DEVICE)
303}
304
305#[cfg(not(any(unix, windows)))]
306fn is_cross_device_rename(_err: &std::io::Error) -> bool {
307 false
308}