Skip to content

fix(husky): stage server files from worktree root in pre-commit hook#41835

Open
wyattwalter wants to merge 1 commit into
releasefrom
fix/husky-worktree-staging
Open

fix(husky): stage server files from worktree root in pre-commit hook#41835
wyattwalter wants to merge 1 commit into
releasefrom
fix/husky-worktree-staging

Conversation

@wyattwalter
Copy link
Copy Markdown
Contributor

@wyattwalter wyattwalter commented May 21, 2026

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

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 Change Stack

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 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 21, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: adb7d06f-536b-4ff8-8a7f-c244da1a682b

📥 Commits

Reviewing files that changed from the base of the PR and between d44df5f and 2c9d714.

📒 Files selected for processing (1)
  • app/client/.husky/check-staged-files.sh

Walkthrough

The Git hook check-staged-files.sh is refactored to encapsulate directory context handling within the apply_spotless_and_commit_staged_files function. The function now internally changes to app/server and runs Maven Spotless via a subshell, eliminating the need for the caller to manage pushd/popd operations.

Changes

Spotless execution context refactoring

Layer / File(s) Summary
Spotless execution context refactoring
app/client/.husky/check-staged-files.sh
The apply_spotless_and_commit_staged_files function manages directory context internally using a subshell (cd app/server && mvn spotless:apply). Staged server files are collected with full paths and passed to git add without path prefix stripping. The calling site removes the corresponding pushd/popd wrapper.

🎯 2 (Simple) | ⏱️ ~8 minutes

A hook refactored clean,
Subshells now hold the context,
Callers breathe easier. 🐚

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main fix: running the spotless stage operation from the worktree root instead of from a subdirectory, which directly addresses the root cause of incorrect file staging in git worktrees.
Description check ✅ Passed The description covers the issue, root cause, fix, reproduction steps, and test plan comprehensively. However, it lacks a reference to the related issue number and lacks explicit DevRel/Marketing communication checkbox selection.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/husky-worktree-staging

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants