File size: 10,704 Bytes
40d7073 | 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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | "use strict";
/**
* Diff Embeddings - Semantic encoding of git diffs
*
* Generates embeddings for code changes to enable:
* - Change classification (feature, bugfix, refactor)
* - Similar change detection
* - Risk assessment
* - Review prioritization
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseDiff = parseDiff;
exports.classifyChange = classifyChange;
exports.calculateRiskScore = calculateRiskScore;
exports.analyzeFileDiff = analyzeFileDiff;
exports.getCommitDiff = getCommitDiff;
exports.getStagedDiff = getStagedDiff;
exports.getUnstagedDiff = getUnstagedDiff;
exports.analyzeCommit = analyzeCommit;
exports.findSimilarCommits = findSimilarCommits;
const child_process_1 = require("child_process");
const onnx_embedder_1 = require("./onnx-embedder");
/**
* Parse a unified diff into hunks
*/
function parseDiff(diff) {
const hunks = [];
const lines = diff.split('\n');
let currentFile = '';
let currentHunk = null;
for (const line of lines) {
// File header
if (line.startsWith('diff --git')) {
const match = line.match(/diff --git a\/(.+) b\/(.+)/);
if (match) {
currentFile = match[2];
}
}
// Hunk header
if (line.startsWith('@@')) {
if (currentHunk) {
hunks.push(currentHunk);
}
const match = line.match(/@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@/);
if (match) {
currentHunk = {
file: currentFile,
oldStart: parseInt(match[1]),
oldLines: parseInt(match[2] || '1'),
newStart: parseInt(match[3]),
newLines: parseInt(match[4] || '1'),
content: '',
additions: [],
deletions: [],
};
}
}
else if (currentHunk) {
// Content lines
if (line.startsWith('+') && !line.startsWith('+++')) {
currentHunk.additions.push(line.substring(1));
currentHunk.content += line + '\n';
}
else if (line.startsWith('-') && !line.startsWith('---')) {
currentHunk.deletions.push(line.substring(1));
currentHunk.content += line + '\n';
}
else if (line.startsWith(' ')) {
currentHunk.content += line + '\n';
}
}
}
if (currentHunk) {
hunks.push(currentHunk);
}
return hunks;
}
/**
* Classify a change based on patterns
*/
function classifyChange(diff, message = '') {
const lowerMessage = message.toLowerCase();
const lowerDiff = diff.toLowerCase();
// Check message patterns
if (/\b(fix|bug|issue|error|crash|patch)\b/.test(lowerMessage))
return 'bugfix';
if (/\b(feat|feature|add|new|implement)\b/.test(lowerMessage))
return 'feature';
if (/\b(refactor|clean|improve|optimize)\b/.test(lowerMessage))
return 'refactor';
if (/\b(doc|readme|comment|jsdoc)\b/.test(lowerMessage))
return 'docs';
if (/\b(test|spec|coverage)\b/.test(lowerMessage))
return 'test';
if (/\b(config|ci|cd|build|deps)\b/.test(lowerMessage))
return 'config';
// Check diff patterns
if (/\.(md|txt|rst)$/.test(diff))
return 'docs';
if (/\.(test|spec)\.[jt]sx?/.test(diff))
return 'test';
if (/\.(json|ya?ml|toml|ini)$/.test(diff))
return 'config';
// Check content patterns
if (/\bcatch\b|\btry\b|\berror\b/.test(lowerDiff) && /\bfix\b/.test(lowerDiff))
return 'bugfix';
if (/\bfunction\b|\bclass\b|\bexport\b/.test(lowerDiff))
return 'feature';
return 'unknown';
}
/**
* Calculate risk score for a diff
*/
function calculateRiskScore(analysis) {
let risk = 0;
// Size risk
const totalChanges = analysis.totalAdditions + analysis.totalDeletions;
if (totalChanges > 500)
risk += 0.3;
else if (totalChanges > 200)
risk += 0.2;
else if (totalChanges > 50)
risk += 0.1;
// Complexity risk
if (analysis.complexity > 20)
risk += 0.2;
else if (analysis.complexity > 10)
risk += 0.1;
// File type risk
if (analysis.file.includes('auth') || analysis.file.includes('security'))
risk += 0.2;
if (analysis.file.includes('database') || analysis.file.includes('migration'))
risk += 0.15;
if (analysis.file.includes('api') || analysis.file.includes('endpoint'))
risk += 0.1;
// Pattern risk (deletions of error handling, etc.)
for (const hunk of analysis.hunks) {
for (const del of hunk.deletions) {
if (/\bcatch\b|\berror\b|\bvalidat/.test(del))
risk += 0.1;
if (/\bif\b.*\bnull\b|\bundefined\b/.test(del))
risk += 0.05;
}
}
return Math.min(1, risk);
}
/**
* Analyze a single file diff
*/
async function analyzeFileDiff(file, diff, message = '') {
const hunks = parseDiff(diff).filter(h => h.file === file || h.file === '');
const totalAdditions = hunks.reduce((sum, h) => sum + h.additions.length, 0);
const totalDeletions = hunks.reduce((sum, h) => sum + h.deletions.length, 0);
// Calculate complexity (branch keywords in additions)
let complexity = 0;
for (const hunk of hunks) {
for (const add of hunk.additions) {
if (/\bif\b|\belse\b|\bfor\b|\bwhile\b|\bswitch\b|\bcatch\b|\?/.test(add)) {
complexity++;
}
}
}
const category = classifyChange(diff, message);
const analysis = {
file,
hunks,
totalAdditions,
totalDeletions,
complexity,
riskScore: 0,
category,
};
analysis.riskScore = calculateRiskScore(analysis);
// Generate embedding for the diff
if ((0, onnx_embedder_1.isReady)()) {
const diffText = hunks.map(h => h.content).join('\n');
const result = await (0, onnx_embedder_1.embed)(`${category} change in ${file}: ${diffText.substring(0, 500)}`);
analysis.embedding = result.embedding;
}
return analysis;
}
/**
* Get diff for a commit
*/
function getCommitDiff(commitHash = 'HEAD') {
try {
return (0, child_process_1.execSync)(`git show ${commitHash} --format="" 2>/dev/null`, {
encoding: 'utf8',
maxBuffer: 10 * 1024 * 1024,
});
}
catch {
return '';
}
}
/**
* Get diff for staged changes
*/
function getStagedDiff() {
try {
return (0, child_process_1.execSync)('git diff --cached 2>/dev/null', {
encoding: 'utf8',
maxBuffer: 10 * 1024 * 1024,
});
}
catch {
return '';
}
}
/**
* Get diff for unstaged changes
*/
function getUnstagedDiff() {
try {
return (0, child_process_1.execSync)('git diff 2>/dev/null', {
encoding: 'utf8',
maxBuffer: 10 * 1024 * 1024,
});
}
catch {
return '';
}
}
/**
* Analyze a commit
*/
async function analyzeCommit(commitHash = 'HEAD') {
const diff = getCommitDiff(commitHash);
// Get commit metadata
let message = '', author = '', date = '';
try {
const info = (0, child_process_1.execSync)(`git log -1 --format="%s|%an|%aI" ${commitHash} 2>/dev/null`, {
encoding: 'utf8',
}).trim();
[message, author, date] = info.split('|');
}
catch { }
// Parse hunks and group by file
const hunks = parseDiff(diff);
const fileHunks = new Map();
for (const hunk of hunks) {
if (!fileHunks.has(hunk.file)) {
fileHunks.set(hunk.file, []);
}
fileHunks.get(hunk.file).push(hunk);
}
// Analyze each file
const files = [];
for (const [file, fileHunkList] of fileHunks) {
const fileDiff = fileHunkList.map(h => h.content).join('\n');
const analysis = await analyzeFileDiff(file, diff, message);
files.push(analysis);
}
const totalAdditions = files.reduce((sum, f) => sum + f.totalAdditions, 0);
const totalDeletions = files.reduce((sum, f) => sum + f.totalDeletions, 0);
const riskScore = files.length > 0
? files.reduce((sum, f) => sum + f.riskScore, 0) / files.length
: 0;
// Generate commit embedding
let embedding;
if ((0, onnx_embedder_1.isReady)()) {
const commitText = `${message}\n\nFiles changed: ${files.map(f => f.file).join(', ')}\n+${totalAdditions} -${totalDeletions}`;
const result = await (0, onnx_embedder_1.embed)(commitText);
embedding = result.embedding;
}
return {
hash: commitHash,
message,
author,
date,
files,
totalAdditions,
totalDeletions,
riskScore,
embedding,
};
}
/**
* Find similar past commits based on diff embeddings
*/
async function findSimilarCommits(currentDiff, recentCommits = 50, topK = 5) {
if (!(0, onnx_embedder_1.isReady)()) {
await (0, onnx_embedder_1.initOnnxEmbedder)();
}
// Get current diff embedding
const currentEmbedding = (await (0, onnx_embedder_1.embed)(currentDiff.substring(0, 1000))).embedding;
// Get recent commits
let commits = [];
try {
commits = (0, child_process_1.execSync)(`git log -${recentCommits} --format="%H" 2>/dev/null`, {
encoding: 'utf8',
}).trim().split('\n');
}
catch {
return [];
}
// Analyze and compare
const results = [];
for (const hash of commits.slice(0, Math.min(commits.length, recentCommits))) {
const analysis = await analyzeCommit(hash);
if (analysis.embedding) {
const similarity = cosineSimilarity(currentEmbedding, analysis.embedding);
results.push({ hash, similarity, message: analysis.message });
}
}
return results
.sort((a, b) => b.similarity - a.similarity)
.slice(0, topK);
}
function cosineSimilarity(a, b) {
if (a.length !== b.length)
return 0;
let dotProduct = 0;
let normA = 0;
let normB = 0;
for (let i = 0; i < a.length; i++) {
dotProduct += a[i] * b[i];
normA += a[i] * a[i];
normB += b[i] * b[i];
}
const magnitude = Math.sqrt(normA) * Math.sqrt(normB);
return magnitude === 0 ? 0 : dotProduct / magnitude;
}
exports.default = {
parseDiff,
classifyChange,
calculateRiskScore,
analyzeFileDiff,
analyzeCommit,
getCommitDiff,
getStagedDiff,
getUnstagedDiff,
findSimilarCommits,
};
|