public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
From: "ionnss via GitGitGadget" <gitgitgadget@gmail•com>
To: git@vger•kernel.org
Cc: Chris Torek <chris.torek@gmail•com>,
	Phillip Wood <phillip.wood123@gmail•com>,
	ions <zara.leonardo@gmail•com>, ionnss <zara.leonardo@gmail•com>
Subject: [PATCH v3 3/3] libgit-rs: add get_ulong() and get_pathname() methods
Date: Sat, 27 Sep 2025 03:51:51 +0000	[thread overview]
Message-ID: <1ac8d768194b15eaf536000ed5f76f36dd0a39b2.1758945111.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1977.v3.git.1758945111.gitgitgadget@gmail.com>

From: ionnss <zara.leonardo@gmail•com>

Expand the ConfigSet API with additional configuration value types:

- get_ulong(): Parse unsigned long integers for large numeric values
- get_pathname(): Parse file paths, returning PathBuf for type safety

Both functions follow the same pattern as existing get_* methods,
using Git's C functions for consistent parsing behavior.

Add comprehensive tests covering normal cases, edge cases, and
error handling for all new functionality.

Signed-off-by: ionnss <zara.leonardo@gmail•com>
---
 contrib/libgit-rs/src/config.rs    | 57 ++++++++++++++++++++++++++++--
 contrib/libgit-rs/testdata/config4 |  4 +++
 contrib/libgit-sys/src/lib.rs      | 18 +++++++++-
 3 files changed, 76 insertions(+), 3 deletions(-)

diff --git a/contrib/libgit-rs/src/config.rs b/contrib/libgit-rs/src/config.rs
index 72ee88801b..ffd9f311b6 100644
--- a/contrib/libgit-rs/src/config.rs
+++ b/contrib/libgit-rs/src/config.rs
@@ -1,8 +1,8 @@
 use std::ffi::{c_void, CStr, CString};
-use std::path::Path;
+use std::path::{Path, PathBuf};
 
 #[cfg(has_std__ffi__c_char)]
-use std::ffi::{c_char, c_int};
+use std::ffi::{c_char, c_int, c_ulong};
 
 #[cfg(not(has_std__ffi__c_char))]
 #[allow(non_camel_case_types)]
@@ -12,6 +12,10 @@ type c_char = i8;
 #[allow(non_camel_case_types)]
 type c_int = i32;
 
+#[cfg(not(has_std__ffi__c_char))]
+#[allow(non_camel_case_types)]
+type c_ulong = u64;
+
 use libgit_sys::*;
 
 /// A ConfigSet is an in-memory cache for config-like files such as `.gitmodules` or `.gitconfig`.
@@ -82,6 +86,41 @@ impl ConfigSet {
 
         Some(val != 0)
     }
+
+    /// Load the value for the given key and attempt to parse it as an unsigned long. Dies with a fatal error
+    /// if the value cannot be parsed. Returns None if the key is not present.
+    pub fn get_ulong(&mut self, key: &str) -> Option<u64> {
+        let key = CString::new(key).expect("config key should be valid CString");
+        let mut val: c_ulong = 0;
+        unsafe {
+            if libgit_configset_get_ulong(self.0, key.as_ptr(), &mut val as *mut c_ulong) != 0 {
+                return None;
+            }
+        }
+        Some(val as u64)
+    }
+
+    /// Load the value for the given key and attempt to parse it as a file path. Dies with a fatal error
+    /// if the value cannot be converted to a PathBuf. Returns None if the key is not present.
+    pub fn get_pathname(&mut self, key: &str) -> Option<PathBuf> {
+        let key = CString::new(key).expect("config key should be valid CString");
+        let mut val: *mut c_char = std::ptr::null_mut();
+        unsafe {
+            if libgit_configset_get_pathname(self.0, key.as_ptr(), &mut val as *mut *mut c_char)
+                != 0
+            {
+                return None;
+            }
+            let borrowed_str = CStr::from_ptr(val);
+            let owned_str = String::from(
+                borrowed_str
+                    .to_str()
+                    .expect("config path should be valid UTF-8"),
+            );
+            free(val as *mut c_void); // Free the xstrdup()ed pointer from the C side
+            Some(PathBuf::from(owned_str))
+        }
+    }
 }
 
 impl Default for ConfigSet {
@@ -128,5 +167,19 @@ mod tests {
         assert_eq!(cs.get_bool("test.boolHundred"), Some(true)); // "100" → true
         // Test missing boolean key
         assert_eq!(cs.get_bool("missing.boolean"), None);
+        // Test ulong parsing
+        assert_eq!(cs.get_ulong("test.ulongSmall"), Some(42));
+        assert_eq!(cs.get_ulong("test.ulongBig"), Some(4294967296)); // > 32-bit int
+        assert_eq!(cs.get_ulong("missing.ulong"), None);
+        // Test pathname parsing
+        assert_eq!(
+            cs.get_pathname("test.pathRelative"),
+            Some(PathBuf::from("./some/path"))
+        );
+        assert_eq!(
+            cs.get_pathname("test.pathAbsolute"),
+            Some(PathBuf::from("/usr/bin/git"))
+        );
+        assert_eq!(cs.get_pathname("missing.path"), None);
     }
 }
