Skip to content

Commit

Permalink
perf: speed up impostor-commit's fast path (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
woodruffw authored Nov 15, 2024
1 parent 551721c commit 4744e7e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
22 changes: 18 additions & 4 deletions src/audit/impostor_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,28 @@ impl ImpostorCommit {
/// i.e. resolves due to presence in GitHub's fork network but is not actually
/// present in any of the specified `owner/repo`'s tags or branches.
fn impostor(&self, uses: Uses<'_>) -> Result<bool> {
let (branches, tags) = self.named_refs(uses)?;

// If there's no ref or the ref is not a commit, there's nothing to impersonate.
let Some(head_ref) = uses.commit_ref() else {
return Ok(false);
};

for branch in branches {
let (branches, tags) = self.named_refs(uses)?;

// Fast path: almost all commit refs will be at the tip of
// the branch or tag's history, so check those first.
for branch in &branches {
if branch.commit.sha == head_ref {
return Ok(false);
}
}

for tag in &tags {
if tag.commit.sha == head_ref {
return Ok(false);
}
}

for branch in &branches {
if self.named_ref_contains_commit(
&uses,
&format!("refs/heads/{}", &branch.name),
Expand All @@ -75,7 +89,7 @@ impl ImpostorCommit {
}
}

for tag in tags {
for tag in &tags {
if self.named_ref_contains_commit(
&uses,
&format!("refs/tags/{}", &tag.name),
Expand Down
14 changes: 5 additions & 9 deletions src/github_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ impl Client {
#[derive(Deserialize, Clone)]
pub(crate) struct Branch {
pub(crate) name: String,
pub(crate) commit: Object,
}

/// A single tag, as returned by GitHub's tags endpoints.
Expand All @@ -214,23 +215,18 @@ pub(crate) struct Branch {
#[derive(Deserialize, Clone)]
pub(crate) struct Tag {
pub(crate) name: String,
pub(crate) commit: TagCommit,
pub(crate) commit: Object,
}

/// Represents the SHA ref bound to a tag.
/// Represents a git object.
#[derive(Deserialize, Clone)]
pub(crate) struct TagCommit {
pub(crate) struct Object {
pub(crate) sha: String,
}

#[derive(Deserialize)]
pub(crate) struct GitRef {
pub(crate) object: GitObj,
}

#[derive(Deserialize)]
pub(crate) struct GitObj {
pub(crate) sha: String,
pub(crate) object: Object,
}

#[derive(Clone, Deserialize)]
Expand Down

0 comments on commit 4744e7e

Please sign in to comment.