Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
247 changes: 100 additions & 147 deletions src/commands/config/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,130 +1080,86 @@ fn clear_previous_branch_reported(repo: &Repository) -> anyhow::Result<bool> {
Ok(false)
}

fn clear_markers_reported(repo: &Repository) -> anyhow::Result<bool> {
let cleared = clear_all_markers(repo)?;
if cleared > 0 {
eprintln!(
"{}",
success_message(cformat!(
"Cleared <bold>{cleared}</> marker{}",
if cleared == 1 { "" } else { "s" }
))
);
return Ok(true);
fn report_cleared_count(cleared: usize, singular: &str, plural: &str) -> bool {
if cleared == 0 {
return false;
}
Ok(false)
let noun = if cleared == 1 { singular } else { plural };
eprintln!(
"{}",
success_message(cformat!("Cleared <bold>{cleared}</> {noun}"))
);
true
}

fn clear_markers_reported(repo: &Repository) -> anyhow::Result<bool> {
Ok(report_cleared_count(
clear_all_markers(repo)?,
"marker",
"markers",
))
}

fn clear_ci_status_reported(repo: &Repository) -> anyhow::Result<bool> {
// The PR-number width ratchet is part of the CI cache category — it is
// derived from the same fetches and re-learns on the next one.
let cleared = CachedCiStatus::clear_all(repo)? + MaxPrNumber::clear(repo)?;
if cleared > 0 {
eprintln!(
"{}",
success_message(cformat!(
"Cleared <bold>{cleared}</> CI cache entr{}",
if cleared == 1 { "y" } else { "ies" }
))
);
return Ok(true);
}
Ok(false)
Ok(report_cleared_count(
CachedCiStatus::clear_all(repo)? + MaxPrNumber::clear(repo)?,
"CI cache entry",
"CI cache entries",
))
}

fn clear_summary_reported(repo: &Repository) -> anyhow::Result<bool> {
let cleared = CachedSummary::clear_all(repo)?;
if cleared > 0 {
eprintln!(
"{}",
success_message(cformat!(
"Cleared <bold>{cleared}</> summary cache entr{}",
if cleared == 1 { "y" } else { "ies" }
))
);
return Ok(true);
}
Ok(false)
Ok(report_cleared_count(
CachedSummary::clear_all(repo)?,
"summary cache entry",
"summary cache entries",
))
}

/// Clear all SHA-keyed git command caches: parsed results (merge-tree,
/// ancestry, diff-stats) plus rendered picker previews (log, branch-diff,
/// upstream-diff). Surfaced as one user-facing category — see the parity
/// docstring at the top of this file.
fn clear_git_commands_reported(repo: &Repository) -> anyhow::Result<bool> {
let cleared = sha_cache::clear_all(repo)? + preview_cache::clear_all(repo)?;
if cleared > 0 {
eprintln!(
"{}",
success_message(cformat!(
"Cleared <bold>{cleared}</> git commands cache entr{}",
if cleared == 1 { "y" } else { "ies" }
))
);
return Ok(true);
}
Ok(false)
Ok(report_cleared_count(
sha_cache::clear_all(repo)? + preview_cache::clear_all(repo)?,
"git commands cache entry",
"git commands cache entries",
))
}

fn clear_vars_reported(repo: &Repository) -> anyhow::Result<bool> {
let cleared = clear_all_vars(repo)?;
if cleared > 0 {
eprintln!(
"{}",
success_message(cformat!(
"Cleared <bold>{cleared}</> variable{}",
if cleared == 1 { "" } else { "s" }
))
);
return Ok(true);
}
Ok(false)
Ok(report_cleared_count(
clear_all_vars(repo)?,
"variable",
"variables",
))
}

fn clear_logs_reported(repo: &Repository) -> anyhow::Result<bool> {
let cleared = clear_logs(repo)?;
if cleared > 0 {
eprintln!(
"{}",
success_message(cformat!(
"Cleared <bold>{cleared}</> log file{}",
if cleared == 1 { "" } else { "s" }
))
);
return Ok(true);
}
Ok(false)
Ok(report_cleared_count(
clear_logs(repo)?,
"log file",
"log files",
))
}

fn clear_hints_reported(repo: &Repository) -> anyhow::Result<bool> {
let cleared = repo.clear_all_hints()?;
if cleared > 0 {
eprintln!(
"{}",
success_message(cformat!(
"Cleared <bold>{cleared}</> hint{}",
if cleared == 1 { "" } else { "s" }
))
);
return Ok(true);
}
Ok(false)
Ok(report_cleared_count(
repo.clear_all_hints()?,
"hint",
"hints",
))
}

fn clear_trash_reported(repo: &Repository) -> anyhow::Result<bool> {
let cleared = clear_trash(repo)?;
if cleared > 0 {
eprintln!(
"{}",
success_message(cformat!(
"Cleared <bold>{cleared}</> trash entr{}",
if cleared == 1 { "y" } else { "ies" }
))
);
return Ok(true);
}
Ok(false)
Ok(report_cleared_count(
clear_trash(repo)?,
"trash entry",
"trash entries",
))
}

// ==================== State Show Commands ====================
Expand Down Expand Up @@ -1719,49 +1675,50 @@ pub fn handle_vars_clear(
None => repo.require_current_branch("clear variable for current branch")?,
};

if !all && key.is_none() {
anyhow::bail!("Specify a key to clear, or use --all to clear all keys");
}

if all {
let entries: Vec<_> = repo.vars_entries(&branch_name).into_iter().collect();
if entries.is_empty() {
eprintln!(
"{}",
info_message(cformat!("No variables for <bold>{branch_name}</>"))
);
} else {
let count = entries.len();
for (key, _) in entries {
let config_key = format!("worktrunk.state.{branch_name}.vars.{key}");
repo.unset_config(&config_key)?;
match (all, key) {
(false, None) => {
anyhow::bail!("Specify a key to clear, or use --all to clear all keys");
}
(true, _) => {
let entries: Vec<_> = repo.vars_entries(&branch_name).into_iter().collect();
if entries.is_empty() {
eprintln!(
"{}",
info_message(cformat!("No variables for <bold>{branch_name}</>"))
);
} else {
let count = entries.len();
for (key, _) in entries {
let config_key = format!("worktrunk.state.{branch_name}.vars.{key}");
repo.unset_config(&config_key)?;
}
eprintln!(
"{}",
success_message(cformat!(
"Cleared <bold>{count}</> variable{} for <bold>{branch_name}</>",
if count == 1 { "" } else { "s" }
))
);
}
eprintln!(
"{}",
success_message(cformat!(
"Cleared <bold>{count}</> variable{} for <bold>{branch_name}</>",
if count == 1 { "" } else { "s" }
))
);
}
} else {
let key = key.expect("key required when --all not set");
validate_vars_key(key)?;
let config_key = format!("worktrunk.state.{branch_name}.vars.{key}");
if repo.unset_config(&config_key)? {
eprintln!(
"{}",
success_message(cformat!(
"Cleared <bold>{key}</> for <bold>{branch_name}</>"
))
);
} else {
eprintln!(
"{}",
info_message(cformat!(
"No variable <bold>{key}</> for <bold>{branch_name}</>"
))
);
(false, Some(key)) => {
validate_vars_key(key)?;
let config_key = format!("worktrunk.state.{branch_name}.vars.{key}");
if repo.unset_config(&config_key)? {
eprintln!(
"{}",
success_message(cformat!(
"Cleared <bold>{key}</> for <bold>{branch_name}</>"
))
);
} else {
eprintln!(
"{}",
info_message(cformat!(
"No variable <bold>{key}</> for <bold>{branch_name}</>"
))
);
}
}
}
Ok(())
Expand All @@ -1775,7 +1732,11 @@ pub fn handle_vars_clear(
/// `unset_config` call propagate errors so user-initiated clears never lie
/// about success.
fn clear_all_markers(repo: &Repository) -> anyhow::Result<usize> {
let output = repo.get_config_regexp(r"^worktrunk\.state\..+\.marker$")?;
clear_matching_config(repo, r"^worktrunk\.state\..+\.marker$")
}

fn clear_matching_config(repo: &Repository, pattern: &str) -> anyhow::Result<usize> {
let output = repo.get_config_regexp(pattern)?;
let mut cleared = 0;
for line in output.lines() {
if let Some(config_key) = line.split_whitespace().next() {
Expand All @@ -1792,15 +1753,7 @@ fn clear_all_markers(repo: &Repository) -> anyhow::Result<usize> {
/// config read failure surfaces as an error — the display-path helper
/// absorbs errors as empty, which would silently report "cleared 0" here.
fn clear_all_vars(repo: &Repository) -> anyhow::Result<usize> {
let output = repo.get_config_regexp(r"^worktrunk\.state\..+\.vars\.")?;
let mut cleared = 0;
for line in output.lines() {
if let Some(config_key) = line.split_whitespace().next() {
repo.unset_config(config_key)?;
cleared += 1;
}
}
Ok(cleared)
clear_matching_config(repo, r"^worktrunk\.state\..+\.vars\.")
}

// ==================== Marker Helpers ====================
Expand Down
Loading
Loading