Skip to content

Commit 877fd19

Browse files
Fixing lint suggestions
1 parent 61da031 commit 877fd19

File tree

3 files changed

+63
-45
lines changed

3 files changed

+63
-45
lines changed

src/gitingest/query_parser.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import re
55
import string
66
import uuid
7+
import warnings
78
from pathlib import Path
89
from typing import Any
910
from urllib.parse import unquote, urlparse
@@ -172,36 +173,35 @@ async def _parse_repo_source(source: str) -> dict[str, Any]:
172173
if _is_valid_git_commit_hash(commit_or_branch):
173174
parsed["commit"] = commit_or_branch
174175
parsed["subpath"] += "/".join(remaining_parts[1:])
175-
176176
else:
177177
parsed["branch"] = await _configure_branch_and_subpath(remaining_parts, url)
178178
parsed["subpath"] += "/".join(remaining_parts)
179179
return parsed
180180

181-
async def _configure_branch_and_subpath(remaining_parts: list[str],url: str) -> str | None:
181+
182+
async def _configure_branch_and_subpath(remaining_parts: list[str], url: str) -> str | None:
182183
"""
183-
Find the branch name from the remaining parts of the URL path.
184+
Configure the branch and subpath based on the remaining parts of the URL.
184185
Parameters
185186
----------
186187
remaining_parts : list[str]
187-
List of path parts extracted from the URL.
188+
The remaining parts of the URL path.
188189
url : str
189-
The repository URL to determine branches.
190-
190+
The URL of the repository.
191191
Returns
192192
-------
193-
str (branch name) or None
193+
str | None
194+
The branch name if found, otherwise None.
194195
195196
"""
196197
try:
197198
# Fetch the list of branches from the remote repository
198199
branches: list[str] = await fetch_remote_branch_list(url)
199200
except Exception as e:
200-
print(f"Warning: Failed to fetch branch list: {str(e)}")
201-
return remaining_parts.pop(0) if remaining_parts else None
201+
warnings.warn(f"Warning: Failed to fetch branch list: {str(e)}")
202+
return remaining_parts.pop(0)
202203

203204
branch = []
204-
205205
while remaining_parts:
206206
branch.append(remaining_parts.pop(0))
207207
branch_name = "/".join(branch)
@@ -210,6 +210,7 @@ async def _configure_branch_and_subpath(remaining_parts: list[str],url: str) ->
210210

211211
return None
212212

