Skip to content

Commit 5d6a3c6

Browse files
committed
fix tests
1 parent cee8cff commit 5d6a3c6

File tree

3 files changed

+18
-41
lines changed

3 files changed

+18
-41
lines changed

src/gitingest/utils/git_utils.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from urllib.parse import urlparse, urlunparse
1212

1313
import git
14+
from gitdb.util import os
1415

1516
from gitingest.utils.compat_func import removesuffix
1617
from gitingest.utils.exceptions import InvalidGitHubTokenError
@@ -120,7 +121,7 @@ async def ensure_git_installed() -> None:
120121

121122

122123
async def check_repo_exists(url: str, token: str | None = None) -> bool:
123-
"""Check whether a remote Git repository is reachable using git ls-remote.
124+
"""Check whether a remote Git repository is reachable.
124125
125126
Parameters
126127
----------
@@ -136,25 +137,12 @@ async def check_repo_exists(url: str, token: str | None = None) -> bool:
136137
137138
"""
138139
try:
139-
await ensure_git_installed()
140-
141-
git_cmd = git.Git()
142-
143-
# Add token to URL if provided and it's a GitHub repository
144-
auth_url = url
145-
if token and is_github_host(url):
146-
auth_url = _add_token_to_url(url, token)
147-
148-
# Use git ls-remote to check if repository exists
149-
# This will return refs if repo exists, or fail if it doesn't
150-
git_cmd.ls_remote(auth_url, "--exit-code")
151-
152-
except git.GitCommandError:
153-
# Repository doesn't exist, is private without proper auth, or other git error
154-
return False
155-
except Exception:
156-
# Git not installed or other system error
140+
# Try to resolve HEAD - if repo exists, this will work
141+
await _resolve_ref_to_sha(url, "HEAD", token=token)
142+
except (ValueError, Exception):
143+
# Repository doesn't exist, is private without proper auth, or other error
157144
return False
145+
158146
return True
159147

160148

@@ -448,7 +436,7 @@ async def _resolve_ref_to_sha(url: str, pattern: str, token: str | None = None)
448436
raise ValueError(msg)
449437

450438
except git.GitCommandError as exc:
451-
msg = f"Failed to resolve {pattern} in {url}: {exc}"
439+
msg = f"Failed to resolve {pattern} in {url}:{os.linesep}{exc}"
452440
raise ValueError(msg) from exc
453441

454442
return sha

src/server/query_processor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ async def process_query(
308308
_print_error(query.url, exc, max_file_size, pattern_type, pattern)
309309
# Clean up repository even if processing failed
310310
_cleanup_repository(clone_config)
311-
return IngestErrorResponse(error=str(exc))
311+
return IngestErrorResponse(error=f"{exc!s}")
312312

313313
if len(content) > MAX_DISPLAY_SIZE:
314314
content = (

tests/test_clone.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import sys
1010
from typing import TYPE_CHECKING
1111

12-
import git
1312
import pytest
1413

1514
from gitingest.clone import clone_repo
@@ -104,19 +103,18 @@ async def test_check_repo_exists(
104103
expected: bool,
105104
mocker: MockerFixture,
106105
) -> None:
107-
"""Verify that ``check_repo_exists`` interprets git ls-remote results correctly."""
108-
mock_git = mocker.patch("git.Git")
109-
mock_git_instance = mock_git.return_value
106+
"""Verify that ``check_repo_exists`` works by using _resolve_ref_to_sha."""
107+
mock_resolve = mocker.patch("gitingest.utils.git_utils._resolve_ref_to_sha")
110108

111109
if git_command_succeeds:
112-
mock_git_instance.ls_remote.return_value = "abc123\trefs/heads/main\n"
110+
mock_resolve.return_value = "abc123def456" # Mock SHA
113111
else:
114-
mock_git_instance.ls_remote.side_effect = git.GitCommandError("ls-remote", 128)
112+
mock_resolve.side_effect = ValueError("Repository not found")
115113

116114
result = await check_repo_exists(DEMO_URL)
117115

118116
assert result is expected
119-
mock_git_instance.ls_remote.assert_called_once_with(DEMO_URL, "--exit-code")
117+
mock_resolve.assert_called_once_with(DEMO_URL, "HEAD", token=None)
120118

121119

122120
@pytest.mark.asyncio
@@ -213,22 +211,13 @@ async def test_check_repo_exists_with_auth_token(mocker: MockerFixture) -> None:
213211
214212
Given a GitHub URL and a token:
215213
When ``check_repo_exists`` is called,
216-
Then it should add the token to the URL and call git ls-remote.
214+
Then it should pass the token to _resolve_ref_to_sha.
217215
"""
218-
mock_git = mocker.patch("git.Git")
219-
mock_git_instance = mock_git.return_value
220-
mock_git_instance.ls_remote.return_value = "abc123\trefs/heads/main\n"
221-
222-
# Mock the _add_token_to_url function
223-
mock_add_token = mocker.patch("gitingest.utils.git_utils._add_token_to_url")
224-
mock_add_token.return_value = "https://x-oauth-basic:token123@github.com/test/repo"
216+
mock_resolve = mocker.patch("gitingest.utils.git_utils._resolve_ref_to_sha")
217+
mock_resolve.return_value = "abc123def456" # Mock SHA
225218

226219
test_token = "token123" # noqa: S105
227220
result = await check_repo_exists("https://github.com/test/repo", token=test_token)
228221

229222
assert result is True
230-
mock_add_token.assert_called_once_with("https://github.com/test/repo", "token123")
231-
mock_git_instance.ls_remote.assert_called_once_with(
232-
"https://x-oauth-basic:token123@github.com/test/repo",
233-
"--exit-code",
234-
)
223+
mock_resolve.assert_called_once_with("https://github.com/test/repo", "HEAD", token=test_token)

0 commit comments

Comments
 (0)