Skip to content

Commit

Permalink
Pick the checkout for the right host, too
Browse files Browse the repository at this point in the history
Ref: #107
  • Loading branch information
nabijaczleweli committed Sep 10, 2019
1 parent f8e5ebd commit add8276
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&registry_url)).map_err(|e| {
println!("Couldn't get package repository: {}.", e);
2
})?;
Expand All @@ -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),
&registry_url,
http_proxy.as_ref().map(String::as_str),
&mut stdout()).map_err(|e| {
println!("Failed to update index repository: {}.", e);
Expand Down
15 changes: 12 additions & 3 deletions src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,8 @@ fn crate_versions_impl(buf: String) -> Vec<Semver> {
.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.
///
Expand All @@ -676,15 +677,23 @@ fn crate_versions_impl(buf: String) -> Vec<Semver> {
/// # 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<PathBuf, &'static str> {
pub fn get_index_path(cargo_dir: &Path, registry_url: Option<&str>) -> Result<PathBuf, &'static str> {
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())
Expand Down

0 comments on commit add8276

Please sign in to comment.