Skip to content

Commit 354befe

Browse files
DeusDataanivaryam
andcommitted
fix(git): correct canonical_root for worktrees and subdirectory projects
detect_changes returned an empty impacted_symbols set for projects indexed inside a git worktree or from a repo subdirectory (DeusData#659): canonical_root was computed wrong. `git rev-parse --git-common-dir` emits a path relative to the directory passed via -C (input_path), NOT to worktree_root, and the "/.git" suffix was stripped textually without resolving ".." components. So e.g. "../.git" joined against worktree_root, or "/ws/scripts/../.git" strip, left an unresolved ".." and never matched the project's stored root path. Fix (src/git/git_context.c): join the relative --git-common-dir against input_path (the -C dir) instead of worktree_root, and realpath()/_fullpath()- normalize the result BEFORE stripping "/.git" (the .git dir always exists for a valid repo, so resolution succeeds). This is consistent with the existing realpath'd root_path used for prefix matching (mcp.c). Reproduce-first: tests/test_git_context.c. The subdirectory case is the genuine guard — a repo indexed from a subdir yields a *relative* --git-common-dir, so the unfixed code leaves an un-normalized "<root>/subdir/.." (verified RED; GREEN after the realpath fix). The linked-worktree case is a supporting invariant, not the DeusData#659 reproducer: git that emits an *absolute* worktree --git-common-dir (e.g. 2.48.x) doesn't manifest the bug, so it passes either way; it still enforces the worktree->main-root invariant. All three git-based tests SKIP_PLATFORM on Windows (the CI shell there cannot init a repo via system()). Distilled from DeusData#672 (thanks @anivaryam); that PR's production fix is taken verbatim. Its repo_root/subdir tests ran git unconditionally and failed the Windows CI leg ("failed to init git repo"); here all three git-based tests are Windows-skipped so ci-ok is green cross-platform. Supersedes DeusData#696, which only realpath'd after the strip without changing the join base and so still yielded the workspace parent for the subdirectory case. Closes DeusData#659 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com> Co-Authored-By: anivaryam <anivaryam.dev@gmail.com>
1 parent f42c066 commit 354befe

4 files changed

Lines changed: 276 additions & 3 deletions

File tree

‎Makefile.cbm‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ TEST_DISCOVER_SRCS = \
340340
tests/test_language.c \
341341
tests/test_userconfig.c \
342342
tests/test_gitignore.c \
343+
tests/test_git_context.c \
343344
tests/test_discover.c
344345

345346
TEST_GRAPH_BUFFER_SRCS = tests/test_graph_buffer.c

‎src/git/git_context.c‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,51 @@ static char *join_root_relative(const char *root, const char *rel) {
126126
return out;
127127
}
128128

129-
static char *derive_canonical_root(const char *worktree_root, const char *git_common_dir) {
129+
static char *derive_canonical_root(const char *input_path, const char *worktree_root,
130+
const char *git_common_dir) {
130131
const char *src = git_common_dir && git_common_dir[0] ? git_common_dir : worktree_root;
131132
if (!src) {
132133
return git_strdup("");
133134
}
134135

135-
char *root = path_is_absolute(src) ? git_strdup(src) : join_root_relative(worktree_root, src);
136+
/* git rev-parse --git-common-dir outputs a path relative to the directory
137+
* passed via -C (input_path), NOT to worktree_root. Using worktree_root as
138+
* the base was wrong when input_path is a subdirectory or a linked worktree
139+
* sibling — e.g. "../.git" joined with worktree_root produced "workspace/.."
140+
* instead of the actual repo root. */
141+
char *root = path_is_absolute(src) ? git_strdup(src) : join_root_relative(input_path, src);
136142
if (!root) {
137143
return NULL;
138144
}
139145

146+
/* Resolve ".." components before stripping the "/.git" suffix.
147+
* Without this, "/workspace/scripts/../.git" would strip to
148+
* "/workspace/scripts/.." instead of "/workspace". The .git directory
149+
* must exist for a valid git repo, so realpath() / _fullpath() succeeds. */
150+
#ifndef _WIN32
151+
{
152+
char resolved[4096];
153+
if (realpath(root, resolved) != NULL) {
154+
free(root);
155+
root = git_strdup(resolved);
156+
if (!root) {
157+
return NULL;
158+
}
159+
}
160+
}
161+
#else
162+
{
163+
char resolved[4096];
164+
if (_fullpath(resolved, root, sizeof(resolved)) != NULL) {
165+
free(root);
166+
root = git_strdup(resolved);
167+
if (!root) {
168+
return NULL;
169+
}
170+
}
171+
}
172+
#endif
173+
140174
size_t len = strlen(root);
141175
while (len > 1 && (root[len - 1] == '/' || root[len - 1] == '\\')) {
142176
root[--len] = '\0';
@@ -251,7 +285,7 @@ int cbm_git_context_resolve(const char *path, cbm_git_context_t *out) {
251285

252286
out->is_worktree =
253287
out->git_dir && out->git_common_dir && strcmp(out->git_dir, out->git_common_dir) != 0;
254-
out->canonical_root = derive_canonical_root(out->worktree_root, out->git_common_dir);
288+
out->canonical_root = derive_canonical_root(path, out->worktree_root, out->git_common_dir);
255289
out->branch_slug = slug_from_branch(out->branch, out->is_detached);
256290
if (git_capture(path, "merge-base HEAD @{upstream}", &out->base_sha) != 0) {
257291
out->base_sha = git_strdup("");

‎tests/test_git_context.c‎

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
/*
2+
* test_git_context.c — Tests for cbm_git_context_resolve(), focusing on
3+
* the canonical_root derivation for git worktrees and subdirectory projects.
4+
*
5+
* Issue #659: canonical_root was computed incorrectly for linked worktrees
6+
* and projects indexed from a subdirectory of the repository root.
7+
* git rev-parse --git-common-dir outputs a path relative to the -C directory
8+
* (input_path), not to worktree_root. Joining it with worktree_root and then
9+
* string-stripping "/.git" left unresolved ".." components in the result.
10+
*
11+
* These tests shell out to `git`, so they SKIP_PLATFORM on Windows (the CI
12+
* shell there cannot init a repo via system()).
13+
*
14+
* Reproduce-first guard: canonical_root_subdir is the genuine RED-without-the-fix
15+
* guard — a repo indexed from a subdirectory yields a relative --git-common-dir
16+
* ("../.git"), so the unfixed code returns an un-normalized "<root>/subdir/.."
17+
* (verified FAIL on the unfixed derive_canonical_root; GREEN with the realpath
18+
* normalization). canonical_root_linked_worktree is a SUPPORTING INVARIANT, not
19+
* the #659 reproducer: on git that emits an *absolute* --git-common-dir for a
20+
* linked worktree (e.g. 2.48.x) the bug does not manifest there, so that test
21+
* passes with or without the fix. It still enforces the worktree->main-root
22+
* invariant and would catch the bug on git builds that emit a relative
23+
* worktree common-dir. canonical_root_repo_root is a baseline (no `..` to
24+
* normalize), not a guard.
25+
*/
26+
#include "test_framework.h"
27+
#include "test_helpers.h"
28+
#include "git/git_context.h"
29+
30+
#include <stdio.h>
31+
#include <string.h>
32+
33+
#ifndef _WIN32
34+
#include <limits.h>
35+
#endif
36+
37+
/* Run a git command inside dir, return 0 on success. */
38+
static int git_run(const char *dir, const char *args) {
39+
char cmd[1024];
40+
snprintf(cmd, sizeof(cmd), "git -C \"%s\" %s >/dev/null 2>&1", dir, args);
41+
return system(cmd);
42+
}
43+
44+
/* Create a minimal git repo at dir (init + empty commit so HEAD exists). */
45+
static int make_git_repo(const char *dir) {
46+
if (th_mkdir_p(dir) != 0) return -1;
47+
if (git_run(dir, "init -q") != 0) return -1;
48+
if (git_run(dir, "config user.email test@example.com") != 0) return -1;
49+
if (git_run(dir, "config user.name Test") != 0) return -1;
50+
/* Create a file so HEAD points to a real commit. */
51+
char path[1024];
52+
snprintf(path, sizeof(path), "%s/.keep", dir);
53+
th_write_file(path, "");
54+
if (git_run(dir, "add .keep") != 0) return -1;
55+
if (git_run(dir, "commit -q -m init") != 0) return -1;
56+
return 0;
57+
}
58+
59+
/* ── canonical_root: normal repo indexed from its root ──────────── */
60+
61+
TEST(canonical_root_repo_root) {
62+
#ifdef _WIN32
63+
SKIP_PLATFORM("git-based canonical_root test not supported on Windows CI");
64+
#else
65+
char *tmp = th_mktempdir("cbm_gitctx");
66+
if (!tmp) FAIL("th_mktempdir returned NULL");
67+
68+
if (make_git_repo(tmp) != 0) {
69+
th_rmtree(tmp);
70+
SKIP_PLATFORM("git not available to init a repo");
71+
}
72+
73+
cbm_git_context_t ctx = {0};
74+
int rc = cbm_git_context_resolve(tmp, &ctx);
75+
if (rc != 0 || !ctx.is_git) {
76+
cbm_git_context_free(&ctx);
77+
th_rmtree(tmp);
78+
FAIL("cbm_git_context_resolve failed or not a git repo");
79+
}
80+
81+
char expected[4096];
82+
if (realpath(tmp, expected) == NULL) {
83+
cbm_git_context_free(&ctx);
84+
th_rmtree(tmp);
85+
FAIL("realpath(tmp) failed");
86+
}
87+
88+
ASSERT_STR_EQ(ctx.canonical_root, expected);
89+
90+
cbm_git_context_free(&ctx);
91+
th_rmtree(tmp);
92+
PASS();
93+
#endif /* _WIN32 */
94+
}
95+
96+
/* ── canonical_root: indexed from a subdirectory (issue #659) ─────
97+
* THE reproduce-first guard: from a subdir, --git-common-dir is relative, so the
98+
* unfixed derive_canonical_root joins it against worktree_root and strips "/.git"
99+
* textually, leaving canonical_root = "<root>/subdir/.." (or "<root>/..") instead
100+
* of "<root>". Verified RED on the unfixed code, GREEN with the realpath fix. */
101+
102+
TEST(canonical_root_subdir) {
103+
#ifdef _WIN32
104+
SKIP_PLATFORM("git-based canonical_root test not supported on Windows CI");
105+
#else
106+
char *tmp = th_mktempdir("cbm_gitctx");
107+
if (!tmp) FAIL("th_mktempdir returned NULL");
108+
109+
if (make_git_repo(tmp) != 0) {
110+
th_rmtree(tmp);
111+
SKIP_PLATFORM("git not available to init a repo");
112+
}
113+
114+
/* Create a subdirectory inside the repo. */
115+
char subdir[1024];
116+
snprintf(subdir, sizeof(subdir), "%s/scripts", tmp);
117+
if (th_mkdir_p(subdir) != 0) {
118+
th_rmtree(tmp);
119+
FAIL("failed to create subdir");
120+
}
121+
122+
cbm_git_context_t ctx = {0};
123+
int rc = cbm_git_context_resolve(subdir, &ctx);
124+
if (rc != 0 || !ctx.is_git) {
125+
cbm_git_context_free(&ctx);
126+
th_rmtree(tmp);
127+
FAIL("cbm_git_context_resolve on subdir failed or not a git repo");
128+
}
129+
130+
/* canonical_root must equal the repo root, NOT "<repo>/.." or "<subdir>/..". */
131+
char expected[4096];
132+
if (realpath(tmp, expected) == NULL) {
133+
cbm_git_context_free(&ctx);
134+
th_rmtree(tmp);
135+
FAIL("realpath(tmp) failed");
136+
}
137+
138+
ASSERT_STR_EQ(ctx.canonical_root, expected);
139+
140+
/* Sanity: canonical_root must not contain ".." or end with a slash. */
141+
ASSERT(strstr(ctx.canonical_root, "..") == NULL);
142+
ASSERT(ctx.canonical_root[strlen(ctx.canonical_root) - 1] != '/');
143+
144+
cbm_git_context_free(&ctx);
145+
th_rmtree(tmp);
146+
PASS();
147+
#endif /* _WIN32 */
148+
}
149+
150+
/* ── canonical_root: linked git worktree (supporting invariant) ────
151+
* NOT the #659 reproducer on modern git: git that emits an *absolute*
152+
* --git-common-dir for a linked worktree (e.g. 2.48.x) takes the path_is_absolute
153+
* branch, so the bug does not manifest and this passes with or without the fix.
154+
* It is kept as an invariant — canonical_root of a linked worktree must equal the
155+
* MAIN repo root (never the worktree root or its parent) — and would fail on a git
156+
* build that emits a *relative* worktree common-dir. The genuine RED-without-fix
157+
* guard for #659 is canonical_root_subdir above. */
158+
159+
TEST(canonical_root_linked_worktree) {
160+
#ifdef _WIN32
161+
SKIP_PLATFORM("git worktree test not implemented for Windows");
162+
#else
163+
/* th_mktempdir() returns a static buffer — copy before the second call. */
164+
char main_tmp[256];
165+
char *raw = th_mktempdir("cbm_main");
166+
if (!raw) FAIL("th_mktempdir returned NULL");
167+
strncpy(main_tmp, raw, sizeof(main_tmp) - 1);
168+
main_tmp[sizeof(main_tmp) - 1] = '\0';
169+
170+
char wt_tmp[256];
171+
raw = th_mktempdir("cbm_worktree");
172+
if (!raw) FAIL("th_mktempdir returned NULL");
173+
strncpy(wt_tmp, raw, sizeof(wt_tmp) - 1);
174+
wt_tmp[sizeof(wt_tmp) - 1] = '\0';
175+
176+
/* Remove the worktree dir first — git worktree add creates it. */
177+
th_rmtree(wt_tmp);
178+
179+
if (make_git_repo(main_tmp) != 0) {
180+
th_rmtree(main_tmp);
181+
SKIP_PLATFORM("git not available to init a repo");
182+
}
183+
184+
/* Create a branch for the worktree. */
185+
if (git_run(main_tmp, "branch wt-branch") != 0) {
186+
th_rmtree(main_tmp);
187+
FAIL("failed to create branch for worktree");
188+
}
189+
190+
/* Add a linked worktree. */
191+
char wt_cmd[1024];
192+
snprintf(wt_cmd, sizeof(wt_cmd), "worktree add \"%s\" wt-branch", wt_tmp);
193+
if (git_run(main_tmp, wt_cmd) != 0) {
194+
th_rmtree(wt_tmp);
195+
th_rmtree(main_tmp);
196+
SKIP_PLATFORM("git worktree add unavailable (git 2.5+ required)");
197+
}
198+
199+
cbm_git_context_t ctx = {0};
200+
int rc = cbm_git_context_resolve(wt_tmp, &ctx);
201+
if (rc != 0 || !ctx.is_git) {
202+
cbm_git_context_free(&ctx);
203+
git_run(main_tmp, "worktree prune");
204+
th_rmtree(main_tmp);
205+
th_rmtree(wt_tmp);
206+
FAIL("cbm_git_context_resolve on linked worktree failed");
207+
}
208+
209+
/* canonical_root must be the MAIN repo root, not the worktree root or its parent. */
210+
char expected[4096];
211+
if (realpath(main_tmp, expected) == NULL) {
212+
cbm_git_context_free(&ctx);
213+
git_run(main_tmp, "worktree prune");
214+
th_rmtree(main_tmp);
215+
th_rmtree(wt_tmp);
216+
FAIL("realpath(main_tmp) failed");
217+
}
218+
219+
ASSERT_STR_EQ(ctx.canonical_root, expected);
220+
ASSERT(strstr(ctx.canonical_root, "..") == NULL);
221+
222+
cbm_git_context_free(&ctx);
223+
git_run(main_tmp, "worktree prune");
224+
th_rmtree(main_tmp);
225+
th_rmtree(wt_tmp);
226+
PASS();
227+
#endif /* _WIN32 */
228+
}
229+
230+
/* ── Suite ──────────────────────────────────────────────────────── */
231+
232+
SUITE(git_context) {
233+
RUN_TEST(canonical_root_repo_root);
234+
RUN_TEST(canonical_root_subdir);
235+
RUN_TEST(canonical_root_linked_worktree);
236+
}

‎tests/test_main.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ extern void suite_mcp(void);
5858
extern void suite_language(void);
5959
extern void suite_userconfig(void);
6060
extern void suite_gitignore(void);
61+
extern void suite_git_context(void);
6162
extern void suite_discover(void);
6263
extern void suite_graph_buffer(void);
6364
extern void suite_registry(void);
@@ -173,6 +174,7 @@ int main(int argc, char **argv) {
173174
RUN_SELECTED_SUITE(language);
174175
RUN_SELECTED_SUITE(userconfig);
175176
RUN_SELECTED_SUITE(gitignore);
177+
RUN_SELECTED_SUITE(git_context);
176178
RUN_SELECTED_SUITE(discover);
177179

178180
/* Graph Buffer (M7) */

0 commit comments

Comments
 (0)