Compare commits

...

1 Commits

Author SHA1 Message Date
Kim Morrison
9fc4ffcfd6 fix(build): use commondir to resolve git directory in worktrees
This PR fixes git revision detection in worktrees where the worktree's
gitdir path passes through another git repository. The previous code
used `_git_find_closest_git_dir` which walks up the filesystem looking
for a `.git` entry, but this finds the wrong repository when the
worktree gitdir is stored inside another git repo (e.g. when the
project is a git submodule of a parent repo that also uses git).

Instead, read the `commondir` file that git places in every worktree's
gitdir, which directly points to the shared git object directory. Fall
back to the old filesystem walk if `commondir` doesn't exist.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 10:21:48 +11:00

View File

@@ -134,7 +134,16 @@ function(get_git_head_revision _refspecvar _hashvar)
#
string(REGEX REPLACE "gitdir: (.*)$" "\\1" git_worktree_dir ${worktree_ref})
string(STRIP ${git_worktree_dir} git_worktree_dir)
_git_find_closest_git_dir("${git_worktree_dir}" GIT_DIR)
# Use the commondir file to find the shared git directory, rather than
# walking up the filesystem (which can find the wrong .git if the
# worktree gitdir is inside another git repository).
if(EXISTS "${git_worktree_dir}/commondir")
file(READ "${git_worktree_dir}/commondir" commondir_ref)
string(STRIP "${commondir_ref}" commondir_ref)
get_filename_component(GIT_DIR "${git_worktree_dir}/${commondir_ref}" ABSOLUTE)
else()
_git_find_closest_git_dir("${git_worktree_dir}" GIT_DIR)
endif()
set(HEAD_SOURCE_FILE "${git_worktree_dir}/HEAD")
endif()
else()