MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / recreate

Method recreate

src/index.ts:539–581  ·  view source on GitHub ↗

* Rebuild the project's database from scratch and return a fresh, empty * instance — the "same result as a fresh init" semantics that `codegraph * index` documents. * * Unlike `open()` followed by `clear()`, this DISCARDS the existing * `.codegraph/codegraph.db` (and its `-wal`/`-shm`

(projectRoot: string)

Source from the content-addressed store, hash-verified

537 * instance holds it until its first indexAll() finishes or it is closed.
538 */
539 static async recreate(projectRoot: string): Promise<CodeGraph> {
540 await initGrammars();
541 const resolvedRoot = path.resolve(projectRoot);
542
543 // Check if initialized — recreate REBUILDS an existing project; it is not a
544 // first-time `init`.
545 if (!isInitialized(resolvedRoot)) {
546 throw new Error(`CodeGraph not initialized in ${resolvedRoot}. Run init() first.`);
547 }
548
549 const dbPath = getDatabasePath(resolvedRoot);
550 const lock = new FileLock(path.join(path.dirname(dbPath), 'codegraph.lock'));
551 try {
552 lock.acquire();
553 } catch {
554 throw new LockUnavailableError('Could not acquire file lock - another process may be indexing');
555 }
556
557 try {
558 try {
559 removeDatabaseFiles(dbPath);
560 } catch (err) {
561 // POSIX unlinks an open file fine; this fires mainly on Windows when a
562 // live daemon/MCP server still holds the database. Turn the raw EBUSY into
563 // an actionable instruction instead of a generic failure.
564 const reason = err instanceof Error ? err.message : String(err);
565 throw new Error(
566 `Could not rebuild the index — the database file is in use (${reason}). ` +
567 `Stop any running CodeGraph MCP server/daemon for this project and retry, ` +
568 `or remove the ${path.dirname(dbPath)} directory and run "codegraph init".`
569 );
570 }
571
572 // Re-create an empty, freshly-schema'd database at the same path.
573 const db = DatabaseConnection.initialize(dbPath);
574 const queries = new QueryBuilder(db.getDb());
575
576 return new CodeGraph(db, queries, resolvedRoot, lock);
577 } catch (err) {
578 lock.release();
579 throw err;
580 }
581 }
582
583 /**
584 * Open synchronously (without sync).

Callers 15

mainFunction · 0.80
reindexFromScratchFunction · 0.80
reindexFromScratchFunction · 0.80
matchesFullIndexFunction · 0.80
rebuildFunction · 0.80
rebuildEdgeSetFunction · 0.80
convergesFunction · 0.80

Calls 10

acquireMethod · 0.95
releaseMethod · 0.95
initGrammarsFunction · 0.90
isInitializedFunction · 0.90
getDatabasePathFunction · 0.90
removeDatabaseFilesFunction · 0.90
getDbMethod · 0.80
resolveMethod · 0.65
joinMethod · 0.45
initializeMethod · 0.45

Tested by 6

reindexFromScratchFunction · 0.64
reindexFromScratchFunction · 0.64
matchesFullIndexFunction · 0.64
rebuildFunction · 0.64
rebuildEdgeSetFunction · 0.64
convergesFunction · 0.64