Skip to content

Commit b453892

Browse files
committed
Add shared function for getting the latest SHA of a git repository
1 parent 027ecfb commit b453892

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

collector/src/bin/collector.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,15 +1274,8 @@ fn main_result() -> anyhow::Result<i32> {
12741274
}
12751275

12761276
Commands::InstallNext { codegen_backends } => {
1277-
let last_sha = Command::new("git")
1278-
.arg("ls-remote")
1279-
.arg("https://github.com/rust-lang/rust.git")
1280-
.arg("master")
1281-
.output()
1282-
.unwrap();
1283-
let last_sha = String::from_utf8(last_sha.stdout).expect("utf8");
1284-
let last_sha = last_sha.split_whitespace().next().expect(&last_sha);
1285-
let commit = get_commit_or_fake_it(last_sha).expect("success");
1277+
let last_sha = get_latest_sha("https://github.com/rust-lang/rust").unwrap();
1278+
let commit = get_commit_or_fake_it(&last_sha).expect("success");
12861279

12871280
let rt = build_async_runtime();
12881281
let mut sysroot = rt
@@ -1559,21 +1552,8 @@ fn needs_git_update(collector: &CollectorConfig) -> bool {
15591552
return false;
15601553
};
15611554

1562-
let mut cmd = Command::new("git");
1563-
cmd.arg("ls-remote")
1564-
.arg("https://github.com/rust-lang/rustc-perf")
1565-
.arg("HEAD");
1566-
let upstream_sha = match command_output(&mut cmd) {
1567-
Ok(output) => String::from_utf8(output.stdout)
1568-
.unwrap()
1569-
.split_whitespace()
1570-
.next()
1571-
.unwrap()
1572-
.to_string(),
1573-
Err(error) => {
1574-
log::error!("Cannot determine latest SHA of rustc-perf: {error:?}");
1575-
return false;
1576-
}
1555+
let Ok(upstream_sha) = get_latest_sha("https://github.com/rust-lang/rustc-perf") else {
1556+
return false;
15771557
};
15781558
if commit_sha != upstream_sha {
15791559
log::warn!(
@@ -1585,6 +1565,23 @@ fn needs_git_update(collector: &CollectorConfig) -> bool {
15851565
}
15861566
}
15871567

1568+
/// Returns the latest known sha of the default branch of the specified `repo`.
1569+
fn get_latest_sha(repo: &str) -> anyhow::Result<String> {
1570+
let mut cmd = Command::new("git");
1571+
cmd.arg("ls-remote").arg(repo).arg("HEAD");
1572+
match command_output(&mut cmd) {
1573+
Ok(output) => Ok(String::from_utf8(output.stdout)?
1574+
.split_whitespace()
1575+
.next()
1576+
.unwrap()
1577+
.to_string()),
1578+
Err(error) => {
1579+
log::error!("Cannot determine latest SHA of {repo}: {error:?}");
1580+
Err(error)
1581+
}
1582+
}
1583+
}
1584+
15881585
/// Error that happened during benchmarking of a job.
15891586
enum BenchmarkJobError {
15901587
/// The error is non-recoverable.

0 commit comments

Comments
 (0)