213+
213214
def _is_valid_git_commit_hash(commit: str) -> bool:
214215
"""
215216
Validate if the provided string is a valid Git commit hash.

src/gitingest/repository_clone.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,27 +144,25 @@ async def _check_repo_exists(url: str) -> bool:
144144
@async_timeout(TIMEOUT)
145145
async def fetch_remote_branch_list(url: str) -> list[str]:
146146
"""
147-
Get the list of branches from the remote repo.
148-
147+
Fetch the list of branches from a remote Git repository.
149148
Parameters
150149
----------
151150
url : str
152-
The URL of the repository.
153-
151+
The URL of the Git repository to fetch branches from.
154152
Returns
155153
-------
156154
list[str]
157-
list of the branches in the remote repository
155+
A list of branch names available in the remote repository.
158156
"""
159157
fetch_branches_command = ["git", "ls-remote", "--heads", url]
160-
stdout, stderr = await _run_git_command(*fetch_branches_command)
158+
stdout, _ = await _run_git_command(*fetch_branches_command)
161159
stdout_decoded = stdout.decode()
162160

163161
return [
164-
line.split('refs/heads/', 1)[1]
165-
for line in stdout_decoded.splitlines()
166-
if line.strip() and 'refs/heads/' in line
167-
]
162+
line.split("refs/heads/", 1)[1]
163+
for line in stdout_decoded.splitlines()
164+
if line.strip() and "refs/heads/" in line
165+
]
168166

169167

170168
async def _run_git_command(*args: str) -> tuple[bytes, bytes]:

tests/query_parser/test_query_parser.py

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
""" Tests for the query_parser module. """
22

33
from pathlib import Path
4+
from unittest.mock import AsyncMock, patch
45

56
import pytest
6-
from unittest.mock import patch, AsyncMock
7-
from gitingest.repository_clone import _check_repo_exists, fetch_remote_branch_list
87

98
from gitingest.ignore_patterns import DEFAULT_IGNORE_PATTERNS
109
from gitingest.query_parser import _parse_patterns, _parse_repo_source, parse_query
@@ -98,15 +97,18 @@ async def test_parse_query_invalid_pattern() -> None:
9897
with pytest.raises(ValueError, match="Pattern.*contains invalid characters"):
9998
await parse_query(url, max_file_size=50, from_web=True, include_patterns="*.py;rm -rf")
10099

100+
101101
async def test_parse_url_with_subpaths() -> None:
102102
"""
103103
Test `_parse_repo_source` with a URL containing a branch and subpath.
104104
Verifies that user name, repository name, branch, and subpath are correctly extracted.
105105
"""
106106
url = "https://github.com/user/repo/tree/main/subdir/file"
107-
with patch('gitingest.repository_clone._run_git_command', new_callable=AsyncMock) as mock_run_git_command:
107+
with patch("gitingest.repository_clone._run_git_command", new_callable=AsyncMock) as mock_run_git_command:
108108
mock_run_git_command.return_value = (b"refs/heads/main\nrefs/heads/dev\nrefs/heads/feature-branch\n", b"")
109-
with patch('gitingest.repository_clone.fetch_remote_branch_list', new_callable=AsyncMock) as mock_fetch_branches:
109+
with patch(
110+
"gitingest.repository_clone.fetch_remote_branch_list", new_callable=AsyncMock
111+
) as mock_fetch_branches:
110112
mock_fetch_branches.return_value = ["main", "dev", "feature-branch"]
111113
result = await _parse_repo_source(url)
112114
assert result["user_name"] == "user"
@@ -227,9 +229,11 @@ async def test_parse_url_branch_and_commit_distinction() -> None:
227229
url_branch = "https://github.com/user/repo/tree/main"
228230
url_commit = "https://github.com/user/repo/tree/abcd1234abcd1234abcd1234abcd1234abcd1234"
229231

230-
with patch('gitingest.repository_clone._run_git_command', new_callable=AsyncMock) as mock_run_git_command:
232+
with patch("gitingest.repository_clone._run_git_command", new_callable=AsyncMock) as mock_run_git_command:
231233
mock_run_git_command.return_value = (b"refs/heads/main\nrefs/heads/dev\nrefs/heads/feature-branch\n", b"")
232-
with patch('gitingest.repository_clone.fetch_remote_branch_list', new_callable=AsyncMock) as mock_fetch_branches:
234+
with patch(
235+
"gitingest.repository_clone.fetch_remote_branch_list", new_callable=AsyncMock
236+
) as mock_fetch_branches:
233237
mock_fetch_branches.return_value = ["main", "dev", "feature-branch"]
234238

235239
result_branch = await _parse_repo_source(url_branch)
@@ -240,6 +244,7 @@ async def test_parse_url_branch_and_commit_distinction() -> None:
240244
assert result_commit["branch"] is None
241245
assert result_commit["commit"] == "abcd1234abcd1234abcd1234abcd1234abcd1234"
242246

247+
243248
async def test_parse_query_uuid_uniqueness() -> None:
244249
"""
245250
Test `parse_query` to ensure that each call generates a unique UUID for the query result.
@@ -283,38 +288,52 @@ async def test_parse_query_with_branch() -> None:
283288
assert result["commit"] is None
284289
assert result["type"] == "blob"
285290

