Skip to content

Commit 3f3ae85

Browse files
DeusDataanivaryam
andcommitted
fix(git): derive canonical_root from git's own absolute common-dir
Replace the manual join(input_path, --git-common-dir) + realpath/_fullpath with `git rev-parse --path-format=absolute --git-common-dir` (git 2.31+), which returns git's own absolute, canonical common-dir. Because a main repo and its linked worktree both query the same git binary, they resolve to the IDENTICAL path, so canonical_root is consistent across worktrees and platforms with no platform-specific path canonicalization. This is the truly platform-agnostic DeusData#659 fix: - git resolves the relative ".." internally, so a project indexed from a subdirectory gets the real repo root (the DeusData#659 bug). - No realpath/_fullpath, so the msys-vs-native path-representation divergence that broke the worktree==main-root invariant (test_pipeline.c git_context_linked_worktree) on the Windows CI leg is eliminated — both sides get git's single canonical representation. Falls back to the previous relative-join derivation (Unix realpath) when git is older than 2.31 and doesn't support --path-format (abs_common_dir empty). The input_path-join fix and the Windows test-harness SKIP_PLATFORM guards from the earlier commits on this branch remain for that fallback path. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com> Co-Authored-By: anivaryam <anivaryam.dev@gmail.com>
1 parent 722ace6 commit 3f3ae85

1 file changed

Lines changed: 52 additions & 38 deletions

File tree

‎src/git/git_context.c‎

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

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. */
129145
static char *derive_canonical_root(const char *input_path, const char *worktree_root,
130-
const char *git_common_dir) {
131-
const char *src = git_common_dir && git_common_dir[0] ? git_common_dir : worktree_root;
132-
if (!src) {
133-
return git_strdup("");
134-
}
135-
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);
142-
if (!root) {
143-
return NULL;
144-
}
145-
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. */
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+
}
150158
#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;
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+
}
158171
}
159172
}
160-
}
161173
#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-
}
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;
170178
}
171-
}
172179
#endif
180+
}
173181

174182
size_t len = strlen(root);
175183
while (len > 1 && (root[len - 1] == '/' || root[len - 1] == '\\')) {
@@ -285,7 +293,13 @@ int cbm_git_context_resolve(const char *path, cbm_git_context_t *out) {
285293

286294
out->is_worktree =
287295
out->git_dir && out->git_common_dir && strcmp(out->git_dir, out->git_common_dir) != 0;
288-
out->canonical_root = derive_canonical_root(path, 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);
289303
out->branch_slug = slug_from_branch(out->branch, out->is_detached);
290304
if (git_capture(path, "merge-base HEAD @{upstream}", &out->base_sha) != 0) {
291305
out->base_sha = git_strdup("");

0 commit comments

Comments
 (0)