Skip to content

Commit 7c31ee8

Browse files
Checkpoint before follow-up message
Co-authored-by: nicoragne <nicoragne@hotmail.fr>
1 parent c9fff75 commit 7c31ee8

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

src/gitingest/utils/git_utils.py

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
from typing import TYPE_CHECKING, Final, Iterable
1111
from urllib.parse import urlparse
1212

13-
import httpx
14-
from starlette.status import HTTP_200_OK, HTTP_401_UNAUTHORIZED, HTTP_403_FORBIDDEN, HTTP_404_NOT_FOUND
13+
1514

1615
from gitingest.utils.compat_func import removesuffix
1716
from gitingest.utils.exceptions import InvalidGitHubTokenError
@@ -112,7 +111,7 @@ async def ensure_git_installed() -> None:
112111

113112

114113
async def check_repo_exists(url: str, token: str | None = None) -> bool:
115-
"""Check whether a remote Git repository is reachable.
114+
"""Check whether a remote Git repository is reachable using git ls-remote.
116115
117116
Parameters
118117
----------
@@ -126,35 +125,30 @@ async def check_repo_exists(url: str, token: str | None = None) -> bool:
126125
bool
127126
``True`` if the repository exists, ``False`` otherwise.
128127
129-
Raises
130-
------
131-
RuntimeError
132-
If the host returns an unrecognised status code.
133-
134128
"""
135-
headers = {}
136-
129+
cmd = ["git", "ls-remote"]
130+
131+
# Add authentication header if token is provided for GitHub repositories
137132
if token and is_github_host(url):
138-
host, owner, repo = _parse_github_url(url)
139-
# Public GitHub vs. GitHub Enterprise
140-
base_api = "https://api.github.com" if host == "github.com" else f"https://{host}/api/v3"
141-
url = f"{base_api}/repos/{owner}/{repo}"
142-
headers["Authorization"] = f"Bearer {token}"
143-
144-
async with httpx.AsyncClient(follow_redirects=True) as client:
145-
try:
146-
response = await client.head(url, headers=headers)
147-
except httpx.RequestError:
148-
return False
149-
150-
status_code = response.status_code
151-
152-
if status_code == HTTP_200_OK:
153-
return True
154-
if status_code in {HTTP_401_UNAUTHORIZED, HTTP_403_FORBIDDEN, HTTP_404_NOT_FOUND}:
133+
cmd.extend(["-c", create_git_auth_header(token, url=url)])
134+
135+
cmd.extend(["--exit-code", url, "HEAD"])
136+
137+
try:
138+
proc = await asyncio.create_subprocess_exec(
139+
*cmd,
140+
stdout=asyncio.subprocess.PIPE,
141+
stderr=asyncio.subprocess.PIPE,
142+
)
143+
stdout, stderr = await proc.communicate()
144+
145+
# git ls-remote returns 0 if repository exists and is accessible
146+
# returns non-zero if repository doesn't exist or is not accessible
147+
return proc.returncode == 0
148+
149+
except Exception:
150+
# If any exception occurs (e.g., git not available), assume repo doesn't exist
155151
return False
156-
msg = f"Unexpected HTTP status {status_code} for {url}"
157-
raise RuntimeError(msg)
158152

159153

160154
def _parse_github_url(url: str) -> tuple[str, str, str]:

tests/test_clone.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
from typing import TYPE_CHECKING
1212
from unittest.mock import AsyncMock
1313

14-
import httpx
1514
import pytest
16-
from starlette.status import HTTP_200_OK, HTTP_401_UNAUTHORIZED, HTTP_403_FORBIDDEN, HTTP_404_NOT_FOUND
1715

1816
from gitingest.clone import clone_repo
1917
from gitingest.schemas import CloneConfig

0 commit comments

Comments
 (0)