-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbranch-compare.ts
More file actions
831 lines (737 loc) · 26.3 KB
/
Copy pathbranch-compare.ts
File metadata and controls
831 lines (737 loc) · 26.3 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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
import { execFileSync } from 'node:child_process';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { getDatabase } from '../db/better-sqlite3.js';
import { resolveBusyTimeoutMs } from '../db/index.js';
import { buildGraph } from '../domain/graph/builder.js';
import { kindIcon } from '../domain/queries.js';
import { debug } from '../infrastructure/logger.js';
import { getNative, isNativeAvailable } from '../infrastructure/native.js';
import { isTestFile } from '../infrastructure/test-filter.js';
import { toErrorMessage } from '../shared/errors.js';
import { toSymbolRef } from '../shared/normalize.js';
import type { BetterSqlite3Database, EngineMode, NativeDatabase } from '../types.js';
// ─── Git Helpers ────────────────────────────────────────────────────────
function validateGitRef(repoRoot: string, ref: string): string | null {
try {
const sha = execFileSync('git', ['rev-parse', '--verify', ref], {
cwd: repoRoot,
encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'pipe'],
}).trim();
return sha;
} catch (e) {
debug(`validateGitRef failed for "${ref}": ${toErrorMessage(e)}`);
return null;
}
}
function getChangedFilesBetweenRefs(repoRoot: string, base: string, target: string): string[] {
const output = execFileSync('git', ['diff', '--name-only', `${base}..${target}`], {
cwd: repoRoot,
encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'pipe'],
}).trim();
if (!output) return [];
return output.split('\n').filter(Boolean);
}
function createWorktree(repoRoot: string, ref: string, dir: string): void {
execFileSync('git', ['worktree', 'add', '--detach', dir, ref], {
cwd: repoRoot,
encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'pipe'],
});
}
function removeWorktree(repoRoot: string, dir: string): void {
try {
execFileSync('git', ['worktree', 'remove', '--force', dir], {
cwd: repoRoot,
encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'pipe'],
});
} catch (e) {
debug(`removeWorktree: git worktree remove failed for ${dir}: ${toErrorMessage(e)}`);
try {
fs.rmSync(dir, { recursive: true, force: true });
} catch (rmErr) {
debug(`removeWorktree: rmSync fallback failed for ${dir}: ${toErrorMessage(rmErr)}`);
}
try {
execFileSync('git', ['worktree', 'prune'], {
cwd: repoRoot,
encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'pipe'],
});
} catch (pruneErr) {
debug(`removeWorktree: git worktree prune failed: ${toErrorMessage(pruneErr)}`);
}
}
}
// ─── Symbol Loading ─────────────────────────────────────────────────────
interface SymbolInfo {
id: number;
name: string;
kind: string;
file: string;
line: number;
lineCount: number;
fanIn: number;
fanOut: number;
}
interface CallerInfo {
name: string;
kind: string;
file: string;
line: number;
}
interface ChangedSymbol {
name: string;
kind: string;
file: string;
base: { line: number; lineCount: number; fanIn: number; fanOut: number };
target: { line: number; lineCount: number; fanIn: number; fanOut: number };
changes: { lineCount: number; fanIn: number; fanOut: number };
impact?: CallerInfo[];
}
function makeSymbolKey(kind: string, file: string, name: string): string {
return `${kind}::${file}::${name}`;
}
interface RawNodeRow {
id: number;
name: string;
kind: string;
file: string;
line: number;
end_line: number | null;
}
/** Try opening a NativeDatabase handle for batched fan-in/fan-out metrics. */
export function openNativeDbForFanMetrics(dbPath: string): NativeDatabase | undefined {
if (!isNativeAvailable()) return undefined;
try {
const native = getNative();
return native.NativeDatabase.openReadonly(dbPath, resolveBusyTimeoutMs(dbPath));
} catch (e) {
debug(`loadSymbolsFromDb: native path failed: ${toErrorMessage(e)}`);
return undefined;
}
}
/** Query all non-file/directory nodes belonging to the given changed files. */
function queryChangedFileNodes(db: BetterSqlite3Database, changedFiles: string[]): RawNodeRow[] {
const placeholders = changedFiles.map(() => '?').join(', ');
return db
.prepare(
`SELECT n.id, n.name, n.kind, n.file, n.line, n.end_line
FROM nodes n
WHERE n.file IN (${placeholders})
AND n.kind NOT IN ('file', 'directory')
ORDER BY n.file, n.line`,
)
.all(...changedFiles) as RawNodeRow[];
}
/** Build the public SymbolInfo shape from a raw row + its resolved fan metrics. */
function makeSymbolInfo(row: RawNodeRow, fanIn: number, fanOut: number): SymbolInfo {
const lineCount = row.end_line ? row.end_line - row.line + 1 : 0;
return {
id: row.id,
name: row.name,
kind: row.kind,
file: row.file,
line: row.line,
lineCount,
fanIn,
fanOut,
};
}
/** Native fast path: batch all fan-in/fan-out lookups in one napi call. */
function buildSymbolsViaNativeBatch(
filtered: RawNodeRow[],
nativeDb: NativeDatabase,
): Map<string, SymbolInfo> {
const symbols = new Map<string, SymbolInfo>();
const nodeIds = filtered.map((r) => r.id);
const metrics = nativeDb.batchFanMetrics!(nodeIds);
const metricsMap = new Map(metrics.map((m) => [m.nodeId, m]));
for (const row of filtered) {
const m = metricsMap.get(row.id);
const key = makeSymbolKey(row.kind, row.file, row.name);
symbols.set(key, makeSymbolInfo(row, m?.fanIn ?? 0, m?.fanOut ?? 0));
}
return symbols;
}
/** JS fallback: per-row fan-in/fan-out COUNT queries. */
function buildSymbolsViaJsFallback(
db: BetterSqlite3Database,
filtered: RawNodeRow[],
): Map<string, SymbolInfo> {
const symbols = new Map<string, SymbolInfo>();
const fanInStmt = db.prepare(
`SELECT COUNT(*) AS cnt FROM edges WHERE target_id = ? AND kind = 'calls'`,
);
const fanOutStmt = db.prepare(
`SELECT COUNT(*) AS cnt FROM edges WHERE source_id = ? AND kind = 'calls'`,
);
for (const row of filtered) {
const fanIn = (fanInStmt.get(row.id) as { cnt: number }).cnt;
const fanOut = (fanOutStmt.get(row.id) as { cnt: number }).cnt;
const key = makeSymbolKey(row.kind, row.file, row.name);
symbols.set(key, makeSymbolInfo(row, fanIn, fanOut));
}
return symbols;
}
function loadSymbolsFromDb(
dbPath: string,
changedFiles: string[],
noTests: boolean,
): Map<string, SymbolInfo> {
const Database = getDatabase();
const db = new Database(dbPath, { readonly: true });
const nativeDb = openNativeDbForFanMetrics(dbPath);
try {
if (changedFiles.length === 0) {
return new Map();
}
const rows = queryChangedFileNodes(db, changedFiles);
// Filter first, then batch fan metrics for all surviving rows
const filtered = noTests ? rows.filter((r) => !isTestFile(r.file)) : rows;
if (nativeDb?.batchFanMetrics && filtered.length > 0) {
return buildSymbolsViaNativeBatch(filtered, nativeDb);
}
return buildSymbolsViaJsFallback(db, filtered);
} finally {
db.close();
if (nativeDb) {
try {
nativeDb.close();
} catch (e) {
debug(`loadSymbolsFromDb: nativeDb close failed: ${toErrorMessage(e)}`);
}
}
}
}
// ─── Caller BFS ─────────────────────────────────────────────────────────
function loadCallersFromDb(
dbPath: string,
nodeIds: number[],
maxDepth: number,
noTests: boolean,
): CallerInfo[] {
if (nodeIds.length === 0) return [];
const Database = getDatabase();
const db = new Database(dbPath, { readonly: true });
try {
const allCallers = new Set<string>();
for (const startId of nodeIds) {
bfsCallersFromNode(db, startId, maxDepth, noTests, allCallers);
}
return [...allCallers].map((s) => JSON.parse(s) as CallerInfo);
} finally {
db.close();
}
}
/** Direct DB callers of a single node id (one BFS-frontier expansion step). */
function queryDirectCallers(
db: BetterSqlite3Database,
nodeId: number,
): Array<{ id: number; name: string; kind: string; file: string; line: number }> {
return db
.prepare(
`SELECT DISTINCT n.id, n.name, n.kind, n.file, n.line
FROM edges e JOIN nodes n ON e.source_id = n.id
WHERE e.target_id = ? AND e.kind = 'calls'`,
)
.all(nodeId) as Array<{ id: number; name: string; kind: string; file: string; line: number }>;
}
/** BFS up to maxDepth from a single starting node, adding newly-seen callers to allCallers. */
function bfsCallersFromNode(
db: BetterSqlite3Database,
startId: number,
maxDepth: number,
noTests: boolean,
allCallers: Set<string>,
): void {
const visited = new Set<number>([startId]);
let frontier = [startId];
for (let d = 1; d <= maxDepth; d++) {
const nextFrontier: number[] = [];
for (const fid of frontier) {
const callers = queryDirectCallers(db, fid);
for (const c of callers) {
if (!visited.has(c.id) && (!noTests || !isTestFile(c.file))) {
visited.add(c.id);
nextFrontier.push(c.id);
allCallers.add(JSON.stringify(toSymbolRef(c)));
}
}
}
frontier = nextFrontier;
if (frontier.length === 0) break;
}
}
// ─── Symbol Comparison ──────────────────────────────────────────────────
/** Symbols present in `targetSymbols` but not `baseSymbols`. */
function findAddedSymbols(
baseSymbols: Map<string, SymbolInfo>,
targetSymbols: Map<string, SymbolInfo>,
): SymbolInfo[] {
const added: SymbolInfo[] = [];
for (const [key, sym] of targetSymbols) {
if (!baseSymbols.has(key)) added.push(sym);
}
return added;
}
/** Symbols present in `baseSymbols` but not `targetSymbols`. */
function findRemovedSymbols(
baseSymbols: Map<string, SymbolInfo>,
targetSymbols: Map<string, SymbolInfo>,
): SymbolInfo[] {
const removed: SymbolInfo[] = [];
for (const [key, sym] of baseSymbols) {
if (!targetSymbols.has(key)) removed.push(sym);
}
return removed;
}
/** Build a ChangedSymbol entry from a base/target pair whose metrics diverged. */
function buildChangedSymbol(baseSym: SymbolInfo, targetSym: SymbolInfo): ChangedSymbol | null {
const lineCountDelta = targetSym.lineCount - baseSym.lineCount;
const fanInDelta = targetSym.fanIn - baseSym.fanIn;
const fanOutDelta = targetSym.fanOut - baseSym.fanOut;
if (lineCountDelta === 0 && fanInDelta === 0 && fanOutDelta === 0) return null;
return {
name: baseSym.name,
kind: baseSym.kind,
file: baseSym.file,
base: {
line: baseSym.line,
lineCount: baseSym.lineCount,
fanIn: baseSym.fanIn,
fanOut: baseSym.fanOut,
},
target: {
line: targetSym.line,
lineCount: targetSym.lineCount,
fanIn: targetSym.fanIn,
fanOut: targetSym.fanOut,
},
changes: {
lineCount: lineCountDelta,
fanIn: fanInDelta,
fanOut: fanOutDelta,
},
};
}
/** Symbols present in both maps whose line count / fan-in / fan-out diverged. */
function findChangedSymbols(
baseSymbols: Map<string, SymbolInfo>,
targetSymbols: Map<string, SymbolInfo>,
): ChangedSymbol[] {
const changed: ChangedSymbol[] = [];
for (const [key, baseSym] of baseSymbols) {
const targetSym = targetSymbols.get(key);
if (!targetSym) continue;
const entry = buildChangedSymbol(baseSym, targetSym);
if (entry) changed.push(entry);
}
return changed;
}
function compareSymbols(
baseSymbols: Map<string, SymbolInfo>,
targetSymbols: Map<string, SymbolInfo>,
): { added: SymbolInfo[]; removed: SymbolInfo[]; changed: ChangedSymbol[] } {
return {
added: findAddedSymbols(baseSymbols, targetSymbols),
removed: findRemovedSymbols(baseSymbols, targetSymbols),
changed: findChangedSymbols(baseSymbols, targetSymbols),
};
}
// ─── Main Data Function ─────────────────────────────────────────────────
interface BranchCompareOpts {
repoRoot?: string;
depth?: number;
noTests?: boolean;
engine?: string;
}
interface BranchCompareSummary {
added: number;
removed: number;
changed: number;
totalImpacted: number;
filesAffected: number;
}
type SymbolWithoutId = Omit<SymbolInfo, 'id'> & { impact?: CallerInfo[] };
interface BranchCompareResult {
error?: string;
baseRef?: string;
targetRef?: string;
baseSha?: string;
targetSha?: string;
changedFiles?: string[];
added?: SymbolWithoutId[];
removed?: SymbolWithoutId[];
changed?: ChangedSymbol[];
summary?: BranchCompareSummary;
}
/**
* Attach caller-impact data to each symbol, given a strategy for resolving
* its DB node id (removed symbols carry their own id; changed symbols must
* be looked up in the base-commit symbol map).
*/
function attachImpact<T>(
symbols: T[],
resolveId: (sym: T) => number | undefined,
dbPath: string,
maxDepth: number,
noTests: boolean,
): void {
for (const sym of symbols) {
const id = resolveId(sym);
const symCallers = loadCallersFromDb(dbPath, id ? [id] : [], maxDepth, noTests);
(sym as T & { impact?: CallerInfo[] }).impact = symCallers;
}
}
/** Confirm repoRoot is a git repo and resolve baseRef/targetRef to full SHAs. */
function validateBranchCompareRefs(
repoRoot: string,
baseRef: string,
targetRef: string,
): { baseSha: string; targetSha: string } | { error: string } {
try {
execFileSync('git', ['rev-parse', '--git-dir'], {
cwd: repoRoot,
encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'pipe'],
});
} catch (e) {
debug(`branchCompareData: git check failed: ${toErrorMessage(e)}`);
return { error: 'Not a git repository' };
}
const baseSha = validateGitRef(repoRoot, baseRef);
if (!baseSha) return { error: `Invalid git ref: "${baseRef}"` };
const targetSha = validateGitRef(repoRoot, targetRef);
if (!targetSha) return { error: `Invalid git ref: "${targetRef}"` };
return { baseSha, targetSha };
}
/** Create detached worktrees for both refs and build their graphs. */
async function setupCompareWorktrees(
repoRoot: string,
baseSha: string,
targetSha: string,
baseDir: string,
targetDir: string,
engine: EngineMode,
): Promise<{ baseDbPath: string; targetDbPath: string }> {
createWorktree(repoRoot, baseSha, baseDir);
createWorktree(repoRoot, targetSha, targetDir);
await buildGraph(baseDir, { engine, skipRegistry: true });
await buildGraph(targetDir, { engine, skipRegistry: true });
return {
baseDbPath: path.join(baseDir, '.codegraph', 'graph.db'),
targetDbPath: path.join(targetDir, '.codegraph', 'graph.db'),
};
}
interface SymbolDiffWithImpact {
added: SymbolInfo[];
removed: SymbolInfo[];
changed: ChangedSymbol[];
allImpacted: Set<string>;
impactedFiles: Set<string>;
}
/** Resolve base-commit node ids for removed/changed symbols (for BFS impact queries). */
function resolveImpactfulIds(
removed: SymbolInfo[],
changed: ChangedSymbol[],
baseSymbols: Map<string, SymbolInfo>,
): { removedIds: number[]; changedIds: number[] } {
const removedIds = removed.map((s) => s.id).filter(Boolean);
const changedIds = changed
.map((s) => baseSymbols.get(makeSymbolKey(s.kind, s.file, s.name))?.id)
.filter((id): id is number => Boolean(id));
return { removedIds, changedIds };
}
/** Collapse removed+changed caller lists into the summary's impacted-symbol/file sets. */
function computeImpactedFileSets(
removedImpact: CallerInfo[],
changedImpact: CallerInfo[],
): { allImpacted: Set<string>; impactedFiles: Set<string> } {
const allImpacted = new Set<string>();
for (const c of removedImpact) allImpacted.add(`${c.file}:${c.name}`);
for (const c of changedImpact) allImpacted.add(`${c.file}:${c.name}`);
const impactedFiles = new Set<string>();
for (const key of allImpacted) impactedFiles.add(key.split(':')[0]!);
return { allImpacted, impactedFiles };
}
/** Load symbols from both DBs, diff them, and attach/compute blast-radius impact data. */
function diffSymbolsWithImpact(
baseDbPath: string,
targetDbPath: string,
normalizedFiles: string[],
noTests: boolean,
maxDepth: number,
): SymbolDiffWithImpact {
const baseSymbols = loadSymbolsFromDb(baseDbPath, normalizedFiles, noTests);
const targetSymbols = loadSymbolsFromDb(targetDbPath, normalizedFiles, noTests);
const { added, removed, changed } = compareSymbols(baseSymbols, targetSymbols);
const { removedIds, changedIds } = resolveImpactfulIds(removed, changed, baseSymbols);
const removedImpact = loadCallersFromDb(baseDbPath, removedIds, maxDepth, noTests);
const changedImpact = loadCallersFromDb(baseDbPath, changedIds, maxDepth, noTests);
attachImpact(removed, (s) => s.id, baseDbPath, maxDepth, noTests);
attachImpact(
changed,
(s) => baseSymbols.get(makeSymbolKey(s.kind, s.file, s.name))?.id,
baseDbPath,
maxDepth,
noTests,
);
const { allImpacted, impactedFiles } = computeImpactedFileSets(removedImpact, changedImpact);
return { added, removed, changed, allImpacted, impactedFiles };
}
/** Strip the internal `.id` field, keeping `.impact` where it was attached. */
function shapeBranchCompareSymbolLists(
added: SymbolInfo[],
removed: SymbolInfo[],
): { cleanAdded: SymbolWithoutId[]; cleanRemoved: SymbolWithoutId[] } {
const cleanAdded = added.map(({ id: _id, ...rest }) => rest as SymbolWithoutId);
const cleanRemoved = removed.map(({ id: _id, ...rest }) => {
const result = rest as SymbolWithoutId;
if ((rest as SymbolInfo & { impact?: CallerInfo[] }).impact) {
result.impact = (rest as SymbolInfo & { impact?: CallerInfo[] }).impact;
}
return result;
});
return { cleanAdded, cleanRemoved };
}
/** Result shape when there are no changed files between the two refs. */
function emptyBranchCompareResult(
baseRef: string,
targetRef: string,
baseSha: string,
targetSha: string,
): BranchCompareResult {
return {
baseRef,
targetRef,
baseSha,
targetSha,
changedFiles: [],
added: [],
removed: [],
changed: [],
summary: { added: 0, removed: 0, changed: 0, totalImpacted: 0, filesAffected: 0 },
};
}
/** Assemble the final BranchCompareResult from the diff + cleaned symbol lists. */
function buildBranchCompareResult(
refs: { baseRef: string; targetRef: string; baseSha: string; targetSha: string },
normalizedFiles: string[],
diff: SymbolDiffWithImpact,
cleaned: { cleanAdded: SymbolWithoutId[]; cleanRemoved: SymbolWithoutId[] },
): BranchCompareResult {
return {
...refs,
changedFiles: normalizedFiles,
added: cleaned.cleanAdded,
removed: cleaned.cleanRemoved,
changed: diff.changed,
summary: {
added: diff.added.length,
removed: diff.removed.length,
changed: diff.changed.length,
totalImpacted: diff.allImpacted.size,
filesAffected: diff.impactedFiles.size,
},
};
}
/** Resolve branchCompareData's opts (repoRoot/maxDepth/noTests/engine) with their defaults. */
function resolveBranchCompareOptions(opts: BranchCompareOpts): {
repoRoot: string;
maxDepth: number;
noTests: boolean;
engine: EngineMode;
} {
return {
repoRoot: opts.repoRoot || process.cwd(),
maxDepth: opts.depth || 3,
noTests: opts.noTests || false,
engine: (opts.engine || 'wasm') as EngineMode,
};
}
/** Create the scratch tmpdir + base/target subdirectory paths for the dual worktrees. */
function createCompareTempDirs(): { tmpBase: string; baseDir: string; targetDir: string } {
const tmpBase = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-bc-'));
return { tmpBase, baseDir: path.join(tmpBase, 'base'), targetDir: path.join(tmpBase, 'target') };
}
/** Remove both worktrees and the scratch tmpdir (best-effort, always runs in `finally`). */
function cleanupCompareTempDirs(
repoRoot: string,
baseDir: string,
targetDir: string,
tmpBase: string,
): void {
removeWorktree(repoRoot, baseDir);
removeWorktree(repoRoot, targetDir);
try {
fs.rmSync(tmpBase, { recursive: true, force: true });
} catch (cleanupErr) {
debug(`branchCompareData: temp cleanup failed: ${toErrorMessage(cleanupErr)}`);
}
}
/** Set up worktrees, diff the symbols, and shape the final result (the try-block body). */
async function runBranchCompareInWorktrees(
resolvedRefs: { baseRef: string; targetRef: string; baseSha: string; targetSha: string },
dirs: { repoRoot: string; baseDir: string; targetDir: string; engine: EngineMode },
changedFiles: string[],
noTests: boolean,
maxDepth: number,
): Promise<BranchCompareResult> {
const { baseSha, targetSha } = resolvedRefs;
const { baseDbPath, targetDbPath } = await setupCompareWorktrees(
dirs.repoRoot,
baseSha,
targetSha,
dirs.baseDir,
dirs.targetDir,
dirs.engine,
);
const normalizedFiles = changedFiles.map((f) => f.replace(/\\/g, '/'));
const diff = diffSymbolsWithImpact(baseDbPath, targetDbPath, normalizedFiles, noTests, maxDepth);
const cleaned = shapeBranchCompareSymbolLists(diff.added, diff.removed);
return buildBranchCompareResult(resolvedRefs, normalizedFiles, diff, cleaned);
}
export async function branchCompareData(
baseRef: string,
targetRef: string,
opts: BranchCompareOpts = {},
): Promise<BranchCompareResult> {
const { repoRoot, maxDepth, noTests, engine } = resolveBranchCompareOptions(opts);
const refs = validateBranchCompareRefs(repoRoot, baseRef, targetRef);
if ('error' in refs) return refs;
const { baseSha, targetSha } = refs;
try {
const changedFiles = getChangedFilesBetweenRefs(repoRoot, baseSha, targetSha);
if (changedFiles.length === 0) {
return emptyBranchCompareResult(baseRef, targetRef, baseSha, targetSha);
}
const { tmpBase, baseDir, targetDir } = createCompareTempDirs();
try {
return await runBranchCompareInWorktrees(
{ baseRef, targetRef, baseSha, targetSha },
{ repoRoot, baseDir, targetDir, engine },
changedFiles,
noTests,
maxDepth,
);
} finally {
cleanupCompareTempDirs(repoRoot, baseDir, targetDir, tmpBase);
}
} catch (err) {
return { error: toErrorMessage(err) };
}
}
// ─── Mermaid Output ─────────────────────────────────────────────────────
interface MermaidNodeIdState {
counter: number;
map: Map<string, string>;
}
function mermaidNodeId(state: MermaidNodeIdState, key: string): string {
if (!state.map.has(key)) {
state.map.set(key, `n${state.counter++}`);
}
return state.map.get(key)!;
}
function addMermaidSubgraph(
lines: string[],
state: MermaidNodeIdState,
prefix: string,
label: string,
symbols: Array<{ kind: string; file: string; name: string }>,
fillColor: string,
strokeColor: string,
): void {
if (symbols.length === 0) return;
lines.push(` subgraph sg_${prefix}["${label}"]`);
for (const sym of symbols) {
const key = `${prefix}::${sym.kind}::${sym.file}::${sym.name}`;
const nid = mermaidNodeId(state, key);
lines.push(` ${nid}["[${kindIcon(sym.kind)}] ${sym.name}"]`);
}
lines.push(' end');
lines.push(` style sg_${prefix} fill:${fillColor},stroke:${strokeColor}`);
}
function collectImpactedCallers(
impactSources: Array<{ impact?: CallerInfo[] }>,
): Map<string, CallerInfo> {
const allImpacted = new Map<string, CallerInfo>();
for (const sym of impactSources) {
if (!sym.impact) continue;
for (const c of sym.impact) {
const key = `impact::${c.kind}::${c.file}::${c.name}`;
if (!allImpacted.has(key)) allImpacted.set(key, c);
}
}
return allImpacted;
}
/** Render the "Impacted Callers" subgraph block, if there are any impacted callers. */
function renderImpactedCallersSubgraph(
lines: string[],
state: MermaidNodeIdState,
allImpacted: Map<string, CallerInfo>,
): void {
if (allImpacted.size === 0) return;
lines.push(' subgraph sg_impact["Impacted Callers"]');
for (const [key, c] of allImpacted) {
const nid = mermaidNodeId(state, key);
lines.push(` ${nid}["[${kindIcon(c.kind)}] ${c.name}"]`);
}
lines.push(' end');
lines.push(' style sg_impact fill:#f3e5f5,stroke:#9c27b0');
}
/** Draw the dotted "impacted by" edges from each removed/changed symbol to its callers. */
function renderImpactEdges(
lines: string[],
state: MermaidNodeIdState,
impactSources: Array<{ kind: string; file: string; name: string; impact?: CallerInfo[] }>,
removed: SymbolWithoutId[],
): void {
for (const sym of impactSources) {
if (!sym.impact) continue;
const prefix = removed.includes(sym as SymbolWithoutId) ? 'removed' : 'changed';
const symKey = `${prefix}::${sym.kind}::${sym.file}::${sym.name}`;
for (const c of sym.impact) {
const callerKey = `impact::${c.kind}::${c.file}::${c.name}`;
if (state.map.has(symKey) && state.map.has(callerKey)) {
lines.push(` ${state.map.get(symKey)} -.-> ${state.map.get(callerKey)}`);
}
}
}
}
/** True if the compare result has no added/removed/changed symbols to render. */
function hasNoBranchDifferences(data: BranchCompareResult): boolean {
return (
(data.added?.length ?? 0) === 0 &&
(data.removed?.length ?? 0) === 0 &&
(data.changed?.length ?? 0) === 0
);
}
/** Render the three top-level Added/Removed/Changed subgraphs. */
function renderAddedRemovedChangedSubgraphs(
lines: string[],
state: MermaidNodeIdState,
data: BranchCompareResult,
): void {
addMermaidSubgraph(lines, state, 'added', 'Added', data.added || [], '#e8f5e9', '#4caf50');
addMermaidSubgraph(lines, state, 'removed', 'Removed', data.removed || [], '#ffebee', '#f44336');
addMermaidSubgraph(lines, state, 'changed', 'Changed', data.changed || [], '#fff3e0', '#ff9800');
}
export function branchCompareMermaid(data: BranchCompareResult): string {
if (data.error) return data.error;
if (hasNoBranchDifferences(data)) {
return 'flowchart TB\n none["No structural differences detected"]';
}
const lines = ['flowchart TB'];
const state: MermaidNodeIdState = { counter: 0, map: new Map() };
renderAddedRemovedChangedSubgraphs(lines, state, data);
const impactSources = [...(data.removed || []), ...(data.changed || [])];
const allImpacted = collectImpactedCallers(impactSources);
renderImpactedCallersSubgraph(lines, state, allImpacted);
renderImpactEdges(lines, state, impactSources, data.removed || []);
return lines.join('\n');
}