(74904dc) Let a ticket field be cleared back to unset, and bump to 0.18.0

`set --clear <field>` is the one way to take a value away, repeatable, with
the field as a `ValueEnum` value: an unknown one is a usage error listing the
six that are not, and the shells complete them for free. Not a per-field
`--clear-due`, which is a flag per field to keep in step with the one it
undoes, and not an empty value, which is already a paragraph break for a
section and would make an accidentally empty variable a silent delete.

What goes back is what `add` writes for a field it was never given, through
the new `NOT_SET` and `NONE` constants and each section's placeholder, so a
cleared ticket and one filed without the field read the same. Sections are in
scope and go back through `clear_section_body` rather than
`set_section_body`: the latter runs its items through `Section::render`, which
would have written `- [ ] _No acceptance criteria provided._`.

Owner is `chown --clear <ticket>`, so the field keeps its one writer. The flag
carries the ticket because the ticket is chown's second positional and a bare
flag would land it in the user's slot for the handler to read back out, which
is hand-parsing what clap can state. It writes `_Not set_` rather than the git
user: `add` falls back to the git user because it is filing something new, and
an owner taken away says nobody owns this.

The empty-value error split in two rather than being reworded. `non_empty`
says only that the value cannot be empty, for the arguments with no empty
form; `clearable` names `--clear`, for the arguments whose field has one. One
message for both would have lied about `ls --tag`.

