Synchronously indexes a repository in a given context.
(path: str, context: Optional[str] = None)
| 193 | |
| 194 | |
| 195 | def index_helper(path: str, context: Optional[str] = None): |
| 196 | """Synchronously indexes a repository in a given context.""" |
| 197 | time_start = time.time() |
| 198 | services = _initialize_services(context) |
| 199 | if not all(services[:3]): |
| 200 | return |
| 201 | |
| 202 | db_manager, graph_builder, code_finder, ctx = services |
| 203 | path_obj = Path(path).resolve() |
| 204 | |
| 205 | if not path_obj.exists(): |
| 206 | console.print(f"[red]Error: Path does not exist: {path_obj}[/red]") |
| 207 | db_manager.close_driver() |
| 208 | return |
| 209 | |
| 210 | indexed_repos = code_finder.list_indexed_repositories() |
| 211 | repo_exists = any_repo_matches_path(indexed_repos, path_obj) |
| 212 | |
| 213 | if repo_exists: |
| 214 | # Check if the repository actually has files (not just an empty node from interrupted indexing) |
| 215 | # Use variable-length path to handle both flat (Repository->File) and |
| 216 | # hierarchical (Repository->Directory->...->File) graph structures |
| 217 | try: |
| 218 | with db_manager.get_driver().session() as session: |
| 219 | result = session.run( |
| 220 | "MATCH (r:Repository {path: $path})-[:CONTAINS*]->(f:File) RETURN count(DISTINCT f) as file_count", |
| 221 | path=str(path_obj) |
| 222 | ) |
| 223 | record = result.single() |
| 224 | file_count = record["file_count"] if record else 0 |
| 225 | |
| 226 | if file_count > 0: |
| 227 | console.print(f"[yellow]Repository '{path}' is already indexed with {file_count} files. Skipping.[/yellow]") |
| 228 | console.print("[dim]💡 Tip: Use 'cgc index --force' to re-index[/dim]") |
| 229 | db_manager.close_driver() |
| 230 | return |
| 231 | else: |
| 232 | console.print(f"[yellow]Repository '{path}' exists but has no files (likely interrupted). Re-indexing...[/yellow]") |
| 233 | except Exception as e: |
| 234 | console.print(f"[yellow]Warning: Could not check file count: {e}. Proceeding with indexing...[/yellow]") |
| 235 | |
| 236 | # Auto-register the repo into the named context (auto-creates if needed) |
| 237 | if context and ctx.mode == "named": |
| 238 | register_repo_in_context(context, str(path_obj), auto_create=True) |
| 239 | |
| 240 | console.print(f"Starting indexing for: {path_obj}") |
| 241 | |
| 242 | try: |
| 243 | asyncio.run(_run_index_with_progress(graph_builder, path_obj, is_dependency=False, cgcignore_path=ctx.cgcignore_path)) |
| 244 | time_end = time.time() |
| 245 | elapsed = time_end - time_start |
| 246 | console.print(f"[green]Successfully finished indexing: {path} in {elapsed:.2f} seconds[/green]") |
| 247 | |
| 248 | # Check if auto-watch is enabled |
| 249 | try: |
| 250 | from codegraphcontext.cli.config_manager import get_config_value |
| 251 | auto_watch = get_config_value('ENABLE_AUTO_WATCH') |
| 252 | if auto_watch and str(auto_watch).lower() == 'true': |
no test coverage detected