diff options
Diffstat (limited to 'makima/src/bin/makima.rs')
| -rw-r--r-- | makima/src/bin/makima.rs | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/makima/src/bin/makima.rs b/makima/src/bin/makima.rs index c4183f3..df3e8e7 100644 --- a/makima/src/bin/makima.rs +++ b/makima/src/bin/makima.rs @@ -864,6 +864,128 @@ async fn run_directive( let result = client.create_order(&req).await?; println!("{}", serde_json::to_string(&result.0)?); } + DirectiveCommand::Verify(args) => { + run_directive_verify(args).await?; + } + } + + Ok(()) +} + +/// Run `makima directive verify` — checks that the current HEAD merges cleanly +/// into `<remote>/<base>`. Prints a JSON result and exits non-zero on conflict. +/// +/// Implementation uses `git merge-tree --write-tree` (Git ≥ 2.38), which performs +/// the merge in-memory and lists conflicting paths without touching the working +/// tree or creating any commits. +async fn run_directive_verify( + args: makima::daemon::cli::directive::VerifyArgs, +) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { + use std::process::Command; + + fn git(args: &[&str]) -> std::io::Result<std::process::Output> { + Command::new("git").args(args).output() + } + + let head_ref = args.head.as_deref().unwrap_or("HEAD").to_string(); + let base_ref = format!("{}/{}", args.remote, args.base); + + if !args.skip_fetch { + eprintln!("Fetching {} {}...", args.remote, args.base); + let fetch = git(&["fetch", &args.remote, &args.base])?; + if !fetch.status.success() { + return Err(format!( + "git fetch {} {} failed: {}", + args.remote, + args.base, + String::from_utf8_lossy(&fetch.stderr) + ) + .into()); + } + } + + let head_rev = { + let out = git(&["rev-parse", &head_ref])?; + if !out.status.success() { + return Err(format!( + "git rev-parse {} failed: {}", + head_ref, + String::from_utf8_lossy(&out.stderr) + ) + .into()); + } + String::from_utf8_lossy(&out.stdout).trim().to_string() + }; + let base_rev = { + let out = git(&["rev-parse", &base_ref])?; + if !out.status.success() { + return Err(format!( + "git rev-parse {} failed (did you fetch?): {}", + base_ref, + String::from_utf8_lossy(&out.stderr) + ) + .into()); + } + String::from_utf8_lossy(&out.stdout).trim().to_string() + }; + + eprintln!("Verifying merge: {} ({}) <- {} ({})", base_ref, &base_rev[..7.min(base_rev.len())], head_ref, &head_rev[..7.min(head_rev.len())]); + + let merge = Command::new("git") + .args(["merge-tree", "--write-tree", "--name-only", "--no-messages", &base_rev, &head_rev]) + .output()?; + + let stdout = String::from_utf8_lossy(&merge.stdout).to_string(); + let stderr = String::from_utf8_lossy(&merge.stderr).to_string(); + let success = merge.status.success(); + + let conflicting_files: Vec<String> = if success { + Vec::new() + } else { + stdout + .lines() + .skip(1) + .filter(|l| !l.is_empty()) + .map(|l| l.to_string()) + .collect() + }; + + let result = serde_json::json!({ + "ok": success, + "base": base_ref, + "head": head_ref, + "baseSha": base_rev, + "headSha": head_rev, + "conflictingFiles": conflicting_files, + "goal": args.goal, + }); + println!("{}", serde_json::to_string(&result)?); + + if !success { + eprintln!("\n[FAIL] Merge would conflict in {} file(s):", conflicting_files.len()); + for f in &conflicting_files { + eprintln!(" - {}", f); + } + if !stderr.is_empty() { + eprintln!("\ngit stderr:\n{}", stderr); + } + eprintln!( + "\nFix the conflicts before pushing. Typical workflow:\n \ + git fetch {remote} {base}\n \ + git merge {remote}/{base}\n \ + # resolve conflicts, commit, then re-run `makima directive verify`", + remote = args.remote, + base = args.base, + ); + std::process::exit(1); + } + + if let Some(goal) = &args.goal { + eprintln!("\n[OK] No merge conflicts."); + eprintln!("Reminder — directive goal:\n {}\n", goal); + eprintln!("Confirm the diff (`git diff {}...HEAD`) actually delivers this goal before creating the PR.", base_ref); + } else { + eprintln!("[OK] No merge conflicts with {}.", base_ref); } Ok(()) |
