Skip to content

Commit 5839d10

Browse files
committed
Handle explicit base ranges in shallow clones
1 parent 62a2c03 commit 5839d10

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

‎socketsecurity/core/git_interface.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ def __init__(self, path: str, base_commit_sha: str | None = None):
183183
provider="explicit base commit",
184184
base_ref=self.base_commit_sha,
185185
head_ref=None,
186+
use_merge_base=False,
186187
)
187188
if detected:
188189
detection_source = "explicit-base-commit"
@@ -348,8 +349,9 @@ def _detect_pull_request_changes(
348349
provider: str,
349350
base_ref: str,
350351
head_ref: str | None,
352+
use_merge_base: bool = True,
351353
) -> bool:
352-
"""Detect a full PR range locally, fetching only refs needed to complete it."""
354+
"""Detect a base-to-head range locally, fetching only refs needed to complete it."""
353355
base_commit = self._resolve_ref(base_ref)
354356
if base_commit is None:
355357
base_commit = self._fetch_ref(base_ref, f"{provider} pull-request base ref missing")
@@ -358,7 +360,8 @@ def _detect_pull_request_changes(
358360
return False
359361

360362
head_commit = self.commit.hexsha
361-
diff_range = f"{base_commit}...{head_commit}"
363+
range_separator = "..." if use_merge_base else ".."
364+
diff_range = f"{base_commit}{range_separator}{head_commit}"
362365
try:
363366
diff_files = self.repo.git.diff("--name-only", diff_range)
364367
self.show_files = diff_files.splitlines()
@@ -384,7 +387,7 @@ def _detect_pull_request_changes(
384387
try:
385388
diff_files = self.repo.git.diff(
386389
"--name-only",
387-
f"{base_commit}...{head_commit}",
390+
f"{base_commit}{range_separator}{head_commit}",
388391
)
389392
self.show_files = diff_files.splitlines()
390393
log.debug(

‎tests/unit/test_git_interface.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,37 @@ def test_explicit_base_commit_covers_the_whole_range(
305305
)
306306

307307

308+
def test_explicit_base_commit_does_not_require_merge_base(
309+
commit_range_repo, tmp_path, mocker,
310+
):
311+
shallow_path = tmp_path / "shallow-range-repo"
312+
_git(
313+
tmp_path,
314+
"clone",
315+
"--depth=1",
316+
"--branch=feature",
317+
commit_range_repo.path.as_uri(),
318+
str(shallow_path),
319+
)
320+
mocker.patch.object(Git, "ensure_safe_directory")
321+
322+
repository = Git(
323+
str(shallow_path),
324+
base_commit_sha=commit_range_repo.base_sha,
325+
)
326+
327+
# Fetching the base supplies both endpoint trees but does not deepen the
328+
# feature history enough to calculate a merge base.
329+
merge_base = subprocess.run(
330+
["git", "merge-base", commit_range_repo.base_sha, "HEAD"],
331+
cwd=shallow_path,
332+
capture_output=True,
333+
text=True,
334+
)
335+
assert merge_base.returncode != 0
336+
assert sorted(repository.changed_files) == ["App.java", "pom.xml"]
337+
338+
308339
def test_explicit_base_commit_takes_precedence_over_ci_environment(
309340
commit_range_repo, monkeypatch, mocker, caplog,
310341
):

0 commit comments

Comments
 (0)