From 29711a22ead7868d1d138d3b6756cce75e4d8402 Mon Sep 17 00:00:00 2001 From: mbiuki Date: Thu, 21 May 2026 17:15:40 -0400 Subject: [PATCH] fix(husky): use git -C in pre-commit hook to work with worktrees 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 --- core-web/.husky/pre-commit | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/core-web/.husky/pre-commit b/core-web/.husky/pre-commit index 51b44d6ea173..3190474c323f 100755 --- a/core-web/.husky/pre-commit +++ b/core-web/.husky/pre-commit @@ -80,7 +80,7 @@ check_sdk_client_affected() { print_color "$BLUE" "â„šī¸ ${file_to_remove} does not exist" fi - if ! git add "${root_dir}/dotCMS/src/main/webapp/ext/uve/dot-uve.js"; then + if ! git -C "${root_dir}" add -- "dotCMS/src/main/webapp/ext/uve/dot-uve.js"; then print_color "$RED" "❌ Failed to stage computed dot-uve.js" exit 1 fi @@ -124,7 +124,7 @@ perform_frontend_fixes() { return 1 else print_color "$GREEN" "✅ Completed yarn install adding yarn.lock if it was modified" - git add "${root_dir}/core-web/yarn.lock" + git -C "${root_dir}" add -- "core-web/yarn.lock" fi if ! yarn nx affected -t lint --exclude='tag:skip:lint' --fix=true; then @@ -143,7 +143,7 @@ perform_frontend_fixes() { for file in $files; do if echo "$modified_files" | grep -Fxq "$file"; then - if ! git add -- "${root_dir}/${file}"; then + if ! git -C "${root_dir}" add -- "${file}"; then has_errors=true fi else @@ -157,7 +157,7 @@ perform_frontend_fixes() { printf "\n" for file in "${unmatched_files[@]}"; do printf " %s\n" "${file}" - git restore "${file}" + git -C "${root_dir}" restore -- "${file}" done printf "\n" print_color "$YELLOW" "💡 You can fix these files by running the following commands:" @@ -303,7 +303,7 @@ if needs_maven; then print_color "$GREEN" "✅ Completed Maven compile with OpenAPI generation" # Stage the updated OpenAPI file if it was regenerated if [ -f "${root_dir}/dotCMS/src/main/webapp/WEB-INF/openapi/openapi.yaml" ]; then - git add "${root_dir}/dotCMS/src/main/webapp/WEB-INF/openapi/openapi.yaml" + git -C "${root_dir}" add -- "dotCMS/src/main/webapp/WEB-INF/openapi/openapi.yaml" print_color "$GREEN" "📋 Updated OpenAPI specification staged for commit" fi fi @@ -384,7 +384,7 @@ if [ "$backup_untracked" = true ] && [ -n "${untracked_files}" ]; then cp "${root_dir}/${file}" "${temp_dir}/${file}" # Copy the file to the temp directory, preserving the directory structure print_color "$BLUE" "💾 Backing up ${file}" # Restore the original file state in the repo, removing unstaged changes - git restore "${root_dir}/${file}" # Using relative path relative to current directory + git -C "${root_dir}" restore -- "${file}" # use -C to keep git rooted at repo root in worktrees fi done @@ -393,7 +393,7 @@ if [ "$backup_untracked" = true ] && [ -n "${untracked_files}" ]; then for file in $untracked_files; do if echo "${staged_files}" | grep -q "^${file}$"; then - git restore "${root_dir}/${file}" # Using relative path relative to current directory + git -C "${root_dir}" restore -- "${file}" # use -C to keep git rooted at repo root in worktrees fi done