Skip to content

Commit

Permalink
Merge #334
Browse files Browse the repository at this point in the history
334: [fetch] use cli git for fetching crates.io index r=ordian a=ordian

Fixes #333.
Cargo has a fair amount of code for git authentication handling ([link](https://github.com/rust-lang/cargo/blob/57986eac7157261c33f0123bade7ccd20f15200f/src/cargo/sources/git/utils.rs#L410-L624)), but still has a cli fallback ([link](https://github.com/rust-lang/cargo/blob/57986eac7157261c33f0123bade7ccd20f15200f/src/cargo/sources/git/utils.rs#L704-L714)).
See rust-lang/cargo#2078 for details on all the arguments passed to git.

Co-authored-by: Andronik Ordian <[email protected]>
  • Loading branch information
bors[bot] and ordian authored Oct 9, 2019
2 parents ca18ede + 188eaff commit 34f08ca
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ reqwest = "0.9.19"
serde = "1.0.98"
serde_derive = "1.0.98"
serde_json = "1.0.40"
subprocess = "0.1.18"
termcolor = "1.0.5"
toml_edit = "0.1.5"
atty = "0.2.13"
Expand Down
43 changes: 36 additions & 7 deletions src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::errors::*;
use crate::registry::{registry_path, registry_url};
use crate::{Dependency, Manifest};
use env_proxy;
use git2::Repository;
use regex::Regex;
use reqwest;
use semver;
Expand Down Expand Up @@ -98,15 +97,45 @@ pub fn update_registry_index(manifest_path: &Path) -> Result<()> {
output.reset()?;
writeln!(output, " '{}' index", registry_url)?;

repo.remote_anonymous(registry_url.as_str())?.fetch(
&["refs/heads/master:refs/remotes/origin/master"],
None,
None,
)?;
let refspec = "refs/heads/master:refs/remotes/origin/master";
fetch_with_cli(&repo, registry_url.as_str(), refspec)?;

Ok(())
}

// https://github.com/rust-lang/cargo/blob/57986eac7157261c33f0123bade7ccd20f15200f/src/cargo/sources/git/utils.rs#L758
fn fetch_with_cli(
repo: &git2::Repository,
url: &str,
refspec: &str,
) -> Result<()> {
let cmd = subprocess::Exec::shell("git").arg("fetch")
.arg("--tags") // fetch all tags
.arg("--force") // handle force pushes
.arg("--update-head-ok") // see discussion in rust-lang/cargo#2078
.arg(url)
.arg(refspec)
// If cargo is run by git (for example, the `exec` command in `git
// rebase`), the GIT_DIR is set by git and will point to the wrong
// location (this takes precedence over the cwd). Make sure this is
// unset so git will look at cwd for the repo.
.env_remove("GIT_DIR")
// The reset of these may not be necessary, but I'm including them
// just to be extra paranoid and avoid any issues.
.env_remove("GIT_WORK_TREE")
.env_remove("GIT_INDEX_FILE")
.env_remove("GIT_OBJECT_DIRECTORY")
.env_remove("GIT_ALTERNATE_OBJECT_DIRECTORIES")
.cwd(repo.path());

let _ = cmd.capture().map_err(|e| match e {
subprocess::PopenError::IoError(io) => ErrorKind::Io(io),
subprocess::PopenError::LogicError(_) |
subprocess::PopenError::Utf8Error(_) => unreachable!("expected only io error"),
})?;
Ok(())
}

#[test]
fn get_latest_stable_version_from_json() {
let versions: Vec<CrateVersion> = serde_json::from_str(
Expand Down Expand Up @@ -215,7 +244,7 @@ fn fuzzy_query_registry_index(
registry_path: impl AsRef<Path>,
) -> Result<Vec<CrateVersion>> {
let crate_name = crate_name.into();
let repo = Repository::open(registry_path)?;
let repo = git2::Repository::open(registry_path)?;
let tree = repo
.find_reference("refs/remotes/origin/master")?
.peel_to_tree()?;
Expand Down

0 comments on commit 34f08ca

Please sign in to comment.