Skip to content

Commit c107161

Browse files
committed
Detect changes using crates.io index repository
This assures that no matter how often we poll, we will always see all changes since the last time we fetched. Fixes #89
1 parent 3b8526a commit c107161

File tree

5 files changed

+26
-62
lines changed

5 files changed

+26
-62
lines changed

Cargo.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ rustc-serialize = "0.3"
1313
regex = "0.1"
1414
postgres = { version = "0.11", features = [ "time", "rustc-serialize" ] }
1515
clap = "2.14"
16+
crates-index-diff = "2.0.0"
17+
git2 = "0.4"
1618
time = "0.1"
1719
hyper = "0.9"
1820
semver = "0.5"

src/docbuilder/queue.rs

Lines changed: 7 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,24 @@
22
33

44
use super::DocBuilder;
5-
use rustc_serialize::json::{Json, Array};
6-
use hyper;
7-
use hyper::header::{Accept, qitem};
85
use db::connect_db;
96
use errors::*;
7+
use crates_index_diff::{ChangeKind, Index};
108

119

1210
impl DocBuilder {
1311
/// Updates crates.io-index repository and adds new crates into build queue
1412
pub fn get_new_crates(&mut self) -> Result<()> {
1513
try!(self.load_database_cache());
16-
17-
let body = {
18-
use std::io::Read;
19-
let client = hyper::Client::new();
20-
let mut res = try!(client.get("https://crates.io/summary")
21-
.header(Accept(vec![qitem("application/json".parse().unwrap())]))
22-
.send());
23-
let mut body = String::new();
24-
try!(res.read_to_string(&mut body));
25-
body
26-
};
27-
28-
let json = try!(Json::from_str(&body));
29-
30-
let crates = {
31-
let mut crates: Vec<(String, String)> = Vec::new();
32-
for section in ["just_updated", "new_crates"].iter() {
33-
match json.as_object()
34-
.and_then(|o| o.get(&section[..]))
35-
.and_then(|j| j.as_array())
36-
.map(get_crates_from_array) {
37-
Some(mut c) => crates.append(c.as_mut()),
38-
None => continue,
39-
}
40-
}
41-
crates
42-
};
43-
4414
let conn = try!(connect_db());
45-
for (name, version) in crates {
46-
if self.db_cache.contains(&format!("{}-{}", name, version)[..]) {
15+
let index = try!(Index::from_path_or_cloned(&self.options.crates_io_index_path));
16+
let changes = try!(index.fetch_changes());
17+
for krate in changes.iter().filter(|k| k.kind != ChangeKind::Yanked) {
18+
if self.db_cache.contains(&format!("{}-{}", krate.name, krate.version)) {
4719
continue;
4820
}
49-
let _ = conn.execute("INSERT INTO queue (name, version) VALUES ($1, $2)",
50-
&[&name, &version]);
21+
conn.execute("INSERT INTO queue (name, version) VALUES ($1, $2)",
22+
&[&krate.name, &krate.version]).ok();
5123
}
5224

5325
Ok(())
@@ -80,33 +52,6 @@ impl DocBuilder {
8052
}
8153
}
8254

83-
84-
/// Returns Vec<CRATE_NAME, CRATE_VERSION> from a summary array
85-
fn get_crates_from_array<'a>(crates: &'a Array) -> Vec<(String, String)> {
86-
let mut crates_vec: Vec<(String, String)> = Vec::new();
87-
for crte in crates {
88-
let name = match crte.as_object()
89-
.and_then(|o| o.get("id"))
90-
.and_then(|i| i.as_string())
91-
.map(|s| s.to_owned()) {
92-
Some(s) => s,
93-
None => continue,
94-
};
95-
let version = match crte.as_object()
96-
.and_then(|o| o.get("max_version"))
97-
.and_then(|v| v.as_string())
98-
.map(|s| s.to_owned()) {
99-
Some(s) => s,
100-
None => continue,
101-
};
102-
crates_vec.push((name, version));
103-
}
104-
crates_vec
105-
}
106-
107-
108-
109-
11055
#[cfg(test)]
11156
mod test {
11257
extern crate env_logger;

src/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use postgres;
66
use cargo;
77
use hyper;
88
use magic::MagicError;
9+
use git2;
910

1011

1112
error_chain! {
@@ -22,6 +23,7 @@ error_chain! {
2223
postgres::error::ConnectError, PostgresConnectError;
2324
postgres::error::Error, PostgresError;
2425
hyper::Error, HyperError;
26+
git2::Error, Git2Error;
2527
MagicError, MagicError;
2628
Box<cargo::CargoError>, CargoError;
2729
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ extern crate url;
2525
extern crate params;
2626
extern crate libc;
2727
extern crate badge;
28+
extern crate crates_index_diff;
29+
extern crate git2;
2830

2931
pub use self::docbuilder::DocBuilder;
3032
pub use self::docbuilder::ChrootBuilderResult;

0 commit comments

Comments
 (0)