(8556048) Take a relation off with relate --clear, and bump to 0.32.0
`relate` only added. A `--blocked-by` pointed at the wrong ticket was on that ticket forever, refusing every move it made until the target was done or `-f` was passed again, and a target that was deleted did not clean up either: the board carried a dead edge that nothing could clear and every later read of the ticket paid for. `--clear <kind> <target>` takes one off. It is on `relate` rather than on `set` for the reason `RELATIONS` is out of `section_args`: a relation names a target that has to be resolved first, so one writer owns both directions, the way `chown` owns setting an owner and clearing one. It names the target and not just the kind, because a ticket can hold several relations of one kind and emptying all of them is not what anybody is asking for. That is also why it is not spelled the way `set --clear` is, which names a field and empties it whole. Both spellings of the kind are taken, `blocked-by` from the flag and `blocked by` from the label: both are in front of the reader, the flag being what recorded the relation and the label what `show` prints and the file stores, so refusing the printed form would trap anyone clearing a row they had just read. `RelationKind::flag` is a match rather than a `replace` on the label, so the flag, the `--clear` value and the completion list cannot drift. `find_relation` searches the ticket's own edges by id prefix before it searches the board, and that order is the feature rather than an optimisation: no search of the board reaches a target whose ticket is gone, so the stored-target tier is the only thing that can clear a dangling edge, which is the case most likely to need clearing. Two stored targets under one prefix name the candidates instead of guessing, as an ambiguous ticket search already does. An edge the ticket does not hold reports no change, and a target naming nothing at all is an error. The two are different facts: the first is the state the caller asked for, and the second is a typo that a no-op would swallow. The resolver's own reason is carried through, so an ambiguous search says it is ambiguous rather than being reported as nothing found. Recording and clearing the same relation in one call is refused, and the check runs on what was *asked* for and not on what would be written. A relation already in the file is not among the additions, so comparing against those would have let `--blocked-by X --clear blocked-by X` quietly remove it. Removals rewrite the section from what is left and additions are appended to that, in that order and in two passes: a call with no `--clear` then takes exactly the path it always took. The last edge leaves the placeholder, which is the line `append` treats as no content, so the next `relate` replaces it rather than writing underneath it. The two values are one flag, so clap cannot type them apart. `num_args = 2` is per occurrence, which makes an odd number a usage error and every chunk a full pair, and `cleared` parses the kind before the ticket is resolved. Nothing is logged, in either direction, since `relate` never has: a log recording only the destructive half would read as though the additions had never happened. `chowns_clear_is_completed_with_tickets_and_sets_is_not` found the first rule carrying `-l clear` and there are now three, on three commands, each taking something different. It is `each_clear_flag_is_completed_for_its_own_command`, scoped per command, and `relate --clear` offers the kinds rather than a board listing, since tickets where the kind goes would be candidates it refuses. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
7994d98913
commit
0f0767ac00
+146
-5
@@ -1105,16 +1105,97 @@ pub fn cmd_append(args: &AppendArgs, start: &Path) -> Result<()> {
|
||||
/// The kinds are a flag each rather than a `--kind` value, so an unknown kind is a
|
||||
/// usage error at the door instead of a line written into the file and refused on the
|
||||
/// way back out.
|
||||
/// The stored relation a `--clear` names, or `None` when the ticket does not hold it.
|
||||
///
|
||||
/// Two tiers, in this order. A uuid prefix against the targets this ticket already
|
||||
/// stores, which is the only tier that can reach a dangling edge: the target's ticket is
|
||||
/// gone, so no search of the board resolves it, and that is the case most likely to need
|
||||
/// clearing. Then the ordinary ticket search, so a title or a prefix of a live ticket
|
||||
/// works here the way it does in every other argument.
|
||||
///
|
||||
/// The prefix tier is the ticket's own relations and not the board, so it stays cheap and
|
||||
/// cannot be made ambiguous by a ticket somewhere else. `MIN_ID_SEARCH` is the same floor
|
||||
/// `resolve_in` puts under its id tier, and the comparison is case insensitive for the
|
||||
/// same reason: an id typed back off a listing has to resolve in whatever case it was
|
||||
/// read.
|
||||
///
|
||||
/// A search that reaches neither tier is an error, not a no-op. A relation the ticket does
|
||||
/// not hold reports no change, which is what the caller asked for either way, but a search
|
||||
/// naming nothing at all is a typo and reporting no change for it would swallow it.
|
||||
fn find_relation(
|
||||
existing: &[Relation],
|
||||
kind: RelationKind,
|
||||
search: &str,
|
||||
title: &str,
|
||||
worklane_dir: &Path,
|
||||
) -> Result<Option<Relation>> {
|
||||
let held: Vec<&Relation> = existing.iter().filter(|held| held.kind == kind).collect();
|
||||
let wanted = search.to_lowercase();
|
||||
|
||||
let by_prefix: Vec<&Relation> = held
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|held| {
|
||||
search.chars().count() >= MIN_ID_SEARCH
|
||||
&& held.target.to_lowercase().starts_with(&wanted)
|
||||
})
|
||||
.collect();
|
||||
|
||||
match by_prefix.as_slice() {
|
||||
[only] => return Ok(Some((*only).clone())),
|
||||
// Named rather than guessed at, the way an ambiguous ticket search is: the
|
||||
// candidates are printed and nothing is written.
|
||||
[_, _, ..] => {
|
||||
return Err(anyhow!(
|
||||
"'{}' matches {} '{}' relations on '{}': {}. Name more of the id",
|
||||
search,
|
||||
by_prefix.len(),
|
||||
kind.label(),
|
||||
title,
|
||||
by_prefix
|
||||
.iter()
|
||||
.map(|held| short_id(&held.target))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
));
|
||||
}
|
||||
[] => {}
|
||||
}
|
||||
|
||||
match resolve_ticket(search, worklane_dir) {
|
||||
Ok((_status, _target_title, target)) => {
|
||||
let target_uuid = parse_ticket_field(&target, "ID");
|
||||
Ok(held.into_iter().find(|held| held.target == target_uuid).cloned())
|
||||
}
|
||||
// The resolver's own reason is carried through, so an ambiguous search says it is
|
||||
// ambiguous rather than being reported as nothing at all.
|
||||
Err(reason) => Err(anyhow!(
|
||||
"No '{}' relation on '{}' matches '{}': {}",
|
||||
kind.label(),
|
||||
title,
|
||||
search,
|
||||
reason
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cmd_relate(args: &RelateArgs, start: &Path) -> Result<()> {
|
||||
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 existing = parse_relations(&original, &title)?;
|
||||
|
||||
// Every target resolved and every refusal raised before a byte is written, so a
|
||||
// call naming one good ticket and one bad one records neither.
|
||||
// Every target resolved, every kind parsed and every refusal raised before a byte is
|
||||
// written, so a call naming one good ticket and one bad one records neither.
|
||||
let asked = args.cleared()?;
|
||||
|
||||
let mut items: Vec<String> = Vec::new();
|
||||
let mut changed: Vec<&str> = Vec::new();
|
||||
// Every addition the call asked for, whether or not it is one to write. `items` drops
|
||||
// the ones already held, and a contradiction has to be caught on what was *asked*: a
|
||||
// relation already in the file is not in `items`, so comparing against that would let
|
||||
// `--blocked-by X --clear blocked-by X` quietly remove it.
|
||||
let mut requested: Vec<Relation> = Vec::new();
|
||||
|
||||
for (kind, targets) in args.requested() {
|
||||
for search in targets {
|
||||
@@ -1136,6 +1217,7 @@ pub fn cmd_relate(args: &RelateArgs, start: &Path) -> Result<()> {
|
||||
}
|
||||
|
||||
let relation = Relation { kind, target: target_uuid };
|
||||
requested.push(relation.clone());
|
||||
|
||||
// Already held, either in the file or earlier in this call. Recording it
|
||||
// twice would read as two dependencies on one ticket.
|
||||
@@ -1150,15 +1232,74 @@ pub fn cmd_relate(args: &RelateArgs, start: &Path) -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
if items.is_empty() {
|
||||
let mut dropped: Vec<Relation> = Vec::new();
|
||||
let mut cleared: Vec<&str> = Vec::new();
|
||||
|
||||
for (kind, search) in asked {
|
||||
let Some(relation) = find_relation(&existing, kind, search, &title, &worklane_dir)? else {
|
||||
continue;
|
||||
};
|
||||
|
||||
// Both directions in one call, the way `set --clear` refuses a field it is also
|
||||
// replacing: nothing is written rather than one of the two silently winning.
|
||||
if requested.contains(&relation) {
|
||||
return Err(anyhow!(
|
||||
"--{} and --clear {} name the same relation to '{}', so nothing was written",
|
||||
kind.flag(),
|
||||
kind.flag(),
|
||||
short_id(&relation.target)
|
||||
));
|
||||
}
|
||||
|
||||
if dropped.contains(&relation) {
|
||||
continue;
|
||||
}
|
||||
|
||||
dropped.push(relation);
|
||||
if !cleared.contains(&kind.label()) {
|
||||
cleared.push(kind.label());
|
||||
}
|
||||
}
|
||||
|
||||
if items.is_empty() && dropped.is_empty() {
|
||||
report(&[], &title, "Recorded");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let content = append_section_body(&ensure_section(&original, &RELATIONS), &RELATIONS, &items)?;
|
||||
// Removals first, by rewriting the section from what is left, then additions appended
|
||||
// to that. Two passes rather than one rebuild of the whole section: a call with no
|
||||
// `--clear` then takes exactly the path it always took, and `append_section_body` goes
|
||||
// on deciding where a new line lands.
|
||||
let mut content = original.clone();
|
||||
|
||||
if !dropped.is_empty() {
|
||||
let kept: Vec<String> = existing
|
||||
.iter()
|
||||
.filter(|held| !dropped.contains(held))
|
||||
.map(|held| held.item())
|
||||
.collect();
|
||||
|
||||
// The placeholder rather than an empty body when the last edge goes, which is the
|
||||
// line `add` writes and the line `append` treats as no content, so the section can
|
||||
// be written to again without it being left above what follows.
|
||||
content = match kept.is_empty() {
|
||||
true => clear_section_body(&content, &RELATIONS)?,
|
||||
false => set_section_body(&content, &RELATIONS, &kept)?,
|
||||
};
|
||||
}
|
||||
|
||||
if !items.is_empty() {
|
||||
content = append_section_body(&ensure_section(&content, &RELATIONS), &RELATIONS, &items)?;
|
||||
}
|
||||
|
||||
save_ticket(&worklane_dir, &status, &uuid, &title, None, &content)?;
|
||||
|
||||
report(&changed, &title, "Recorded");
|
||||
if !changed.is_empty() {
|
||||
report(&changed, &title, "Recorded");
|
||||
}
|
||||
if !cleared.is_empty() {
|
||||
report(&cleared, &title, "Cleared");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user