Skip to content

Commit 1d6cfb7

Browse files
authored
Merge pull request #152 from kraktus/refactor
Code refactor
2 parents 7c7c405 + 62c8c78 commit 1d6cfb7

File tree

8 files changed

+277
-450
lines changed

8 files changed

+277
-450
lines changed

Cargo.lock

-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ name = "cargo-bisect-rustc"
99
readme = "README.md"
1010
repository = "https://github.com/rust-lang/cargo-bisect-rustc"
1111
version = "0.6.3"
12-
edition = "2018"
12+
edition = "2021"
1313

1414
[dependencies]
1515
dialoguer = "0.6.2"
@@ -20,7 +20,6 @@ flate2 = "1.0.14"
2020
git2 = "0.13.5"
2121
log = "0.4"
2222
pbr = "1.0.2"
23-
regex ="1.3.7"
2423
reqwest = { version = "0.10.4", features = ["blocking", "json"] }
2524
rustc_version = "0.2.3"
2625
serde = { version = "1.0.110", features = ["derive"] }
@@ -32,7 +31,6 @@ tempdir = "0.3.7"
3231
xz2 = "0.1.6"
3332
chrono = "0.4.11"
3433
colored = "2"
35-
once_cell = "1.4.1"
3634

3735
[dev-dependencies]
3836
quickcheck = "0.9.2"

src/git.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl Commit {
2020
fn from_git2_commit(commit: &mut Git2Commit<'_>) -> Self {
2121
Commit {
2222
sha: commit.id().to_string(),
23-
date: Utc.timestamp(commit.time().seconds(), 0),
23+
date: Utc.timestamp(commit.time().seconds(), 0).date(),
2424
summary: String::from_utf8_lossy(commit.summary_bytes().unwrap()).to_string(),
2525
}
2626
}
@@ -51,8 +51,7 @@ fn lookup_rev<'rev>(repo: &'rev RustcRepo, rev: &str) -> Result<Git2Commit<'rev>
5151
.id();
5252
let revision_id = revision
5353
.as_tag()
54-
.map(|tag| tag.target_id())
55-
.unwrap_or_else(|| revision.id());
54+
.map_or_else(|| revision.id(), git2::Tag::target_id);
5655

5756
let common_base = repo.merge_base(master_id, revision_id)?;
5857

