Skip to content

Commit 0b84923

Browse files
committed
Auto merge of #1285 - alexcrichton:issue-636, r=huonw
Due to libgit2 not supporting HTTP proxies, the custom transport API of the library must be used to reimplement the HTTP transport with proxy support. The git2-curl crate implements this transport on top of the curl-rust crate. By using libcurl we gain all sorts of proxy support for free. This transport is not used by default, however, as it is not well battle tested and the architecture is not currently ideal (download the entire repo into memory on a clone). Only when an HTTP proxy is present is the new transport used. The other drawback of git2-curl is that it does not currently support authentication. If a private git repository is cloned or authentication is required then it will generate an error instead of correctly asking for credentials. Closes #636
2 parents 9404539 + b20b0f6 commit 0b84923

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ curl = "0.1"
1616
tar = "0.1"
1717
flate2 = "0.1"
1818
git2 = "0.1"
19+
git2-curl = "0.1"
1920
glob = "0.1"
2021
time = "0.1"
2122
log = "0.2"

src/bin/cargo.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![feature(collections, core, io, path, env)]
22

3+
extern crate "git2-curl" as git2_curl;
34
extern crate "rustc-serialize" as rustc_serialize;
45
extern crate cargo;
56
extern crate env_logger;
@@ -88,6 +89,8 @@ macro_rules! each_subcommand{ ($mac:ident) => ({
8889
fn execute(flags: Flags, config: &Config) -> CliResult<Option<()>> {
8990
config.shell().set_verbose(flags.flag_verbose);
9091

92+
init_git_transports(config);
93+
9194
if flags.flag_list {
9295
println!("Installed Commands:");
9396
for command in list_commands().into_iter() {
@@ -247,3 +250,32 @@ fn list_command_directory() -> Vec<Path> {
247250
}
248251
dirs
249252
}
253+
254+
fn init_git_transports(config: &Config) {
255+
// Only use a custom transport if a proxy is configured, right now libgit2
256+
// doesn't support proxies and we have to use a custom transport in this
257+
// case. The custom transport, however, is not as well battle-tested.
258+
match cargo::ops::http_proxy(config) {
259+
Ok(Some(..)) => {}
260+
_ => return
261+
}
262+
263+
let handle = match cargo::ops::http_handle(config) {
264+
Ok(handle) => handle,
265+
Err(..) => return,
266+
};
267+
268+
// The unsafety of the registration function derives from two aspects:
269+
//
270+
// 1. This call must be synchronized with all other registration calls as
271+
// well as construction of new transports.
272+
// 2. The argument is leaked.
273+
//
274+
// We're clear on point (1) because this is only called at the start of this
275+
// binary (we know what the state of the world looks like) and we're mostly
276+
// clear on point (2) because we'd only free it after everything is done
277+
// anyway
278+
unsafe {
279+
git2_curl::register(handle);
280+
}
281+
}

0 commit comments

Comments
 (0)