Skip to content

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

fix(state): don't repopulate default-branch cache on state get (#3024)

Maximilian Roos12d904c9ddd8
diff --git a/src/commands/config/state.rs b/src/commands/config/state.rs
index 06aca7b0e..9f90dbb48 100644
--- a/src/commands/config/state.rs
+++ b/src/commands/config/state.rs
@@ -1081,8 +1081,9 @@ pub fn handle_state_show(format: OutputFormat) -> anyhow::Result<()> {
/// Output state as JSON
fn handle_state_show_json(repo: &Repository) -> anyhow::Result<()> {
- // Get default branch
- let default_branch = repo.default_branch();
+ // Read-only: report the cached default branch without detecting/persisting
+ // (see handle_state_show_table).
+ let default_branch = repo.cached_default_branch();
// Get previous branch
let previous_branch = repo.switch_previous();
@@ -1190,11 +1191,12 @@ fn handle_state_show_table(repo: &Repository) -> anyhow::Result<()> {
// Build complete output as a string
let mut out = String::new();
- // Show default branch cache
+ // Show default branch cache. Read-only: this inspection must not detect
+ // or persist, or it would silently repopulate a just-cleared cache.
writeln!(out, "{}", format_heading("DEFAULT BRANCH", None))?;
- match repo.default_branch() {
+ match repo.cached_default_branch() {
Some(branch) => writeln!(out, "{}", format_with_gutter(&branch, None))?,
- None => writeln!(out, "{}", format_with_gutter("(not available)", None))?,
+ None => writeln!(out, "{}", format_with_gutter("(none)", None))?,
}
writeln!(out)?;
diff --git a/src/git/repository/config.rs b/src/git/repository/config.rs
index 18e6429dd..40c053f57 100644
--- a/src/git/repository/config.rs
+++ b/src/git/repository/config.rs
@@ -551,6 +551,24 @@ impl Repository {
self.unset_config_value("worktrunk.default-branch")
}
+ /// Read the persisted default-branch cache without detecting or writing.
+ ///
+ /// Unlike `default_branch()`, this never falls through to detection
+ /// (`origin/HEAD`, `git ls-remote`, local inference) and never persists a
+ /// result. It reports only what is currently stored in
+ /// `worktrunk.default-branch`, so state-inspection commands
+ /// (`wt config state`) can display the cache without the act of inspecting
+ /// it repopulating the very value being cleared.
+ ///
+ /// Returns `None` when nothing is cached.
+ pub fn cached_default_branch(&self) -> Option<String> {
+ self.config_value("worktrunk.default-branch")
+ .ok()
+ .flatten()
+ .map(|s| s.trim().to_string())
+ .filter(|s| !s.is_empty())
+ }
+
// =========================================================================
// Project config
// =========================================================================
diff --git a/tests/integration_tests/config_state.rs b/tests/integration_tests/config_state.rs
index c8ee80c08..db3d54c03 100644
--- a/tests/integration_tests/config_state.rs
+++ b/tests/integration_tests/config_state.rs
@@ -1040,7 +1040,7 @@ fn test_state_get_empty(repo: TestRepo) {
state_get_settings().bind(|| {
assert_snapshot!(String::from_utf8_lossy(&output.stdout), @"
DEFAULT BRANCH
-   main
+   (none)
PREVIOUS BRANCH
  (none)
@@ -1114,6 +1114,12 @@ fn test_state_get_with_ci_entries(repo: TestRepo) {
#[rstest]
fn test_state_get_comprehensive(repo: TestRepo) {
+ // Populate the default-branch cache (the dump reads it, never detects)
+ repo.git_command()
+ .args(["config", "worktrunk.default-branch", "main"])
+ .run()
+ .unwrap();
+
// Set up previous branch
repo.git_command()
.args(["config", "worktrunk.history", "feature"])
@@ -1207,7 +1213,7 @@ fn test_state_get_json_empty(repo: TestRepo) {
{
"ci_status": [],
"command_log": [],
- "default_branch": "main",
+ "default_branch": null,
"diagnostic": [],
"git_commands_cache": 0,
"hints": [],
@@ -1223,6 +1229,12 @@ fn test_state_get_json_empty(repo: TestRepo) {
#[rstest]
fn test_state_get_json_comprehensive(repo: TestRepo) {
+ // Populate the default-branch cache (the dump reads it, never detects)
+ repo.git_command()
+ .args(["config", "worktrunk.default-branch", "main"])
+ .run()
+ .unwrap();
+
// Set up previous branch
repo.git_command()
.args(["config", "worktrunk.history", "feature"])
@@ -1375,7 +1387,7 @@ fn test_state_get_json_with_logs(repo: TestRepo) {
"size": "<SIZE>"
}
],
- "default_branch": "main",
+ "default_branch": null,
"diagnostic": [],
"git_commands_cache": 0,
"hints": [],
diff --git a/tests/snapshots/integration__integration_tests__config_state__state_get_with_ci_entries.snap b/tests/snapshots/integration__integration_tests__config_state__state_get_with_ci_entries.snap
index e16ae663e..1b4470187 100644
--- a/tests/snapshots/integration__integration_tests__config_state__state_get_with_ci_entries.snap
+++ b/tests/snapshots/integration__integration_tests__config_state__state_get_with_ci_entries.snap
@@ -3,7 +3,7 @@ source: tests/integration_tests/config_state.rs
expression: "String::from_utf8_lossy(&output.stdout)"
---
DEFAULT BRANCH
-  main
+  (none)
PREVIOUS BRANCH
  (none)