Skip to content

Commit

Permalink
Auto merge of #13969 - weihanglo:full-commit-sha, r=epage
Browse files Browse the repository at this point in the history
fix: check if rev is full commit sha for github fast path
  • Loading branch information
bors committed May 27, 2024
2 parents 37e5f13 + 26c88db commit 95eeafa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
12 changes: 4 additions & 8 deletions src/cargo/sources/git/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::core::global_cache_tracker;
use crate::core::GitReference;
use crate::core::SourceId;
use crate::core::{Dependency, Package, PackageId};
use crate::sources::git::utils::rev_to_oid;
use crate::sources::git::utils::GitRemote;
use crate::sources::source::MaybePackage;
use crate::sources::source::QueryKind;
Expand Down Expand Up @@ -171,14 +172,9 @@ enum Revision {

impl Revision {
fn new(rev: &str) -> Revision {
let oid = git2::Oid::from_str(rev).ok();
match oid {
// Git object ID is supposed to be a hex string of 20 (SHA1) or 32 (SHA256) bytes.
// Its length must be double to the underlying bytes (40 or 64),
// otherwise libgit2 would happily zero-pad the returned oid.
// See rust-lang/cargo#13188
Some(oid) if oid.as_bytes().len() * 2 == rev.len() => Revision::Locked(oid),
_ => Revision::Deferred(GitReference::Rev(rev.to_string())),
match rev_to_oid(rev) {
Some(oid) => Revision::Locked(oid),
None => Revision::Deferred(GitReference::Rev(rev.to_string())),
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion src/cargo/sources/git/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,7 @@ fn github_fast_path(
// to is itself. Don't bother talking to GitHub in that case
// either. (This ensures that we always attempt to fetch the
// commit directly even if we can't reach the GitHub API.)
if let Ok(oid) = rev.parse() {
if let Some(oid) = rev_to_oid(rev) {
debug!("github fast path is already a full commit hash {rev}");
return Ok(FastPathRev::NeedsFetch(oid));
}
Expand Down Expand Up @@ -1614,3 +1614,19 @@ mod tests {
}
}
}

/// Turns a full commit hash revision into an oid.
///
/// Git object ID is supposed to be a hex string of 20 (SHA1) or 32 (SHA256) bytes.
/// Its length must be double to the underlying bytes (40 or 64),
/// otherwise libgit2 would happily zero-pad the returned oid.
///
/// See:
///
/// * <https://github.com/rust-lang/cargo/issues/13188>
/// * <https://github.com/rust-lang/cargo/issues/13968>
pub(super) fn rev_to_oid(rev: &str) -> Option<Oid> {
Oid::from_str(rev)
.ok()
.filter(|oid| oid.as_bytes().len() * 2 == rev.len())
}

0 comments on commit 95eeafa

Please sign in to comment.