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

Function index

src/codegraphcontext/cli/main.py:1438–1500  ·  view source on GitHub ↗

Indexes a directory or file by adding it to the code graph. If no path is provided, it indexes the current directory. Use --force to delete the existing index and rebuild from scratch. Use --summarize to display a summary after indexing.

(
    path: Optional[str] = typer.Argument(None, help="Path to the directory or file to index. Defaults to the current directory."),
    force: bool = typer.Option(False, "--force", "-f", help="Force re-index (delete existing and rebuild)"),
    context: Optional[str] = typer.Option(None, "--context", "-c", help="Specific context to use (overrides mode/default)"),
    summarize: bool = typer.Option(False, "--summarize", "-s", help="Show a summary of the indexed codebase after indexing"),
    no_progress: bool = typer.Option(False, "--no-progress", help="Disable live progress rendering during indexing."),
)

Source from the content-addressed store, hash-verified

1436
1437@app.command()
1438def index(
1439 path: Optional[str] = typer.Argument(None, help="Path to the directory or file to index. Defaults to the current directory."),
1440 force: bool = typer.Option(False, "--force", "-f", help="Force re-index (delete existing and rebuild)"),
1441 context: Optional[str] = typer.Option(None, "--context", "-c", help="Specific context to use (overrides mode/default)"),
1442 summarize: bool = typer.Option(False, "--summarize", "-s", help="Show a summary of the indexed codebase after indexing"),
1443 no_progress: bool = typer.Option(False, "--no-progress", help="Disable live progress rendering during indexing."),
1444):
1445 """
1446 Indexes a directory or file by adding it to the code graph.
1447 If no path is provided, it indexes the current directory.
1448
1449 Use --force to delete the existing index and rebuild from scratch.
1450 Use --summarize to display a summary after indexing.
1451 """
1452 _load_credentials()
1453 if path is None:
1454 path = str(Path.cwd())
1455
1456 try:
1457 if force:
1458 console.print("[yellow]Force re-indexing (--force flag detected)[/yellow]")
1459 reindex_helper(path, context, no_progress=no_progress)
1460 else:
1461 index_helper(path, context, no_progress=no_progress)
1462 except typer.Exit:
1463 # typer.Exit subclasses RuntimeError and str() is empty, so the handler
1464 # below caught it, printed nothing, and returned 0 — every helper that
1465 # raised `typer.Exit(code=1)` (missing path, failed indexing) silently
1466 # became a success. Re-raise control flow before catching errors.
1467 raise
1468 except Exception as e:
1469 if str(e):
1470 console.print(f"[red]An error occurred during indexing: {e}[/red]")
1471 else:
1472 console.print("[red]An error occurred during indexing.[/red]")
1473 raise typer.Exit(code=1)
1474
1475 if summarize:
1476 import os
1477
1478 py_files = []
1479 for root, dirs, files in os.walk(path):
1480 dirs[:] = [d for d in dirs if not d.startswith('.') and d not in ['venv', '__pycache__', 'node_modules']]
1481 for file in files:
1482 if file.endswith('.py'):
1483 py_files.append(os.path.join(root, file))
1484
1485 total_lines = 0
1486 for f in py_files:
1487 try:
1488 with open(f, 'r', encoding='utf-8', errors='ignore') as fp:
1489 total_lines += len(fp.readlines())
1490 except:
1491 pass
1492
1493 console.print("\n[bold cyan]📊 Codebase Summary:[/bold cyan]")
1494 console.print(f" • Path indexed : [green]{path}[/green]")
1495 console.print(f" • Python files : [yellow]{len(py_files)}[/yellow]")

Callers 1

index_abbrevFunction · 0.85

Calls 6

_load_credentialsFunction · 0.85
reindex_helperFunction · 0.85
index_helperFunction · 0.85
printMethod · 0.80
appendMethod · 0.65
walkMethod · 0.45

Tested by

no test coverage detected