fix(husky): make LTS pre-commit hook work in git worktrees#35800
Conversation
The hook cd's into core-web/ mid-execution (for yarn/nx scoping) and then invokes git on absolute paths to files outside core-web. In a normal clone that worked because git rediscovers the repo root from cwd. In a git worktree the calling commit sets GIT_DIR to an absolute path that the hook inherits, which makes git infer GIT_WORK_TREE from cwd (core-web) and reject absolute paths to non-core-web files: fatal: .../parent/pom.xml: '.../parent/pom.xml' is outside repository at '.../core-web' This broke worktree-based development for non-core-web changes on the LTS branches and forced #35797 and #35798 to be authored from fresh shallow clones rather than worktrees. Wraps each git invocation that takes an absolute path with `git -C "${root_dir}"`, which is unambiguous regardless of cwd or inherited GIT_DIR. No behavior change in normal-clone workflows. Refs #35799
Legal RiskThe following dependencies were released under a license that RecommendationWhile merging is not directly blocked, it's best to pause and consider what it means to use this license before continuing. If you are unsure, reach out to your security team or Semgrep admin to address this issue. EPL-2.0 GPL-2.0 MPL-2.0 |
Test verification ✅Reproduced the original failure scenario in a fresh Setupcd ~/Documents/dotCMS/core
git fetch origin security/fix-husky-hook-worktree-25.07.10_lts
git worktree add /tmp/hook-test-25 origin/security/fix-husky-hook-worktree-25.07.10_lts
cd /tmp/hook-test-25
mkdir -p core-web/.husky/_ && <install husky.sh helper>TestMade a valid non-core-web change (modified ResultPre-fix (on base branch After this PR's changes, the hook progresses past the backup step and the commit lands: The key line Test plan checklist
|
Summary
Closes #35799 for the 25.07.10 LTS line.
The
core-web/.husky/pre-commithook fails on non-core-web commits when authored from a git worktree (rather than a fresh clone). The hookcds intocore-web/mid-execution and then runs git on absolute paths to files outside that subtree. In a normal clone this works because git rediscovers the repo root from cwd. In a worktree, the callinggit commitsetsGIT_DIRto an absolute path; the hook inherits it, and subsequentgitcalls fromcore-web/inferGIT_WORK_TREE=core-webfrom cwd — they then reject absolute paths to files outsidecore-web/with:This blocked worktree-based dev for LTS branches and forced PRs #35797 and #35798 to be authored from fresh shallow clones rather than worktrees.
Fix
Replace every
git add "${root_dir}/<path>"orgit restore "${root_dir}/<path>"withgit -C "${root_dir}" add -- "<path>"/git -C "${root_dir}" restore -- "<path>". The-Cflag tells git to operate as if it were invoked fromroot_dir, which is unambiguous regardless of cwd or any inheritedGIT_DIR. Seven sites updated:check_sdk_client_affectedadd dot-uve.jsperform_frontend_fixesadd yarn.lockperform_frontend_fixesadd ${file}perform_frontend_fixesrestore ${file}add openapi.yamlrestore ${file}(the one that surfaced the bug)restore ${file}No behavior change in normal-clone workflows;
git -Cand the previous absolute-path form are equivalent there.Test plan
git worktree addworktree — hook completes (this was the failing scenario)Companion work
release-24.12.27_ltsin a sibling PRmainis not affected (different hook).