Skip to content

Add support for custom git extensions #791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libgit2-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,10 @@ git_enum! {
GIT_OPT_ENABLE_HTTP_EXPECT_CONTINUE,
GIT_OPT_GET_MWINDOW_FILE_LIMIT,
GIT_OPT_SET_MWINDOW_FILE_LIMIT,
GIT_OPT_SET_ODB_PACKED_PRIORITY,
GIT_OPT_SET_ODB_LOOSE_PRIORITY,
GIT_OPT_GET_EXTENSIONS,
GIT_OPT_SET_EXTENSIONS,
}
}

Expand Down
59 changes: 59 additions & 0 deletions src/opts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Bindings to libgit2's git_libgit2_opts function.

use std::ffi::CString;
use std::ptr;

use crate::string_array::StringArray;
use crate::util::Binding;
use crate::{raw, Buf, ConfigLevel, Error, IntoCString};

Expand Down Expand Up @@ -119,6 +121,63 @@ pub fn strict_hash_verification(enabled: bool) {
debug_assert!(error >= 0);
}

/// Returns the list of git extensions that are supported. This is the list of
/// built-in extensions supported by libgit2 and custom extensions that have
/// been added with [`set_extensions`]. Extensions that have been negated will
/// not be returned.
///
/// # Safety
///
/// libgit2 stores user extensions in a static variable.
/// This function is effectively reading a `static mut` and should be treated as such
pub unsafe fn get_extensions() -> Result<StringArray, Error> {
crate::init();

let mut extensions = raw::git_strarray {
strings: ptr::null_mut(),
count: 0,
};

try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_GET_EXTENSIONS as libc::c_int,
&mut extensions
));

Ok(StringArray::from_raw(extensions))
}

/// Set that the given git extensions are supported by the caller. Extensions
/// supported by libgit2 may be negated by prefixing them with a `!`.
/// For example: setting extensions to `[ "!noop", "newext" ]` indicates that
/// the caller does not want to support repositories with the `noop` extension
/// but does want to support repositories with the `newext` extension.
///
/// # Safety
///
/// libgit2 stores user extensions in a static variable.
/// This function is effectively modifying a `static mut` and should be treated as such
pub unsafe fn set_extensions<E>(extensions: &[E]) -> Result<(), Error>
where
for<'x> &'x E: IntoCString,
{
crate::init();

let extensions = extensions
.iter()
.map(|e| e.into_c_string())
.collect::<Result<Vec<_>, _>>()?;

let extension_ptrs = extensions.iter().map(|e| e.as_ptr()).collect::<Vec<_>>();

try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_SET_EXTENSIONS as libc::c_int,
extension_ptrs.as_ptr(),
extension_ptrs.len() as libc::size_t
));

Ok(())
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
19 changes: 19 additions & 0 deletions tests/add_extensions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! Test for `set_extensions`, which writes a global state maintained by libgit2

use git2::opts::{get_extensions, set_extensions};
use git2::Error;

#[test]
fn test_add_extensions() -> Result<(), Error> {
unsafe {
set_extensions(&["custom"])?;
}

let extensions = unsafe { get_extensions() }?;

assert_eq!(extensions.len(), 2);
assert_eq!(extensions.get(0), Some("noop"));
assert_eq!(extensions.get(1), Some("custom"));

Ok(())
}
14 changes: 14 additions & 0 deletions tests/get_extensions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! Test for `get_extensions`, which reads a global state maintained by libgit2

use git2::opts::get_extensions;
use git2::Error;

#[test]
fn test_get_extensions() -> Result<(), Error> {
let extensions = unsafe { get_extensions() }?;

assert_eq!(extensions.len(), 1);
assert_eq!(extensions.get(0), Some("noop"));

Ok(())
}
19 changes: 19 additions & 0 deletions tests/remove_extensions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! Test for `set_extensions`, which writes a global state maintained by libgit2

use git2::opts::{get_extensions, set_extensions};
use git2::Error;

#[test]
fn test_remove_extensions() -> Result<(), Error> {
unsafe {
set_extensions(&["custom", "!ignore", "!noop", "other"])?;
}

let extensions = unsafe { get_extensions() }?;

assert_eq!(extensions.len(), 2);
assert_eq!(extensions.get(0), Some("custom"));
assert_eq!(extensions.get(1), Some("other"));

Ok(())
}