Skip to content

Make url optional via a new "cred" feature gate for credential helpers #1168

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ categories = ["api-bindings"]
edition = "2018"

[dependencies]
url = "2.5.4"
url = { version = "2.5.4", optional = true }
bitflags = "2.1.0"
libc = "0.2"
log = "0.4.8"
Expand All @@ -30,12 +30,15 @@ openssl-probe = { version = "0.1", optional = true }
clap = { version = "4.4.13", features = ["derive"] }
time = { version = "0.3.37", features = ["formatting"] }
tempfile = "3.1.0"
url = "2.5.4"

[features]
unstable = []
default = ["ssh", "https"]
ssh = ["libgit2-sys/ssh"]
https = ["libgit2-sys/https", "openssl-sys", "openssl-probe"]
default = ["ssh", "https", "cred"]
ssh = ["libgit2-sys/ssh", "cred"]
https = ["libgit2-sys/https", "openssl-sys", "openssl-probe", "cred"]
# Include support for credentials, which pulls in the `url` crate and all its dependencies
cred = ["dep:url"]
vendored-libgit2 = ["libgit2-sys/vendored"]
vendored-openssl = ["openssl-sys/vendored", "libgit2-sys/vendored-openssl"]
zlib-ng-compat = ["libgit2-sys/zlib-ng-compat"]
Expand Down
26 changes: 17 additions & 9 deletions src/cred.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
#[cfg(feature = "cred")]
use log::{debug, trace};
use std::ffi::CString;
use std::io::Write;
use std::mem;
use std::path::Path;
use std::process::{Command, Stdio};
use std::ptr;

use crate::util::Binding;
use crate::{raw, Config, Error, IntoCString};
#[cfg(feature = "cred")]
use crate::Config;
use crate::{raw, Error, IntoCString};

/// A structure to represent git credentials in libgit2.
pub struct Cred {
raw: *mut raw::git_cred,
}

/// Management of the gitcredentials(7) interface.
#[cfg(feature = "cred")]
pub struct CredentialHelper {
/// A public field representing the currently discovered username from
/// configuration.
Expand Down Expand Up @@ -118,6 +120,7 @@ impl Cred {
/// successful.
///
/// [1]: https://www.kernel.org/pub/software/scm/git/docs/gitcredentials.html
#[cfg(feature = "cred")]
pub fn credential_helper(
config: &Config,
url: &str,
Expand Down Expand Up @@ -189,6 +192,7 @@ impl Drop for Cred {
}
}

#[cfg(feature = "cred")]
impl CredentialHelper {
/// Create a new credential helper object which will be used to probe git's
/// local credential configuration.
Expand Down Expand Up @@ -292,6 +296,12 @@ impl CredentialHelper {
// see https://www.kernel.org/pub/software/scm/git/docs/technical
// /api-credentials.html#_credential_helpers
fn add_command(&mut self, cmd: Option<&str>) {
fn is_absolute_path(path: &str) -> bool {
path.starts_with('/')
|| path.starts_with('\\')
|| cfg!(windows) && path.chars().nth(1).is_some_and(|x| x == ':')
}

let cmd = match cmd {
Some("") | None => return,
Some(s) => s,
Expand Down Expand Up @@ -352,6 +362,9 @@ impl CredentialHelper {
cmd: &str,
username: &Option<String>,
) -> (Option<String>, Option<String>) {
use std::io::Write;
use std::process::{Command, Stdio};

macro_rules! my_try( ($e:expr) => (
match $e {
Ok(e) => e,
Expand Down Expand Up @@ -481,13 +494,8 @@ impl CredentialHelper {
}
}

fn is_absolute_path(path: &str) -> bool {
path.starts_with('/')
|| path.starts_with('\\')
|| cfg!(windows) && path.chars().nth(1).is_some_and(|x| x == ':')
}

#[cfg(test)]
#[cfg(feature = "cred")]
mod test {
use std::env;
use std::fs::File;
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ pub use crate::buf::Buf;
pub use crate::cherrypick::CherrypickOptions;
pub use crate::commit::{Commit, Parents};
pub use crate::config::{Config, ConfigEntries, ConfigEntry};
pub use crate::cred::{Cred, CredentialHelper};
pub use crate::cred::Cred;
#[cfg(feature = "cred")]
pub use crate::cred::CredentialHelper;
pub use crate::describe::{Describe, DescribeFormatOptions, DescribeOptions};
pub use crate::diff::{Deltas, Diff, DiffDelta, DiffFile, DiffOptions};
pub use crate::diff::{DiffBinary, DiffBinaryFile, DiffBinaryKind, DiffPatchidOptions};
Expand Down