Skip to content

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

fix(switch): clear a stale marker when --create makes the branch (#4704)

Worktrunk Botea0dbee6ce39
diff --git a/docs/src/content/docs/config.md b/docs/src/content/docs/config.md
index 33cdb2feb..6d2e743fe 100644
--- a/docs/src/content/docs/config.md
+++ b/docs/src/content/docs/config.md
@@ -1464,7 +1464,7 @@ Stored in git config as `worktrunk.state.<branch>.marker`. Set directly with:
$ git config worktrunk.state.feature.marker '{"marker":"🚧","set_at":0}'
```
-When `wt` deletes a branch, it clears that branch's marker.
+When `wt` deletes a branch or `wt switch --create` creates one, it clears that branch's marker. A branch deleted and recreated with plain `git` keeps its marker until `wt config state marker clear`.
Without a subcommand, runs `get` for the current branch. For `--branch`, use `get --branch=NAME`.
diff --git a/plugins/worktrunk/skills/worktrunk/reference/config.md b/plugins/worktrunk/skills/worktrunk/reference/config.md
index 5dfa6a0bb..9c91427b5 100644
--- a/plugins/worktrunk/skills/worktrunk/reference/config.md
+++ b/plugins/worktrunk/skills/worktrunk/reference/config.md
@@ -1456,7 +1456,7 @@ Stored in git config as `worktrunk.state.<branch>.marker`. Set directly with:
$ git config worktrunk.state.feature.marker '{"marker":"🚧","set_at":0}'
```
-When `wt` deletes a branch, it clears that branch's marker.
+When `wt` deletes a branch or `wt switch --create` creates one, it clears that branch's marker. A branch deleted and recreated with plain `git` keeps its marker until `wt config state marker clear`.
Without a subcommand, runs `get` for the current branch. For `--branch`, use `get --branch=NAME`.
diff --git a/skills/worktrunk/reference/config.md b/skills/worktrunk/reference/config.md
index 5dfa6a0bb..9c91427b5 100644
--- a/skills/worktrunk/reference/config.md
+++ b/skills/worktrunk/reference/config.md
@@ -1456,7 +1456,7 @@ Stored in git config as `worktrunk.state.<branch>.marker`. Set directly with:
$ git config worktrunk.state.feature.marker '{"marker":"🚧","set_at":0}'
```
-When `wt` deletes a branch, it clears that branch's marker.
+When `wt` deletes a branch or `wt switch --create` creates one, it clears that branch's marker. A branch deleted and recreated with plain `git` keeps its marker until `wt config state marker clear`.
Without a subcommand, runs `get` for the current branch. For `--branch`, use `get --branch=NAME`.
diff --git a/src/cli/config.rs b/src/cli/config.rs
index 64117a89d..fa15b1d12 100644
--- a/src/cli/config.rs
+++ b/src/cli/config.rs
@@ -1242,7 +1242,7 @@ Stored in git config as `worktrunk.state.<branch>.marker`. Set directly with:
$ git config worktrunk.state.feature.marker '{"marker":"🚧","set_at":0}'
```
-When `wt` deletes a branch, it clears that branch's marker.
+When `wt` deletes a branch or `wt switch --create` creates one, it clears that branch's marker. A branch deleted and recreated with plain `git` keeps its marker until `wt config state marker clear`.
Without a subcommand, runs `get` for the current branch. For `--branch`, use `get --branch=NAME`."#
)]
diff --git a/src/commands/worktree/switch.rs b/src/commands/worktree/switch.rs
index 4ddeabd5e..32f76a051 100644
--- a/src/commands/worktree/switch.rs
+++ b/src/commands/worktree/switch.rs
@@ -1299,6 +1299,12 @@ fn execute_switch(
)
.into());
}
+ if *create_branch {
+ // A new branch starts without the marker of an earlier
+ // branch that had the same name and was deleted
+ // outside `wt`.
+ repo.clear_branch_marker(&branch);
+ }
(
*create_branch,
diff --git a/src/git/remove.rs b/src/git/remove.rs
index b454c75c9..bd5786895 100644
--- a/src/git/remove.rs
+++ b/src/git/remove.rs
@@ -645,7 +645,7 @@ pub fn delete_branch_if_safe(
// behavior for the Worktree path). The user explicitly chose -D.
if force_delete {
repo.run_command(&["branch", "-D", "--", branch_name])?;
- clear_deleted_branch_marker(repo, branch_name);
+ repo.clear_branch_marker(branch_name);
return Ok(BranchDeletionResult {
outcome: BranchDeletionOutcome::ForceDeleted,
integration_target: target.to_string(),
@@ -675,7 +675,7 @@ pub fn delete_branch_if_safe(
None => BranchDeletionOutcome::NotDeleted,
};
if matches!(outcome, BranchDeletionOutcome::Integrated(_)) {
- clear_deleted_branch_marker(repo, branch_name);
+ repo.clear_branch_marker(branch_name);
}
Ok(BranchDeletionResult {
@@ -684,22 +684,6 @@ pub fn delete_branch_if_safe(
})
}
-/// Drop the activity marker of a branch `wt` just deleted.
-///
-/// Markers are keyed by branch name in shared git config, so one left behind
-/// would reappear on a later branch reusing the name. Best effort: the branch
-/// is already gone, so a failed unset is logged rather than surfaced.
-fn clear_deleted_branch_marker(repo: &Repository, branch_name: &str) {
- // `<name>.vars.marker` is also branch `<name>`'s var `marker`; only
- // marker JSON belongs to the deleted branch (as in `clear_all_markers`).
- if repo.branch_marker(branch_name).is_none() {
- return;
- }
- if let Err(e) = repo.unset_config(&format!("worktrunk.state.{branch_name}.marker")) {
- tracing::debug!(error = %e, "Failed to clear marker for deleted branch {branch_name}: {e}");
- }
-}
-
/// Find a live checkout of `branch` using a new repository cache.
///
/// Removal planning intentionally caches `git worktree list`, but branch
diff --git a/src/git/repository/config.rs b/src/git/repository/config.rs
index ab0d70cb0..9873a0f66 100644
--- a/src/git/repository/config.rs
+++ b/src/git/repository/config.rs
@@ -173,6 +173,23 @@ impl Repository {
parsed.marker
}
+ /// Drop the marker of a branch `wt` just deleted or created.
+ ///
+ /// Markers are keyed by branch name in shared git config, so one left
+ /// behind by a deleted branch would reappear on a later branch reusing the
+ /// name. Best effort: the branch change already happened, so a failed
+ /// unset is logged rather than surfaced.
+ pub fn clear_branch_marker(&self, branch: &str) {
+ // `<name>.vars.marker` is also branch `<name>`'s var `marker`; only
+ // marker JSON belongs to `branch` (as in `clear_all_markers`).
+ if self.branch_marker(branch).is_none() {
+ return;
+ }
+ if let Err(e) = self.unset_config(&format!("worktrunk.state.{branch}.marker")) {
+ tracing::debug!(error = %e, "Failed to clear marker for branch {branch}: {e}");
+ }
+ }
+
/// Read user-defined branch-keyed marker.
pub fn user_marker(&self, branch: Option<&str>) -> Option<String> {
branch.and_then(|branch| self.branch_marker(branch))
diff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rs
index 5773c15fd..e4cc649ba 100644
--- a/tests/integration_tests/switch.rs
+++ b/tests/integration_tests/switch.rs
@@ -188,6 +188,48 @@ fn test_switch_create_traces_worktree_add(repo: TestRepo) {
);
}
+/// A marker left by a deleted branch of the same name doesn't carry over to
+/// the branch `--create` makes; switching to an existing branch keeps its
+/// marker. Regression for #4683.
+#[rstest]
+fn test_switch_create_clears_stale_marker(repo: TestRepo) {
+ let marker = r#"{"marker":"πŸ€–","set_at":0}"#;
+ repo.run_git(&["config", "worktrunk.state.reused.marker", marker]);
+ repo.run_git(&["branch", "kept"]);
+ repo.run_git(&["config", "worktrunk.state.kept.marker", marker]);
+
+ for args in [&["--create", "reused"][..], &["kept"]] {
+ let output = repo
+ .wt_command()
+ .arg("switch")
+ .args(args)
+ .arg("--no-cd")
+ .output()
+ .unwrap();
+ assert!(
+ output.status.success(),
+ "switch {args:?} failed: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ }
+
+ let read = |branch: &str| {
+ repo.git_command()
+ .args([
+ "config",
+ "--get",
+ &format!("worktrunk.state.{branch}.marker"),
+ ])
+ .run()
+ .unwrap()
+ };
+ assert!(
+ !read("reused").status.success(),
+ "stale marker should be cleared"
+ );
+ assert_eq!(String::from_utf8_lossy(&read("kept").stdout).trim(), marker);
+}
+
#[rstest]
fn test_switch_create_existing_branch_error(mut repo: TestRepo) {
// Create a branch first
diff --git a/tests/snapshots/integration__integration_tests__help__help_config_state_marker.snap b/tests/snapshots/integration__integration_tests__help__help_config_state_marker.snap
index d8680a4bb..9c0d55c5e 100644
--- a/tests/snapshots/integration__integration_tests__help__help_config_state_marker.snap
+++ b/tests/snapshots/integration__integration_tests__help__help_config_state_marker.snap
@@ -97,7 +97,7 @@ Stored in git config as worktrunk.state.<branch>.marker. Set directly wi
  git config worktrunk.state.feature.marker '{"marker":"🚧","set_at":0}'
-When wt deletes a branch, it clears that branch's marker.
+When wt deletes a branch or wt switch --create creates one, it clears that branch's marker. A branch deleted and recreated with plain git keeps its marker until wt config state marker clear.
Without a subcommand, runs get for the current branch. For --branch, use get --branch=NAME.