From 2c9d7140690addb2ae2ca8d0164a2a6918c14a02 Mon Sep 17 00:00:00 2001 From: Wyatt Walter Date: Thu, 21 May 2026 13:20:14 -0500 Subject: [PATCH] fix(husky): stage server files from worktree root in pre-commit hook MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When git runs the pre-commit hook in a linked worktree it sets GIT_DIR but leaves GIT_WORK_TREE unset, so `git add` from a subdirectory treats cwd as the worktree root. The previous flow pushd'd into app/server, stripped the app/server/ prefix from the path list, then ran `git add appsmith-interfaces/...` — which staged an orphan tree at the worktree root in addition to the correct entry. Only manifested on worktrees, not the main checkout. Run mvn in a subshell instead of pushd'ing, and stage with full app/server/ paths from the worktree root. Behavior is unchanged in the main checkout and now correct in worktrees too. Co-Authored-By: Claude Opus 4.7 --- app/client/.husky/check-staged-files.sh | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/app/client/.husky/check-staged-files.sh b/app/client/.husky/check-staged-files.sh index cf456b2339c6..d2e94b6f391a 100644 --- a/app/client/.husky/check-staged-files.sh +++ b/app/client/.husky/check-staged-files.sh @@ -5,10 +5,14 @@ is_client_change=$(git diff --cached --name-only | grep -c "app/client") is_merge_commit=$(git rev-parse -q --verify MERGE_HEAD) -# Function to apply Spotless and only commit staged files +# Function to apply Spotless and only commit staged files. +# Runs mvn in a subshell so we don't pushd the script's cwd into app/server. +# Staging from the worktree root with full paths avoids a worktree-only bug: +# git invokes hooks with GIT_DIR set but GIT_WORK_TREE unset, so `git add` from +# a subdirectory treats cwd as the worktree root and stages files at the wrong path. apply_spotless_and_commit_staged_files() { - staged_server_files=$(git diff --cached --name-only | grep "app/server"| sed 's|app/server/||') - mvn spotless:apply + staged_server_files=$(git diff --cached --name-only | grep "app/server") + (cd app/server && mvn spotless:apply) # Check if Spotless succeeded if [ $? -ne 0 ]; then echo "Spotless apply failed, Please run mvn spotless:apply" @@ -23,9 +27,7 @@ if [ "$is_merge_commit" ]; then else if [ "$is_server_change" -ge 1 ]; then echo "Applying Spotless to server files..." - pushd app/server > /dev/null apply_spotless_and_commit_staged_files - popd > /dev/null else echo "Skipping server side check..." fi