| 285 | /* ── Public API ──────────────────────────────────────────────────── */ |
| 286 | |
| 287 | cbm_userconfig_t *cbm_userconfig_load(const char *repo_path) { |
| 288 | cbm_userconfig_t *cfg = calloc(CBM_ALLOC_ONE, sizeof(cbm_userconfig_t)); |
| 289 | if (!cfg) { |
| 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | cbm_userext_t *entries = NULL; |
| 294 | int count = 0; |
| 295 | |
| 296 | /* ── Step 1: Load global config ── */ |
| 297 | enum { PATH_BUF_SZ = 1280 }; |
| 298 | const char *cfg_base = cbm_app_config_dir(); |
| 299 | const char *cfg_fallback = cfg_base ? cfg_base : "/tmp"; |
| 300 | char global_path[PATH_BUF_SZ]; |
| 301 | snprintf(global_path, sizeof(global_path), "%s/codebase-memory-mcp/config.json", cfg_fallback); |
| 302 | |
| 303 | if (load_config_file(global_path, &entries, &count) != 0) { |
| 304 | for (int i = 0; i < count; i++) { |
| 305 | free(entries[i].ext); |
| 306 | } |
| 307 | free(entries); |
| 308 | free(cfg); |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | int global_count = count; /* entries[0..global_count) are from global */ |
| 313 | |
| 314 | /* ── Step 2: Load project config ── */ |
| 315 | if (repo_path && repo_path[0]) { |
| 316 | char project_path[PATH_BUF_SZ]; |
| 317 | snprintf(project_path, sizeof(project_path), "%s/.codebase-memory.json", repo_path); |
| 318 | |
| 319 | if (load_config_file(project_path, &entries, &count) != 0) { |
| 320 | /* Free already-allocated entries */ |
| 321 | for (int i = 0; i < count; i++) { |
| 322 | free(entries[i].ext); |
| 323 | } |
| 324 | free(entries); |
| 325 | free(cfg); |
| 326 | return NULL; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * ── Step 3: Dedup — project entries win over global ── |
| 332 | * |
| 333 | * For any extension that appears in both global (indices 0..global_count) |
| 334 | * and project (indices global_count..count), remove the global entry by |
| 335 | * replacing it with the last global entry (order-insensitive dedup). |
| 336 | */ |
| 337 | for (int p = global_count; p < count; p++) { |
| 338 | for (int g = 0; g < global_count; g++) { |
| 339 | if (entries[g].ext && strcmp(entries[g].ext, entries[p].ext) == 0) { |
| 340 | /* Remove global entry: overwrite with last global entry */ |
| 341 | free(entries[g].ext); |
| 342 | entries[g] = entries[global_count - SKIP_ONE]; |
| 343 | entries[global_count - SKIP_ONE].ext = NULL; /* mark as consumed */ |
| 344 | global_count--; |
no test coverage detected