mars_agents/cli/
resolve_cmd.rs1use std::path::PathBuf;
4
5use crate::error::MarsError;
6use crate::hash;
7use crate::types::{ContentHash, DestPath};
8
9use super::output;
10
11#[derive(Debug, clap::Args)]
13pub struct ResolveArgs {
14 pub file: Option<PathBuf>,
16}
17
18pub fn run(args: &ResolveArgs, ctx: &super::MarsContext, json: bool) -> Result<i32, MarsError> {
20 let mars_dir = ctx.project_root.join(".mars");
21 let lock_path = mars_dir.join("sync.lock");
22 let _sync_lock = crate::fs::FileLock::acquire(&lock_path)?;
23
24 let mut lock = crate::lock::load(&ctx.project_root)?;
25 let mut resolved_files = Vec::new();
26 let mut still_conflicted = Vec::new();
27
28 let items_to_check: Vec<DestPath> = if let Some(file) = &args.file {
29 let rel_input = if file.is_absolute() {
31 file.strip_prefix(&mars_dir).map_or_else(
32 |_| file.to_string_lossy().to_string(),
33 |r| r.to_string_lossy().to_string(),
34 )
35 } else {
36 file.to_string_lossy().to_string()
37 };
38 let rel = DestPath::new(&rel_input).map_err(|e| MarsError::Source {
39 source_name: "resolve".to_string(),
40 message: format!("invalid managed item path `{}`: {e}", file.display()),
41 })?;
42 if lock.items.contains_key(&rel) {
43 vec![rel]
44 } else {
45 return Err(MarsError::Source {
46 source_name: "resolve".to_string(),
47 message: format!("{} is not a managed item", file.display()),
48 });
49 }
50 } else {
51 lock.items.keys().cloned().collect()
53 };
54
55 for dest_path_str in &items_to_check {
56 let disk_path = dest_path_str.resolve(&mars_dir);
57 if !disk_path.exists() {
58 continue;
59 }
60
61 if crate::merge::file_has_conflict_markers(&disk_path) {
63 still_conflicted.push(dest_path_str.clone());
64 continue;
65 }
66
67 if let Some(item) = lock.items.get_mut(dest_path_str) {
69 let new_hash = hash::compute_hash(&disk_path, item.kind)?;
70 if new_hash != item.installed_checksum {
71 item.installed_checksum = ContentHash::from(new_hash);
72 resolved_files.push(dest_path_str.to_string());
73 }
74 }
75 }
76
77 if !resolved_files.is_empty() {
79 crate::lock::write(&ctx.project_root, &lock)?;
80 }
81
82 if json {
83 output::print_json(&serde_json::json!({
84 "resolved": resolved_files,
85 "still_conflicted": still_conflicted,
86 }));
87 } else {
88 for file in &resolved_files {
89 output::print_success(&format!("resolved {file}"));
90 }
91 for file in &still_conflicted {
92 eprintln!(" ! {file} still has conflict markers");
93 }
94 if resolved_files.is_empty() && still_conflicted.is_empty() {
95 output::print_info("no conflicts to resolve");
96 }
97 }
98
99 if still_conflicted.is_empty() {
100 Ok(0)
101 } else {
102 Ok(1)
103 }
104}