Skip to content

Make url optional via a new "cred" feature gate for credential helpers; disable default features #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

Merged
merged 3 commits into from
Aug 12, 2025
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "git2"
version = "0.20.2"
version = "0.21.0"
authors = ["Josh Triplett <josh@joshtriplett.org>", "Alex Crichton <alex@alexcrichton.com>"]
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand All @@ -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 = ["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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ libgit2 bindings for Rust.

```toml
[dependencies]
git2 = "0.20.2"
git2 = "0.21"
```

## Rust version requirements
Expand Down
4 changes: 2 additions & 2 deletions git2-curl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "git2-curl"
version = "0.21.0"
version = "0.22.0"
authors = ["Josh Triplett <josh@joshtriplett.org>", "Alex Crichton <alex@alexcrichton.com>"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/rust-lang/git2-rs"
Expand All @@ -16,7 +16,7 @@ edition = "2018"
curl = "0.4.33"
url = "2.5.4"
log = "0.4"
git2 = { path = "..", version = "0.20", default-features = false }
git2 = { path = "..", version = "0.21", default-features = false }

[dev-dependencies]
civet = "0.11"
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
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
//! source `Repository`, to ensure that they do not outlive the repository
//! itself.

#![doc(html_root_url = "https://docs.rs/git2/0.20")]
#![doc(html_root_url = "https://docs.rs/git2/0.21")]
#![allow(trivial_numeric_casts, trivial_casts)]
#![deny(missing_docs)]
#![warn(rust_2018_idioms)]
Expand All @@ -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
Loading