Skip to content

Commit 70eecd3

Browse files
committed
Support update --precise for registry deps
Closes #1105
1 parent 2b9a3d6 commit 70eecd3

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

src/cargo/ops/cargo_generate_lockfile.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,16 @@ pub fn update_lockfile(manifest_path: &Path,
6060
to_avoid.insert(dep);
6161
match opts.precise {
6262
Some(precise) => {
63+
// TODO: see comment in `resolve.rs` as well, but this
64+
// seems like a pretty hokey reason to single out
65+
// the registry as well.
66+
let precise = if dep.get_source_id().is_registry() {
67+
format!("{}={}", dep.get_name(), precise)
68+
} else {
69+
precise.to_string()
70+
};
6371
let precise = dep.get_source_id().clone()
64-
.with_precise(Some(precise.to_string()));
72+
.with_precise(Some(precise));
6573
try!(registry.add_sources(&[precise]));
6674
}
6775
None => {}

src/cargo/sources/registry.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ use std::io::fs::PathExtensions;
163163
use std::collections::HashMap;
164164

165165
use curl::http;
166-
use git2;
167166
use flate2::reader::GzDecoder;
168-
use rustc_serialize::json;
167+
use git2;
169168
use rustc_serialize::hex::ToHex;
169+
use rustc_serialize::json;
170170
use tar::Archive;
171171
use url::Url;
172172

@@ -477,10 +477,26 @@ impl<'a, 'b> Registry for RegistrySource<'a, 'b> {
477477
}
478478
}
479479

480-
let summaries = try!(self.summaries(dep.get_name()));
481-
let mut summaries = summaries.iter().filter(|&&(_, yanked)| {
482-
dep.get_source_id().get_precise().is_some() || !yanked
483-
}).map(|&(ref s, _)| s.clone()).collect::<Vec<_>>();
480+
let mut summaries = {
481+
let summaries = try!(self.summaries(dep.get_name()));
482+
summaries.iter().filter(|&&(_, yanked)| {
483+
dep.get_source_id().get_precise().is_some() || !yanked
484+
}).map(|s| s.0.clone()).collect::<Vec<_>>()
485+
};
486+
487+
// Handle `cargo update --precise` here. If specified, our own source
488+
// will have a precise version listed of the form `<pkg>=<req>` where
489+
// `<pkg>` is the name of a crate on this source and `<req>` is the
490+
// version requested (agument to `--precise`).
491+
summaries.retain(|s| {
492+
match self.source_id.get_precise() {
493+
Some(p) if p.starts_with(dep.get_name()) => {
494+
let vers = &p[dep.get_name().len() + 1..];
495+
s.get_version().to_string() == vers
496+
}
497+
_ => true,
498+
}
499+
});
484500
summaries.query(dep)
485501
}
486502
}
@@ -491,8 +507,10 @@ impl<'a, 'b> Source for RegistrySource<'a, 'b> {
491507
// to look for, so we always atempt to perform an update here.
492508
//
493509
// If we have a precise version, then we'll update lazily during the
494-
// querying phase.
495-
if self.source_id.get_precise().is_none() {
510+
// querying phase. Note that precise in this case is only
511+
// `Some("locked")` as other `Some` values indicate a `cargo update
512+
// --precise` request
513+
if self.source_id.get_precise() != Some("locked") {
496514
try!(self.do_update());
497515
}
498516
Ok(())

tests/test_cargo_registry.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,23 +462,43 @@ test!(update_lockfile {
462462
.file("src/main.rs", "fn main() {}");
463463
p.build();
464464

465+
println!("0.0.1");
465466
r::mock_pkg("bar", "0.0.1", &[]);
466467
assert_that(p.process(cargo_dir().join("cargo")).arg("build"),
467468
execs().with_status(0));
468469

469470
r::mock_pkg("bar", "0.0.2", &[]);
471+
r::mock_pkg("bar", "0.0.3", &[]);
470472
fs::rmdir_recursive(&paths::home().join(".cargo/registry")).unwrap();
473+
println!("0.0.2 update");
471474
assert_that(p.process(cargo_dir().join("cargo")).arg("update")
472-
.arg("-p").arg("bar"),
475+
.arg("-p").arg("bar").arg("--precise").arg("0.0.2"),
473476
execs().with_status(0).with_stdout(format!("\
474477
{updating} registry `[..]`
475478
", updating = UPDATING).as_slice()));
476479

480+
println!("0.0.2 build");
477481
assert_that(p.process(cargo_dir().join("cargo")).arg("build"),
478482
execs().with_status(0).with_stdout(format!("\
479483
{downloading} [..] v0.0.2 (registry file://[..])
480484
{compiling} bar v0.0.2 (registry file://[..])
481485
{compiling} foo v0.0.1 ({dir})
486+
", downloading = DOWNLOADING, compiling = COMPILING,
487+
dir = p.url()).as_slice()));
488+
489+
println!("0.0.3 update");
490+
assert_that(p.process(cargo_dir().join("cargo")).arg("update")
491+
.arg("-p").arg("bar"),
492+
execs().with_status(0).with_stdout(format!("\
493+
{updating} registry `[..]`
494+
", updating = UPDATING).as_slice()));
495+
496+
println!("0.0.3 build");
497+
assert_that(p.process(cargo_dir().join("cargo")).arg("build"),
498+
execs().with_status(0).with_stdout(format!("\
499+
{downloading} [..] v0.0.3 (registry file://[..])
500+
{compiling} bar v0.0.3 (registry file://[..])
501+
{compiling} foo v0.0.1 ({dir})
482502
", downloading = DOWNLOADING, compiling = COMPILING,
483503
dir = p.url()).as_slice()));
484504
});

0 commit comments

Comments
 (0)