🔧 Summary
Git commands called from Lefthook use incorrect working directory in Git worktree
Lefthook version
1.12.1
Steps to reproduce
- Create a new Git repository with an empty directory
subdir in it
- Configure Lefthook using
assert_lefthook_installed: true
pre-commit:
commands:
date:
root: "subdir/"
run: >
pwd &&
date > test.txt &&
git add -v test.txt
touch subdir/a; git add subdir
- Commit and observe that
test.txt is created in subdir
- Create a git worktree using
git worktree add ../lefthook-test-worktree
cd ../lefthook-test-worktree; touch subdir/b; git add subdir
- Commit this change
Expected results
The date in subdir/test.txt is updated
Actual results
A new file test.txt is created in the root of the repository. This is because GIT_DIR is set when running the git add command in the worktree, causing the file to be added in the wrong directory.