refactor: build path commit listings in core

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 06:00:20 +01:00
1 parent fdd7595a71
commit 85f60ec525
2 files changed
+21 -33

No files matched your search

+3 -16
View File
@@ -3,7 +3,6 @@
from __future__ import annotations
import hashlib
import urllib.parse
from core import upstream
@@ -14,25 +13,13 @@ PER_PAGE = 100
MAX_PAGES = 20
def _history_url(repo: upstream.Repo, path: str, page: int) -> str:
quoted = urllib.parse.quote(path)
if repo.family == "gitlab":
project = urllib.parse.quote(repo.slug, safe="")
return (f"{repo.api_base}/projects/{project}/repository/commits"
f"?path={quoted}&per_page={PER_PAGE}&page={page}")
if repo.family == "github":
return (f"{repo.api_base}/repos/{repo.slug}/commits"
f"?path={quoted}&per_page={PER_PAGE}&page={page}")
return (f"{repo.api_base}/repos/{repo.slug}/commits"
f"?path={quoted}&limit={PER_PAGE}&page={page}")
def path_history(repo: upstream.Repo, path: str, cache_dir: str) -> list[str]:
"""Commits that touched a path, newest first."""
shas: list[str] = []
for page in range(1, MAX_PAGES + 1):
payload = upstream.cached_api(_history_url(repo, path, page), cache_dir, False,
max_age=upstream.MOVING_TTL)
payload = upstream.cached_api(
upstream.path_commits_url(repo, path, per_page=PER_PAGE, page=page),
cache_dir, False, max_age=upstream.MOVING_TTL)
if not isinstance(payload, list) or not payload:
break
shas += [str(entry.get("sha") or entry.get("id")) for entry in payload]
+18 -17
View File
@@ -1269,6 +1269,23 @@ def commit_date(repo: Repo, sha: str, cache_dir: str, offline: bool = False) ->
return stamp.isoformat() if stamp is not None else None
def path_commits_url(
repo: Repo, path: str, *, per_page: int = 100,
since: str | None = None, page: int | None = None,
) -> str:
"""The forge's listing of the commits that touch a path."""
if repo.family == "gitlab":
base = f"{repo.api_base}/projects/{project_path(repo)}/repository/commits"
else:
base = f"{repo.api_base}/repos/{repo.slug}/commits"
url = f"{base}?path={urllib.parse.quote(path)}"
if since is not None:
url += f"&since={urllib.parse.quote(since)}"
size_key = "per_page" if repo.family in ("github", "gitlab") else "limit"
url += f"&{size_key}={per_page}"
return url if page is None else f"{url}&page={page}"
def commits_touching(
repo: Repo, path: str, base: str, cache_dir: str, offline: bool = False,
head: str | None = None,
@@ -1284,23 +1301,7 @@ def commits_touching(
since = commit_date(repo, base, cache_dir, offline)
if since is None:
return 0
quoted = urllib.parse.quote(path)
stamp = urllib.parse.quote(since)
if repo.family == "gitlab":
url = (
f"{repo.api_base}/projects/{project_path(repo)}"
f"/repository/commits?path={quoted}&since={stamp}&per_page=100"
)
elif repo.family == "github":
url = (
f"{repo.api_base}/repos/{repo.slug}/commits"
f"?path={quoted}&since={stamp}&per_page=100"
)
else:
url = (
f"{repo.api_base}/repos/{repo.slug}/commits"
f"?path={quoted}&since={stamp}&limit=100"
)
url = path_commits_url(repo, path, since=since)
# The list grows with every push touching the path: cached for good, the
# triage drift score stayed at whatever the first run counted.
payload = cached_api(url, cache_dir, offline, MOVING_TTL)