From add8276d2cf06598a3a6fef6cc2a1fdb3a124443 Mon Sep 17 00:00:00 2001 From: nabijaczleweli Date: Mon, 9 Sep 2019 00:07:17 +0200 Subject: [PATCH] Pick the checkout for the right host, too Ref: #107 --- src/main.rs | 5 +++-- src/ops/mod.rs | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 79700df7e7..d3cba3d17d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -68,7 +68,8 @@ fn actual_main() -> Result<(), i32> { (false, false) => packages = cargo_update::ops::intersect_packages(&packages, &opts.to_update, opts.install), } - let registry = cargo_update::ops::get_index_path(&opts.cargo_dir.1).map_err(|e| { + let registry_url = cargo_update::ops::get_index_url(&crates_file); + let registry = cargo_update::ops::get_index_path(&opts.cargo_dir.1, Some(®istry_url)).map_err(|e| { println!("Couldn't get package repository: {}.", e); 2 })?; @@ -77,7 +78,7 @@ fn actual_main() -> Result<(), i32> { 2 })?; cargo_update::ops::update_index(&mut registry_repo, - &cargo_update::ops::get_index_url(&crates_file), + ®istry_url, http_proxy.as_ref().map(String::as_str), &mut stdout()).map_err(|e| { println!("Failed to update index repository: {}.", e); diff --git a/src/ops/mod.rs b/src/ops/mod.rs index 142b6b6488..359ee62711 100644 --- a/src/ops/mod.rs +++ b/src/ops/mod.rs @@ -658,7 +658,8 @@ fn crate_versions_impl(buf: String) -> Vec { .collect() } -/// Get the location of the latest registry index in the specified cargo directory. +/// Get the location of the latest registry index whose name optionally starts with the registry URL's domain name in the +/// specified cargo directory. /// /// If no indices exist, an appropriate `Err` is returned. /// @@ -676,15 +677,23 @@ fn crate_versions_impl(buf: String) -> Vec { /// # let _ = fs::create_dir(&cargo_dir); /// # let idx_dir = cargo_dir.join("registry").join("index").join("github.com-1ecc6299db9ec823"); /// # let _ = fs::create_dir_all(&idx_dir); -/// let index = get_index_path(&cargo_dir).unwrap(); +/// // These are equivalent for most users, +/// // but you might need to specify the URL to find the right checkout if you use more than one registry +/// let index = get_index_path(&cargo_dir, None).unwrap(); +/// let index = get_index_path(&cargo_dir, Some("https://github.com/rust-lang/crates.io-index")).unwrap(); +/// /// // Use find_package_data() to look for packages /// # assert_eq!(index, idx_dir); /// ``` -pub fn get_index_path(cargo_dir: &Path) -> Result { +pub fn get_index_path(cargo_dir: &Path, registry_url: Option<&str>) -> Result { + let registry_url = registry_url.map(|u| Url::parse(u).map_err(|_| "registry URL not an URL")).transpose()?; + let registry_host = registry_url.as_ref().and_then(|u| u.host_str()); + Ok(fs::read_dir(cargo_dir.join("registry").join("index")) .map_err(|_| "index directory nonexistant")? .map(Result::unwrap) .filter(|i| i.file_type().unwrap().is_dir()) + .filter(|i| registry_host.map(|rh| i.file_name().to_string_lossy().starts_with(rh)).unwrap_or(true)) .max_by_key(latest_modified) .ok_or("empty index directory")? .path())