-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcochange.js
More file actions
498 lines (441 loc) · 15.7 KB
/
Copy pathcochange.js
File metadata and controls
498 lines (441 loc) · 15.7 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/**
* Git co-change analysis — surfaces files that historically change together.
*
* Uses git log to find temporal coupling between files, computes Jaccard
* similarity coefficients, and stores results in the codegraph database.
*/
import { execFileSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import { normalizePath } from './constants.js';
import { closeDb, findDbPath, initSchema, openDb, openReadonlyOrFail } from './db.js';
import { warn } from './logger.js';
import { isTestFile } from './queries.js';
/**
* Scan git history and return parsed commit data.
* @param {string} repoRoot - Absolute path to the git repo root
* @param {object} [opts]
* @param {string} [opts.since] - Git date expression (e.g. "1 year ago")
* @param {string} [opts.afterSha] - Only include commits after this SHA
* @returns {{ commits: Array<{sha: string, epoch: number, files: string[]}> }}
*/
export function scanGitHistory(repoRoot, opts = {}) {
const args = [
'log',
'--name-only',
'--pretty=format:%H%n%at',
'--no-merges',
'--diff-filter=AMRC',
];
if (opts.since) args.push(`--since=${opts.since}`);
if (opts.afterSha) args.push(`${opts.afterSha}..HEAD`);
args.push('--', '.');
let output;
try {
output = execFileSync('git', args, {
cwd: repoRoot,
encoding: 'utf-8',
maxBuffer: 50 * 1024 * 1024,
stdio: ['pipe', 'pipe', 'pipe'],
});
} catch (e) {
warn(`Failed to scan git history: ${e.message}`);
return { commits: [] };
}
if (!output.trim()) return { commits: [] };
const commits = [];
// Split on double newlines to get blocks; each block is sha\nepoch\nfile1\nfile2...
const blocks = output.trim().split(/\n\n+/);
for (const block of blocks) {
const lines = block.split('\n').filter((l) => l.length > 0);
if (lines.length < 2) continue;
const sha = lines[0];
const epoch = parseInt(lines[1], 10);
if (Number.isNaN(epoch)) continue;
const files = lines.slice(2).map((f) => normalizePath(f));
if (files.length > 0) {
commits.push({ sha, epoch, files });
}
}
return { commits };
}
/**
* Compute co-change pairs from parsed commit data.
* @param {Array<{sha: string, epoch: number, files: string[]}>} commits
* @param {object} [opts]
* @param {number} [opts.minSupport=3] - Minimum number of co-occurrences
* @param {number} [opts.maxFilesPerCommit=50] - Skip commits with too many files
* @param {Set<string>} [opts.knownFiles] - If provided, only include pairs where both files are in this set
* @returns {Map<string, {commitCount: number, jaccard: number, lastEpoch: number}>}
*/
export function computeCoChanges(commits, opts = {}) {
const minSupport = opts.minSupport ?? 3;
const maxFilesPerCommit = opts.maxFilesPerCommit ?? 50;
const knownFiles = opts.knownFiles || null;
const fileCommitCounts = new Map();
const pairCounts = new Map();
const pairLastEpoch = new Map();
for (const commit of commits) {
let { files } = commit;
if (files.length > maxFilesPerCommit) continue;
if (knownFiles) {
files = files.filter((f) => knownFiles.has(f));
}
// Count per-file commits
for (const f of files) {
fileCommitCounts.set(f, (fileCommitCounts.get(f) || 0) + 1);
}
// Generate all unique pairs (canonical: a < b)
const sorted = [...new Set(files)].sort();
for (let i = 0; i < sorted.length; i++) {
for (let j = i + 1; j < sorted.length; j++) {
const key = `${sorted[i]}\0${sorted[j]}`;
pairCounts.set(key, (pairCounts.get(key) || 0) + 1);
const prev = pairLastEpoch.get(key) || 0;
if (commit.epoch > prev) pairLastEpoch.set(key, commit.epoch);
}
}
}
// Filter by minSupport and compute Jaccard
const results = new Map();
for (const [key, count] of pairCounts) {
if (count < minSupport) continue;
const [fileA, fileB] = key.split('\0');
const countA = fileCommitCounts.get(fileA) || 0;
const countB = fileCommitCounts.get(fileB) || 0;
const jaccard = count / (countA + countB - count);
results.set(key, {
commitCount: count,
jaccard,
lastEpoch: pairLastEpoch.get(key) || 0,
});
}
return { pairs: results, fileCommitCounts };
}
/**
* Analyze git history and populate co-change data in the database.
* @param {string} [customDbPath] - Path to graph.db
* @param {object} [opts]
* @param {string} [opts.since] - Git date expression
* @param {number} [opts.minSupport] - Minimum co-occurrence count
* @param {number} [opts.maxFilesPerCommit] - Max files per commit
* @param {boolean} [opts.full] - Force full re-scan
* @returns {{ pairsFound: number, commitsScanned: number, since: string, minSupport: number }}
*/
export function analyzeCoChanges(customDbPath, opts = {}) {
const dbPath = findDbPath(customDbPath);
const db = openDb(dbPath);
initSchema(db);
const repoRoot = path.resolve(path.dirname(dbPath), '..');
if (!fs.existsSync(path.join(repoRoot, '.git'))) {
closeDb(db);
return { error: `Not a git repository: ${repoRoot}` };
}
const since = opts.since || '1 year ago';
const minSupport = opts.minSupport ?? 3;
const maxFilesPerCommit = opts.maxFilesPerCommit ?? 50;
// Check for incremental state
let afterSha = null;
if (!opts.full) {
try {
const row = db
.prepare("SELECT value FROM co_change_meta WHERE key = 'last_analyzed_commit'")
.get();
if (row) afterSha = row.value;
} catch {
/* table may not exist yet */
}
}
// If full re-scan, clear existing data
if (opts.full) {
db.exec('DELETE FROM co_changes');
db.exec('DELETE FROM co_change_meta');
db.exec('DELETE FROM file_commit_counts');
}
// Collect known files from the graph for filtering
let knownFiles = null;
try {
const rows = db.prepare('SELECT DISTINCT file FROM nodes').all();
knownFiles = new Set(rows.map((r) => r.file));
} catch {
/* nodes table may not exist */
}
const { commits } = scanGitHistory(repoRoot, { since, afterSha });
const { pairs: coChanges, fileCommitCounts } = computeCoChanges(commits, {
minSupport,
maxFilesPerCommit,
knownFiles,
});
// Upsert per-file commit counts so Jaccard can be recomputed from totals
const fileCountUpsert = db.prepare(`
INSERT INTO file_commit_counts (file, commit_count) VALUES (?, ?)
ON CONFLICT(file) DO UPDATE SET commit_count = commit_count + excluded.commit_count
`);
// Upsert pair counts (accumulate commit_count, jaccard placeholder — recomputed below)
const pairUpsert = db.prepare(`
INSERT INTO co_changes (file_a, file_b, commit_count, jaccard, last_commit_epoch)
VALUES (?, ?, ?, 0, ?)
ON CONFLICT(file_a, file_b) DO UPDATE SET
commit_count = commit_count + excluded.commit_count,
last_commit_epoch = MAX(co_changes.last_commit_epoch, excluded.last_commit_epoch)
`);
const insertMany = db.transaction(() => {
for (const [file, count] of fileCommitCounts) {
fileCountUpsert.run(file, count);
}
for (const [key, data] of coChanges) {
const [fileA, fileB] = key.split('\0');
pairUpsert.run(fileA, fileB, data.commitCount, data.lastEpoch);
}
});
insertMany();
// Recompute Jaccard for all affected pairs from total file commit counts
const affectedFiles = [...fileCommitCounts.keys()];
if (affectedFiles.length > 0) {
const ph = affectedFiles.map(() => '?').join(',');
db.prepare(`
UPDATE co_changes SET jaccard = (
SELECT CAST(co_changes.commit_count AS REAL) / (
COALESCE(fa.commit_count, 0) + COALESCE(fb.commit_count, 0) - co_changes.commit_count
)
FROM file_commit_counts fa, file_commit_counts fb
WHERE fa.file = co_changes.file_a AND fb.file = co_changes.file_b
)
WHERE file_a IN (${ph}) OR file_b IN (${ph})
`).run(...affectedFiles, ...affectedFiles);
}
// Update metadata
const metaUpsert = db.prepare(`
INSERT INTO co_change_meta (key, value) VALUES (?, ?)
ON CONFLICT(key) DO UPDATE SET value = excluded.value
`);
if (commits.length > 0) {
metaUpsert.run('last_analyzed_commit', commits[0].sha);
}
metaUpsert.run('analyzed_at', new Date().toISOString());
metaUpsert.run('since', since);
metaUpsert.run('min_support', String(minSupport));
const totalPairs = db.prepare('SELECT COUNT(*) as cnt FROM co_changes').get().cnt;
closeDb(db);
return {
pairsFound: totalPairs,
commitsScanned: commits.length,
since,
minSupport,
};
}
/**
* Query co-change partners for a specific file.
* @param {string} file - File path (partial match supported)
* @param {string} [customDbPath]
* @param {object} [opts]
* @param {number} [opts.limit=20]
* @param {number} [opts.minJaccard=0.3]
* @param {boolean} [opts.noTests]
* @returns {{ file: string, partners: Array, meta: object }}
*/
export function coChangeData(file, customDbPath, opts = {}) {
const db = openReadonlyOrFail(customDbPath);
const limit = opts.limit || 20;
const minJaccard = opts.minJaccard ?? 0.3;
const noTests = opts.noTests || false;
// Check if co_changes table exists
try {
db.prepare('SELECT 1 FROM co_changes LIMIT 1').get();
} catch {
closeDb(db);
return { error: 'No co-change data found. Run `codegraph co-change --analyze` first.' };
}
// Resolve file via partial match
const resolvedFile = resolveCoChangeFile(db, file);
if (!resolvedFile) {
closeDb(db);
return { error: `No co-change data found for file matching "${file}"` };
}
const rows = db
.prepare(
`SELECT file_a, file_b, commit_count, jaccard, last_commit_epoch
FROM co_changes
WHERE (file_a = ? OR file_b = ?) AND jaccard >= ?
ORDER BY jaccard DESC`,
)
.all(resolvedFile, resolvedFile, minJaccard);
const partners = [];
for (const row of rows) {
const partner = row.file_a === resolvedFile ? row.file_b : row.file_a;
if (noTests && isTestFile(partner)) continue;
partners.push({
file: partner,
commitCount: row.commit_count,
jaccard: row.jaccard,
lastCommitDate: row.last_commit_epoch
? new Date(row.last_commit_epoch * 1000).toISOString().slice(0, 10)
: null,
});
if (partners.length >= limit) break;
}
const meta = getCoChangeMeta(db);
closeDb(db);
return { file: resolvedFile, partners, meta };
}
/**
* Query top global co-change pairs.
* @param {string} [customDbPath]
* @param {object} [opts]
* @param {number} [opts.limit=20]
* @param {number} [opts.minJaccard=0.3]
* @param {boolean} [opts.noTests]
* @returns {{ pairs: Array, meta: object }}
*/
export function coChangeTopData(customDbPath, opts = {}) {
const db = openReadonlyOrFail(customDbPath);
const limit = opts.limit || 20;
const minJaccard = opts.minJaccard ?? 0.3;
const noTests = opts.noTests || false;
try {
db.prepare('SELECT 1 FROM co_changes LIMIT 1').get();
} catch {
closeDb(db);
return { error: 'No co-change data found. Run `codegraph co-change --analyze` first.' };
}
const rows = db
.prepare(
`SELECT file_a, file_b, commit_count, jaccard, last_commit_epoch
FROM co_changes
WHERE jaccard >= ?
ORDER BY jaccard DESC`,
)
.all(minJaccard);
const pairs = [];
for (const row of rows) {
if (noTests && (isTestFile(row.file_a) || isTestFile(row.file_b))) continue;
pairs.push({
fileA: row.file_a,
fileB: row.file_b,
commitCount: row.commit_count,
jaccard: row.jaccard,
lastCommitDate: row.last_commit_epoch
? new Date(row.last_commit_epoch * 1000).toISOString().slice(0, 10)
: null,
});
if (pairs.length >= limit) break;
}
const meta = getCoChangeMeta(db);
closeDb(db);
return { pairs, meta };
}
/**
* Batch-query co-change partners for a set of files.
* Takes an already-open readonly DB handle (for diff-impact integration).
* @param {string[]} files - File paths to query
* @param {import('better-sqlite3').Database} db - Already-open DB handle
* @param {object} [opts]
* @param {number} [opts.minJaccard=0.3]
* @param {number} [opts.limit=20]
* @param {boolean} [opts.noTests]
* @returns {Array<{file: string, coupledWith: string, commitCount: number, jaccard: number}>}
*/
export function coChangeForFiles(files, db, opts = {}) {
const minJaccard = opts.minJaccard ?? 0.3;
const limit = opts.limit ?? 20;
const noTests = opts.noTests || false;
const inputSet = new Set(files);
if (files.length === 0) return [];
const placeholders = files.map(() => '?').join(',');
const rows = db
.prepare(
`SELECT file_a, file_b, commit_count, jaccard
FROM co_changes
WHERE (file_a IN (${placeholders}) OR file_b IN (${placeholders}))
AND jaccard >= ?
ORDER BY jaccard DESC
LIMIT ?`,
)
.all(...files, ...files, minJaccard, limit);
const results = [];
for (const row of rows) {
const partner = inputSet.has(row.file_a) ? row.file_b : row.file_a;
const source = inputSet.has(row.file_a) ? row.file_a : row.file_b;
if (inputSet.has(partner)) continue;
if (noTests && isTestFile(partner)) continue;
results.push({
file: partner,
coupledWith: source,
commitCount: row.commit_count,
jaccard: row.jaccard,
});
}
return results;
}
/**
* Format co-change data for CLI output (single file).
*/
export function formatCoChange(data) {
if (data.error) return data.error;
if (data.partners.length === 0) return `No co-change partners found for ${data.file}`;
const lines = [`\nCo-change partners for ${data.file}:\n`];
for (const p of data.partners) {
const pct = `${(p.jaccard * 100).toFixed(0)}%`.padStart(4);
const commits = `${p.commitCount} commits`.padStart(12);
lines.push(` ${pct} ${commits} ${p.file}`);
}
if (data.meta?.analyzedAt) {
lines.push(`\n Analyzed: ${data.meta.analyzedAt} | Window: ${data.meta.since || 'all'}`);
}
return lines.join('\n');
}
/**
* Format top co-change pairs for CLI output (global view).
*/
export function formatCoChangeTop(data) {
if (data.error) return data.error;
if (data.pairs.length === 0) return 'No co-change pairs found.';
const lines = ['\nTop co-change pairs:\n'];
for (const p of data.pairs) {
const pct = `${(p.jaccard * 100).toFixed(0)}%`.padStart(4);
const commits = `${p.commitCount} commits`.padStart(12);
lines.push(` ${pct} ${commits} ${p.fileA} <-> ${p.fileB}`);
}
if (data.meta?.analyzedAt) {
lines.push(`\n Analyzed: ${data.meta.analyzedAt} | Window: ${data.meta.since || 'all'}`);
}
return lines.join('\n');
}
// ─── Internal Helpers ────────────────────────────────────────────────────
function resolveCoChangeFile(db, file) {
// Exact match first
const exact = db
.prepare(
'SELECT file_a FROM co_changes WHERE file_a = ? UNION SELECT file_b FROM co_changes WHERE file_b = ? LIMIT 1',
)
.get(file, file);
if (exact) return exact.file_a;
// Partial match (ends with)
const partial = db
.prepare(
`SELECT file_a AS file FROM co_changes WHERE file_a LIKE ?
UNION
SELECT file_b AS file FROM co_changes WHERE file_b LIKE ?
LIMIT 1`,
)
.get(`%${file}`, `%${file}`);
if (partial) return partial.file;
return null;
}
function getCoChangeMeta(db) {
try {
const rows = db.prepare('SELECT key, value FROM co_change_meta').all();
const meta = {};
for (const row of rows) {
meta[row.key] = row.value;
}
return {
analyzedAt: meta.analyzed_at || null,
since: meta.since || null,
minSupport: meta.min_support ? parseInt(meta.min_support, 10) : null,
lastCommit: meta.last_analyzed_commit || null,
};
} catch {
return null;
}
}