MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cbm_userconfig_load

Function cbm_userconfig_load

src/discover/userconfig.c:320–399  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Calls 3

cbm_app_config_dirFunction · 0.85
load_config_fileFunction · 0.85
userconfig_source_digestFunction · 0.85

Tested by

no test coverage detected