1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! `memstead review-mark` — read and move the per-mem review mark.
//!
//! The mark is the engine's one pointer per mem to the last
//! human-approved state (mem-repo state: every consumer of the mem
//! sees the same mark). `list` answers "what has un-reviewed
//! changes"; `set` moves a mem's mark to an explicitly named state
//! (never implicitly "now" — writers may have advanced the mem
//! mid-review); `clear` returns a mem to the ordinary markless
//! state; `diff` reports the accumulated per-entity delta from the
//! mark to the current head, in the same change-envelope shape
//! `memstead changes` uses. Marks never gate writes — ignoring them
//! entirely is a first-class state.
use clap::{Args as ClapArgs, Parser, Subcommand};
use crate::CliError;
use crate::output::{print_json, print_markdown};
use crate::setup::CliContext;
#[derive(Parser, Debug)]
pub struct Args {
#[command(subcommand)]
pub action: ReviewMarkAction,
}
#[derive(Subcommand, Debug)]
pub enum ReviewMarkAction {
/// Every mem's mark (or its absence) alongside the current head.
/// A mem with no mark is an ordinary state, not a warning.
List,
/// Set a mem's mark to an explicitly named state — the state the
/// review actually covered. The value is a backend cursor: a
/// commit SHA for git-branch mems, an RFC 3339 timestamp for
/// folder mems (the same cursor `memstead changes --since`
/// consumes; `list` shows each mem's current head in that
/// vocabulary). An invalid cursor refuses with `INVALID_CURSOR`
/// and leaves the mark untouched.
Set(SetArgs),
/// Clear a mem's mark, returning it to the markless state.
Clear(ClearArgs),
/// The accumulated per-entity delta from the mem's mark to its
/// current head. Refuses with `REVIEW_MARK_NOT_SET` on a markless
/// mem — marklessness is visible in `list`, never silently
/// equated with "no changes".
Diff(DiffArgs),
}
#[derive(ClapArgs, Debug)]
pub struct SetArgs {
/// Writable mem name.
pub mem: String,
/// The reviewed state (backend cursor — see `set --help`).
pub state: String,
/// Provenance note (≤280 chars). Under `[mutations].require_notes`
/// a missing note adds a `NOTE_MISSING` warning; the write still
/// commits (warn-and-commit, like every note-gated mutation).
#[arg(long)]
pub note: Option<String>,
}
#[derive(ClapArgs, Debug)]
pub struct ClearArgs {
/// Writable mem name.
pub mem: String,
/// Provenance note (≤280 chars).
#[arg(long)]
pub note: Option<String>,
}
#[derive(ClapArgs, Debug)]
pub struct DiffArgs {
/// Mem name.
pub mem: String,
/// Rename detection threshold in [0.1, 1.0]; mirrors
/// `memstead changes --rename-similarity`. Git-branch mems only —
/// folder mems have no rename detection (renames surface as
/// updates).
#[arg(long)]
pub rename_similarity: Option<f32>,
}
pub fn run(ctx: &CliContext, args: Args) -> anyhow::Result<()> {
let mut engine = ctx.cli_engine()?.into_base();
match args.action {
ReviewMarkAction::List => {
let marks = engine.review_marks();
if ctx.json {
print_json(&serde_json::json!({ "marks": marks }))?;
return Ok(());
}
let mut lines = vec!["# Review marks".to_string(), String::new()];
if marks.is_empty() {
lines.push("_no mems loaded_".to_string());
}
for s in &marks {
let head = s.head.as_deref().unwrap_or("(no head)");
let line = match s.mark.as_deref() {
None => format!("- `{}` — no mark (head `{head}`)", s.mem),
Some(mark) if Some(mark) == s.head.as_deref() => {
format!("- `{}` — mark `{mark}` at head (nothing unreviewed)", s.mem)
}
Some(mark) => format!(
"- `{}` — mark `{mark}`, head `{head}` (unreviewed changes — `memstead review-mark diff {}`)",
s.mem, s.mem
),
};
lines.push(line);
}
print_markdown(&lines.join("\n"));
Ok(())
}
ReviewMarkAction::Set(a) => {
let outcome = engine
.set_review_mark(&a.mem, Some(&a.state), a.note.as_deref())
.map_err(CliError::from_engine_op)?;
report_outcome(ctx, &outcome, "set")
}
ReviewMarkAction::Clear(a) => {
let outcome = engine
.set_review_mark(&a.mem, None, a.note.as_deref())
.map_err(CliError::from_engine_op)?;
report_outcome(ctx, &outcome, "cleared")
}
ReviewMarkAction::Diff(a) => {
let report = engine
.review_mark_diff(&a.mem, a.rename_similarity)
.map_err(CliError::from_engine_op)?;
if ctx.json {
print_json(&report)?;
return Ok(());
}
let mut lines = vec![
format!(
"# Unreviewed changes in `{}` since mark `{}`",
report.mem, report.since
),
String::new(),
format!("- HEAD: `{}`", report.head),
format!("- Changes: {}", report.changes.len()),
String::new(),
];
if report.changes.is_empty() {
lines.push("_head is at the mark — nothing unreviewed_".to_string());
} else {
for change in &report.changes {
use memstead_base::ChangeEnvelope::*;
let type_suffix = |t: &Option<String>| {
t.as_ref().map(|s| format!(" [{s}]")).unwrap_or_default()
};
let title_suffix = |t: &Option<String>| {
t.as_ref().map(|s| format!(" — {s}")).unwrap_or_default()
};
lines.push(match change {
Added {
id,
title,
entity_type,
} => format!(
"- **added** `{id}`{}{}",
type_suffix(entity_type),
title_suffix(title)
),
Updated {
id,
title,
entity_type,
} => format!(
"- **updated** `{id}`{}{}",
type_suffix(entity_type),
title_suffix(title)
),
Removed {
id,
title,
entity_type,
} => format!(
"- **removed** `{id}`{}{}",
type_suffix(entity_type),
title_suffix(title)
),
Renamed {
from_id,
to_id,
title,
entity_type,
} => format!(
"- **renamed** `{from_id}` → `{to_id}`{}{}",
type_suffix(entity_type),
title_suffix(title)
),
});
}
}
print_markdown(&lines.join("\n"));
Ok(())
}
}
}
fn report_outcome(
ctx: &CliContext,
outcome: &memstead_base::SetReviewMarkOutcome,
verb: &str,
) -> anyhow::Result<()> {
if ctx.json {
print_json(outcome)?;
return Ok(());
}
let mut lines = vec![match outcome.mark.as_deref() {
Some(mark) => format!("Review mark {verb} on `{}`: `{mark}`", outcome.mem),
None => format!("Review mark {verb} on `{}`", outcome.mem),
}];
if let Some(prev) = outcome.previous.as_deref() {
lines.push(format!("- previous: `{prev}`"));
}
for w in &outcome.warnings {
lines.push(format!("- warning: {w}"));
}
print_markdown(&lines.join("\n"));
Ok(())
}