-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze_diff.py
More file actions
221 lines (187 loc) · 6.96 KB
/
analyze_diff.py
File metadata and controls
221 lines (187 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python3
"""
Enrichment: Git Diff Semantic Analyzer
For each unique (date, repo) group in the time range, fetches the actual code diff
for those commits and passes it to Claude for semantic analysis. Replaces meaningless
commit messages like "wip" or "fix stuff" with a one-sentence description of what
the code actually does.
Input:
--github path to github.json (output of fetch_github.py)
--since YYYY-MM-DD
--until YYYY-MM-DD
--handle GitHub username
Output (stdout): JSON
{
"enrichment": "analyze_diff",
"since": "...",
"until": "...",
"results": [
{
"repo": "owner/repo",
"date": "YYYY-MM-DD",
"semantic_summary": "one or two sentences describing the substance of the changes",
"change_type": "refactor|feature|bugfix|revert|config|docs"
},
...
]
}
"""
import json
import argparse
import sys
import subprocess
from collections import defaultdict
def gh_api_json(path, label=""):
"""Call gh api and return parsed JSON, or None on failure."""
result = subprocess.run(
["gh", "api", path],
capture_output=True, text=True
)
if result.returncode != 0:
print(f"[analyze_diff] gh api failed ({label}): {result.stderr.strip()[:120]}", file=sys.stderr)
return None
try:
return json.loads(result.stdout)
except json.JSONDecodeError:
print(f"[analyze_diff] JSON parse error ({label})", file=sys.stderr)
return None
def fetch_diff_for_commit(repo, sha):
"""
Fetch the patch for a single commit. Returns a list of (filename, patch) tuples.
Caps at 5 files.
"""
data = gh_api_json(f"/repos/{repo}/commits/{sha}", label=f"commit {repo}@{sha}")
if not data:
return []
results = []
for file_info in data.get("files", [])[:5]:
filename = file_info.get("filename", "?")
patch = file_info.get("patch", "(binary or no patch)")
results.append((filename, patch))
return results
def build_diff_text(repo, shas):
"""
Fetch patches for up to 3 commits in a (date, repo) group.
Returns a single combined diff string, capped at 150 lines total.
"""
all_lines = []
for sha in shas[:3]:
file_patches = fetch_diff_for_commit(repo, sha)
for filename, patch in file_patches:
all_lines.append(f"--- {filename}")
all_lines.extend(patch.split("\n"))
if all_lines:
all_lines.append("") # blank line between commits
if len(all_lines) > 150:
all_lines = all_lines[:150]
all_lines.append("... (truncated at 150 lines)")
return "\n".join(all_lines)
def call_claude(prompt):
"""Run claude -p and return the response text, or None on failure."""
result = subprocess.run(
["claude", "-p", prompt],
capture_output=True, text=True, timeout=120
)
if result.returncode != 0:
print(f"[analyze_diff] claude error: {result.stderr.strip()[:120]}", file=sys.stderr)
return None
return result.stdout.strip()
def extract_json(text):
"""Extract a JSON object from Claude's response (handles ```json fences)."""
if not text:
return None
s = text.strip()
if "```" in s:
parts = s.split("```")
for part in parts:
candidate = part.lstrip("json").strip()
try:
return json.loads(candidate)
except json.JSONDecodeError:
continue
try:
return json.loads(s)
except json.JSONDecodeError:
# Find the first { ... } block
start = s.find("{")
end = s.rfind("}") + 1
if start >= 0 and end > start:
try:
return json.loads(s[start:end])
except json.JSONDecodeError:
pass
return None
def analyze_repo_day(repo, date, commit_messages, diff_text):
"""Ask Claude what the code changes in this repo actually did."""
messages_block = "\n".join(f"- {m}" for m in commit_messages)
diff_block = diff_text if diff_text.strip() else "(diff unavailable)"
prompt = f"""Analyze these git commits for repository {repo} on {date}.
Commit messages:
{messages_block}
Code diff (may be truncated):
{diff_block}
Return ONLY a valid JSON object — no explanation, no markdown:
{{
"repo": "{repo}",
"date": "{date}",
"semantic_summary": "one or two sentences describing what the code actually does (not restating commit messages)",
"change_type": "refactor|feature|bugfix|revert|config|docs"
}}"""
raw = call_claude(prompt)
result = extract_json(raw)
if result and isinstance(result, dict) and "semantic_summary" in result:
return result
return None
def main():
parser = argparse.ArgumentParser(description="Git diff semantic analyzer enrichment")
parser.add_argument("--github", required=True, help="Path to github.json from fetch_github.py")
parser.add_argument("--since", required=True, help="Start date YYYY-MM-DD")
parser.add_argument("--until", required=True, help="End date YYYY-MM-DD")
parser.add_argument("--handle", required=True, help="GitHub username")
args = parser.parse_args()
try:
with open(args.github) as f:
github_data = json.load(f)
except Exception as e:
print(f"[analyze_diff] Could not load {args.github}: {e}", file=sys.stderr)
sys.exit(1)
if github_data.get("service") != "github":
print("[analyze_diff] Input is not github service data", file=sys.stderr)
sys.exit(1)
# Group commits by (date, repo)
by_date_repo = defaultdict(list) # (date, repo) -> [message, ...]
sha_by_date_repo = defaultdict(list) # (date, repo) -> [sha, ...]
for c in github_data.get("commits", []):
repo = c.get("repo", "?")
date = c.get("date", "?")
if repo == "?" or "/" not in repo:
continue
key = (date, repo)
by_date_repo[key].append(c.get("message", ""))
sha_by_date_repo[key].append(c.get("sha", ""))
results = []
for (date, repo), messages in sorted(by_date_repo.items()):
shas = sha_by_date_repo[(date, repo)]
diff_text = build_diff_text(repo, shas)
result = analyze_repo_day(repo, date, messages[:6], diff_text)
if result:
results.append(result)
else:
# Fallback: pass through with raw messages as summary
print(f"[analyze_diff] Claude call failed for {repo} {date} — using raw messages", file=sys.stderr)
results.append({
"repo": repo,
"date": date,
"semantic_summary": "; ".join(m for m in messages[:3] if m),
"change_type": "unknown",
})
output = {
"enrichment": "analyze_diff",
"since": args.since,
"until": args.until,
"results": results,
}
print(json.dumps(output, indent=2))
print(f"[analyze_diff] {len(results)} repo-day(s) analyzed", file=sys.stderr)
if __name__ == "__main__":
main()