fix(husky): make LTS pre-commit hook work in git worktrees#35801
Conversation
Same fix as #35800 applied to the 24.12.27 LTS branch. The hook cd's into core-web/ mid-execution 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. Wraps each absolute-path git invocation 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-24.12.27_lts
git worktree add /tmp/hook-test-24 origin/security/fix-husky-hook-worktree-24.12.27_lts
cd /tmp/hook-test-24
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 commit lands cleanly: Hook output on 24.12.27 LTS is more concise than 25.07.10 (less scaffolding), but the critical failure mode is gone: the file-backup step no longer aborts on the absolute-path Confirmed 6 Test plan checklist
|
Summary
Closes #35799 for the 24.12.27 LTS line. Companion to #35800 (same fix on 25.07.10 LTS).
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/:This blocked worktree-based dev for LTS branches and forced PR #35798 to be authored from a fresh shallow clone rather than a worktree.
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. Six sites updated incore-web/.husky/pre-commit:check_sdk_client_affectedadd sdk-editor.jsperform_frontend_fixesadd yarn.lockperform_frontend_fixesadd ${file}perform_frontend_fixesrestore ${file}restore ${file}(the one that surfaced the bug)restore ${file}(25.07.10 LTS has a 7th site for the OpenAPI add that doesn't exist on 24.12.27 LTS.)
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)