Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch GitHub commits by long hash more efficiently #10079

Merged
merged 1 commit into from
Jun 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/cargo/sources/git/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,8 +815,11 @@ pub fn fetch(
}

GitReference::Rev(rev) => {
let is_github = || Url::parse(url).map_or(false, |url| is_github(&url));
if rev.starts_with("refs/") {
refspecs.push(format!("+{0}:{0}", rev));
} else if is_github() && is_long_hash(rev) {
refspecs.push(format!("+{0}:refs/commit/{0}", rev));
} else {
// We don't know what the rev will point to. To handle this
// situation we fetch all branches and tags, and then we pray
Expand Down Expand Up @@ -1036,7 +1039,7 @@ fn github_up_to_date(
config: &Config,
) -> CargoResult<bool> {
let url = Url::parse(url)?;
if url.host_str() != Some("github.com") {
if !is_github(&url) {
return Ok(false);
}

Expand All @@ -1047,6 +1050,8 @@ fn github_up_to_date(
GitReference::Rev(rev) => {
if rev.starts_with("refs/") {
rev
} else if is_long_hash(rev) {
return Ok(reference.resolve(repo).is_ok());
} else {
debug!("can't use github fast path with `rev = \"{}\"`", rev);
return Ok(false);
Expand Down Expand Up @@ -1089,3 +1094,11 @@ fn github_up_to_date(
handle.perform()?;
Ok(handle.response_code()? == 304)
}

fn is_github(url: &Url) -> bool {
url.host_str() == Some("github.com")
}

fn is_long_hash(rev: &str) -> bool {
rev.len() == 40 && rev.chars().all(|ch| ch.is_ascii_hexdigit())
}