Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Clear repeated Git config values completely (#4566)
Maximilian Roos56c1b1534c8e
diff --git a/src/git/repository/config.rs b/src/git/repository/config.rsindex 760a6e734..ab0d70cb0 100644--- a/src/git/repository/config.rs+++ b/src/git/repository/config.rs@@ -103,7 +103,7 @@ impl Repository {Ok(())}- /// Unset a config key and keep the bulk config map coherent.+ /// Unset every value for a config key and keep the bulk config map coherent.////// Returns `true` if the key was cleared, `false` if it didn't exist./// Propagates actual git config errors (corrupt config, permission denied).@@ -111,12 +111,14 @@ impl Repository {/// Removes the canonical form (matching what git emits) to stay in/// sync with `set_config_value` and `config_last`.pub(super) fn unset_config_value(&self, key: &str) -> anyhow::Result<bool> {- let args = ["config", "--unset", key];+ // --unset also returns 5 for repeated values without removing them.+ // --unset-all makes 5 unambiguously mean that the key was absent.+ let args = ["config", "--unset-all", key];let output = self.run_command_output(&args)?;let existed = if output.status.success() {true} else if output.status.code() == Some(5) {- // --unset exit code 5 = key didn't exist+ // --unset-all exit code 5 = key didn't existfalse} else {return Err(CommandError::from_failed_output("git", &args, &output).into());@@ -1141,7 +1143,33 @@ mod tests {let err = repo.unset_config("inva lid.key").unwrap_err();let cmd_err = CommandError::find_in(&err).expect("error should carry a CommandError");- assert_eq!(cmd_err.command_string(), "git config --unset inva lid.key");+ assert_eq!(+ cmd_err.command_string(),+ "git config --unset-all inva lid.key"+ );+ }++ #[test]+ fn test_unset_config_clears_all_values_and_cache() {+ let test = TestRepo::with_initial_commit();+ let key = "worktrunk.state.main.vars.example";+ for value in ["first", "second"] {+ let output = test+ .git_command()+ .args(["config", "--add", key, value])+ .run()+ .unwrap();+ assert!(output.status.success());+ }+ let repo = Repository::at(test.root_path()).unwrap();+ // Populate the bulk cache before clearing, so both cached and fresh+ // reads must agree with what was removed from disk.+ assert_eq!(repo.config_value(key).unwrap().as_deref(), Some("second"));+ assert!(repo.unset_config(key).unwrap());+ assert_eq!(repo.config_value(key).unwrap(), None);+ let fresh = Repository::at(test.root_path()).unwrap();+ assert_eq!(fresh.config_value(key).unwrap(), None);+ assert!(!repo.unset_config(key).unwrap());}#[test]diff --git a/tests/integration_tests/config_state.rs b/tests/integration_tests/config_state.rsindex b9d3dd8aa..7e273ce7f 100644--- a/tests/integration_tests/config_state.rs+++ b/tests/integration_tests/config_state.rs@@ -3466,3 +3466,30 @@ fn state_branch_flag_resolves_selectors(mut repo: TestRepo) {String::from_utf8_lossy(&get.stderr));}++/// A key with repeated Git config entries must actually disappear after clear.+#[rstest]+#[case("example")]+#[case("--all")]+fn test_vars_clear_removes_repeated_values(repo: TestRepo, #[case] argument: &str) {+ let key = "worktrunk.state.main.vars.example";+ for value in ["first", "second"] {+ let output = repo+ .git_command()+ .args(["config", "--add", key, value])+ .run()+ .unwrap();+ assert!(output.status.success());+ }+ let output = wt_state_cmd(&repo, "vars", "clear", &[argument])+ .output()+ .unwrap();+ assert!(output.status.success(), "{output:?}");+ let remaining = repo+ .git_command()+ .args(["config", "--get-all", key])+ .run()+ .unwrap();+ assert_eq!(remaining.status.code(), Some(1), "{remaining:?}");+ assert!(remaining.stdout.is_empty());+}