Replacing and clearing one field in a single call is refused rather than
resolved by whichever was applied last, and cleared fields are reported as
`Cleared due on '...'` beside `Updated priority on '...'`, since one list
would say `Updated` of a field that was emptied.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ansho Rei
2026-08-23 00:04:28 +02:00
co-authored by Claude Opus 5
parent 175c1d016d
commit cc5f9b8a46
15 changed files with 810 additions and 92 deletions
+69 -24
View File
@@ -6,7 +6,7 @@ use std::path::Path;
use crate::campaign::*;
use crate::command::{
AddArgs, AppendArgs, CampaignAddArgs, CheckArgs, LsArgs, RelateArgs, SetArgs,
AddArgs, AppendArgs, CampaignAddArgs, CheckArgs, ChownArgs, LsArgs, RelateArgs, SetArgs,
};
use crate::install;
use crate::util::*;
@@ -805,6 +805,19 @@ fn save_ticket(
Ok(())
}
/// Take an edit, or record that it changed nothing.
///
/// Each edit is compared against the content going in, so a field set to the value it
/// already holds is reported as unchanged rather than claimed as an update. That is also
/// what makes clearing an already unset field free: the sentinel it writes is the one
/// already there, so the strings match and nothing is written.
fn apply(content: &mut String, changed: &mut Vec<&'static str>, field: &'static str, next: String) {
if *content != next {
content.clone_from(&next);
changed.push(field);
}
}
fn report(fields: &[&str], title: &str, verb: &str) {
if fields.is_empty() {
println!("No changes to '{}'", color_text(title, Color::Green));
@@ -820,25 +833,24 @@ fn report(fields: &[&str], title: &str, verb: &str) {
}
pub fn cmd_set(args: &SetArgs, start: &Path) -> Result<()> {
if let Some(field) = args.contested() {
let name = field.name();
return Err(anyhow!(
"--{name} and --clear {name} contradict each other, so nothing was written"
));
}
let worklane_dir = find_worklane_dir(start)?;
let (status, title, original) = resolve_ticket(&args.ticket, &worklane_dir)?;
let uuid = parse_ticket_field(&original, "ID");
let mut content = original.clone();
let mut changed: Vec<&str> = Vec::new();
// Each edit is compared against the content going in, so a field set to the value
// it already holds is reported as unchanged rather than claimed as an update.
let mut apply = |content: &mut String, field: &'static str, next: String| {
if *content != next {
content.clone_from(&next);
changed.push(field);
}
};
let mut cleared: Vec<&str> = Vec::new();
if let Some(new_title) = &args.title {
let next = set_title(&content, new_title)?;
apply(&mut content, "title", next);
apply(&mut content, &mut changed, "title", next);
}
for (section, values) in section_args(&args.description, &args.acceptance, &args.plan) {
@@ -849,32 +861,46 @@ pub fn cmd_set(args: &SetArgs, start: &Path) -> Result<()> {
let items = section.normalise(values);
if items.is_empty() {
return Err(anyhow!(
"An empty {} is not a way to clear the section, and clearing is not supported yet",
section.label
"An empty value is a paragraph break, not a way to empty the {}: use --clear {}",
section.label,
section.clear.map_or("", Clearable::name)
));
}
let next = set_section_body(&content, section, &items)?;
apply(&mut content, section.label, next);
apply(&mut content, &mut changed, section.label, next);
}
if !args.tags.is_empty() {
let next = set_metadata_field(&content, "Tags", &args.tags.join(", "), &title)?;
apply(&mut content, "tags", next);
apply(&mut content, &mut changed, "tags", next);
}
if let Some(due) = &args.due {
let next = set_metadata_field(&content, "Due", due, &title)?;
apply(&mut content, "due", next);
apply(&mut content, &mut changed, "due", next);
}
if let Some(priority) = args.priority {
let next = set_metadata_field(&content, "Priority", &priority.to_string(), &title)?;
apply(&mut content, "priority", next);
apply(&mut content, &mut changed, "priority", next);
}
if changed.is_empty() {
report(&changed, &title, "Updated");
// Last, and into a list of their own. No field can be in both, since `contested`
// refused that above, so the order is free and the report is what decides it: a
// caller is told what was replaced and what was emptied in those words.
for field in &args.clear {
let next = match field.unset() {
Unset::Field { field, sentinel } => {
set_metadata_field(&content, field, sentinel, &title)?
}
Unset::Section(section) => clear_section_body(&content, section)?,
};
apply(&mut content, &mut cleared, field.label(), next);
}
if changed.is_empty() && cleared.is_empty() {
report(&[], &title, "Updated");
return Ok(());
}
@@ -882,7 +908,12 @@ pub fn cmd_set(args: &SetArgs, start: &Path) -> Result<()> {
save_ticket(&worklane_dir, &status, &uuid, &title, renamed, &content)?;
let shown = args.title.clone().unwrap_or(title);
report(&changed, &shown, "Updated");
if !changed.is_empty() {
report(&changed, &shown, "Updated");
}
if !cleared.is_empty() {
report(&cleared, &shown, "Cleared");
}
Ok(())
}
@@ -895,13 +926,23 @@ pub fn cmd_set(args: &SetArgs, start: &Path) -> Result<()> {
/// stay silent on the one it cannot, a ticket whose title is the username.
///
/// No-op detection compares the whole file, the way `set` does, so an owner set to the
/// value it already holds is reported unchanged and nothing is written.
pub fn cmd_chown(user: &str, ticket_id: &str, start: &Path) -> Result<()> {
/// value it already holds is reported unchanged and nothing is written. The same
/// comparison is what makes clearing an already unset owner free.
///
/// **Clearing writes [`NOT_SET`], not the git user.** `add` falls back to the git user
/// for a ticket it is filing, which is a default for something new; an owner taken away
/// says nobody owns this, and putting the person running the command there instead would
/// hand them a ticket they were giving up.
pub fn cmd_chown(args: &ChownArgs, start: &Path) -> Result<()> {
let (ticket_id, user) = args.target().ok_or_else(|| {
anyhow!("chown takes a user and a ticket, or --clear with the ticket alone")
})?;
let worklane_dir = find_worklane_dir(start)?;
let (status, title, original) = resolve_ticket(ticket_id, &worklane_dir)?;
let uuid = parse_ticket_field(&original, "ID");
let content = set_metadata_field(&original, "Owner", user, &title)?;
let content = set_metadata_field(&original, "Owner", user.unwrap_or(NOT_SET), &title)?;
if content == original {
report(&[], &title, "Updated");
@@ -910,7 +951,11 @@ pub fn cmd_chown(user: &str, ticket_id: &str, start: &Path) -> Result<()> {
save_ticket(&worklane_dir, &status, &uuid, &title, None, &content)?;
report(&["owner"], &title, "Updated");
let verb = match user {
Some(_) => "Updated",
None => "Cleared",
};
report(&["owner"], &title, verb);
Ok(())
}