MCPcopy Create free account
hub / github.com/CodeGraphContext/CodeGraphContext / computeCommonPrefix

Function computeCommonPrefix

website/src/lib/parser.worker.ts:618–636  ·  view source on GitHub ↗

* Compute the longest common directory prefix of all queued file paths. * e.g. ["/home/user/proj/src/a.ts", "/home/user/proj/lib/b.ts"] * → "/home/user/proj"

(paths: string[])

Source from the content-addressed store, hash-verified

616 * → "/home/user/proj"
617 */
618function computeCommonPrefix(paths: string[]): string {
619 if (paths.length === 0) return '';
620 const dirs = paths.map(p => {
621 const norm = p.replace(/\\/g, '/');
622 return norm.substring(0, norm.lastIndexOf('/'));
623 });
624 const first = dirs[0].split('/');
625 let common = first;
626 for (let i = 1; i < dirs.length; i++) {
627 const parts = dirs[i].split('/');
628 const newCommon: string[] = [];
629 for (let j = 0; j < Math.min(common.length, parts.length); j++) {
630 if (common[j] === parts[j]) newCommon.push(common[j]);
631 else break;
632 }
633 common = newCommon;
634 }
635 return common.join('/');
636}
637
638/**
639 * Lazily create (or return cached) Folder nodes for the repo-relative segments only.

Callers 1

parser.worker.tsFile · 0.85

Calls 2

mapMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected