//! Stash commands (M4.9): `git.stash`, `git.stashList`, `git.stashApply` //! and `git.stashDrop`, run like every `git.*` command (a pending outcome //! off the lock, git and the repository cached by [`super::git`]). The //! three that change the repository emit `gitChanged`. use lutra_vcs::StashEntry; use serde::Deserialize; use serde_json::{Value, json}; use super::git::start_with; use super::params::parse; use crate::{Command, CommandSpec, EngineError, Outcome, Registry, State}; /// Largest index the commands take; the list holds at most 1,000 entries. const MAX_INDEX: usize = 100_000; /// Registers the stash commands. /// /// # Errors /// [`EngineError::Internal`] if an id is taken. pub fn register(registry: &mut Registry) -> Result<(), EngineError> { registry.register(Box::new(Stash))?; registry.register(Box::new(StashList))?; registry.register(Box::new(StashApply))?; registry.register(Box::new(StashDrop))?; Ok(()) } /// One entry as the commands return it. fn entry_json(entry: &StashEntry) -> Value { json!({ "index": entry.index, "hash": entry.hash, "branch": entry.branch, "message": entry.message, "wip": entry.wip, "date": entry.date, }) } /// Refuses an index past [`MAX_INDEX`]. fn check_index(command: &str, index: usize) -> Result<(), EngineError> { if index > MAX_INDEX { return Err(EngineError::invalid_params( command, format!("index must be 0 to {MAX_INDEX}"), )); } Ok(()) } /// Params of `git.stash`. #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields, rename_all = "camelCase")] struct StashParams { /// The entry's message; blank means git's "WIP on" message. #[serde(default)] message: Option, /// Stash untracked files too. #[serde(default)] include_untracked: bool, } /// `git.stash`. struct Stash; impl Command for Stash { fn spec(&self) -> &CommandSpec { &CommandSpec { id: "git.stash", label: "Git: Stash Changes", menu_path: None, default_shortcut: None, params: "{ message?: string (blank: git's \"WIP on \" message; line breaks become spaces), includeUntracked?: boolean (false) }; stashes every local change of the repository's work tree and resets it to HEAD; returns { entry: { index: 0, hash, branch, message, wip, date } }; refused with a clear message without a commit, with unmerged files, or with nothing to stash. Dirty buffers are not saved 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: StashParams = parse(command, params)?; start_with( state, command, true, Box::new(move |repo| { let entry = repo.stash(p.message.as_deref(), p.include_untracked)?; Ok(json!({"entry": entry_json(&entry)})) }), ) } } /// `git.stashList`. struct StashList; impl Command for StashList { fn spec(&self) -> &CommandSpec { &CommandSpec { id: "git.stashList", label: "Git: List Stashes", menu_path: None, default_shortcut: None, params: "none; returns { stashes: [{ index, hash, branch: string | null, message, wip, date (ISO 8601) }] } newest first (index 0 is stash@{0}), at most 1,000", } } 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, false, Box::new(move |repo| { let stashes: Vec = repo.stash_list()?.iter().map(entry_json).collect(); Ok(json!({"stashes": stashes})) }), ) } } /// Params of `git.stashApply`. #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] struct ApplyParams { /// The entry. index: usize, /// Drop the entry after applying it cleanly. #[serde(default)] pop: bool, /// The entry's hash as listed; refuses when the stash changed since. #[serde(default)] hash: Option, } /// `git.stashApply`. struct StashApply; impl Command for StashApply { fn spec(&self) -> &CommandSpec { &CommandSpec { id: "git.stashApply", label: "Git: Apply Stash", menu_path: None, default_shortcut: None, params: "{ index: number (stash@{index}), pop?: boolean (false: keep the entry), hash?: the entry's hash from git.stashList, refused when the entry at index differs }; returns {}; refused with a clear message for a missing entry, unmerged files, local changes that would be overwritten, untracked files in the way, or conflicts (the files then hold conflict markers and the entry is kept)", } } 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: ApplyParams = parse(command, params)?; check_index(command, p.index)?; start_with( state, command, true, Box::new(move |repo| { repo.stash_apply(p.index, p.pop, p.hash.as_deref())?; Ok(json!({})) }), ) } } /// Params of `git.stashDrop`. #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] struct DropParams { /// The entry. index: usize, /// The entry's hash as listed. #[serde(default)] hash: Option, } /// `git.stashDrop`. struct StashDrop; impl Command for StashDrop { fn spec(&self) -> &CommandSpec { &CommandSpec { id: "git.stashDrop", label: "Git: Drop Stash", menu_path: None, default_shortcut: None, params: "{ index: number (stash@{index}), hash?: the entry's hash from git.stashList, refused when the entry at index differs }; deletes the entry; returns {}", } } 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: DropParams = parse(command, params)?; check_index(command, p.index)?; start_with( state, command, true, Box::new(move |repo| { repo.stash_drop(p.index, p.hash.as_deref())?; Ok(json!({})) }), ) } } /// The ids of the commands this module registers, checked against the /// registry by the engine's listing test. Add a new command's id here. #[cfg(test)] pub(crate) const COMMAND_IDS: &[&str] = &[ "git.stash", "git.stashApply", "git.stashDrop", "git.stashList", ];