feat(maestro): shared unsaved-work probe for worktrees (#79)
One function, unsaved_work(), returns the structured breakdown — uncommitted tracked changes, untracked non-ignored files, unpushed commits — so the kill guard and the natural-exit classifier (#82) answer from the same place. Ignored files never count: every Rust checkout has a target/, and counting those would leave no session killable. WorktreeManager::remove drops its own dirty-tree guard; git itself still refuses a dirty tree, and the real guard moves onto the kill.
This commit is contained in:
@@ -143,6 +143,62 @@ pub struct SessionWorktree {
|
||||
pub base_repo: PathBuf,
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Unsaved-work breakdown (#79)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// What a worktree holds that would be lost if its directory were destroyed.
|
||||
///
|
||||
/// The kill guard refuses `DELETE /api/sessions/:id` with this breakdown, and
|
||||
/// natural exits (#82) classify with the same daemon-side probe, so the two
|
||||
/// paths cannot drift. Ignored files never count: every Rust checkout has a
|
||||
/// `target/`, and counting those would leave no session killable.
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct UnsavedWork {
|
||||
/// Tracked files with uncommitted modifications or staged changes.
|
||||
#[serde(default)]
|
||||
pub dirty_tracked_files: u32,
|
||||
/// Untracked files that are not ignored.
|
||||
#[serde(default)]
|
||||
pub untracked_files: u32,
|
||||
/// Commits on the branch reachable from no remote-tracking ref.
|
||||
#[serde(default)]
|
||||
pub unpushed_commits: u32,
|
||||
}
|
||||
|
||||
impl UnsavedWork {
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.dirty_tracked_files == 0 && self.untracked_files == 0 && self.unpushed_commits == 0
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for UnsavedWork {
|
||||
/// "3 uncommitted files, 2 untracked files, 2 unpushed commits" — zero
|
||||
/// parts are omitted; all-zero renders "no unsaved work".
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let parts = [
|
||||
(self.dirty_tracked_files, "uncommitted file"),
|
||||
(self.untracked_files, "untracked file"),
|
||||
(self.unpushed_commits, "unpushed commit"),
|
||||
];
|
||||
let said: Vec<String> = parts
|
||||
.into_iter()
|
||||
.filter(|(n, _)| *n > 0)
|
||||
.map(|(n, unit)| {
|
||||
if n == 1 {
|
||||
format!("1 {unit}")
|
||||
} else {
|
||||
format!("{n} {unit}s")
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
if said.is_empty() {
|
||||
return write!(f, "no unsaved work");
|
||||
}
|
||||
write!(f, "{}", said.join(", "))
|
||||
}
|
||||
}
|
||||
|
||||
/// HTTP `POST /api/sessions` body.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct CreateSessionRequest {
|
||||
@@ -248,6 +304,10 @@ pub struct BranchInfo {
|
||||
pub struct ErrorInfo {
|
||||
pub code: String,
|
||||
pub message: String,
|
||||
/// Set only by the kill guard (#79): the unsaved work that blocked
|
||||
/// `DELETE /api/sessions/:id`, so a client can render counts directly.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub unsaved_work: Option<UnsavedWork>,
|
||||
}
|
||||
|
||||
/// The envelope every `/api/*` failure is wrapped in: `{"error": {…}}`.
|
||||
@@ -268,6 +328,7 @@ impl ApiError {
|
||||
error: ErrorInfo {
|
||||
code: code.into(),
|
||||
message: message.into(),
|
||||
unsaved_work: None,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -578,6 +639,7 @@ mod tests {
|
||||
ClientControl::Error(ErrorInfo {
|
||||
code: "bad_frame".into(),
|
||||
message: "decode failed".into(),
|
||||
unsaved_work: None,
|
||||
}),
|
||||
resize,
|
||||
] {
|
||||
|
||||
Reference in New Issue
Block a user