diff --git a/contrib/libgit-rs/testdata/config4 b/contrib/libgit-rs/testdata/config4
index c0755a32be..bd621ab480 100644
--- a/contrib/libgit-rs/testdata/config4
+++ b/contrib/libgit-rs/testdata/config4
@@ -7,3 +7,7 @@
 	boolZero = 0
 	boolZeroZero = 00
 	boolHundred = 100
+	ulongSmall = 42
+	ulongBig = 4294967296
+	pathRelative = ./some/path
+	pathAbsolute = /usr/bin/git
diff --git a/contrib/libgit-sys/src/lib.rs b/contrib/libgit-sys/src/lib.rs
index b104fda8f6..07386572ec 100644
--- a/contrib/libgit-sys/src/lib.rs
+++ b/contrib/libgit-sys/src/lib.rs
@@ -1,7 +1,7 @@
 use std::ffi::c_void;
 
 #[cfg(has_std__ffi__c_char)]
-use std::ffi::{c_char, c_int};
+use std::ffi::{c_char, c_int, c_ulong};
 
 #[cfg(not(has_std__ffi__c_char))]
 #[allow(non_camel_case_types)]
@@ -11,6 +11,10 @@ pub type c_char = i8;
 #[allow(non_camel_case_types)]
 pub type c_int = i32;
 
+#[cfg(not(has_std__ffi__c_char))]
+#[allow(non_camel_case_types)]
+pub type c_ulong = u64;
+
 extern crate libz_sys;
 
 #[allow(non_camel_case_types)]
@@ -49,6 +53,18 @@ extern "C" {
         dest: *mut c_int,
     ) -> c_int;
 
+    pub fn libgit_configset_get_ulong(
+        cs: *mut libgit_config_set,
+        key: *const c_char,
+        dest: *mut c_ulong,
+    ) -> c_int;
+
+    pub fn libgit_configset_get_pathname(
+        cs: *mut libgit_config_set,
+        key: *const c_char,
+        dest: *mut *mut c_char,
+    ) -> c_int;
+
 }
 
 #[cfg(test)]
-- 
gitgitgadget

  parent reply	other threads:[~2025-09-27  3:51 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-25 11:44 [PATCH 0/2] libgit-rs: add get_bool() method to ConfigSet ions via GitGitGadget
2025-09-25 11:44 ` [PATCH 1/2] po: fix escaped underscores in README.md ionnss via GitGitGadget
2025-09-25 11:44 ` [PATCH 2/2] libgit-rs: add get_bool() method to ConfigSet ionnss via GitGitGadget
2025-09-26  6:43   ` Chris Torek
2025-09-26  9:58   ` Phillip Wood
2025-09-26 17:15     ` Junio C Hamano
2025-09-27  0:07 ` [PATCH v2 0/3] " ions via GitGitGadget
2025-09-27  0:07   ` [PATCH v2 1/3] po: fix escaped underscores in README.md ionnss via GitGitGadget
2025-09-27  0:07   ` [PATCH v2 2/3] libgit-rs: add get_bool() method to ConfigSet ionnss via GitGitGadget
2025-09-27  0:07   ` [PATCH v2 3/3] libgit-rs: address review feedback for get_bool() ionnss via GitGitGadget
2025-09-27  2:01   ` [PATCH v2 0/3] libgit-rs: add get_bool() method to ConfigSet Junio C Hamano
2025-09-27  3:51   ` [PATCH v3 " ions via GitGitGadget
2025-09-27  3:51     ` [PATCH v3 1/3] po: fix escaped underscores in README.md ionnss via GitGitGadget
2025-09-29 13:26       ` Phillip Wood
2025-09-27  3:51     ` [PATCH v3 2/3] libgit-rs: add get_bool() method to ConfigSet ionnss via GitGitGadget
2025-09-29 13:23       ` Phillip Wood
2025-09-27  3:51     ` ionnss via GitGitGadget [this message]
2025-09-29 13:23       ` [PATCH v3 3/3] libgit-rs: add get_ulong() and get_pathname() methods Phillip Wood
2025-09-30  8:46     ` [PATCH v4] libgit-rs: add get_bool(), get_ulong(), " ions via GitGitGadget
2025-10-01 10:15       ` Phillip Wood
2025-10-06 21:20         ` brian m. carlson
2025-10-08 13:36           ` Phillip Wood

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1ac8d768194b15eaf536000ed5f76f36dd0a39b2.1758945111.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail$(echo .)com \
    --cc=chris.torek@gmail$(echo .)com \
    --cc=git@vger$(echo .)kernel.org \
    --cc=phillip.wood123@gmail$(echo .)com \
    --cc=zara.leonardo@gmail$(echo .)com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox