Codelint-MCP / src /utils /git_utils.py
OsamaAliMid's picture
Add CodeLint MCP Premium Edition application
ec37394
"""Git Repository Utilities"""
import subprocess
import logging
from pathlib import Path
from typing import Dict, Any
logger = logging.getLogger(__name__)
def get_git_diff(repo_path: str, base_branch: str, head_branch: str) -> Dict[str, Any]:
"""
Get Git diff between branches.
Returns:
dict: Changed files with line ranges
"""
try:
# Get list of changed files
result = subprocess.run(
["git", "diff", "--name-only", f"{base_branch}...{head_branch}"],
cwd=repo_path,
capture_output=True,
text=True,
timeout=30
)
if result.returncode != 0:
raise RuntimeError(f"Git diff failed: {result.stderr}")
changed_files = [f for f in result.stdout.strip().split("\n") if f]
# Get detailed diff for each file
files_with_changes = []
for file_path in changed_files:
# Skip if not Python/JS/TS
if not file_path.endswith((".py", ".js", ".ts")):
continue
diff_result = subprocess.run(
["git", "diff", f"{base_branch}...{head_branch}", "--", file_path],
cwd=repo_path,
capture_output=True,
text=True,
timeout=30
)
files_with_changes.append({
"path": file_path,
"diff": diff_result.stdout,
"full_path": str(Path(repo_path) / file_path)
})
return {
"changed_files": changed_files,
"analyzable_files": files_with_changes,
"base_branch": base_branch,
"head_branch": head_branch
}
except Exception as e:
logger.error(f"Git diff failed: {e}")
raise RuntimeError(f"Git operations failed: {e}")
def is_git_repository(path: str) -> bool:
"""Check if path is a Git repository"""
try:
subprocess.run(
["git", "rev-parse", "--git-dir"],
cwd=path,
capture_output=True,
timeout=5,
check=True
)
return True
except:
return False