Skip to content

Commit 70672fa

Browse files
committed
Compare --commit-sha with the commit CI names
When the clone lacks the CI commit, Git falls back to HEAD. A --commit-sha naming that checkout then matched self.commit and was not treated as an explicit ref, so the reachability check never ran and DEFAULT_BRANCH=true with --scm github could still mark a scan of another ref as the default branch. Compare with the CI SHA when there is one.
1 parent 8cae773 commit 70672fa

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎socketsecurity/core/git_interface.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def __init__(self, path: str, base_commit_sha: str | None = None):
5757
((source, sha) for source, sha in ci_commits if sha),
5858
(None, None),
5959
)
60+
self._ci_sha = ci_sha
6061

6162
if ci_sha:
6263
try:
@@ -520,7 +521,10 @@ def apply_explicit_ref(self, branch: str | None, commit_sha: str | None) -> None
520521
self._explicit_branch = branch if branch and branch != self.branch else None
521522
self._explicit_commit_sha = None
522523
self._explicit_commit_found = False
523-
if commit_sha and not self.commit.hexsha.startswith(commit_sha.lower()):
524+
# Compare with the commit CI names, even when the clone lacks it and
525+
# self.commit fell back to HEAD.
526+
ci_commit = (self._ci_sha or self.commit.hexsha).lower()
527+
if commit_sha and not ci_commit.startswith(commit_sha.lower()):
524528
self._explicit_commit_sha = commit_sha
525529
try:
526530
commit = self.repo.commit(

‎tests/core/test_default_branch_flags.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ def test_scm_detection_honors_an_explicit_ref_off_the_default_branch(
9393
assert _flags(_scan_params(cli_runtime)) == (False, False)
9494

9595

96+
def test_scm_detection_honors_an_explicit_commit_when_the_ci_commit_is_not_cloned(
97+
cli_runtime, github_dispatch_on_develop, release_tag_checkout, monkeypatch # noqa: F811
98+
):
99+
monkeypatch.setenv("GITHUB_SHA", "f" * 40)
100+
101+
assert github_dispatch_on_develop(["--commit-sha", release_tag_checkout.release_sha]) == 0
102+
103+
assert _flags(_scan_params(cli_runtime)) == (False, False)
104+
105+
96106
def test_scm_detection_still_marks_the_default_branch_run(
97107
cli_runtime, github_dispatch_on_develop # noqa: F811
98108
):

‎tests/unit/test_git_interface.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,20 @@ def test_older_commit_in_default_branch_history_still_qualifies(
623623
assert workflow_run_on_develop.commit_message.strip() == "base"
624624

625625

626+
def test_explicit_commit_is_checked_when_the_ci_commit_is_missing_from_the_clone(
627+
release_tag_checkout, monkeypatch, mocker
628+
):
629+
# The clone lacks GITHUB_SHA, so the commit falls back to HEAD: the release tag.
630+
monkeypatch.setenv("GITHUB_REF", "refs/heads/develop")
631+
monkeypatch.setenv("GITHUB_SHA", "f" * 40)
632+
mocker.patch.object(Git, "ensure_safe_directory")
633+
repository = Git(str(release_tag_checkout.path))
634+
635+
repository.apply_explicit_ref(None, release_tag_checkout.release_sha)
636+
637+
assert repository.explicit_ref_allows_default_branch() is False
638+
639+
626640
def test_explicit_commit_missing_from_the_checkout_is_not_default(workflow_run_on_develop):
627641
workflow_run_on_develop.apply_explicit_ref(None, "0" * 40)
628642

0 commit comments

Comments
 (0)