Skip to content

Commit

Permalink
Follow [source.$SRCNAME] specs in Cargo config
Browse files Browse the repository at this point in the history
Ref: #107
  • Loading branch information
nabijaczleweli committed Sep 10, 2019
1 parent cc540fa commit f8e5ebd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn actual_main() -> Result<(), i32> {
2
})?;
cargo_update::ops::update_index(&mut registry_repo,
"https://github.com/rust-lang/crates.io-index",
&cargo_update::ops::get_index_url(&crates_file),
http_proxy.as_ref().map(String::as_str),
&mut stdout()).map_err(|e| {
println!("Failed to update index repository: {}.", e);
Expand Down
31 changes: 30 additions & 1 deletion src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::fs::{self, DirEntry, File};
use std::path::{PathBuf, Path};
use std::io::{Write, Read};
use std::time::SystemTime;
use std::borrow::Cow;
use std::{cmp, env};
use regex::Regex;
use url::Url;
Expand Down Expand Up @@ -364,7 +365,7 @@ impl GitRepoPackage {
let repo = if clone_dir.exists() {
let mut r = Repository::open(clone_dir);
if let Ok(ref mut r) = r.as_mut() {
let rm = r.find_remote("origin")
r.find_remote("origin")
.or_else(|_| r.remote_anonymous(&self.url))
.and_then(|mut rm| {
with_authentication(&self.url, |creds| {
Expand Down Expand Up @@ -772,6 +773,34 @@ fn fetch_options_from_proxy_url_and_callbacks<'a>(proxy_url: Option<&str>, callb
ret
}

/// Get the URL to update index from the config file parallel to the specified crates file
///
/// Get `source.crates-io.registry` or follow `source.crates-io.replace-with`,
/// to `source.$SRCNAME.registry` or follow `source.$SRCNAME.replace-with`,
/// et caetera.
///
/// Defaults to `https://github.com/rust-lang/crates.io-index`
///
/// Consult [#107](https://github.com/nabijaczleweli/cargo-update/issues/107) for details
pub fn get_index_url(crates_file: &Path) -> Cow<'static, str> {
get_index_url_impl(crates_file).map(Cow::from).unwrap_or(Cow::from("https://github.com/rust-lang/crates.io-index"))
}

fn get_index_url_impl(crates_file: &Path) -> Option<String> {
let config = fs::read_to_string(crates_file.with_file_name("config")).ok()?;
let config = toml::from_str::<toml::Value>(&config).ok()?;

let sources = config.get("source")?;

let mut cur_source = sources.get("crates-io")?;
loop {
match cur_source.get("replace-with") {
Some(redir) => cur_source = sources.get(redir.as_str()?)?,
None => return Some(cur_source.get("registry")?.as_str()?.to_string()),
}
}
}

/// Based on
/// https://github.com/rust-lang/cargo/blob/bb28e71202260180ecff658cd0fa0c7ba86d0296/src/cargo/sources/git/utils.rs#L344
/// and
Expand Down

0 comments on commit f8e5ebd

Please sign in to comment.