Skip to content

Commit a5904a2

Browse files
committed
libgit-rs: add get_bool() method to ConfigSet
Add support for parsing boolean configuration values in the Rust ConfigSet API. The method follows Git's standard boolean parsing rules, accepting true/yes/on/1 as true and false/no/off/0 as false. The implementation reuses the existing get_string() infrastructure and adds case-insensitive boolean parsing logic. Signed-off-by: ionnss <[email protected]>
1 parent d781078 commit a5904a2

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

contrib/libgit-rs/src/config.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@ impl ConfigSet {
6868
Some(owned_str)
6969
}
7070
}
71+
72+
pub fn get_bool(&mut self, key: &str) -> Option<bool> {
73+
let key = CString::new(key).expect("Couldn't convert key to CString");
74+
let mut val: *mut c_char = std::ptr::null_mut();
75+
unsafe {
76+
if libgit_configset_get_string(self.0, key.as_ptr(), &mut val as *mut *mut c_char) != 0
77+
{
78+
return None;
79+
}
80+
let borrowed_str = CStr::from_ptr(val);
81+
let owned_str =
82+
String::from(borrowed_str.to_str().expect("Couldn't convert val to str"));
83+
free(val as *mut c_void); // Free the xstrdup()ed pointer from the C side
84+
match owned_str.to_lowercase().as_str() {
85+
"true" | "yes" | "on" | "1" => Some(true),
86+
"false" | "no" | "off" | "0" => Some(false),
87+
_ => None,
88+
}
89+
}
90+
}
7191
}
7292

7393
impl Default for ConfigSet {
@@ -102,5 +122,9 @@ mod tests {
102122
assert_eq!(cs.get_int("trace2.eventNesting"), Some(3));
103123
// ConfigSet returns None for missing key
104124
assert_eq!(cs.get_string("foo.bar"), None);
125+
// Test boolean parsing
126+
assert_eq!(cs.get_bool("test.booleanValue"), Some(true));
127+
// Test missing boolean key
128+
assert_eq!(cs.get_bool("missing.boolean"), None);
105129
}
106130
}

contrib/libgit-rs/testdata/config3

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
[trace2]
22
eventNesting = 3
3+
[test]
4+
booleanValue = true

0 commit comments

Comments
 (0)