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.

fix(git): identify a worktree by its canonical gitdir

Nohj72f1a52dc6bb
diff --git a/src/cli/hook_augment.c b/src/cli/hook_augment.c
index d3baace47..10e7741a1 100644
--- a/src/cli/hook_augment.c
+++ b/src/cli/hook_augment.c
@@ -1002,10 +1002,6 @@ static char *ha_resolve_indexed_project_with_root(cbm_mcp_server_t *srv, const c
return ha_registry_project_for_path(srv, cwd, root_out, root_out_size);
}
-static char *ha_resolve_indexed_project(cbm_mcp_server_t *srv, const char *cwd) {
- return ha_resolve_indexed_project_with_root(srv, cwd, NULL, 0U);
-}
-
static const char *ha_hook_event_name(yyjson_val *root) {
const char *event = ha_obj_str(root, "hook_event_name");
return event ? event : ha_obj_str(root, "hookEventName");
diff --git a/src/git/git_context.c b/src/git/git_context.c
index 8e2629cc4..a63d3385e 100644
--- a/src/git/git_context.c
+++ b/src/git/git_context.c
@@ -228,7 +228,9 @@ static bool walk_linked_worktree_root(const char *path, char *root_out, size_t r
/* 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++) {
+ /* Bound is the path itself: each step drops one component and stops at
+ * the filesystem root. A fixed depth would miss a deep session cwd. */
+ for (;;) {
int kind = linked_worktree_anchor(current);
if (kind >= 0) {
if (kind != 1) {
@@ -248,7 +250,6 @@ static bool walk_linked_worktree_root(const char *path, char *root_out, size_t r
return false;
}
}
- return false;
}
bool cbm_git_is_linked_worktree(const char *path) {
@@ -271,7 +272,14 @@ bool cbm_git_same_linked_worktree(const char *a, const char *b) {
!read_gitlink_target(root_b, git_b, sizeof(git_b))) {
return false;
}
- return strcmp(git_a, git_b) == 0;
+ /* A relative gitdir is joined onto the walked path. Canonicalize both so
+ * a symlink spelling of the cwd still matches the real project root.
+ * Fall back to the raw string when the gitdir does not exist. */
+ char norm_a[GIT_OUTPUT_MAX];
+ char norm_b[GIT_OUTPUT_MAX];
+ const char *left = cbm_canonical_path(git_a, norm_a, sizeof(norm_a)) ? norm_a : git_a;
+ const char *right = cbm_canonical_path(git_b, norm_b, sizeof(norm_b)) ? norm_b : git_b;
+ return strcmp(left, right) == 0;
}
static char *join_root_relative(const char *root, const char *rel) {