//! Merge commands (M4.8): `git.merge`, `git.mergeAbort`, //! `git.conflictVersions` and `git.markResolved`, on top of //! `lutra_vcs::Repo`'s merge calls. //! //! Each runs through [`super::git::start_with`]: a pending outcome, git and //! the repository detected and cached as for every `git.*` command, and //! `gitChanged` after the calls that change the repository (merge, abort, //! mark resolved). A merge that stops on conflicts is a success with the //! conflicted paths, so `gitChanged` follows it too. use lutra_vcs::MergeResult; use serde::Deserialize; use serde_json::{Value, json}; use super::git::{rel_path, start_with}; use super::params::parse; use crate::{Command, CommandSpec, EngineError, Outcome, Registry, State}; /// Registers the merge commands. /// /// # Errors /// [`EngineError::Internal`] if an id is taken. pub fn register(registry: &mut Registry) -> Result<(), EngineError> { registry.register(Box::new(Merge))?; registry.register(Box::new(MergeAbort))?; registry.register(Box::new(ConflictVersionsCommand))?; registry.register(Box::new(MarkResolved))?; Ok(()) } /// The wire name of a merge result. fn result_name(result: MergeResult) -> &'static str { match result { MergeResult::Merged => "merged", MergeResult::UpToDate => "upToDate", MergeResult::Conflicts => "conflicts", } } /// Params of `git.merge`. #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] struct MergeParams { /// The branch (or revision) to merge into the current branch. branch: String, } /// `git.merge`. struct Merge; impl Command for Merge { fn spec(&self) -> &CommandSpec { &CommandSpec { id: "git.merge", label: "Git: Merge Branch into Current", menu_path: None, default_shortcut: None, params: "{ branch: local or remote branch }; git merge --no-edit (fast-forward when possible, hooks run, 120 s); returns { result: \"merged\" | \"upToDate\" | \"conflicts\", conflicts: [project-relative path] }; stopping on conflicts is not an error. Refused while a merge is in progress; git's refusal (local changes would be overwritten) is the error text. Save dirty buffers first.", } } fn enabled(&self, state: &State) -> bool { state.has_project() } fn run(&self, state: &mut State, params: Value) -> Result { let command = self.spec().id; let p: MergeParams = parse(command, params)?; let branch = p.branch; start_with( state, command, true, Box::new(move |repo| { let outcome = repo.merge(&branch)?; Ok(json!({ "result": result_name(outcome.result), "conflicts": outcome.conflicts, })) }), ) } } /// `git.mergeAbort`. struct MergeAbort; impl Command for MergeAbort { fn spec(&self) -> &CommandSpec { &CommandSpec { id: "git.mergeAbort", label: "Git: Abort Merge", menu_path: None, default_shortcut: None, params: "none; git merge --abort: the work tree and index return to the state before the merge; returns {}; fails with git's message when no merge is in progress", } } fn enabled(&self, state: &State) -> bool { state.has_project() } fn run(&self, state: &mut State, params: Value) -> Result { let command = self.spec().id; let _: serde_json::Map = parse(command, params)?; start_with( state, command, true, Box::new(|repo| repo.merge_abort().map(|()| json!({}))), ) } } /// Params of `git.conflictVersions`. #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] struct PathParams { /// The conflicted file. path: String, } /// `git.conflictVersions`. struct ConflictVersionsCommand; impl Command for ConflictVersionsCommand { fn spec(&self) -> &CommandSpec { &CommandSpec { id: "git.conflictVersions", label: "Git: Conflict Versions", menu_path: None, default_shortcut: None, params: "{ path: project-relative path of a conflicted file }; returns { base?: string, ours: string | null, theirs: string | null, binary, tooLarge }: the index stages :1: (common ancestor; absent when added on both sides), :2: (current branch) and :3: (merged branch) as stored; null when deleted on that side; all texts null when one version is binary or over 32 MiB. Fails when the file is not in conflict.", } } fn enabled(&self, state: &State) -> bool { state.has_project() } fn run(&self, state: &mut State, params: Value) -> Result { let command = self.spec().id; let p: PathParams = parse(command, params)?; let path = rel_path(state, command, &p.path)?; start_with( state, command, false, Box::new(move |repo| { let v = repo.conflict_versions(&path)?; let mut value = json!({ "ours": v.ours, "theirs": v.theirs, "binary": v.binary, "tooLarge": v.too_large, }); if let (Some(base), Some(map)) = (v.base, value.as_object_mut()) { map.insert("base".into(), Value::String(base)); } Ok(value) }), ) } } /// Params of `git.markResolved`. #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] struct PathsParams { /// The resolved files. paths: Vec, } /// `git.markResolved`. struct MarkResolved; impl Command for MarkResolved { fn spec(&self) -> &CommandSpec { &CommandSpec { id: "git.markResolved", label: "Git: Mark Resolved", menu_path: None, default_shortcut: None, params: "{ paths: [project-relative path] }; stages the files as they are on disk (a file missing from disk is staged as deleted), which clears their conflict; returns {}. Save the result first.", } } fn enabled(&self, state: &State) -> bool { state.has_project() } fn run(&self, state: &mut State, params: Value) -> Result { let command = self.spec().id; let p: PathsParams = parse(command, params)?; if p.paths.is_empty() { return Err(EngineError::invalid_params(command, "paths is empty")); } let paths = p .paths .iter() .map(|path| rel_path(state, command, path)) .collect::, _>>()?; start_with( state, command, true, Box::new(move |repo| repo.mark_resolved(&paths).map(|()| json!({}))), ) } }