From 6ad326a9d021a423564f78d6f2daa8ec3a0e8893 Mon Sep 17 00:00:00 2001 From: Benjamin Tan Date: Sat, 2 Mar 2024 22:39:26 +0800 Subject: [PATCH] libgit2: Add support for OpenSSH instead of libssh2 This commit changes the original `ssh` feature into two new ones: `ssh-libssh2` and `ssh-openssh`. By default, the `ssh-libssh2` feature is enabled for backwards compatibility. To use OpenSSH instead, the following listing in `Cargo.toml` can be used: git2-rs = { version = "...", default-features = false, features = ["https", "ssh-openssh"] } --- Cargo.toml | 6 ++++-- libgit2-sys/Cargo.toml | 6 ++++-- libgit2-sys/build.rs | 17 +++++++++++++---- libgit2-sys/lib.rs | 6 +++--- src/cred.rs | 2 +- systest/Cargo.toml | 2 +- 6 files changed, 26 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 987479b633..b3eb86b7c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,11 +34,13 @@ tempfile = "3.1.0" [features] unstable = [] default = ["ssh", "https", "ssh_key_from_memory"] -ssh = ["libgit2-sys/ssh"] +ssh = ["ssh-libssh2"] +ssh-libssh2 = ["libgit2-sys/ssh-libssh2"] +ssh-openssh = ["libgit2-sys/ssh-openssh"] +ssh_key_from_memory = ["libgit2-sys/ssh_key_from_memory"] https = ["libgit2-sys/https", "openssl-sys", "openssl-probe"] vendored-libgit2 = ["libgit2-sys/vendored"] vendored-openssl = ["openssl-sys/vendored", "libgit2-sys/vendored-openssl"] -ssh_key_from_memory = ["libgit2-sys/ssh_key_from_memory"] zlib-ng-compat = ["libgit2-sys/zlib-ng-compat"] [workspace] diff --git a/libgit2-sys/Cargo.toml b/libgit2-sys/Cargo.toml index b3adf89ddc..7c74949acf 100644 --- a/libgit2-sys/Cargo.toml +++ b/libgit2-sys/Cargo.toml @@ -33,9 +33,11 @@ cc = { version = "1.0.43", features = ['parallel'] } openssl-sys = { version = "0.9.45", optional = true } [features] -ssh = ["libssh2-sys"] +ssh = ["ssh-libssh2"] +ssh-libssh2 = ["libssh2-sys"] +ssh-openssh = [] +ssh_key_from_memory = ["ssh-libssh2"] https = ["openssl-sys"] -ssh_key_from_memory = [] vendored = [] vendored-openssl = ["openssl-sys/vendored"] zlib-ng-compat = ["libz-sys/zlib-ng", "libssh2-sys?/zlib-ng-compat"] diff --git a/libgit2-sys/build.rs b/libgit2-sys/build.rs index 3cece57076..b16df8f072 100644 --- a/libgit2-sys/build.rs +++ b/libgit2-sys/build.rs @@ -23,7 +23,9 @@ fn try_system_libgit2() -> Result { fn main() { let https = env::var("CARGO_FEATURE_HTTPS").is_ok(); - let ssh = env::var("CARGO_FEATURE_SSH").is_ok(); + let ssh_libssh2 = env::var("CARGO_FEATURE_SSH_LIBSSH2").is_ok(); + let ssh_libssh2_key_from_memory = env::var("CARGO_FEATURE_SSH_KEY_FROM_MEMORY").is_ok(); + let ssh_openssh = env::var("CARGO_FEATURE_SSH_OPENSSH").is_ok(); let vendored = env::var("CARGO_FEATURE_VENDORED").is_ok(); let zlib_ng_compat = env::var("CARGO_FEATURE_ZLIB_NG_COMPAT").is_ok(); @@ -175,13 +177,20 @@ The build is now aborting. To disable, unset the variable or use `LIBGIT2_NO_VEN features.push_str("#define GIT_ARCH_64 1\n"); } - if ssh { + if ssh_openssh || ssh_libssh2 { if let Some(path) = env::var_os("DEP_SSH2_INCLUDE") { cfg.include(path); } features.push_str("#define GIT_SSH 1\n"); - features.push_str("#define GIT_SSH_LIBSSH2 1\n"); - features.push_str("#define GIT_SSH_LIBSSH2_MEMORY_CREDENTIALS 1\n"); + if ssh_openssh { + features.push_str("#define GIT_SSH_EXEC 1\n"); + } + if ssh_libssh2 { + features.push_str("#define GIT_SSH_LIBSSH2 1\n"); + if ssh_libssh2_key_from_memory { + features.push_str("#define GIT_SSH_LIBSSH2_MEMORY_CREDENTIALS 1\n"); + } + } } if https { features.push_str("#define GIT_HTTPS 1\n"); diff --git a/libgit2-sys/lib.rs b/libgit2-sys/lib.rs index 2ea565284f..a4b8e2a19d 100644 --- a/libgit2-sys/lib.rs +++ b/libgit2-sys/lib.rs @@ -5,7 +5,7 @@ extern crate libz_sys as libz; use libc::{c_char, c_int, c_uchar, c_uint, c_void, size_t}; -#[cfg(feature = "ssh")] +#[cfg(feature = "ssh-libssh2")] use libssh2_sys as libssh2; use std::ffi::CStr; @@ -4326,12 +4326,12 @@ pub fn openssl_init() { #[doc(hidden)] pub fn openssl_init() {} -#[cfg(feature = "ssh")] +#[cfg(feature = "ssh-libssh2")] fn ssh_init() { libssh2::init(); } -#[cfg(not(feature = "ssh"))] +#[cfg(not(feature = "ssh-libssh2"))] fn ssh_init() {} #[doc(hidden)] diff --git a/src/cred.rs b/src/cred.rs index fc0b941935..6686580387 100644 --- a/src/cred.rs +++ b/src/cred.rs @@ -659,7 +659,7 @@ echo password=$2 } #[test] - #[cfg(feature = "ssh")] + #[cfg(feature = "ssh-libssh2")] fn ssh_key_from_memory() { let cred = Cred::ssh_key_from_memory( "test", diff --git a/systest/Cargo.toml b/systest/Cargo.toml index fe5cabcf23..aacf329caf 100644 --- a/systest/Cargo.toml +++ b/systest/Cargo.toml @@ -6,7 +6,7 @@ build = "build.rs" edition = "2018" [dependencies] -libgit2-sys = { path = "../libgit2-sys", features = ['https', 'ssh'] } +libgit2-sys = { path = "../libgit2-sys", features = ['https', 'ssh-libssh2'] } libc = "0.2" [build-dependencies]