Skip to main content

mars_agents/cli/
resolve_cmd.rs

1//! `mars resolve` — mark conflicts as resolved after user fixes them.
2
3use std::path::{Path, PathBuf};
4
5use crate::error::MarsError;
6use crate::hash;
7use crate::types::{ContentHash, DestPath};
8
9use super::output;
10
11/// Arguments for `mars resolve`.
12#[derive(Debug, clap::Args)]
13pub struct ResolveArgs {
14    /// Specific file to resolve (default: all conflicted).
15    pub file: Option<PathBuf>,
16}
17
18/// Run `mars resolve`.
19pub fn run(args: &ResolveArgs, ctx: &super::MarsContext, json: bool) -> Result<i32, MarsError> {
20    let mut lock = crate::lock::load(&ctx.project_root)?;
21    let mut resolved_files = Vec::new();
22    let mut still_conflicted = Vec::new();
23
24    let items_to_check: Vec<DestPath> = if let Some(file) = &args.file {
25        // Check specific file
26        let rel = DestPath::from(file.as_path());
27        if lock.items.contains_key(&rel) {
28            vec![rel]
29        } else {
30            return Err(MarsError::Source {
31                source_name: "resolve".to_string(),
32                message: format!("{} is not a managed item", file.display()),
33            });
34        }
35    } else {
36        // Check all items
37        lock.items.keys().cloned().collect()
38    };
39
40    let mars_dir = ctx.project_root.join(".mars");
41    for dest_path_str in &items_to_check {
42        let disk_path = mars_dir.join(dest_path_str);
43        if !disk_path.exists() {
44            continue;
45        }
46
47        // Check for conflict markers
48        if has_conflict_markers(&disk_path)? {
49            still_conflicted.push(dest_path_str.clone());
50            continue;
51        }
52
53        // File has no conflict markers — update lock checksums
54        if let Some(item) = lock.items.get_mut(dest_path_str) {
55            let new_hash = hash::compute_hash(&disk_path, item.kind)?;
56            if new_hash != item.installed_checksum {
57                item.installed_checksum = ContentHash::from(new_hash);
58                resolved_files.push(dest_path_str.to_string());
59            }
60        }
61    }
62
63    // Write updated lock
64    if !resolved_files.is_empty() {
65        crate::lock::write(&ctx.project_root, &lock)?;
66    }
67
68    if json {
69        output::print_json(&serde_json::json!({
70            "resolved": resolved_files,
71            "still_conflicted": still_conflicted,
72        }));
73    } else {
74        for file in &resolved_files {
75            output::print_success(&format!("resolved {file}"));
76        }
77        for file in &still_conflicted {
78            eprintln!("  ! {file} still has conflict markers");
79        }
80        if resolved_files.is_empty() && still_conflicted.is_empty() {
81            output::print_info("no conflicts to resolve");
82        }
83    }
84
85    if still_conflicted.is_empty() {
86        Ok(0)
87    } else {
88        Ok(1)
89    }
90}
91
92/// Check if a file contains git conflict markers.
93fn has_conflict_markers(path: &Path) -> Result<bool, MarsError> {
94    let content = std::fs::read_to_string(path)?;
95    Ok(content.contains("<<<<<<<") && content.contains(">>>>>>>"))
96}