| 387 | /* ── Public API ──────────────────────────────────────────────────── */ |
| 388 | |
| 389 | static cbm_userconfig_t *userconfig_load(const char *repo_path, bool retain_source) { |
| 390 | /* The retained source first: a failure here has nothing else to undo. */ |
| 391 | cbm_userconfig_source_t *source = NULL; |
| 392 | if (retain_source) { |
| 393 | CBMArena arena; |
| 394 | cbm_arena_init(&arena); |
| 395 | source = cbm_arena_calloc(&arena, sizeof(*source)); |
| 396 | if (!source) { |
| 397 | cbm_arena_destroy(&arena); |
| 398 | return NULL; |
| 399 | } |
| 400 | source->arena = arena; |
| 401 | source->state = CBM_USERCONFIG_SOURCE_ABSENT; |
| 402 | } |
| 403 | cbm_userconfig_t *cfg = calloc(CBM_ALLOC_ONE, sizeof(cbm_userconfig_t)); |
| 404 | if (!cfg) { |
| 405 | userconfig_source_free(source); |
| 406 | return NULL; |
| 407 | } |
| 408 | cfg->project_source = source; |
| 409 | |
| 410 | cbm_userext_t *entries = NULL; |
| 411 | int count = 0; |
| 412 | |
| 413 | /* ── Step 1: Load global config ── */ |
| 414 | enum { PATH_BUF_SZ = 1280 }; |
| 415 | const char *cfg_base = cbm_app_config_dir(); |
| 416 | const char *cfg_fallback = cfg_base ? cfg_base : "/tmp"; |
| 417 | char global_path[PATH_BUF_SZ]; |
| 418 | snprintf(global_path, sizeof(global_path), "%s/codebase-memory-mcp/config.json", cfg_fallback); |
| 419 | |
| 420 | if (load_config_file(global_path, &entries, &count, cfg->global_source_sha256) != 0) { |
| 421 | for (int i = 0; i < count; i++) { |
| 422 | free(entries[i].ext); |
| 423 | } |
| 424 | free(entries); |
| 425 | userconfig_source_free(cfg->project_source); |
| 426 | free(cfg); |
| 427 | return NULL; |
| 428 | } |
| 429 | |
| 430 | int global_count = count; /* entries[0..global_count) are from global */ |
| 431 | |
| 432 | /* ── Step 2: Load project config ── */ |
| 433 | userconfig_source_digest("not-applicable", NULL, 0, cfg->project_source_sha256); |
| 434 | if (repo_path && repo_path[0]) { |
| 435 | char project_path[PATH_BUF_SZ]; |
| 436 | int path_len = |
| 437 | snprintf(project_path, sizeof(project_path), "%s/.codebase-memory.json", repo_path); |
| 438 | int rc; |
| 439 | if (retain_source) { |
| 440 | if (path_len < 0 || (size_t)path_len >= sizeof(project_path)) { |
| 441 | cfg->project_source->state = CBM_USERCONFIG_SOURCE_ERROR; |
| 442 | userconfig_source_digest("path-error", NULL, 0, cfg->project_source_sha256); |
| 443 | rc = 0; |
| 444 | } else { |
| 445 | rc = load_project_source(project_path, cfg, &entries, &count); |
| 446 | } |
no test coverage detected