Skip to content

Commit da9c98d

Browse files
committed
Make url optional via a new "cred" feature gate for credential helpers
Add an enabled-by-default feature flag for credential helpers, which are the only uses of the `url` crate. This reduces a `default-features = false` build of git2 from 54 dependencies to 17, for a configuration that many tools (e.g. anything that only cares about local repositories) can happily use. In addition to enabling it by default, make the "https" and "ssh" features depend on "cred".
1 parent 53442d4 commit da9c98d

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

Cargo.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ categories = ["api-bindings"]
1616
edition = "2018"
1717

1818
[dependencies]
19-
url = "2.5.4"
19+
url = { version = "2.5.4", optional = true }
2020
bitflags = "2.1.0"
2121
libc = "0.2"
2222
log = "0.4.8"
@@ -30,12 +30,15 @@ openssl-probe = { version = "0.1", optional = true }
3030
clap = { version = "4.4.13", features = ["derive"] }
3131
time = { version = "0.3.37", features = ["formatting"] }
3232
tempfile = "3.1.0"
33+
url = "2.5.4"
3334

3435
[features]
3536
unstable = []
36-
default = ["ssh", "https"]
37-
ssh = ["libgit2-sys/ssh"]
38-
https = ["libgit2-sys/https", "openssl-sys", "openssl-probe"]
37+
default = ["ssh", "https", "cred"]
38+
ssh = ["libgit2-sys/ssh", "cred"]
39+
https = ["libgit2-sys/https", "openssl-sys", "openssl-probe", "cred"]
40+
# Include support for credentials, which pulls in the `url` crate and all its dependencies
41+
cred = ["dep:url"]
3942
vendored-libgit2 = ["libgit2-sys/vendored"]
4043
vendored-openssl = ["openssl-sys/vendored", "libgit2-sys/vendored-openssl"]
4144
zlib-ng-compat = ["libgit2-sys/zlib-ng-compat"]

src/cred.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1+
#[cfg(feature = "cred")]
12
use log::{debug, trace};
23
use std::ffi::CString;
3-
use std::io::Write;
44
use std::mem;
55
use std::path::Path;
6-
use std::process::{Command, Stdio};
76
use std::ptr;
87

98
use crate::util::Binding;
10-
use crate::{raw, Config, Error, IntoCString};
9+
#[cfg(feature = "cred")]
10+
use crate::Config;
11+
use crate::{raw, Error, IntoCString};
1112

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

1718
/// Management of the gitcredentials(7) interface.
19+
#[cfg(feature = "cred")]
1820
pub struct CredentialHelper {
1921
/// A public field representing the currently discovered username from
2022
/// configuration.
@@ -118,6 +120,7 @@ impl Cred {
118120
/// successful.
119121
///
120122
/// [1]: https://www.kernel.org/pub/software/scm/git/docs/gitcredentials.html
123+
#[cfg(feature = "cred")]
121124
pub fn credential_helper(
122125
config: &Config,
123126
url: &str,
@@ -189,6 +192,7 @@ impl Drop for Cred {
189192
}
190193
}
191194

195+
#[cfg(feature = "cred")]
192196
impl CredentialHelper {
193197
/// Create a new credential helper object which will be used to probe git's
194198
/// local credential configuration.
@@ -292,6 +296,12 @@ impl CredentialHelper {
292296
// see https://www.kernel.org/pub/software/scm/git/docs/technical
293297
// /api-credentials.html#_credential_helpers
294298
fn add_command(&mut self, cmd: Option<&str>) {
299+
fn is_absolute_path(path: &str) -> bool {
300+
path.starts_with('/')
301+
|| path.starts_with('\\')
302+
|| cfg!(windows) && path.chars().nth(1).is_some_and(|x| x == ':')
303+
}
304+
295305
let cmd = match cmd {
296306
Some("") | None => return,
297307
Some(s) => s,
@@ -352,6 +362,9 @@ impl CredentialHelper {
352362
cmd: &str,
353363
username: &Option<String>,
354364
) -> (Option<String>, Option<String>) {
365+
use std::io::Write;
366+
use std::process::{Command, Stdio};
367+
355368
macro_rules! my_try( ($e:expr) => (
356369
match $e {
357370
Ok(e) => e,
@@ -481,13 +494,8 @@ impl CredentialHelper {
481494
}
482495
}
483496

484-
fn is_absolute_path(path: &str) -> bool {
485-
path.starts_with('/')
486-
|| path.starts_with('\\')
487-
|| cfg!(windows) && path.chars().nth(1).is_some_and(|x| x == ':')
488-
}
489-
490497
#[cfg(test)]
498+
#[cfg(feature = "cred")]
491499
mod test {
492500
use std::env;
493501
use std::fs::File;

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ pub use crate::buf::Buf;
8888
pub use crate::cherrypick::CherrypickOptions;
8989
pub use crate::commit::{Commit, Parents};
9090
pub use crate::config::{Config, ConfigEntries, ConfigEntry};
91-
pub use crate::cred::{Cred, CredentialHelper};
91+
pub use crate::cred::Cred;
92+
#[cfg(feature = "cred")]
93+
pub use crate::cred::CredentialHelper;
9294
pub use crate::describe::{Describe, DescribeFormatOptions, DescribeOptions};
9395
pub use crate::diff::{Deltas, Diff, DiffDelta, DiffFile, DiffOptions};
9496
pub use crate::diff::{DiffBinary, DiffBinaryFile, DiffBinaryKind, DiffPatchidOptions};

0 commit comments

Comments
 (0)