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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(git): detect linked worktrees from nested directories
Nohj007100739656
diff --git a/src/git/git_context.c b/src/git/git_context.cindex 3a3da7acb..3fbf5b65d 100644--- a/src/git/git_context.c+++ b/src/git/git_context.c@@ -148,22 +148,97 @@ static bool read_gitlink_target(const char *path, char *out, size_t out_size) {return got;}-bool cbm_git_is_linked_worktree(const char *path) {- if (!path || !path[0]) {- return false;+/* 1 = linked-worktree gitlink, 0 = a different git anchor (stop walking),+ * -1 = no .git at this directory (keep walking). */+static int linked_worktree_anchor(const char *path) {+ char dot_git[GIT_OUTPUT_MAX];+ int n = snprintf(dot_git, sizeof(dot_git), "%s/.git", path);+ if (n < 0 || n >= (int)sizeof(dot_git)) {+ return 0;+ }+ cbm_path_info_t info;+ if (cbm_path_info_utf8(dot_git, &info) != CBM_PATH_INFO_OK) {+ return -1;+ }+ /* A directory .git is a main checkout. Anything else that is not a+ * regular gitlink file (a symlink, for example) is also an anchor:+ * do not follow it, and do not blame a parent worktree for this tree. */+ if (!info.is_regular) {+ return 0;}char git_dir[GIT_OUTPUT_MAX];if (!read_gitlink_target(path, git_dir, sizeof(git_dir))) {- return false;+ return 0;}/* Only linked worktrees carry <gitdir>/commondir; a submodule gitlink* points at <super>/.git/modules/<name>, which does not. */char commondir[GIT_OUTPUT_MAX];- int n = snprintf(commondir, sizeof(commondir), "%s/commondir", git_dir);+ n = snprintf(commondir, sizeof(commondir), "%s/commondir", git_dir);if (n < 0 || n >= (int)sizeof(commondir)) {+ return 0;+ }+ return path_is_regular_file(commondir) ? 1 : 0;+}++static bool parent_directory(char *path) {+ size_t n = strlen(path);+ while (n > 1 && (path[n - 1] == '/' || path[n - 1] == '\\')) {+ path[--n] = '\0';+ }+#ifdef _WIN32+ if ((n == 2 && path[1] == ':') ||+ (n == 3 && path[1] == ':' && (path[2] == '/' || path[2] == '\\'))) {return false;}- return path_is_regular_file(commondir);+#endif+ if (n <= 1) {+ return false;+ }+ char *slash = NULL;+ for (size_t i = 0; i < n; i++) {+ if (path[i] == '/' || path[i] == '\\') {+ slash = path + i;+ }+ }+ if (!slash) {+ return false;+ }+ if (slash == path) {+ path[1] = '\0';+ return true;+ }+ *slash = '\0';+ return true;+}++bool cbm_git_is_linked_worktree(const char *path) {+ if (!path || !path[0]) {+ return false;+ }+ char current[GIT_OUTPUT_MAX];+ int n = snprintf(current, sizeof(current), "%s", path);+ if (n < 0 || n >= (int)sizeof(current)) {+ return false;+ }+ size_t len = strlen(current);+ while (len > 1 && (current[len - 1] == '/' || current[len - 1] == '\\')) {+ current[--len] = '\0';+ }+ /* A session cwd or index_repository path is often a subdirectory. Walk+ * ancestors until a git anchor so ignore_worktrees applies to the whole+ * linked checkout, not only its root. */+ for (int depth = 0; depth < 64; depth++) {+ int kind = linked_worktree_anchor(current);+ if (kind >= 0) {+ return kind == 1;+ }+ char previous[GIT_OUTPUT_MAX];+ snprintf(previous, sizeof(previous), "%s", current);+ if (!parent_directory(current) || strcmp(previous, current) == 0) {+ return false;+ }+ }+ return false;}static char *join_root_relative(const char *root, const char *rel) {diff --git a/src/git/git_context.h b/src/git/git_context.hindex 633e28018..30f2f545a 100644--- a/src/git/git_context.h+++ b/src/git/git_context.h@@ -19,13 +19,14 @@ typedef struct {char *base_sha;} cbm_git_context_t;-/* True when path is the root of a LINKED git worktree (`git worktree add`).+/* True when path is a LINKED git worktree (`git worktree add`) or a+ * subdirectory of one.*- * Plumbing-only, no subprocess: <path>/.git must be a regular file holding a- * "gitdir: <path>" pointer AND that gitdir must contain a `commondir` file.- * The commondir check is what separates a linked worktree from a submodule —- * a submodule's .git is also a gitlink file, but its gitdir- * (<super>/.git/modules/<name>) has no commondir entry.+ * Plumbing-only, no subprocess. Walk ancestors until a git anchor. A linked+ * worktree's <root>/.git is a regular file holding a "gitdir: <path>" pointer+ * AND that gitdir contains a `commondir` file. The walk stops at any other+ * anchor: a main checkout (`.git` is a directory) or a submodule (gitlink+ * file whose gitdir, <super>/.git/modules/<name>, has no commondir).** Callers run this on every session start, so it stays fork-free; the richer* cbm_git_context_resolve() shells out to git and is not usable on that path. */diff --git a/tests/test_git_context.c b/tests/test_git_context.cindex 894cb68aa..5b3c4b779 100644--- a/tests/test_git_context.c+++ b/tests/test_git_context.c@@ -296,16 +296,30 @@ TEST(is_linked_worktree_true_for_linked_worktree) {}bool worktree_detected = cbm_git_is_linked_worktree(wt_tmp);+ /* A subdirectory has no .git of its own. The predicate must still see the+ * linked worktree above it, or ignore_worktrees misses nested sessions. */+ char nested[1024];+ snprintf(nested, sizeof(nested), "%s/nested/deeper", wt_tmp);+ bool nested_ok = th_mkdir_p(nested) == 0;+ bool nested_detected = nested_ok && cbm_git_is_linked_worktree(nested);/* 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);+ char main_nested[1024];+ snprintf(main_nested, sizeof(main_nested), "%s/src", main_tmp);+ bool main_nested_ok = th_mkdir_p(main_nested) == 0;+ bool main_nested_detected = main_nested_ok && cbm_git_is_linked_worktree(main_nested);git_run(main_tmp, "worktree prune");th_rmtree(main_tmp);th_rmtree(wt_tmp);ASSERT(worktree_detected);+ ASSERT(nested_ok);+ ASSERT(nested_detected);ASSERT(!main_detected);+ ASSERT(main_nested_ok);+ ASSERT(!main_nested_detected);PASS();#endif /* _WIN32 */}@@ -351,6 +365,10 @@ TEST(is_linked_worktree_false_for_submodule_and_nongit) {char subm[1024];snprintf(subm, sizeof(subm), "%s/subm", super_tmp);bool submodule_detected = sub_rc == 0 && cbm_git_is_linked_worktree(subm);+ char sub_nested[1024];+ snprintf(sub_nested, sizeof(sub_nested), "%s/nested", subm);+ bool sub_nested_made = sub_rc == 0 && th_mkdir_p(sub_nested) == 0;+ bool sub_nested_detected = sub_nested_made && cbm_git_is_linked_worktree(sub_nested);th_rmtree(super_tmp);th_rmtree(child_tmp);@@ -359,8 +377,11 @@ TEST(is_linked_worktree_false_for_submodule_and_nongit) {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. */+ /* A submodule gitlink has no commondir → must not be treated as a worktree,+ * including a subdirectory, which must not walk into the superproject. */ASSERT(!submodule_detected);+ ASSERT(sub_nested_made);+ ASSERT(!sub_nested_detected);PASS();#endif /* _WIN32 */}