fix(git): fix canonical_root for worktrees and subdirectory projects#672
Open
anivaryam wants to merge 4 commits into
Open
fix(git): fix canonical_root for worktrees and subdirectory projects#672anivaryam wants to merge 4 commits into
anivaryam wants to merge 4 commits into
Conversation
git rev-parse --git-common-dir outputs a path relative to the directory passed via -C (the indexed path), not to worktree_root. When the indexed path is a subdirectory of the repo root, or a linked worktree sibling, joining the relative git_common_dir with worktree_root produced a path with unresolved ".." components. Stripping the "/.git" suffix then left e.g. "/workspace/.." rather than "/workspace". Two changes in derive_canonical_root(): 1. Use input_path (the original indexed directory) as the join base for relative git_common_dir paths, so "../.git" from scripts/ resolves against scripts/, not against the already-resolved worktree root. 2. Call realpath() (POSIX) / _fullpath() (Windows) on the joined .git path before stripping the suffix, collapsing any ".." components into the canonical filesystem path. The .git directory always exists for a valid repository, so realpath() succeeds. Add test_git_context.c covering all three cases: - repo indexed from its root (existing behaviour, must not regress) - project inside a repo, indexed from a subdirectory (issue DeusData#659) - project in a linked git worktree (issue DeusData#659 primary case) Fixes DeusData#659 Signed-off-by: anivaryam <anivaryam.dev@gmail.com>
check-no-test-skips.sh prohibits plain SKIP(); git worktree add is available since git 2.5 (2015) so failure is a real error, not a platform/version skip. Signed-off-by: anivaryam <anivaryam.dev@gmail.com>
- Replace MAX_PATH (requires windows.h) with 4096 in both src/git/git_context.c and tests/test_git_context.c; use sizeof(resolved) instead of the literal for _fullpath() - Wrap canonical_root_linked_worktree body in #ifdef _WIN32 / #else / #endif so realpath() is excluded from Windows compilation (SKIP_PLATFORM is a runtime skip — the body still compiled) Signed-off-by: anivaryam <anivaryam.dev@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #659 —
detect_changesreturns emptyimpacted_symbolsfor projects indexed inside git worktrees or as repo subdirectories becausecanonical_rootwas computed incorrectly.Root cause:
git rev-parse --git-common-diroutputs a path relative to the directory passed via-C(the indexed path,input_path), not toworktree_root. Two errors inderive_canonical_root():git_common_dir(e.g."../.git") was joined withworktree_rootinstead ofinput_path. For a project atworkspace/scripts/,worktree_rootisworkspace/but the relative path is relative toscripts/— joining it withworkspace/producedworkspace/../.git, which strips toworkspace/...scripts/../.git), stripping the/.gitsuffix leavesscripts/..— a path with unresolved..components. Without normalization this is stored as-is and never matches the project's stored root path.Fix (
src/git/git_context.c):input_path(the original indexed directory) as the join base for relativegit_common_dirpaths.realpath()(POSIX) /_fullpath()(Windows) on the joined.gitpath before stripping the suffix. The.gitdirectory always exists for a valid repository, so the call succeeds and collapses any..components.Tests (
tests/test_git_context.c):Three new tests in
suite_git_context:canonical_root_repo_root— normal repo indexed from its root (regression guard)canonical_root_subdir— project indexed from a subdirectory of the repo (issue detect_changes: impacted_symbols always empty for projects inside git worktrees (canonical_root miscalculated) #659)canonical_root_linked_worktree— project in a linked git worktree (issue detect_changes: impacted_symbols always empty for projects inside git worktrees (canonical_root miscalculated) #659 primary case)Test plan
canonical_root_repo_rootpassescanonical_root_subdirpasses (was failing before fix)canonical_root_linked_worktreepasses (was failing before fix)make testsuite has no regressions