Skip to content

Commit aef1787

Browse files
refactor: reuse coChangeForFiles in diffImpactData
Replace inline co-change SQL query with a call to the existing coChangeForFiles helper, fixing both duplicated logic and a potential empty-array SQL error (WHERE file_a IN ()). Addresses Greptile review feedback on PR #95.
1 parent 61785f7 commit aef1787

1 file changed

Lines changed: 9 additions & 25 deletions

File tree

src/queries.js

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { execFileSync } from 'node:child_process';
22
import fs from 'node:fs';
33
import path from 'node:path';
4+
import { coChangeForFiles } from './cochange.js';
45
import { findCycles } from './cycles.js';
56
import { findDbPath, openReadonlyOrFail } from './db.js';
67
import { debug } from './logger.js';
@@ -731,34 +732,17 @@ export function diffImpactData(customDbPath, opts = {}) {
731732
for (const key of allAffected) affectedFiles.add(key.split(':')[0]);
732733

733734
// Look up historically coupled files from co-change data
734-
const historicallyCoupled = [];
735+
let historicallyCoupled = [];
735736
try {
736737
db.prepare('SELECT 1 FROM co_changes LIMIT 1').get();
737738
const changedFilesList = [...changedRanges.keys()];
738-
const staticFiles = new Set([...changedRanges.keys(), ...affectedFiles]);
739-
const placeholders = changedFilesList.map(() => '?').join(',');
740-
const coRows = db
741-
.prepare(
742-
`SELECT file_a, file_b, commit_count, jaccard
743-
FROM co_changes
744-
WHERE (file_a IN (${placeholders}) OR file_b IN (${placeholders}))
745-
AND jaccard >= 0.3
746-
ORDER BY jaccard DESC
747-
LIMIT 20`,
748-
)
749-
.all(...changedFilesList, ...changedFilesList);
750-
for (const row of coRows) {
751-
const partner = changedFilesList.includes(row.file_a) ? row.file_b : row.file_a;
752-
const source = changedFilesList.includes(row.file_a) ? row.file_a : row.file_b;
753-
if (!staticFiles.has(partner) && (!noTests || !isTestFile(partner))) {
754-
historicallyCoupled.push({
755-
file: partner,
756-
coupledWith: source,
757-
jaccard: row.jaccard,
758-
commitCount: row.commit_count,
759-
});
760-
}
761-
}
739+
const coResults = coChangeForFiles(changedFilesList, db, {
740+
minJaccard: 0.3,
741+
limit: 20,
742+
noTests,
743+
});
744+
// Exclude files already found via static analysis
745+
historicallyCoupled = coResults.filter((r) => !affectedFiles.has(r.file));
762746
} catch {
763747
/* co_changes table doesn't exist — skip silently */
764748
}

0 commit comments

Comments
 (0)