Skip to content

Commit 7343b83

Browse files
rootclaude
andcommitted
fix: resolve CodeQL security issues and pre-commit hook violations
🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent d12805d commit 7343b83

File tree

5 files changed

+29
-16
lines changed

5 files changed

+29
-16
lines changed

src/gitingest/__main__.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,27 @@ async def _async_main(
165165
output : str | None
166166
The path where the output file will be written (default: ``digest.txt`` in current directory).
167167
Use ``"-"`` to write to ``stdout``.
168+
mcp_server : bool
169+
If ``True``, starts the MCP (Model Context Protocol) server instead of normal operation (default: ``False``).
168170
169171
Raises
170172
------
171173
click.Abort
172174
Raised if an error occurs during execution and the command must be aborted.
175+
click.ClickException
176+
Raised if MCP server dependencies are not installed when MCP mode is requested.
173177
174178
"""
175179
# Check if MCP server mode is requested
176180
if mcp_server:
177-
from gitingest.mcp_server import start_mcp_server
178-
179-
await start_mcp_server()
181+
# Dynamic import to avoid circular imports and optional dependency
182+
try:
183+
from gitingest.mcp_server import start_mcp_server
184+
185+
await start_mcp_server()
186+
except ImportError as e:
187+
msg = f"MCP server dependencies not installed: {e}"
188+
raise click.ClickException(msg) from e
180189
return
181190

182191
try:

src/gitingest/entrypoint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import shutil
88
import stat
99
import sys
10-
from collections.abc import AsyncGenerator, Callable
1110
from contextlib import asynccontextmanager
1211
from pathlib import Path
1312
from typing import TYPE_CHECKING
@@ -25,6 +24,7 @@
2524
from gitingest.utils.query_parser_utils import KNOWN_GIT_HOSTS
2625

2726
if TYPE_CHECKING:
27+
from collections.abc import AsyncGenerator, Callable
2828
from types import TracebackType
2929

3030
from gitingest.schemas import IngestionQuery

src/gitingest/mcp_server.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
from __future__ import annotations
44

5-
from collections.abc import Sequence
6-
from typing import Any
5+
from typing import TYPE_CHECKING, Any
76

87
from mcp.server import Server
98
from mcp.server.stdio import stdio_server
@@ -12,6 +11,9 @@
1211
from gitingest.entrypoint import ingest_async
1312
from gitingest.utils.logging_config import get_logger
1413

14+
if TYPE_CHECKING:
15+
from collections.abc import Sequence
16+
1517
# Initialize logger for this module
1618
logger = get_logger(__name__)
1719

@@ -86,7 +88,7 @@ async def call_tool(name: str, arguments: dict[str, Any]) -> Sequence[TextConten
8688
return await _handle_ingest_repository(arguments)
8789
return [TextContent(type="text", text=f"Unknown tool: {name}")]
8890
except Exception as e:
89-
logger.error(f"Error in tool call {name}: {e}", exc_info=True)
91+
logger.exception("Error in tool call %s", name)
9092
return [TextContent(type="text", text=f"Error executing {name}: {e!s}")]
9193

9294

@@ -144,17 +146,17 @@ async def _handle_ingest_repository(arguments: dict[str, Any]) -> Sequence[TextC
144146
return [TextContent(type="text", text=response_content)]
145147

146148
except Exception as e:
147-
logger.error(f"Error during ingestion: {e}", exc_info=True)
149+
logger.exception("Error during ingestion")
148150
return [TextContent(type="text", text=f"Error ingesting repository: {e!s}")]
149151

150152

151-
async def start_mcp_server():
153+
async def start_mcp_server() -> None:
152154
"""Start the MCP server with stdio transport."""
153155
logger.info("Starting Gitingest MCP server with stdio transport")
154156
await _run_stdio()
155157

156158

157-
async def _run_stdio():
159+
async def _run_stdio() -> None:
158160
"""Run the MCP server with stdio transport."""
159161
async with stdio_server() as (read_stream, write_stream):
160162
await app.run(

src/gitingest/utils/compat_func.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Compatibility functions for Python 3.8."""
22

3-
import os
43
from pathlib import Path
54

65

@@ -20,7 +19,7 @@ def readlink(path: Path) -> Path:
2019
The target of the symlink.
2120
2221
"""
23-
return Path(os.readlink(path))
22+
return Path(path).readlink()
2423

2524

2625
def removesuffix(s: str, suffix: str) -> str:

src/gitingest/utils/git_utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import base64
77
import re
88
import sys
9-
from collections.abc import Generator, Iterable
109
from contextlib import contextmanager
1110
from pathlib import Path
1211
from typing import TYPE_CHECKING, Final
@@ -19,6 +18,8 @@
1918
from gitingest.utils.logging_config import get_logger
2019

2120
if TYPE_CHECKING:
21+
from collections.abc import Generator, Iterable
22+
2223
from gitingest.schemas import CloneConfig
2324

2425
# Initialize logger for this module
@@ -221,7 +222,6 @@ async def fetch_remote_branches_or_tags(url: str, *, ref_type: str, token: str |
221222
git_cmd = git.Git()
222223

223224
# Prepare environment with authentication if needed
224-
env = None
225225
if token and is_github_host(url):
226226
auth_url = _add_token_to_url(url, token)
227227
url = auth_url
@@ -266,6 +266,11 @@ def create_git_repo(local_path: str, url: str, token: str | None = None) -> git.
266266
git.Repo
267267
A GitPython Repo object configured with authentication.
268268
269+
Raises
270+
------
271+
ValueError
272+
If the provided local_path is not a valid git repository.
273+
269274
"""
270275
try:
271276
repo = git.Repo(local_path)
@@ -552,8 +557,6 @@ def _add_token_to_url(url: str, token: str) -> str:
552557
The URL with embedded authentication.
553558
554559
"""
555-
from urllib.parse import urlparse, urlunparse
556-
557560
parsed = urlparse(url)
558561
# Add token as username in URL (GitHub supports this)
559562
netloc = f"x-oauth-basic:{token}@{parsed.hostname}"

0 commit comments

Comments
 (0)