Skip to content

Commit 5bb8501

Browse files
committed
Auto merge of #5621 - knight42:cargo-search-relaced-registry, r=alexcrichton
Update replaced registry before search Close #5550. It seems that updating the replaced registry before search has not been well considered in cargo and I have to add a function to trait `core::source::Source` to get the replaced `SourceId`. I am not sure whether this is a good design, any advice is welcome.
2 parents f102a24 + 7d279fe commit 5bb8501

File tree

5 files changed

+174
-202
lines changed

5 files changed

+174
-202
lines changed

src/cargo/core/source/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ pub trait Source {
1414
/// Returns the `SourceId` corresponding to this source
1515
fn source_id(&self) -> &SourceId;
1616

17+
/// Returns the replaced `SourceId` corresponding to this source
18+
fn replaced_source_id(&self) -> &SourceId {
19+
self.source_id()
20+
}
21+
1722
/// Returns whether or not this source will return summaries with
1823
/// checksums listed.
1924
fn supports_checksums(&self) -> bool;
@@ -95,6 +100,11 @@ impl<'a, T: Source + ?Sized + 'a> Source for Box<T> {
95100
(**self).source_id()
96101
}
97102

103+
/// Forwards to `Source::replaced_source_id`
104+
fn replaced_source_id(&self) -> &SourceId {
105+
(**self).replaced_source_id()
106+
}
107+
98108
/// Forwards to `Source::update`
99109
fn update(&mut self) -> CargoResult<()> {
100110
(**self).update()

src/cargo/ops/registry.rs

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
use std::{cmp, env};
21
use std::collections::BTreeMap;
32
use std::fs::{self, File};
43
use std::iter::repeat;
54
use std::time::Duration;
5+
use std::{cmp, env};
66

77
use curl::easy::{Easy, SslOpt};
88
use git2;
99
use registry::{NewCrate, NewCrateDependency, Registry};
1010

1111
use url::percent_encoding::{percent_encode, QUERY_ENCODE_SET};
1212

13-
use version;
14-
use core::source::Source;
15-
use core::{Package, SourceId, Workspace};
1613
use core::dependency::Kind;
1714
use core::manifest::ManifestMetadata;
15+
use core::source::Source;
16+
use core::{Package, SourceId, Workspace};
1817
use ops;
19-
use sources::RegistrySource;
18+
use sources::{RegistrySource, SourceConfigMap};
2019
use util::config::{self, Config};
21-
use util::paths;
22-
use util::ToUrl;
2320
use util::errors::{CargoResult, CargoResultExt};
2421
use util::important_paths::find_root_manifest_for_wd;
22+
use util::paths;
23+
use util::ToUrl;
24+
use version;
2525

2626
pub struct RegistryConfig {
2727
pub index: Option<String>,
@@ -311,11 +311,7 @@ pub fn registry(
311311
index: index_config,
312312
} = registry_configuration(config, registry.clone())?;
313313
let token = token.or(token_config);
314-
let sid = match (index_config, index, registry) {
315-
(_, _, Some(registry)) => SourceId::alt_registry(config, &registry)?,
316-
(Some(index), _, _) | (None, Some(index), _) => SourceId::for_registry(&index.to_url()?)?,
317-
(None, None, _) => SourceId::crates_io(config)?,
318-
};
314+
let sid = get_source_id(config, index_config.or(index), registry)?;
319315
let api_host = {
320316
let mut src = RegistrySource::remote(&sid, config);
321317
src.update()
@@ -354,10 +350,11 @@ pub fn needs_custom_http_transport(config: &Config) -> CargoResult<bool> {
354350
let check_revoke = config.get_bool("http.check-revoke")?;
355351
let user_agent = config.get_string("http.user-agent")?;
356352

357-
Ok(
358-
proxy_exists || timeout.is_some() || cainfo.is_some() || check_revoke.is_some()
359-
|| user_agent.is_some(),
360-
)
353+
Ok(proxy_exists
354+
|| timeout.is_some()
355+
|| cainfo.is_some()
356+
|| check_revoke.is_some()
357+
|| user_agent.is_some())
361358
}
362359

363360
/// Configure a libcurl http handle with the defaults options for Cargo
@@ -553,6 +550,22 @@ pub fn yank(
553550
Ok(())
554551
}
555552

553+
fn get_source_id(
554+
config: &Config,
555+
index: Option<String>,
556+
reg: Option<String>,
557+
) -> CargoResult<SourceId> {
558+
match (reg, index) {
559+
(Some(r), _) => SourceId::alt_registry(config, &r),
560+
(_, Some(i)) => SourceId::for_registry(&i.to_url()?),
561+
_ => {
562+
let map = SourceConfigMap::new(config)?;
563+
let src = map.load(&SourceId::crates_io(config)?)?;
564+
Ok(src.replaced_source_id().clone())
565+
}
566+
}
567+
}
568+
556569
pub fn search(
557570
query: &str,
558571
config: &Config,
@@ -572,7 +585,22 @@ pub fn search(
572585
prefix
573586
}
574587

575-
let (mut registry, _) = registry(config, None, index, reg)?;
588+
let sid = get_source_id(config, index, reg)?;
589+
590+
let mut regsrc = RegistrySource::remote(&sid, config);
591+
let cfg = match regsrc.config() {
592+
Ok(c) => c,
593+
Err(_) => {
594+
regsrc
595+
.update()
596+
.chain_err(|| format!("failed to update {}", &sid))?;
597+
regsrc.config()?
598+
}
599+
};
600+
601+
let api_host = cfg.unwrap().api.unwrap();
602+
let handle = http_handle(config)?;
603+
let mut registry = Registry::new_handle(api_host, None, handle);
576604
let (crates, total_crates) = registry
577605
.search(query, limit)
578606
.chain_err(|| "failed to retrieve search results from the registry")?;

src/cargo/sources/replaced.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ impl<'cfg> Source for ReplacedSource<'cfg> {
6060
&self.to_replace
6161
}
6262

63+
fn replaced_source_id(&self) -> &SourceId {
64+
&self.replace_with
65+
}
66+
6367
fn update(&mut self) -> CargoResult<()> {
6468
self.inner
6569
.update()

0 commit comments

Comments
 (0)