Skip to content

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

fix: distinguish missing vs corrupt git config errors (#1610)

worktrunk-boted2224f6b61e
diff --git a/src/git/repository/config.rs b/src/git/repository/config.rs
index b71dedbc2..85f645047 100644
--- a/src/git/repository/config.rs
+++ b/src/git/repository/config.rs
@@ -9,10 +9,19 @@ use super::{DefaultBranchName, GitError, Repository};
impl Repository {
/// Get a git config value. Returns None if the key doesn't exist.
+ ///
+ /// Distinguishes "key not found" (exit code 1) from actual errors
+ /// (corrupt config, permission denied, etc.) which are propagated.
pub fn config_value(&self, key: &str) -> anyhow::Result<Option<String>> {
- match self.run_command(&["config", key]) {
- Ok(value) => Ok(Some(value.trim().to_string())),
- Err(_) => Ok(None), // Config key doesn't exist
+ let output = self.run_command_output(&["config", key])?;
+ if output.status.success() {
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ Ok(Some(stdout.trim().to_string()))
+ } else if output.status.code() == Some(1) {
+ Ok(None) // Config key doesn't exist
+ } else {
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ anyhow::bail!("git config {}: {}", key, stderr.trim());
}
}
@@ -84,10 +93,19 @@ impl Repository {
}
/// Clear a hint so it will show again.
+ ///
+ /// Returns `true` if the hint was cleared, `false` if it didn't exist.
+ /// Propagates actual git config errors (corrupt config, permission denied).
pub fn clear_hint(&self, name: &str) -> anyhow::Result<bool> {
- match self.run_command(&["config", "--unset", &format!("worktrunk.hints.{name}")]) {
- Ok(_) => Ok(true),
- Err(_) => Ok(false), // Key didn't exist
+ let key = format!("worktrunk.hints.{name}");
+ let output = self.run_command_output(&["config", "--unset", &key])?;
+ if output.status.success() {
+ Ok(true)
+ } else if output.status.code() == Some(5) {
+ Ok(false) // Key didn't exist (--unset uses exit code 5)
+ } else {
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ anyhow::bail!("git config --unset {}: {}", key, stderr.trim());
}
}
@@ -365,10 +383,20 @@ impl Repository {
/// `default_branch()` will re-detect (using git's cache or querying remote).
///
/// Returns `true` if cache was cleared, `false` if no cache existed.
+ /// Propagates actual git config errors (corrupt config, permission denied).
pub fn clear_default_branch_cache(&self) -> anyhow::Result<bool> {
- Ok(self
- .run_command(&["config", "--unset", "worktrunk.default-branch"])
- .is_ok())
+ let output = self.run_command_output(&["config", "--unset", "worktrunk.default-branch"])?;
+ if output.status.success() {
+ Ok(true)
+ } else if output.status.code() == Some(5) {
+ Ok(false) // Key didn't exist (--unset uses exit code 5)
+ } else {
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ anyhow::bail!(
+ "git config --unset worktrunk.default-branch: {}",
+ stderr.trim()
+ );
+ }
}
// =========================================================================
diff --git a/tests/integration_tests/repository.rs b/tests/integration_tests/repository.rs
index d7545e4eb..a0bdfc8e2 100644
--- a/tests/integration_tests/repository.rs
+++ b/tests/integration_tests/repository.rs
@@ -337,6 +337,53 @@ fn test_set_config() {
assert_eq!(value, Some("new-value".to_string()));
}
+// =============================================================================
+// config_value() error handling: corrupt config propagation
+// =============================================================================
+
+#[test]
+fn test_config_value_propagates_error_on_corrupt_config() {
+ let repo = TestRepo::new();
+ let root = repo.root_path().to_path_buf();
+
+ // Create repository before corrupting config
+ let repository = Repository::at(root.clone()).unwrap();
+
+ // Corrupt the git config file after repository creation
+ let config_path = root.join(".git/config");
+ fs::write(&config_path, "[invalid section\n").unwrap();
+
+ let result = repository.config_value("test.key");
+
+ // Should propagate the error, not silently return None
+ assert!(
+ result.is_err(),
+ "config_value() should propagate errors from corrupt config, not return Ok(None)"
+ );
+}
+
+#[test]
+fn test_clear_hint_propagates_error_on_corrupt_config() {
+ let repo = TestRepo::new();
+ let root = repo.root_path().to_path_buf();
+
+ // Create repository and set a hint before corrupting config
+ let repository = Repository::at(root.clone()).unwrap();
+ repository.mark_hint_shown("test-hint").unwrap();
+
+ // Corrupt the git config file
+ let config_path = root.join(".git/config");
+ fs::write(&config_path, "[invalid section\n").unwrap();
+
+ let result = repository.clear_hint("test-hint");
+
+ // Should propagate the error, not silently return Ok(false)
+ assert!(
+ result.is_err(),
+ "clear_hint() should propagate errors from corrupt config, not return Ok(false)"
+ );
+}
+
// =============================================================================
// Bug #1: Tag/branch name collision tests
// =============================================================================