@@ -109,13 +108,8 @@ fn find_origin_remote(repo: &Repository) -> Result<String, Error> {
109108
repo.remotes()?
110109
.iter()
111110
.filter_map(|name| name.and_then(|name| repo.find_remote(name).ok()))
112-
.find(|remote| {
113-
remote
114-
.url()
115-
.map(|url| url.contains(RUST_SRC_URL))
116-
.unwrap_or(false)
117-
})
118-
.and_then(|remote| remote.name().map(|name| name.to_string()))
111+
.find(|remote| remote.url().map_or(false, |url| url.contains(RUST_SRC_URL)))
112+
.and_then(|remote| remote.name().map(std::string::ToString::to_string))
119113
.ok_or_else(|| {
120114
failure::format_err!(
121115
"rust-lang/rust remote not found. \

src/github.rs

+81-76
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use failure::Error;
1+
use failure::{Error, format_err};
22
use reqwest::{self, blocking::Client, blocking::Response};
33
use serde::{Deserialize, Serialize};
44

5-
use crate::Commit;
5+
use crate::{Commit, GitDate, parse_to_utc_date};
66

77
#[derive(Serialize, Deserialize, Debug)]
88
struct GithubCommitComparison {
@@ -26,11 +26,12 @@ struct GithubAuthor {
2626
name: String,
2727
}
2828

29-
type GitDate = chrono::DateTime<chrono::Utc>;
30-
3129
impl GithubCommitElem {
3230
fn date(&self) -> Result<GitDate, Error> {
33-
Ok(self.commit.committer.date.parse()?)
31+
let (date_str, _) = self.commit.committer.date.split_once("T").ok_or_else(|| {
32+
format_err!("commit date should folllow the ISO 8061 format, eg: 2022-05-04T09:55:51Z")
33+
})?;
34+
Ok(parse_to_utc_date(date_str)?)
3435
}
3536

3637
fn git_commit(self) -> Result<Commit, Error> {
@@ -74,9 +75,58 @@ pub(crate) struct CommitsQuery<'a> {
7475
/// Returns the bors merge commits between the two specified boundaries
7576
/// (boundaries inclusive).
7677
77-
impl<'a> CommitsQuery<'a> {
78+
impl CommitsQuery<'_> {
7879
pub fn get_commits(&self) -> Result<Vec<Commit>, Error> {
79-
get_commits(*self)
80+
// build up commit sequence, by feeding in `sha` as the starting point, and
81+
// working way backwards to max(`self.since_date`, `self.earliest_sha`).
82+
let mut commits = Vec::new();
83+
84+
// focus on Pull Request merges, all authored and committed by bors.
85+
let author = "bors";
86+
87+
let client = Client::builder().default_headers(headers()?).build()?;
88+
for page in 1.. {
89+
let url = CommitsUrl {
90+
page,
91+
author,
92+
since: self.since_date,
93+
sha: self.most_recent_sha,
94+
}
95+
.url();
96+
97+
let response: Response = client.get(&url).send()?;
98+
99+
let action = parse_paged_elems(response, |elem: GithubCommitElem| {
100+
let date = elem.date()?;
101+
let sha = elem.sha.clone();
102+
let summary = elem.commit.message;
103+
let commit = Commit { sha, date, summary };
104+
commits.push(commit);
105+
106+
Ok(if elem.sha == self.earliest_sha {
107+
eprintln!(
108+
"ending github query because we found starting sha: {}",
109+
elem.sha
110+
);
111+
Loop::Break
112+
} else {
113+
Loop::Next
114+
})
115+
})?;
116+
117+
if let Loop::Break = action {
118+
break;
119+
}
120+
}
121+
122+
eprintln!(
123+
"get_commits_between returning commits, len: {}",
124+
commits.len()
125+
);
126+
127+
// reverse to obtain chronological order
128+
commits.reverse();
129+
Ok(commits)
80130
}
81131
}
82132

@@ -97,7 +147,7 @@ struct CommitDetailsUrl<'a> {
97147
sha: &'a str,
98148
}
99149

100-
impl<'a> ToUrl for CommitsUrl<'a> {
150+
impl ToUrl for CommitsUrl<'_> {
101151
fn url(&self) -> String {
102152
format!(
103153
"https://api.github.com/repos/{OWNER}/{REPO}/commits\
@@ -114,7 +164,7 @@ impl<'a> ToUrl for CommitsUrl<'a> {
114164
}
115165
}
116166

117-
impl<'a> ToUrl for CommitDetailsUrl<'a> {
167+
impl ToUrl for CommitDetailsUrl<'_> {
118168
fn url(&self) -> String {
119169
// "origin/master" is set as `sha` when there is no `--end=` definition
120170
// specified on the command line. We define the GitHub master branch
@@ -134,92 +184,47 @@ impl<'a> ToUrl for CommitDetailsUrl<'a> {
134184
}
135185
}
136186

137-
fn get_commits(q: CommitsQuery) -> Result<Vec<Commit>, Error> {
138-
// build up commit sequence, by feeding in `sha` as the starting point, and
139-
// working way backwards to max(`q.since_date`, `q.earliest_sha`).
140-
let mut commits = Vec::new();
141-
142-
// focus on Pull Request merges, all authored and committed by bors.
143-
let author = "bors";
144-
145-
let client = Client::builder().default_headers(headers()?).build()?;
146-
for page in 1.. {
147-
let url = CommitsUrl {
148-
page,
149-
author,
150-
since: q.since_date,
151-
sha: q.most_recent_sha,
152-
}
153-
.url();
154-
155-
let response: Response = client.get(&url).send()?;
156-
157-
let action = parse_paged_elems(response, |elem: GithubCommitElem| {
158-
let date: chrono::DateTime<chrono::Utc> = match elem.commit.committer.date.parse() {
159-
Ok(date) => date,
160-
Err(err) => return Loop::Err(err.into()),
161-
};
162-
let sha = elem.sha.clone();
163-
let summary = elem.commit.message;
164-
let commit = Commit { sha, date, summary };
165-
commits.push(commit);
166-
167-
if elem.sha == q.earliest_sha {
168-
eprintln!(
169-
"ending github query because we found starting sha: {}",
170-
elem.sha
171-
);
172-
return Loop::Break;
173-
}
174-
175-
Loop::Next
176-
})?;
177-
178-
if let Loop::Break = action {
179-
break;
180-
}
181-
}
182-
183-
eprintln!(
184-
"get_commits_between returning commits, len: {}",
185-
commits.len()
186-
);
187-
188-
// reverse to obtain chronological order
189-
commits.reverse();
190-
Ok(commits)
191-
}
192-
193-
enum Loop<E> {
187+
enum Loop {
194188
Break,
195189
Next,
196-
Err(E),
197190
}
198-
enum Void {}
199191

200-
fn parse_paged_elems<Elem: for<'a> serde::Deserialize<'a>>(
192+
fn parse_paged_elems(
201193
response: Response,
202-
mut k: impl FnMut(Elem) -> Loop<Error>,
203-
) -> Result<Loop<Void>, Error> {
204-
// parse the JSON into an array of the expected Elem type
205-
let elems: Vec<Elem> = response.json()?;
194+
mut k: impl FnMut(GithubCommitElem) -> Result<Loop, Error>,
195+
) -> Result<Loop, Error> {
196+
let elems: Vec<GithubCommitElem> = response.json()?;
206197

207198
if elems.is_empty() {
208199
// we've run out of useful pages to lookup
209200
return Ok(Loop::Break);
210201
}
211202

212-
for elem in elems.into_iter() {
213-
let act = k(elem);
203+
for elem in elems {
204+
let act = k(elem)?;
214205

215206
// the callback will tell us if we should terminate loop early (e.g. due to matching `sha`)
216207
match act {
217208
Loop::Break => return Ok(Loop::Break),
218-
Loop::Err(e) => return Err(e),
219209
Loop::Next => continue,
220210
}
221211
}
222212

223213
// by default, we keep searching on next page from github.
224214
Ok(Loop::Next)
225215
}
216+
217+
#[cfg(test)]
218+
mod tests {
219+
use super::*;
220+
221+
#[test]
222+
fn test_github() {
223+
let c = get_commit("25674202bb7415e0c0ecd07856749cfb7f591be6").unwrap();
224+
let expected_c = Commit { sha: "25674202bb7415e0c0ecd07856749cfb7f591be6".to_string(),
225+
date: parse_to_utc_date("2022-05-04").unwrap(),
226+
summary: "Auto merge of #96695 - JohnTitor:rollup-oo4fc1h, r=JohnTitor\n\nRollup of 6 pull requests\n\nSuccessful merges:\n\n - #96597 (openbsd: unbreak build on native platform)\n - #96662 (Fix typo in lint levels doc)\n - #96668 (Fix flaky rustdoc-ui test because it did not replace time result)\n - #96679 (Quick fix for #96223.)\n - #96684 (Update `ProjectionElem::Downcast` documentation)\n - #96686 (Add some TAIT-related tests)\n\nFailed merges:\n\nr? `@ghost`\n`@rustbot` modify labels: rollup".to_string()
227+
};
228+
assert_eq!(c, expected_c)
229+
}
230+
}

src/least_satisfying.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ where
2424
if rm_no + 1 == lm_yes {
2525
return lm_yes;
2626
}
27-
for (left, right) in unknown_ranges.iter().cloned() {
27+
for (left, right) in unknown_ranges.iter().copied() {
2828
// if we're straddling an unknown range, then pretend it doesn't exist
2929
if rm_no + 1 == left && right + 1 == lm_yes {
3030
return lm_yes;
@@ -68,12 +68,15 @@ where
6868

6969
#[cfg(test)]
7070
mod tests {
71-
use super::Satisfies::*;
71+
use super::Satisfies::{No, Unknown, Yes};
7272
use super::{least_satisfying, Satisfies};
7373
use quickcheck::{QuickCheck, TestResult};
7474

7575
fn prop(xs: Vec<Option<bool>>) -> TestResult {
76-
let mut satisfies_v = xs.into_iter().map(|o| o.into()).collect::<Vec<Satisfies>>();
76+
let mut satisfies_v = xs
77+
.into_iter()
78+
.map(std::convert::Into::into)
79+
.collect::<Vec<Satisfies>>();
7780
satisfies_v.insert(0, Satisfies::No);
7881
satisfies_v.push(Satisfies::Yes);
7982

0 commit comments

Comments
 (0)