Commit ·
5a0d646
0
Parent(s):
initial commit
Browse files- .DS_Store +0 -0
- .python-version +1 -0
- README.md +44 -0
- app.py +160 -0
- commitlens.py +422 -0
- requirements.txt +4 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
.python-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
3.12.12
|
README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: CommitLens
|
| 3 |
+
emoji: 🔍
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: indigo
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: true
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# CommitLens — AI-Powered Commit Analysis
|
| 13 |
+
|
| 14 |
+
Analyses the latest commit of any GitHub repository and generates per-file summaries plus a final markdown report using [JetBrains Mellum 2](https://huggingface.co/JetBrains/Mellum2-12B-A2.5B-Thinking-GGUF-Q8_0).
|
| 15 |
+
|
| 16 |
+
## How it works
|
| 17 |
+
|
| 18 |
+
1. **Enter a GitHub repo URL** (public or private)
|
| 19 |
+
2. **Optionally provide a GitHub token** for higher rate limits or private repos
|
| 20 |
+
3. **Click *Run Analysis*** — the app:
|
| 21 |
+
- Fetches the two most recent commits via the GitHub API
|
| 22 |
+
- Filters for source-code files (`.py`, `.js`, `.ts`, `.go`, `.rs`, etc.)
|
| 23 |
+
- Builds a structured prompt per file (commit metadata + before/after code + diff)
|
| 24 |
+
- Sends each prompt to **Mellum 2** for a concise per-file summary
|
| 25 |
+
- Combines all summaries and asks the model again for a comprehensive markdown report
|
| 26 |
+
4. **View results** — per-file summaries and the final `.md` report side by side
|
| 27 |
+
|
| 28 |
+
## Model
|
| 29 |
+
|
| 30 |
+
This Space loads the **Mellum2-12B-A2.5B-Thinking-Q8_0** GGUF model (~7 GB) via `llama-cpp-python` on startup. First load may take several minutes.
|
| 31 |
+
|
| 32 |
+
## Files
|
| 33 |
+
|
| 34 |
+
| File | Purpose |
|
| 35 |
+
|------|---------|
|
| 36 |
+
| `app.py` | Gradio application entry point |
|
| 37 |
+
| `commitlens.py` | GitHub API pipeline (fetch, diff, filter, prompt builder) |
|
| 38 |
+
| `requirements.txt` | Python dependencies |
|
| 39 |
+
|
| 40 |
+
## Requirements
|
| 41 |
+
|
| 42 |
+
- `requests`
|
| 43 |
+
- `gradio>=4.0.0`
|
| 44 |
+
- `llama-cpp-python>=0.2.0`
|
app.py
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
CommitLens — Gradio UI
|
| 3 |
+
=======================
|
| 4 |
+
- Asks for a GitHub repo URL (and optional PAT)
|
| 5 |
+
- Runs commitlens.run_pipeline() to get per-file prompts
|
| 6 |
+
- Feeds each prompt to Mellum 2 for a per-file summary
|
| 7 |
+
- Combines all summaries and asks the model for a final .md report
|
| 8 |
+
- Displays everything in the browser
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
from __future__ import annotations
|
| 12 |
+
|
| 13 |
+
import gradio as gr
|
| 14 |
+
import spaces
|
| 15 |
+
from llama_cpp import Llama
|
| 16 |
+
|
| 17 |
+
from commitlens import run_pipeline
|
| 18 |
+
|
| 19 |
+
# ---------------------------------------------------------------------------
|
| 20 |
+
# Model config
|
| 21 |
+
# ---------------------------------------------------------------------------
|
| 22 |
+
|
| 23 |
+
MODEL_REPO_ID = "JetBrains/Mellum2-12B-A2.5B-Thinking-GGUF-Q8_0"
|
| 24 |
+
MODEL_FILENAME = "Mellum2-12B-A2.5B-Thinking-Q8_0.gguf"
|
| 25 |
+
|
| 26 |
+
SUMMARY_SYSTEM_PROMPT = (
|
| 27 |
+
"You are an expert code reviewer. Analyze the following commit change context "
|
| 28 |
+
"and provide a concise, clear summary of what changed in this file, why it might "
|
| 29 |
+
"have changed, and any notable patterns or potential issues."
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
FINAL_SYSTEM_PROMPT = (
|
| 33 |
+
"You are a technical documentation expert. Given the following per-file summaries "
|
| 34 |
+
"of code changes from a commit, produce a well-formatted markdown document that "
|
| 35 |
+
"provides a comprehensive overview of the commit. Include sections for:\n"
|
| 36 |
+
"- Commit Overview\n"
|
| 37 |
+
"- Summary of Changes (per file)\n"
|
| 38 |
+
"- Key Takeaways / Impact\n\n"
|
| 39 |
+
"Use clear markdown formatting (headings, code blocks, bullet lists)."
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# ---------------------------------------------------------------------------
|
| 43 |
+
# Helpers
|
| 44 |
+
# ---------------------------------------------------------------------------
|
| 45 |
+
|
| 46 |
+
_llm = None
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def _get_llm() -> Llama:
|
| 50 |
+
global _llm
|
| 51 |
+
if _llm is None:
|
| 52 |
+
_llm = Llama.from_pretrained(
|
| 53 |
+
repo_id=MODEL_REPO_ID,
|
| 54 |
+
filename=MODEL_FILENAME,
|
| 55 |
+
verbose=False,
|
| 56 |
+
n_gpu_layers=-1,
|
| 57 |
+
)
|
| 58 |
+
return _llm
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def _extract_filename(prompt: str) -> str:
|
| 62 |
+
for line in prompt.splitlines():
|
| 63 |
+
if line.startswith("Filename :"):
|
| 64 |
+
return line.split(":", 1)[1].strip()
|
| 65 |
+
return "unknown"
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
@spaces.GPU
|
| 69 |
+
def _summarize(prompt: str) -> str:
|
| 70 |
+
llm = _get_llm()
|
| 71 |
+
response = llm.create_chat_completion(
|
| 72 |
+
messages=[
|
| 73 |
+
{"role": "system", "content": SUMMARY_SYSTEM_PROMPT},
|
| 74 |
+
{"role": "user", "content": prompt},
|
| 75 |
+
],
|
| 76 |
+
max_tokens=1024,
|
| 77 |
+
)
|
| 78 |
+
return response["choices"][0]["message"]["content"].strip()
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
@spaces.GPU
|
| 82 |
+
def _final_md(combined: str) -> str:
|
| 83 |
+
llm = _get_llm()
|
| 84 |
+
response = llm.create_chat_completion(
|
| 85 |
+
messages=[
|
| 86 |
+
{"role": "system", "content": FINAL_SYSTEM_PROMPT},
|
| 87 |
+
{"role": "user", "content": combined},
|
| 88 |
+
],
|
| 89 |
+
max_tokens=2048,
|
| 90 |
+
)
|
| 91 |
+
return response["choices"][0]["message"]["content"].strip()
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# ---------------------------------------------------------------------------
|
| 95 |
+
# Pipeline
|
| 96 |
+
# ---------------------------------------------------------------------------
|
| 97 |
+
|
| 98 |
+
def process_repo(repo_url: str, token: str, progress: gr.Progress = gr.Progress()):
|
| 99 |
+
try:
|
| 100 |
+
progress(0, desc="Running CommitLens pipeline...")
|
| 101 |
+
prompts = run_pipeline(repo_url, token.strip() or None)
|
| 102 |
+
|
| 103 |
+
if not prompts:
|
| 104 |
+
raise ValueError("No source-code files changed in the latest commit.")
|
| 105 |
+
|
| 106 |
+
per_file_md_parts = []
|
| 107 |
+
for i, prompt in enumerate(prompts):
|
| 108 |
+
fname = _extract_filename(prompt)
|
| 109 |
+
progress(
|
| 110 |
+
(i + 1) / (len(prompts) + 1),
|
| 111 |
+
desc=f"Summarizing [{i+1}/{len(prompts)}] {fname}...",
|
| 112 |
+
)
|
| 113 |
+
summary = _summarize(prompt)
|
| 114 |
+
per_file_md_parts.append(f"## `{fname}`\n\n{summary}")
|
| 115 |
+
|
| 116 |
+
combined = "\n\n---\n\n".join(per_file_md_parts)
|
| 117 |
+
|
| 118 |
+
progress(0.95, desc="Generating final markdown report...")
|
| 119 |
+
final_md = _final_md(combined)
|
| 120 |
+
|
| 121 |
+
return combined, final_md
|
| 122 |
+
|
| 123 |
+
except Exception as e:
|
| 124 |
+
raise gr.Error(str(e))
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
# ---------------------------------------------------------------------------
|
| 128 |
+
# Gradio app
|
| 129 |
+
# ---------------------------------------------------------------------------
|
| 130 |
+
|
| 131 |
+
with gr.Blocks(title="CommitLens", theme=gr.themes.Soft()) as demo:
|
| 132 |
+
gr.Markdown("# CommitLens — AI-Powered Commit Analysis")
|
| 133 |
+
|
| 134 |
+
with gr.Row():
|
| 135 |
+
repo_url = gr.Textbox(
|
| 136 |
+
label="GitHub Repository URL",
|
| 137 |
+
placeholder="https://github.com/owner/repo",
|
| 138 |
+
scale=2,
|
| 139 |
+
)
|
| 140 |
+
token = gr.Textbox(
|
| 141 |
+
label="GitHub Token (for private repos)",
|
| 142 |
+
type="password",
|
| 143 |
+
placeholder="ghp_... or leave empty for public repos",
|
| 144 |
+
scale=1,
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
run_btn = gr.Button("Run Analysis", variant="primary", size="lg")
|
| 148 |
+
|
| 149 |
+
with gr.Row():
|
| 150 |
+
per_file_out = gr.Markdown(label="Per-File Summaries")
|
| 151 |
+
final_out = gr.Markdown(label="Final Report (.md)")
|
| 152 |
+
|
| 153 |
+
run_btn.click(
|
| 154 |
+
fn=process_repo,
|
| 155 |
+
inputs=[repo_url, token],
|
| 156 |
+
outputs=[per_file_out, final_out],
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
if __name__ == "__main__":
|
| 160 |
+
demo.launch()
|
commitlens.py
ADDED
|
@@ -0,0 +1,422 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
CommitLens — Information Extraction Pipeline
|
| 3 |
+
=============================================
|
| 4 |
+
Fetches the two latest commits from a GitHub repository, diffs them,
|
| 5 |
+
and returns a list of per-file structured prompts ready for Melum 2.
|
| 6 |
+
|
| 7 |
+
Usage:
|
| 8 |
+
python commitlens.py <github_repo_url> [--token <PAT>]
|
| 9 |
+
|
| 10 |
+
Example:
|
| 11 |
+
python commitlens.py https://github.com/psf/requests --token ghp_xxx
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
from __future__ import annotations
|
| 15 |
+
|
| 16 |
+
import argparse
|
| 17 |
+
import sys
|
| 18 |
+
from dataclasses import dataclass, field
|
| 19 |
+
from typing import Optional
|
| 20 |
+
from urllib.parse import urlparse
|
| 21 |
+
|
| 22 |
+
import requests # pip install requests
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# ---------------------------------------------------------------------------
|
| 26 |
+
# Configuration
|
| 27 |
+
# ---------------------------------------------------------------------------
|
| 28 |
+
|
| 29 |
+
GITHUB_API = "https://api.github.com"
|
| 30 |
+
|
| 31 |
+
# Source-code extensions to keep (lower-cased, with leading dot)
|
| 32 |
+
KEEP_EXTENSIONS: set[str] = {
|
| 33 |
+
".py", ".js", ".ts", ".tsx", ".jsx",
|
| 34 |
+
".java", ".cpp", ".c", ".h", ".hpp",
|
| 35 |
+
".go", ".rs", ".php", ".rb", ".cs",
|
| 36 |
+
".swift", ".kt", ".scala", ".r", ".m",
|
| 37 |
+
".sh", ".bash", ".zsh",
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
# Config / infra filenames to keep regardless of extension
|
| 41 |
+
KEEP_FILENAMES: set[str] = {
|
| 42 |
+
"dockerfile",
|
| 43 |
+
"docker-compose.yml",
|
| 44 |
+
"docker-compose.yaml",
|
| 45 |
+
"requirements.txt",
|
| 46 |
+
"package.json",
|
| 47 |
+
"pyproject.toml",
|
| 48 |
+
"setup.py",
|
| 49 |
+
"setup.cfg",
|
| 50 |
+
"cargo.toml",
|
| 51 |
+
"go.mod",
|
| 52 |
+
".env.example",
|
| 53 |
+
"makefile",
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
# Extensions/patterns to ignore explicitly
|
| 57 |
+
IGNORE_EXTENSIONS: set[str] = {
|
| 58 |
+
".png", ".jpg", ".jpeg", ".gif", ".svg", ".ico", ".webp",
|
| 59 |
+
".mp4", ".mov", ".avi", ".mkv", ".webm",
|
| 60 |
+
".pdf", ".docx", ".xlsx",
|
| 61 |
+
".zip", ".tar", ".gz", ".bz2", ".7z",
|
| 62 |
+
".exe", ".dll", ".so", ".dylib",
|
| 63 |
+
".woff", ".woff2", ".ttf", ".eot",
|
| 64 |
+
".lock", # package-lock.json, yarn.lock, Cargo.lock, etc.
|
| 65 |
+
".map", # source maps
|
| 66 |
+
".min.js", # minified assets (handled via endswith below)
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
IGNORE_FILENAME_PATTERNS: tuple[str, ...] = (
|
| 70 |
+
".min.js",
|
| 71 |
+
".min.css",
|
| 72 |
+
"-lock.json",
|
| 73 |
+
".lock",
|
| 74 |
+
".pb", # protobuf binaries
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
# ---------------------------------------------------------------------------
|
| 79 |
+
# Data classes
|
| 80 |
+
# ---------------------------------------------------------------------------
|
| 81 |
+
|
| 82 |
+
@dataclass
|
| 83 |
+
class FileContext:
|
| 84 |
+
filename: str
|
| 85 |
+
status: str # added | modified | removed | renamed
|
| 86 |
+
additions: int
|
| 87 |
+
deletions: int
|
| 88 |
+
patch: Optional[str]
|
| 89 |
+
before_content: Optional[str] = None # full file at old commit
|
| 90 |
+
after_content: Optional[str] = None # full file at new commit
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
@dataclass
|
| 94 |
+
class CommitContext:
|
| 95 |
+
message: str
|
| 96 |
+
author: str
|
| 97 |
+
timestamp: str
|
| 98 |
+
old_sha: str
|
| 99 |
+
new_sha: str
|
| 100 |
+
total_additions: int
|
| 101 |
+
total_deletions: int
|
| 102 |
+
total_changed_files: int
|
| 103 |
+
files: list[FileContext] = field(default_factory=list)
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
# ---------------------------------------------------------------------------
|
| 107 |
+
# GitHub helpers
|
| 108 |
+
# ---------------------------------------------------------------------------
|
| 109 |
+
|
| 110 |
+
class GitHubClient:
|
| 111 |
+
def __init__(self, token: Optional[str] = None):
|
| 112 |
+
self.session = requests.Session()
|
| 113 |
+
self.session.headers["Accept"] = "application/vnd.github+json"
|
| 114 |
+
if token:
|
| 115 |
+
self.session.headers["Authorization"] = f"Bearer {token}"
|
| 116 |
+
|
| 117 |
+
def _get(self, url: str, params: Optional[dict] = None) -> dict | list:
|
| 118 |
+
resp = self.session.get(url, params=params, timeout=30)
|
| 119 |
+
resp.raise_for_status()
|
| 120 |
+
return resp.json()
|
| 121 |
+
|
| 122 |
+
def get_raw(self, url: str) -> str:
|
| 123 |
+
resp = self.session.get(url, timeout=30)
|
| 124 |
+
resp.raise_for_status()
|
| 125 |
+
return resp.text
|
| 126 |
+
|
| 127 |
+
# ---- domain methods ---------------------------------------------------
|
| 128 |
+
|
| 129 |
+
def latest_two_shas(self, owner: str, repo: str) -> tuple[str, str]:
|
| 130 |
+
"""Return (old_sha, new_sha) for the two most recent commits."""
|
| 131 |
+
commits = self._get(
|
| 132 |
+
f"{GITHUB_API}/repos/{owner}/{repo}/commits",
|
| 133 |
+
params={"per_page": 2},
|
| 134 |
+
)
|
| 135 |
+
if len(commits) < 2:
|
| 136 |
+
raise ValueError("Repository has fewer than 2 commits.")
|
| 137 |
+
new_sha: str = commits[0]["sha"]
|
| 138 |
+
old_sha: str = commits[1]["sha"]
|
| 139 |
+
return old_sha, new_sha
|
| 140 |
+
|
| 141 |
+
def compare(self, owner: str, repo: str, old: str, new: str) -> dict:
|
| 142 |
+
return self._get(
|
| 143 |
+
f"{GITHUB_API}/repos/{owner}/{repo}/compare/{old}...{new}"
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
def file_content_at(self, owner: str, repo: str, path: str, ref: str) -> Optional[str]:
|
| 147 |
+
"""Return raw file content at a specific commit ref, or None if missing."""
|
| 148 |
+
try:
|
| 149 |
+
meta = self._get(
|
| 150 |
+
f"{GITHUB_API}/repos/{owner}/{repo}/contents/{path}",
|
| 151 |
+
params={"ref": ref},
|
| 152 |
+
)
|
| 153 |
+
download_url: str = meta.get("download_url", "")
|
| 154 |
+
if not download_url:
|
| 155 |
+
return None
|
| 156 |
+
return self.get_raw(download_url)
|
| 157 |
+
except requests.HTTPError:
|
| 158 |
+
return None
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
# ---------------------------------------------------------------------------
|
| 162 |
+
# Filtering
|
| 163 |
+
# ---------------------------------------------------------------------------
|
| 164 |
+
|
| 165 |
+
def _should_keep(filename: str) -> bool:
|
| 166 |
+
lower = filename.lower()
|
| 167 |
+
basename = lower.split("/")[-1]
|
| 168 |
+
|
| 169 |
+
# Explicit ignore patterns take priority
|
| 170 |
+
for pat in IGNORE_FILENAME_PATTERNS:
|
| 171 |
+
if lower.endswith(pat):
|
| 172 |
+
return False
|
| 173 |
+
|
| 174 |
+
# Keep by exact filename match (infra / config files)
|
| 175 |
+
if basename in KEEP_FILENAMES:
|
| 176 |
+
return True
|
| 177 |
+
|
| 178 |
+
# Keep by extension
|
| 179 |
+
for ext in KEEP_EXTENSIONS:
|
| 180 |
+
if lower.endswith(ext):
|
| 181 |
+
return True
|
| 182 |
+
|
| 183 |
+
# Ignore by extension
|
| 184 |
+
for ext in IGNORE_EXTENSIONS:
|
| 185 |
+
if lower.endswith(ext):
|
| 186 |
+
return False
|
| 187 |
+
|
| 188 |
+
# Default: skip unknown binary-looking files
|
| 189 |
+
return False
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
# ---------------------------------------------------------------------------
|
| 193 |
+
# Pipeline steps
|
| 194 |
+
# ---------------------------------------------------------------------------
|
| 195 |
+
|
| 196 |
+
def parse_repo_url(url: str) -> tuple[str, str]:
|
| 197 |
+
"""Extract (owner, repo) from a GitHub URL."""
|
| 198 |
+
parsed = urlparse(url.rstrip("/"))
|
| 199 |
+
parts = [p for p in parsed.path.split("/") if p]
|
| 200 |
+
if len(parts) < 2:
|
| 201 |
+
raise ValueError(f"Cannot parse owner/repo from URL: {url}")
|
| 202 |
+
owner, repo = parts[0], parts[1]
|
| 203 |
+
if repo.endswith(".git"):
|
| 204 |
+
repo = repo[:-4]
|
| 205 |
+
return owner, repo
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
def fetch_commit_context(
|
| 209 |
+
client: GitHubClient,
|
| 210 |
+
owner: str,
|
| 211 |
+
repo: str,
|
| 212 |
+
) -> CommitContext:
|
| 213 |
+
"""Steps 2–6: fetch SHAs, comparison, file contents."""
|
| 214 |
+
|
| 215 |
+
# Step 2 — latest two SHAs
|
| 216 |
+
old_sha, new_sha = client.latest_two_shas(owner, repo)
|
| 217 |
+
|
| 218 |
+
# Step 3 — comparison
|
| 219 |
+
comparison = client.compare(owner, repo, old_sha, new_sha)
|
| 220 |
+
|
| 221 |
+
commit_meta = comparison["commits"][-1]["commit"]
|
| 222 |
+
message: str = commit_meta["message"]
|
| 223 |
+
author: str = commit_meta["author"]["name"]
|
| 224 |
+
timestamp: str = commit_meta["author"]["date"]
|
| 225 |
+
|
| 226 |
+
stats = comparison.get("stats", {})
|
| 227 |
+
total_additions: int = stats.get("additions", 0)
|
| 228 |
+
total_deletions: int = stats.get("deletions", 0)
|
| 229 |
+
raw_files: list[dict] = comparison.get("files", [])
|
| 230 |
+
total_changed_files: int = len(raw_files)
|
| 231 |
+
|
| 232 |
+
# Step 4 — filter files
|
| 233 |
+
filtered: list[dict] = [f for f in raw_files if _should_keep(f["filename"])]
|
| 234 |
+
|
| 235 |
+
# Step 5 + 6 — build FileContext, fetch before/after content
|
| 236 |
+
file_contexts: list[FileContext] = []
|
| 237 |
+
for f in filtered:
|
| 238 |
+
filename: str = f["filename"]
|
| 239 |
+
status: str = f.get("status", "modified")
|
| 240 |
+
|
| 241 |
+
fc = FileContext(
|
| 242 |
+
filename=filename,
|
| 243 |
+
status=status,
|
| 244 |
+
additions=f.get("additions", 0),
|
| 245 |
+
deletions=f.get("deletions", 0),
|
| 246 |
+
patch=f.get("patch"),
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
# Fetch full file content for semantic context (Step 6)
|
| 250 |
+
if status != "added":
|
| 251 |
+
fc.before_content = client.file_content_at(owner, repo, filename, old_sha)
|
| 252 |
+
if status != "removed":
|
| 253 |
+
fc.after_content = client.file_content_at(owner, repo, filename, new_sha)
|
| 254 |
+
|
| 255 |
+
file_contexts.append(fc)
|
| 256 |
+
|
| 257 |
+
return CommitContext(
|
| 258 |
+
message=message,
|
| 259 |
+
author=author,
|
| 260 |
+
timestamp=timestamp,
|
| 261 |
+
old_sha=old_sha,
|
| 262 |
+
new_sha=new_sha,
|
| 263 |
+
total_additions=total_additions,
|
| 264 |
+
total_deletions=total_deletions,
|
| 265 |
+
total_changed_files=total_changed_files,
|
| 266 |
+
files=file_contexts,
|
| 267 |
+
)
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
# ---------------------------------------------------------------------------
|
| 271 |
+
# Step 7 — Build per-file prompts
|
| 272 |
+
# ---------------------------------------------------------------------------
|
| 273 |
+
|
| 274 |
+
def build_prompts(ctx: CommitContext) -> list[str]:
|
| 275 |
+
"""
|
| 276 |
+
Return one structured prompt string per changed file.
|
| 277 |
+
Each prompt contains:
|
| 278 |
+
- commit-level header (message, author, timestamp, stats)
|
| 279 |
+
- file-specific info (status, additions/deletions)
|
| 280 |
+
- before/after content (if available)
|
| 281 |
+
- the diff patch
|
| 282 |
+
"""
|
| 283 |
+
prompts: list[str] = []
|
| 284 |
+
|
| 285 |
+
commit_header = (
|
| 286 |
+
"=== COMMIT METADATA ===\n"
|
| 287 |
+
f"Message : {ctx.message}\n"
|
| 288 |
+
f"Author : {ctx.author}\n"
|
| 289 |
+
f"Timestamp : {ctx.timestamp}\n"
|
| 290 |
+
"\n"
|
| 291 |
+
"=== COMMIT STATS ===\n"
|
| 292 |
+
f"Total files changed : {ctx.total_changed_files}\n"
|
| 293 |
+
f"Total additions : {ctx.total_additions}\n"
|
| 294 |
+
f"Total deletions : {ctx.total_deletions}\n"
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
+
for fc in ctx.files:
|
| 298 |
+
sections: list[str] = [commit_header]
|
| 299 |
+
|
| 300 |
+
# File identity
|
| 301 |
+
sections.append(
|
| 302 |
+
"=== FILE ===\n"
|
| 303 |
+
f"Filename : {fc.filename}\n"
|
| 304 |
+
f"Status : {fc.status}\n"
|
| 305 |
+
f"Additions: {fc.additions}\n"
|
| 306 |
+
f"Deletions: {fc.deletions}\n"
|
| 307 |
+
)
|
| 308 |
+
|
| 309 |
+
# Before content
|
| 310 |
+
if fc.before_content is not None:
|
| 311 |
+
sections.append(
|
| 312 |
+
"=== BEFORE CODE ===\n"
|
| 313 |
+
f"{fc.before_content}\n"
|
| 314 |
+
)
|
| 315 |
+
else:
|
| 316 |
+
sections.append("=== BEFORE CODE ===\n(file did not exist)\n")
|
| 317 |
+
|
| 318 |
+
# After content
|
| 319 |
+
if fc.after_content is not None:
|
| 320 |
+
sections.append(
|
| 321 |
+
"=== AFTER CODE ===\n"
|
| 322 |
+
f"{fc.after_content}\n"
|
| 323 |
+
)
|
| 324 |
+
else:
|
| 325 |
+
sections.append("=== AFTER CODE ===\n(file was deleted)\n")
|
| 326 |
+
|
| 327 |
+
# Diff patch
|
| 328 |
+
if fc.patch:
|
| 329 |
+
sections.append(
|
| 330 |
+
"=== DIFF ===\n"
|
| 331 |
+
f"{fc.patch}\n"
|
| 332 |
+
)
|
| 333 |
+
else:
|
| 334 |
+
sections.append("=== DIFF ===\n(no patch available)\n")
|
| 335 |
+
|
| 336 |
+
prompts.append("\n".join(sections))
|
| 337 |
+
|
| 338 |
+
return prompts
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
# ---------------------------------------------------------------------------
|
| 342 |
+
# Public entry point
|
| 343 |
+
# ---------------------------------------------------------------------------
|
| 344 |
+
|
| 345 |
+
def run_pipeline(
|
| 346 |
+
repo_url: str,
|
| 347 |
+
token: Optional[str] = None,
|
| 348 |
+
) -> list[str]:
|
| 349 |
+
"""
|
| 350 |
+
Full CommitLens pipeline.
|
| 351 |
+
|
| 352 |
+
Parameters
|
| 353 |
+
----------
|
| 354 |
+
repo_url : str
|
| 355 |
+
GitHub repository URL, e.g. ``https://github.com/owner/repo``
|
| 356 |
+
token : str, optional
|
| 357 |
+
GitHub personal access token for authenticated requests (higher rate
|
| 358 |
+
limits, private repos).
|
| 359 |
+
|
| 360 |
+
Returns
|
| 361 |
+
-------
|
| 362 |
+
list[str]
|
| 363 |
+
One prompt string per changed source-code file.
|
| 364 |
+
"""
|
| 365 |
+
owner, repo = parse_repo_url(repo_url)
|
| 366 |
+
client = GitHubClient(token=token)
|
| 367 |
+
ctx = fetch_commit_context(client, owner, repo)
|
| 368 |
+
return build_prompts(ctx)
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
# ---------------------------------------------------------------------------
|
| 372 |
+
# CLI
|
| 373 |
+
# ---------------------------------------------------------------------------
|
| 374 |
+
|
| 375 |
+
def _cli() -> None:
|
| 376 |
+
parser = argparse.ArgumentParser(
|
| 377 |
+
description="CommitLens: extract per-file commit prompts for Melum 2",
|
| 378 |
+
)
|
| 379 |
+
parser.add_argument("repo_url", help="GitHub repository URL")
|
| 380 |
+
parser.add_argument(
|
| 381 |
+
"--token", "-t",
|
| 382 |
+
default=None,
|
| 383 |
+
help="GitHub Personal Access Token (optional but recommended)",
|
| 384 |
+
)
|
| 385 |
+
parser.add_argument(
|
| 386 |
+
"--print-prompts", "-p",
|
| 387 |
+
action="store_true",
|
| 388 |
+
help="Print all generated prompts to stdout",
|
| 389 |
+
)
|
| 390 |
+
args = parser.parse_args()
|
| 391 |
+
|
| 392 |
+
try:
|
| 393 |
+
prompts = run_pipeline(args.repo_url, token=args.token)
|
| 394 |
+
except Exception as exc:
|
| 395 |
+
print(f"[ERROR] {exc}", file=sys.stderr)
|
| 396 |
+
sys.exit(1)
|
| 397 |
+
|
| 398 |
+
print(f"\n[CommitLens] Generated {len(prompts)} prompt(s).\n")
|
| 399 |
+
|
| 400 |
+
if args.print_prompts:
|
| 401 |
+
for i, prompt in enumerate(prompts, 1):
|
| 402 |
+
print(f"{'='*60}")
|
| 403 |
+
print(f"PROMPT {i} / {len(prompts)}")
|
| 404 |
+
print(f"{'='*60}")
|
| 405 |
+
print(prompt)
|
| 406 |
+
print()
|
| 407 |
+
else:
|
| 408 |
+
for i, prompt in enumerate(prompts, 1):
|
| 409 |
+
# Print just the file header so the caller sees what was captured
|
| 410 |
+
first_line = [
|
| 411 |
+
line for line in prompt.splitlines()
|
| 412 |
+
if line.startswith("Filename :")
|
| 413 |
+
]
|
| 414 |
+
label = first_line[0] if first_line else f"File {i}"
|
| 415 |
+
print(f" [{i}] {label}")
|
| 416 |
+
|
| 417 |
+
# Return value is available when imported as a module
|
| 418 |
+
return prompts
|
| 419 |
+
|
| 420 |
+
|
| 421 |
+
if __name__ == "__main__":
|
| 422 |
+
_cli()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
requests>=2.31.0
|
| 2 |
+
gradio>=4.0.0
|
| 3 |
+
llama-cpp-python>=0.2.0
|
| 4 |
+
spaces>=0.4.0
|