291+
286292
@pytest.mark.asyncio
287-
@pytest.mark.parametrize("url, expected_branch, expected_subpath", [
288-
("https://github.com/user/repo/tree/main/src", "main", "/src"),
289-
("https://github.com/user/repo/tree/fix1", "fix1", "/"),
290-
("https://github.com/user/repo/tree/nonexistent-branch/src", "nonexistent-branch", "/src"),
291-
])
293+
@pytest.mark.parametrize(
294+
"url, expected_branch, expected_subpath",
295+
[
296+
("https://github.com/user/repo/tree/main/src", "main", "/src"),
297+
("https://github.com/user/repo/tree/fix1", "fix1", "/"),
298+
("https://github.com/user/repo/tree/nonexistent-branch/src", "nonexistent-branch", "/src"),
299+
],
300+
)
292301
async def test_parse_repo_source_with_failed_git_command(url, expected_branch, expected_subpath):
293302
"""
294303
Test `_parse_repo_source` when git command fails.
295304
Verifies that the function returns the first path component as the branch.
296305
"""
297-
with patch('gitingest.repository_clone.fetch_remote_branch_list', new_callable=AsyncMock) as mock_fetch_branches:
306+
with patch("gitingest.repository_clone.fetch_remote_branch_list", new_callable=AsyncMock) as mock_fetch_branches:
298307
mock_fetch_branches.side_effect = Exception("Failed to fetch branch list")
299-
308+
300309
result = await _parse_repo_source(url)
301-
310+
302311
assert result["branch"] == expected_branch
303312
assert result["subpath"] == expected_subpath
304313

314+
305315
@pytest.mark.asyncio
306-
@pytest.mark.parametrize("url, expected_branch, expected_subpath", [
307-
("https://github.com/user/repo/tree/feature/fix1/src", "feature/fix1", "/src"),
308-
("https://github.com/user/repo/tree/main/src", "main", "/src"),
309-
("https://github.com/user/repo", None, "/"), # No
310-
("https://github.com/user/repo/tree/nonexistent-branch/src", None, "/"), # Non-existent branch
311-
("https://github.com/user/repo/tree/fix", "fix", "/"),
312-
])
316+
@pytest.mark.parametrize(
317+
"url, expected_branch, expected_subpath",
318+
[
319+
("https://github.com/user/repo/tree/feature/fix1/src", "feature/fix1", "/src"),
320+
("https://github.com/user/repo/tree/main/src", "main", "/src"),
321+
("https://github.com/user/repo", None, "/"), # No
322+
("https://github.com/user/repo/tree/nonexistent-branch/src", None, "/"), # Non-existent branch
323+
("https://github.com/user/repo/tree/fix", "fix", "/"),
324+
("https://github.com/user/repo/blob/fix/page.html", "fix", "/page.html"),
325+
],
326+
)
313327
async def test_parse_repo_source_with_various_url_patterns(url, expected_branch, expected_subpath):
314-
with patch('gitingest.repository_clone._run_git_command', new_callable=AsyncMock) as mock_run_git_command, \
315-
patch('gitingest.repository_clone.fetch_remote_branch_list', new_callable=AsyncMock) as mock_fetch_branches:
316-
317-
mock_run_git_command.return_value = (b"refs/heads/feature/fix1\nrefs/heads/main\nrefs/heads/feature-branch\nrefs/heads/fix\n", b"")
328+
with (
329+
patch("gitingest.repository_clone._run_git_command", new_callable=AsyncMock) as mock_run_git_command,
330+
patch("gitingest.repository_clone.fetch_remote_branch_list", new_callable=AsyncMock) as mock_fetch_branches,
331+
):
332+
333+
mock_run_git_command.return_value = (
334+
b"refs/heads/feature/fix1\nrefs/heads/main\nrefs/heads/feature-branch\nrefs/heads/fix\n",
335+
b"",
336+
)
318337
mock_fetch_branches.return_value = ["feature/fix1", "main", "feature-branch"]
319338

320339
result = await _parse_repo_source(url)

0 commit comments

Comments
 (0)