Skip to content

Commit 3d5deb5

Browse files
wyattwalterclaude
andauthored
fix(husky): stage server files from worktree root in pre-commit hook (#41835)
## Summary - Pre-commit hook stages server files at the wrong path when committing from a linked git worktree, creating an orphan tree at the worktree root in addition to the correct entry. - Root cause: git invokes hooks with `GIT_DIR` set but `GIT_WORK_TREE` unset. After `pushd app/server`, `git add` falls back to treating cwd as the worktree root, so `xargs git add appsmith-interfaces/...` (with the `app/server/` prefix stripped) stages files at the wrong location. Only affects worktrees, not the main checkout. - Fix: run `mvn spotless:apply` in a subshell (so the script's cwd stays at worktree root), and stage with full `app/server/...` paths via the unchanged path list. Same behavior in the main checkout; correct behavior in worktrees. ## Reproduction In a linked worktree with a staged change under `app/server/`, the hook stages the file twice — once correctly and once at the worktree root without the `app/server/` prefix. Verified by simulating the hook env (`GIT_DIR` set, cwd at `app/server/`) and running the old vs new `git add` invocation; old path produces the duplicate orphan entry, new path produces only the correct entry. ## Test plan - [ ] Commit a server file change from a linked git worktree (`git worktree add ...`); confirm `git status` after commit shows no orphan entries at the worktree root. - [ ] Commit a server file change from the main checkout; confirm spotless still runs and re-staging works as before. - [ ] Commit a client-only change; confirm `lint-staged` still runs. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Chores** * Updated internal code formatting automation to improve developer workflow efficiency. --- **Note:** This release contains no user-facing changes. The update addresses internal development tooling. <!-- review_stack_entry_start --> [![Review Change Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/appsmithorg/appsmith/pull/41835?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack) <!-- review_stack_entry_end --> <!-- end of auto-generated comment: release notes by coderabbit.ai --> Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 51c5591 commit 3d5deb5

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

app/client/.husky/check-staged-files.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ is_client_change=$(git diff --cached --name-only | grep -c "app/client")
55

66
is_merge_commit=$(git rev-parse -q --verify MERGE_HEAD)
77

8-
# Function to apply Spotless and only commit staged files
8+
# Function to apply Spotless and only commit staged files.
9+
# Runs mvn in a subshell so we don't pushd the script's cwd into app/server.
10+
# Staging from the worktree root with full paths avoids a worktree-only bug:
11+
# git invokes hooks with GIT_DIR set but GIT_WORK_TREE unset, so `git add` from
12+
# a subdirectory treats cwd as the worktree root and stages files at the wrong path.
913
apply_spotless_and_commit_staged_files() {
10-
staged_server_files=$(git diff --cached --name-only | grep "app/server"| sed 's|app/server/||')
11-
mvn spotless:apply
14+
staged_server_files=$(git diff --cached --name-only | grep "app/server")
15+
(cd app/server && mvn spotless:apply)
1216
# Check if Spotless succeeded
1317
if [ $? -ne 0 ]; then
1418
echo "Spotless apply failed, Please run mvn spotless:apply"
@@ -23,9 +27,7 @@ if [ "$is_merge_commit" ]; then
2327
else
2428
if [ "$is_server_change" -ge 1 ]; then
2529
echo "Applying Spotless to server files..."
26-
pushd app/server > /dev/null
2730
apply_spotless_and_commit_staged_files
28-
popd > /dev/null
2931
else
3032
echo "Skipping server side check..."
3133
fi

0 commit comments

Comments
 (0)