| 315 | /* ── Public API ──────────────────────────────────────────────────── */ |
| 316 | |
| 317 | cbm_userconfig_t *cbm_userconfig_load(const char *repo_path) { |
| 318 | cbm_userconfig_t *cfg = calloc(CBM_ALLOC_ONE, sizeof(cbm_userconfig_t)); |
| 319 | if (!cfg) { |
| 320 | return NULL; |
| 321 | } |
| 322 | |
| 323 | cbm_userext_t *entries = NULL; |
| 324 | int count = 0; |
| 325 | |
| 326 | /* ── Step 1: Load global config ── */ |
| 327 | enum { PATH_BUF_SZ = 1280 }; |
| 328 | const char *cfg_base = cbm_app_config_dir(); |
| 329 | const char *cfg_fallback = cfg_base ? cfg_base : "/tmp"; |
| 330 | char global_path[PATH_BUF_SZ]; |
| 331 | snprintf(global_path, sizeof(global_path), "%s/codebase-memory-mcp/config.json", cfg_fallback); |
| 332 | |
| 333 | if (load_config_file(global_path, &entries, &count, cfg->global_source_sha256) != 0) { |
| 334 | for (int i = 0; i < count; i++) { |
| 335 | free(entries[i].ext); |
| 336 | } |
| 337 | free(entries); |
| 338 | free(cfg); |
| 339 | return NULL; |
| 340 | } |
| 341 | |
| 342 | int global_count = count; /* entries[0..global_count) are from global */ |
| 343 | |
| 344 | /* ── Step 2: Load project config ── */ |
| 345 | userconfig_source_digest("not-applicable", NULL, 0, cfg->project_source_sha256); |
| 346 | if (repo_path && repo_path[0]) { |
| 347 | char project_path[PATH_BUF_SZ]; |
| 348 | snprintf(project_path, sizeof(project_path), "%s/.codebase-memory.json", repo_path); |
| 349 | |
| 350 | if (load_config_file(project_path, &entries, &count, cfg->project_source_sha256) != 0) { |
| 351 | /* Free already-allocated entries */ |
| 352 | for (int i = 0; i < count; i++) { |
| 353 | free(entries[i].ext); |
| 354 | } |
| 355 | free(entries); |
| 356 | free(cfg); |
| 357 | return NULL; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * ── Step 3: Dedup — project entries win over global ── |
| 363 | * |
| 364 | * For any extension that appears in both global (indices 0..global_count) |
| 365 | * and project (indices global_count..count), remove the global entry by |
| 366 | * replacing it with the last global entry (order-insensitive dedup). |
| 367 | */ |
| 368 | for (int p = global_count; p < count; p++) { |
| 369 | for (int g = 0; g < global_count; g++) { |
| 370 | if (entries[g].ext && strcmp(entries[g].ext, entries[p].ext) == 0) { |
| 371 | /* Remove global entry: overwrite with last global entry */ |
| 372 | free(entries[g].ext); |
| 373 | entries[g] = entries[global_count - SKIP_ONE]; |
| 374 | entries[global_count - SKIP_ONE].ext = NULL; /* mark as consumed */ |
no test coverage detected