Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

Merge upstream main into fm/cbm-worktree-ignore-1

Nohj3f6969a6c5c4
diff --cc src/mcp/mcp.c
index f27ddb9a8,2ed29799b..cccff1838
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@@ -479,9 -537,13 +537,16 @@@ static const tool_def_t TOOLS[] =
"\"Name override; Non-ASCII bytes are encoded; unsafe characters normalized.\"},"
"\"persistence\":{\"type\":\"boolean\",\"default\":false,\"description\":"
"\"Write .codebase-memory/graph.db.zst.\"},"
+ "\"index_worktree\":{\"type\":\"boolean\",\"default\":false,\"description\":"
+ "\"Index repo_path even when it is a linked git worktree and the "
- "ignore_worktrees config key is enabled. No effect otherwise.\"}"
++ "ignore_worktrees config key is enabled. No effect otherwise.\"},"
+ "\"async\":{\"type\":\"boolean\",\"default\":false,\"description\":"
+ "\"Start the index in the background and return immediately; it keeps running if this "
+ "call is cancelled or times out. Not with status or cross-repo-intelligence. Refused for a "
+ "one-shot cli call that is the only client of a temporary daemon (run daemon start).\"},"
+ "\"status\":{\"type\":\"boolean\",\"default\":false,\"description\":"
+ "\"Report the running or last index job (queued/running/succeeded/failed/cancelled) "
+ "instead of indexing. Pass the same repo_path (and name, if any) as the index call.\"}"
"},\"required\":[\"repo_path\"]}"},
{"search_graph",
@@@ -11148,21 -11397,13 +11400,27 @@@ static char *handle_index_repository(cb
return cbm_mcp_text_result(boundary_err, true);
}
+ /* ignore_worktrees: an EXPLICIT index_repository call on a linked worktree
+ * is refused with the two ways forward (per-call override, or turn the
+ * setting off) rather than silently skipped — a silent success would be
+ * indistinguishable from a real index to the caller. */
+ if (cbm_mcp_ignore_worktrees_enabled(srv) && !cbm_mcp_get_bool_arg(args, "index_worktree") &&
+ cbm_git_is_linked_worktree(repo_path)) {
+ index_args_free(repo_path, mode_str, name_override);
+ return cbm_mcp_text_result(
+ "repo_path is a linked git worktree and ignore_worktrees is enabled. Pass "
+ "index_worktree=true to index it anyway, or run: codebase-memory-mcp config set "
+ "ignore_worktrees false",
+ true);
+ }
+
if (mode_str && strcmp(mode_str, "cross-repo-intelligence") == 0) {
+ if (async_mode) {
+ index_args_free(repo_path, mode_str, name_override);
+ return cbm_mcp_text_result(
+ "async is not supported for mode cross-repo-intelligence (it runs no index job)",
+ true);
+ }
char *result = handle_cross_repo_mode(srv, repo_path, name_override, args);
index_args_free(repo_path, mode_str, name_override);
return result;
diff --cc tests/test_git_context.c
index 08df0dd7b,20bea722f..c721e12cb
--- a/tests/test_git_context.c
+++ b/tests/test_git_context.c
@@@ -232,118 -346,100 +346,212 @@@ TEST(canonical_root_linked_worktree)
#endif /* _WIN32 */
}
+/* ── cbm_git_is_linked_worktree ─────────────────────────────────── */
+/* Detection backing the `ignore_worktrees` config gate. The predicate must be
+ * TRUE only for a linked worktree (`git worktree add`) — never for the main
+ * checkout, a plain directory, or a submodule. The submodule case is the one a
+ * naive "is .git a regular file?" check gets wrong: a submodule's .git is also a
+ * gitlink file, and it is separated here by the absence of a `commondir` entry
+ * in the pointed-at gitdir. */
+
+TEST(is_linked_worktree_true_for_linked_worktree) {
+#ifdef _WIN32
+ SKIP_PLATFORM("git worktree test not implemented for Windows");
+#else
+ /* th_mktempdir() returns a static buffer — copy before the second call. */
+ char main_tmp[256];
+ char *raw = th_mktempdir("cbm_wt_main");
+ if (!raw) FAIL("th_mktempdir returned NULL");
+ strncpy(main_tmp, raw, sizeof(main_tmp) - 1);
+ main_tmp[sizeof(main_tmp) - 1] = '\0';
+
+ char wt_tmp[256];
+ raw = th_mktempdir("cbm_wt_linked");
+ if (!raw) FAIL("th_mktempdir returned NULL");
+ strncpy(wt_tmp, raw, sizeof(wt_tmp) - 1);
+ wt_tmp[sizeof(wt_tmp) - 1] = '\0';
+ th_rmtree(wt_tmp); /* git worktree add creates it */
+
+ if (make_git_repo(main_tmp) != 0) {
+ th_rmtree(main_tmp);
+ SKIP_PLATFORM("git not available to init a repo");
+ }
+ if (git_run(main_tmp, "branch wt-branch") != 0) {
+ th_rmtree(main_tmp);
+ FAIL("failed to create branch for worktree");
+ }
+ char wt_cmd[1024];
+ snprintf(wt_cmd, sizeof(wt_cmd), "worktree add \"%s\" wt-branch", wt_tmp);
+ if (git_run(main_tmp, wt_cmd) != 0) {
+ th_rmtree(wt_tmp);
+ th_rmtree(main_tmp);
+ SKIP_PLATFORM("git worktree add unavailable (git 2.5+ required)");
+ }
+
+ bool worktree_detected = cbm_git_is_linked_worktree(wt_tmp);
+ /* The MAIN checkout of the very same repo must NOT be flagged — otherwise
+ * enabling ignore_worktrees would stop indexing ordinary repositories. */
+ bool main_detected = cbm_git_is_linked_worktree(main_tmp);
+
+ git_run(main_tmp, "worktree prune");
+ th_rmtree(main_tmp);
+ th_rmtree(wt_tmp);
+
+ ASSERT(worktree_detected);
+ ASSERT(!main_detected);
+ PASS();
+#endif /* _WIN32 */
+}
+
+ TEST(git_context_ignores_inherited_repo_env) {
+ #ifdef _WIN32
+ SKIP_PLATFORM("git-based env-isolation test not supported on Windows CI");
+ #else
+ char root[256];
+ char *raw = th_mktempdir("cbm_gitenv");
+ if (!raw)
+ FAIL("th_mktempdir returned NULL");
+ snprintf(root, sizeof(root), "%s", raw);
+ if (make_env_isolation_tree(root) != 0) {
+ th_rmtree(root);
+ SKIP_PLATFORM("git not available to init a repo");
+ }
+ char decoy[512], fixture[512], plain[512];
+ snprintf(decoy, sizeof(decoy), "%s/decoy", root);
+ snprintf(fixture, sizeof(fixture), "%s/fixture", root);
+ snprintf(plain, sizeof(plain), "%s/plain", root);
+
+ char decoy_head[128], decoy_branch[128], fixture_head[128];
+ git_read_line(decoy, "rev-parse HEAD", decoy_head, sizeof(decoy_head));
+ git_read_line(decoy, "rev-parse --abbrev-ref HEAD", decoy_branch, sizeof(decoy_branch));
+ git_read_line(fixture, "rev-parse HEAD", fixture_head, sizeof(fixture_head));
+
+ git_env_snapshot_t snap;
+ git_env_hook_enter(&snap, decoy);
+ cbm_git_context_t plain_ctx = {0};
+ int plain_rc = cbm_git_context_resolve(plain, &plain_ctx);
+ cbm_git_context_t fix_ctx = {0};
+ int fix_rc = cbm_git_context_resolve(fixture, &fix_ctx);
+ git_env_hook_leave(&snap);
+
+ bool plain_is_git = plain_ctx.is_git;
+ char fix_head[128], fix_branch[128];
+ snprintf(fix_head, sizeof(fix_head), "%s", fix_ctx.head_sha ? fix_ctx.head_sha : "");
+ snprintf(fix_branch, sizeof(fix_branch), "%s", fix_ctx.branch ? fix_ctx.branch : "");
+ bool fix_is_git = fix_ctx.is_git;
+ cbm_git_context_free(&plain_ctx);
+ cbm_git_context_free(&fix_ctx);
+
+ char decoy_head_after[128], decoy_branch_after[128];
+ git_read_line(decoy, "rev-parse HEAD", decoy_head_after, sizeof(decoy_head_after));
+ git_read_line(decoy, "rev-parse --abbrev-ref HEAD", decoy_branch_after,
+ sizeof(decoy_branch_after));
+ th_rmtree(root);
+
+ ASSERT_EQ(plain_rc, 0);
+ ASSERT_FALSE(plain_is_git); /* a plain dir is not the decoy's repo */
+ ASSERT_EQ(fix_rc, 0);
+ ASSERT_TRUE(fix_is_git);
+ ASSERT_STR_EQ(fix_head, fixture_head); /* the fixture's HEAD, not the decoy's */
+ ASSERT_STR_EQ(fix_branch, "fixture-branch");
+ ASSERT_STR_EQ(decoy_head_after, decoy_head); /* the decoy is untouched */
+ ASSERT_STR_EQ(decoy_branch_after, decoy_branch);
+ PASS();
+ #endif /* _WIN32 */
+ }
+
+TEST(is_linked_worktree_false_for_submodule_and_nongit) {
+#ifdef _WIN32
+ SKIP_PLATFORM("git worktree test not implemented for Windows");
+#else
+ char super_tmp[256];
+ char *raw = th_mktempdir("cbm_wt_super");
+ if (!raw) FAIL("th_mktempdir returned NULL");
+ strncpy(super_tmp, raw, sizeof(super_tmp) - 1);
+ super_tmp[sizeof(super_tmp) - 1] = '\0';
+
+ char child_tmp[256];
+ raw = th_mktempdir("cbm_wt_child");
+ if (!raw) FAIL("th_mktempdir returned NULL");
+ strncpy(child_tmp, raw, sizeof(child_tmp) - 1);
+ child_tmp[sizeof(child_tmp) - 1] = '\0';
+
+ if (make_git_repo(super_tmp) != 0 || make_git_repo(child_tmp) != 0) {
+ th_rmtree(super_tmp);
+ th_rmtree(child_tmp);
+ SKIP_PLATFORM("git not available to init a repo");
+ }
+
+ /* A plain directory that is not a git repo at all. */
+ char plain[1024];
+ snprintf(plain, sizeof(plain), "%s/plain", super_tmp);
+ if (th_mkdir_p(plain) != 0) {
+ th_rmtree(super_tmp);
+ th_rmtree(child_tmp);
+ FAIL("failed to create plain dir");
+ }
+ bool plain_detected = cbm_git_is_linked_worktree(plain);
+
+ /* file:// submodules are refused by default since the CVE-2022-39253 fix. */
+ char sub_cmd[1024];
+ snprintf(sub_cmd, sizeof(sub_cmd),
+ "-c protocol.file.allow=always submodule add -q \"%s\" subm", child_tmp);
+ int sub_rc = git_run(super_tmp, sub_cmd);
+
+ char subm[1024];
+ snprintf(subm, sizeof(subm), "%s/subm", super_tmp);
+ bool submodule_detected = sub_rc == 0 && cbm_git_is_linked_worktree(subm);
+
+ th_rmtree(super_tmp);
+ th_rmtree(child_tmp);
+
+ ASSERT(!plain_detected);
+ if (sub_rc != 0) {
+ SKIP_PLATFORM("git submodule add unavailable in this environment");
+ }
+ /* A submodule gitlink has no commondir → must not be treated as a worktree. */
+ ASSERT(!submodule_detected);
+ PASS();
+#endif /* _WIN32 */
+}
+
+ TEST(githistory_ignores_inherited_repo_env) {
+ #ifdef _WIN32
+ SKIP_PLATFORM("git-based env-isolation test not supported on Windows CI");
+ #else
+ char root[256];
+ char *raw = th_mktempdir("cbm_gitenv_hist");
+ if (!raw)
+ FAIL("th_mktempdir returned NULL");
+ snprintf(root, sizeof(root), "%s", raw);
+ if (make_env_isolation_tree(root) != 0) {
+ th_rmtree(root);
+ SKIP_PLATFORM("git not available to init a repo");
+ }
+ char decoy[512], plain[512];
+ snprintf(decoy, sizeof(decoy), "%s/decoy", root);
+ snprintf(plain, sizeof(plain), "%s/plain", root);
+
+ git_env_snapshot_t snap;
+ git_env_hook_enter(&snap, decoy);
+ cbm_githistory_result_t result = {0};
+ (void)cbm_pipeline_githistory_compute(plain, &result);
+ git_env_hook_leave(&snap);
+
+ int commits = result.commit_count;
+ int couplings = result.count;
+ free(result.couplings);
+ free(result.file_temporal);
+ th_rmtree(root);
+
+ /* A non-git directory has no history: the decoy's 3 coupled commits
+ * must not be attributed to it. */
+ ASSERT_EQ(commits, 0);
+ ASSERT_EQ(couplings, 0);
+ PASS();
+ #endif /* _WIN32 */
+ }
+
/* ── Suite ──────────────────────────────────────────────────────── */
SUITE(git_context) {
@@@ -350,6 -447,6 +559,8 @@@
RUN_TEST(canonical_root_repo_root);
RUN_TEST(canonical_root_subdir);
RUN_TEST(canonical_root_linked_worktree);
+ RUN_TEST(is_linked_worktree_true_for_linked_worktree);
+ RUN_TEST(is_linked_worktree_false_for_submodule_and_nongit);
+ RUN_TEST(git_context_ignores_inherited_repo_env);
+ RUN_TEST(githistory_ignores_inherited_repo_env);
}