Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix(state): keep a var named `marker` when clearing all markers (#4528)

Worktrunk Boteda83296a8f1
diff --git a/src/commands/config/state.rs b/src/commands/config/state.rs
index dba0660ed..7881ecdd8 100644
--- a/src/commands/config/state.rs
+++ b/src/commands/config/state.rs
@@ -1783,8 +1783,27 @@ pub fn handle_vars_clear(
/// It returns `Err` for real config errors. Both the listing step and each
/// `unset_config` call propagate errors so user-initiated clears never report
/// false success.
+///
+/// The marker pattern also matches a var named `marker`
+/// (`worktrunk.state.<branch>.vars.marker`). Such a key is skipped unless its
+/// value is marker JSON, in which case it is the marker of a branch whose name
+/// ends in `.vars`.
fn clear_all_markers(repo: &Repository) -> anyhow::Result<usize> {
- clear_matching_config(repo, r"^worktrunk\.state\..+\.marker$")
+ let entries = repo.config_regexp_entries(r"^worktrunk\.state\..+\.marker$")?;
+ let mut cleared = 0;
+ for (config_key, value) in entries {
+ if config_key.ends_with(".vars.marker") && !is_marker_json(&value) {
+ continue;
+ }
+ repo.unset_config(&config_key)?;
+ cleared += 1;
+ }
+ Ok(cleared)
+}
+
+fn is_marker_json(value: &str) -> bool {
+ serde_json::from_str::<serde_json::Value>(value)
+ .is_ok_and(|parsed| parsed.get("marker").is_some_and(|v| v.is_string()))
}
fn clear_matching_config(repo: &Repository, pattern: &str) -> anyhow::Result<usize> {
diff --git a/tests/integration_tests/config_state.rs b/tests/integration_tests/config_state.rs
index 7e273ce7f..8fc569736 100644
--- a/tests/integration_tests/config_state.rs
+++ b/tests/integration_tests/config_state.rs
@@ -1087,6 +1087,32 @@ fn test_state_clear_marker_all(repo: TestRepo) {
assert_eq!(String::from_utf8_lossy(&output.stdout).trim(), "");
}
+/// A var named `marker` lives at `worktrunk.state.<branch>.vars.marker`, which
+/// the marker key pattern also matches. Clearing all markers must leave it,
+/// while still clearing the marker of a branch whose name ends in `.vars`.
+#[rstest]
+fn test_state_clear_marker_all_keeps_var_named_marker(repo: TestRepo) {
+ repo.set_marker("feature", "🔧");
+ repo.set_marker("topic.vars", "🚧");
+ repo.git_command()
+ .args(["config", "worktrunk.state.main.vars.marker", "hello"])
+ .run()
+ .unwrap();
+
+ let output = wt_state_cmd(&repo, "marker", "clear", &["--all"])
+ .output()
+ .unwrap();
+ assert!(output.status.success());
+ assert_snapshot!(String::from_utf8_lossy(&output.stderr), @"✓ Cleared 2 markers");
+
+ let output = repo
+ .git_command()
+ .args(["config", "--get", "worktrunk.state.main.vars.marker"])
+ .run()
+ .unwrap();
+ assert_eq!(String::from_utf8_lossy(&output.stdout).trim(), "hello");
+}
+
#[rstest]
fn test_state_clear_marker_all_empty(repo: TestRepo) {
let output = wt_state_cmd(&repo, "marker", "clear", &["--all"])