M4.9: Git stash (stash, list, apply, pop, drop) with clear refusals
- crates/vcs: stash.rs (append) with Repo::stash, stash_list, stash_apply
and stash_drop; refusals decided structurally (no commit, nothing to
stash, unmerged files, conflicts after apply, missing or changed entry)
plus hints for overwritten and untracked files. Works around git leaving
untracked files behind under --literal-pathspecs. Repo::lock is
pub(crate), as for M4.8.
- engine: git.stash, git.stashList, git.stashApply {index, pop, hash?},
git.stashDrop through the M4.6 hook; gitChanged after changes.
- ui: stash dialog (vcs.stash, vcs.showStashes; Git menu, no shortcuts),
two buttons in the Changes window, dirty buffers saved first, drop asks.
- Tests: vcs integration and property tests, engine test, 13 Vitest tests.
- Docs: architecture section, G8, tooling checklist, roadmap, Git row
evidence (score left to the PM).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
65907d925a
commit
e09ef8b8c0
23 changed files with 2681 additions and 13 deletions
221
crates/engine/src/cmd/git_stash.rs
Normal file
221
crates/engine/src/cmd/git_stash.rs
Normal file
|
|
@ -0,0 +1,221 @@
|
|||
//! 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<String>,
|
||||
/// 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 <branch>\" 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<Outcome, EngineError> {
|
||||
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<Outcome, EngineError> {
|
||||
let command = self.spec().id;
|
||||
let _: serde_json::Map<String, Value> = parse(command, params)?;
|
||||
start_with(
|
||||
state,
|
||||
command,
|
||||
false,
|
||||
Box::new(move |repo| {
|
||||
let stashes: Vec<Value> = 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<String>,
|
||||
}
|
||||
|
||||
/// `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<Outcome, EngineError> {
|
||||
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<String>,
|
||||
}
|
||||
|
||||
/// `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<Outcome, EngineError> {
|
||||
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!({}))
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue