Skip to content

Commit 479c263

Browse files
committed
libgit-rs: add get_bool() method to ConfigSet
Add support for parsing boolean configuration values using Git's git_configset_get_bool() C function. This ensures consistent behavior with Git's native boolean parsing logic. The method handles all Git boolean formats (true/false, yes/no, on/off, 1/0) and edge cases like "00" and "100" correctly. Includes comprehensive tests for various boolean formats and edge cases. Signed-off-by: ionnss <[email protected]>
1 parent d781078 commit 479c263

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

contrib/libgit-rs/src/config.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ impl ConfigSet {
6868
Some(owned_str)
6969
}
7070
}
71+
72+
/// Load the value for the given key and attempt to parse it as a boolean. Dies with a fatal error
73+
/// if the value cannot be parsed. Returns None if the key is not present.
74+
pub fn get_bool(&mut self, key: &str) -> Option<bool> {
75+
let key = CString::new(key).expect("config key should be valid CString");
76+
let mut val: c_int = 0;
77+
unsafe {
78+
if libgit_configset_get_bool(self.0, key.as_ptr(), &mut val as *mut c_int) != 0 {
79+
return None;
80+
}
81+
}
82+
83+
Some(val != 0)
84+
}
7185
}
7286

7387
impl Default for ConfigSet {
@@ -95,12 +109,24 @@ mod tests {
95109
Path::new("testdata/config1"),
96110
Path::new("testdata/config2"),
97111
Path::new("testdata/config3"),
112+
Path::new("testdata/config4"),
98113
]);
99114
// ConfigSet retrieves correct value
100115
assert_eq!(cs.get_int("trace2.eventTarget"), Some(1));
101116
// ConfigSet respects last config value set
102117
assert_eq!(cs.get_int("trace2.eventNesting"), Some(3));
103118
// ConfigSet returns None for missing key
104119
assert_eq!(cs.get_string("foo.bar"), None);
120+
// Test boolean parsing - comprehensive tests
121+
assert_eq!(cs.get_bool("test.boolTrue"), Some(true));
122+
assert_eq!(cs.get_bool("test.boolFalse"), Some(false));
123+
assert_eq!(cs.get_bool("test.boolYes"), Some(true));
124+
assert_eq!(cs.get_bool("test.boolNo"), Some(false));
125+
assert_eq!(cs.get_bool("test.boolOne"), Some(true));
126+
assert_eq!(cs.get_bool("test.boolZero"), Some(false));
127+
assert_eq!(cs.get_bool("test.boolZeroZero"), Some(false)); // "00" → false
128+
assert_eq!(cs.get_bool("test.boolHundred"), Some(true)); // "100" → true
129+
// Test missing boolean key
130+
assert_eq!(cs.get_bool("missing.boolean"), None);
105131
}
106132
}

contrib/libgit-rs/testdata/config3

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[trace2]
2-
eventNesting = 3
2+
eventNesting = 3

contrib/libgit-rs/testdata/config4

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[test]
2+
boolTrue = true
3+
boolFalse = false
4+
boolYes = yes
5+
boolNo = no
6+
boolOne = 1
7+
boolZero = 0
8+
boolZeroZero = 00
9+
boolHundred = 100

contrib/libgit-sys/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ extern "C" {
4343
dest: *mut *mut c_char,
4444
) -> c_int;
4545

46+
pub fn libgit_configset_get_bool(
47+
cs: *mut libgit_config_set,
48+
key: *const c_char,
49+
dest: *mut c_int,
50+
) -> c_int;
51+
4652
}
4753

4854
#[cfg(test)]

0 commit comments

Comments
 (0)