Skip to content

Commit b9b7905

Browse files
authored
Merge pull request DeusData#749 from DeusData/distill/672-git-canonical-root
fix(git): correct canonical_root for worktrees and subdirectory projects
2 parents 01c5921 + 3f3ae85 commit b9b7905

4 files changed

Lines changed: 302 additions & 10 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: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,57 @@ 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) {
130-
const char *src = git_common_dir && git_common_dir[0] ? git_common_dir : worktree_root;
131-
if (!src) {
132-
return git_strdup("");
133-
}
134-
135-
char *root = path_is_absolute(src) ? git_strdup(src) : join_root_relative(worktree_root, src);
136-
if (!root) {
137-
return NULL;
129+
/* Derive the canonical repo root.
130+
*
131+
* Preferred (git 2.31+): abs_common_dir is `git rev-parse --path-format=absolute
132+
* --git-common-dir` — git's OWN absolute, canonical common-dir. Because a main
133+
* repo and its linked worktree both ask the same git binary, they resolve to the
134+
* IDENTICAL path, so canonical_root is consistent across worktrees AND platforms
135+
* with no manual join or realpath/_fullpath — which diverged under msys vs native
136+
* path representations (#659 root cause, and the worktree==main-root breakage in
137+
* test_pipeline.c git_context_linked_worktree). git also resolves the relative
138+
* ".." internally, so the subdirectory case (#659) is fixed here too.
139+
*
140+
* Fallback (git < 2.31, no --path-format → abs_common_dir empty): the relative
141+
* --git-common-dir is relative to input_path (the -C dir), so join against it and
142+
* realpath-normalize the "..". Unix only — on Windows git emits an absolute
143+
* common-dir so this branch isn't reached in practice, and _fullpath there
144+
* reintroduces the msys divergence. */
145+
static char *derive_canonical_root(const char *input_path, const char *worktree_root,
146+
const char *git_common_dir, const char *abs_common_dir) {
147+
char *root = NULL;
148+
if (abs_common_dir && abs_common_dir[0] && path_is_absolute(abs_common_dir)) {
149+
root = git_strdup(abs_common_dir);
150+
if (!root) {
151+
return NULL;
152+
}
153+
} else {
154+
const char *src = git_common_dir && git_common_dir[0] ? git_common_dir : worktree_root;
155+
if (!src) {
156+
return git_strdup("");
157+
}
158+
#ifndef _WIN32
159+
root = path_is_absolute(src) ? git_strdup(src) : join_root_relative(input_path, src);
160+
if (!root) {
161+
return NULL;
162+
}
163+
{
164+
char resolved[4096];
165+
if (realpath(root, resolved) != NULL) {
166+
free(root);
167+
root = git_strdup(resolved);
168+
if (!root) {
169+
return NULL;
170+
}
171+
}
172+
}
173+
#else
174+
(void)input_path;
175+
root = path_is_absolute(src) ? git_strdup(src) : join_root_relative(worktree_root, src);
176+
if (!root) {
177+
return NULL;
178+
}
179+
#endif
138180
}
139181

140182
size_t len = strlen(root);
@@ -251,7 +293,13 @@ int cbm_git_context_resolve(const char *path, cbm_git_context_t *out) {
251293

252294
out->is_worktree =
253295
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);
296+
/* git 2.31+ canonical absolute common-dir (best-effort; NULL on older git,
297+
* where derive_canonical_root falls back to the relative common-dir). */
298+
char *abs_common_dir = NULL;
299+
(void)git_capture(path, "rev-parse --path-format=absolute --git-common-dir", &abs_common_dir);
300+
out->canonical_root =
301+
derive_canonical_root(path, out->worktree_root, out->git_common_dir, abs_common_dir);
302+
free(abs_common_dir);
255303
out->branch_slug = slug_from_branch(out->branch, out->is_detached);
256304
if (git_capture(path, "merge-base HEAD @{upstream}", &out->base_sha) != 0) {
257305
out->base_sha = git_strdup("");

‎tests/test_git_context.c‎

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
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+
/* These helpers shell out to git and are only used by the non-Windows test
38+
* bodies below; on Windows every test SKIP_PLATFORMs, so guard them here too or
39+
* they'd be unused-static functions and fail the -Werror build. */
40+
#ifndef _WIN32
41+
/* Run a git command inside dir, return 0 on success. */
42+
static int git_run(const char *dir, const char *args) {
43+
char cmd[1024];
44+
snprintf(cmd, sizeof(cmd), "git -C \"%s\" %s >/dev/null 2>&1", dir, args);
45+
return system(cmd);
46+
}
47+
48+
/* Create a minimal git repo at dir (init + empty commit so HEAD exists). */
49+
static int make_git_repo(const char *dir) {
50+
if (th_mkdir_p(dir) != 0) return -1;
51+
if (git_run(dir, "init -q") != 0) return -1;
52+
if (git_run(dir, "config user.email test@example.com") != 0) return -1;
53+
if (git_run(dir, "config user.name Test") != 0) return -1;
54+
/* Create a file so HEAD points to a real commit. */
55+
char path[1024];
56+
snprintf(path, sizeof(path), "%s/.keep", dir);
57+
th_write_file(path, "");
58+
if (git_run(dir, "add .keep") != 0) return -1;
59+
if (git_run(dir, "commit -q -m init") != 0) return -1;
60+
return 0;
61+
}
62+
#endif /* _WIN32 */
63+
64+
/* ── canonical_root: normal repo indexed from its root ──────────── */
65+
66+
TEST(canonical_root_repo_root) {
67+
#ifdef _WIN32
68+
SKIP_PLATFORM("git-based canonical_root test not supported on Windows CI");
69+
#else
70+
char *tmp = th_mktempdir("cbm_gitctx");
71+
if (!tmp) FAIL("th_mktempdir returned NULL");
72+
73+
if (make_git_repo(tmp) != 0) {
74+
th_rmtree(tmp);
75+
SKIP_PLATFORM("git not available to init a repo");
76+
}
77+
78+
cbm_git_context_t ctx = {0};
79+
int rc = cbm_git_context_resolve(tmp, &ctx);
80+
if (rc != 0 || !ctx.is_git) {
81+
cbm_git_context_free(&ctx);
82+
th_rmtree(tmp);
83+
FAIL("cbm_git_context_resolve failed or not a git repo");
84+
}
85+
86+
char expected[4096];
87+
if (realpath(tmp, expected) == NULL) {
88+
cbm_git_context_free(&ctx);
89+
th_rmtree(tmp);
90+
FAIL("realpath(tmp) failed");
91+
}
92+
93+
ASSERT_STR_EQ(ctx.canonical_root, expected);
94+
95+
cbm_git_context_free(&ctx);
96+
th_rmtree(tmp);
97+
PASS();
98+
#endif /* _WIN32 */
99+
}
100+
101+
/* ── canonical_root: indexed from a subdirectory (issue #659) ─────
102+
* THE reproduce-first guard: from a subdir, --git-common-dir is relative, so the
103+
* unfixed derive_canonical_root joins it against worktree_root and strips "/.git"
104+
* textually, leaving canonical_root = "<root>/subdir/.." (or "<root>/..") instead
105+
* of "<root>". Verified RED on the unfixed code, GREEN with the realpath fix. */
106+
107+
TEST(canonical_root_subdir) {
108+
#ifdef _WIN32
109+
SKIP_PLATFORM("git-based canonical_root test not supported on Windows CI");
110+
#else
111+
char *tmp = th_mktempdir("cbm_gitctx");
112+
if (!tmp) FAIL("th_mktempdir returned NULL");
113+
114+
if (make_git_repo(tmp) != 0) {
115+
th_rmtree(tmp);
116+
SKIP_PLATFORM("git not available to init a repo");
117+
}
118+
119+
/* Create a subdirectory inside the repo. */
120+
char subdir[1024];
121+
snprintf(subdir, sizeof(subdir), "%s/scripts", tmp);
122+
if (th_mkdir_p(subdir) != 0) {
123+
th_rmtree(tmp);
124+
FAIL("failed to create subdir");
125+
}
126+
127+
cbm_git_context_t ctx = {0};
128+
int rc = cbm_git_context_resolve(subdir, &ctx);
129+
if (rc != 0 || !ctx.is_git) {
130+
cbm_git_context_free(&ctx);
131+
th_rmtree(tmp);
132+
FAIL("cbm_git_context_resolve on subdir failed or not a git repo");
133+
}
134+
135+
/* canonical_root must equal the repo root, NOT "<repo>/.." or "<subdir>/..". */
136+
char expected[4096];
137+
if (realpath(tmp, expected) == NULL) {
138+
cbm_git_context_free(&ctx);
139+
th_rmtree(tmp);
140+
FAIL("realpath(tmp) failed");
141+
}
142+
143+
ASSERT_STR_EQ(ctx.canonical_root, expected);
144+
145+
/* Sanity: canonical_root must not contain ".." or end with a slash. */
146+
ASSERT(strstr(ctx.canonical_root, "..") == NULL);
147+
ASSERT(ctx.canonical_root[strlen(ctx.canonical_root) - 1] != '/');
148+
149+
cbm_git_context_free(&ctx);
150+
th_rmtree(tmp);
151+
PASS();
152+
#endif /* _WIN32 */
153+
}
154+
155+
/* ── canonical_root: linked git worktree (supporting invariant) ────
156+
* NOT the #659 reproducer on modern git: git that emits an *absolute*
157+
* --git-common-dir for a linked worktree (e.g. 2.48.x) takes the path_is_absolute
158+
* branch, so the bug does not manifest and this passes with or without the fix.
159+
* It is kept as an invariant — canonical_root of a linked worktree must equal the
160+
* MAIN repo root (never the worktree root or its parent) — and would fail on a git
161+
* build that emits a *relative* worktree common-dir. The genuine RED-without-fix
162+
* guard for #659 is canonical_root_subdir above. */
163+
164+
TEST(canonical_root_linked_worktree) {
165+
#ifdef _WIN32
166+
SKIP_PLATFORM("git worktree test not implemented for Windows");
167+
#else
168+
/* th_mktempdir() returns a static buffer — copy before the second call. */
169+
char main_tmp[256];
170+
char *raw = th_mktempdir("cbm_main");
171+
if (!raw) FAIL("th_mktempdir returned NULL");
172+
strncpy(main_tmp, raw, sizeof(main_tmp) - 1);
173+
main_tmp[sizeof(main_tmp) - 1] = '\0';
174+
175+
char wt_tmp[256];
176+
raw = th_mktempdir("cbm_worktree");
177+
if (!raw) FAIL("th_mktempdir returned NULL");
178+
strncpy(wt_tmp, raw, sizeof(wt_tmp) - 1);
179+
wt_tmp[sizeof(wt_tmp) - 1] = '\0';
180+
181+
/* Remove the worktree dir first — git worktree add creates it. */
182+
th_rmtree(wt_tmp);
183+
184+
if (make_git_repo(main_tmp) != 0) {
185+
th_rmtree(main_tmp);
186+
SKIP_PLATFORM("git not available to init a repo");
187+
}
188+
189+
/* Create a branch for the worktree. */
190+
if (git_run(main_tmp, "branch wt-branch") != 0) {
191+
th_rmtree(main_tmp);
192+
FAIL("failed to create branch for worktree");
193+
}
194+
195+
/* Add a linked worktree. */
196+
char wt_cmd[1024];
197+
snprintf(wt_cmd, sizeof(wt_cmd), "worktree add \"%s\" wt-branch", wt_tmp);
198+
if (git_run(main_tmp, wt_cmd) != 0) {
199+
th_rmtree(wt_tmp);
200+
th_rmtree(main_tmp);
201+
SKIP_PLATFORM("git worktree add unavailable (git 2.5+ required)");
202+
}
203+
204+
cbm_git_context_t ctx = {0};
205+
int rc = cbm_git_context_resolve(wt_tmp, &ctx);
206+
if (rc != 0 || !ctx.is_git) {
207+
cbm_git_context_free(&ctx);
208+
git_run(main_tmp, "worktree prune");
209+
th_rmtree(main_tmp);
210+
th_rmtree(wt_tmp);
211+
FAIL("cbm_git_context_resolve on linked worktree failed");
212+
}
213+
214+
/* canonical_root must be the MAIN repo root, not the worktree root or its parent. */
215+
char expected[4096];
216+
if (realpath(main_tmp, expected) == NULL) {
217+
cbm_git_context_free(&ctx);
218+
git_run(main_tmp, "worktree prune");
219+
th_rmtree(main_tmp);
220+
th_rmtree(wt_tmp);
221+
FAIL("realpath(main_tmp) failed");
222+
}
223+
224+
ASSERT_STR_EQ(ctx.canonical_root, expected);
225+
ASSERT(strstr(ctx.canonical_root, "..") == NULL);
226+
227+
cbm_git_context_free(&ctx);
228+
git_run(main_tmp, "worktree prune");
229+
th_rmtree(main_tmp);
230+
th_rmtree(wt_tmp);
231+
PASS();
232+
#endif /* _WIN32 */
233+
}
234+
235+
/* ── Suite ──────────────────────────────────────────────────────── */
236+
237+
SUITE(git_context) {
238+
RUN_TEST(canonical_root_repo_root);
239+
RUN_TEST(canonical_root_subdir);
240+
RUN_TEST(canonical_root_linked_worktree);
241+
}

‎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)