Skip to content

Git commands called from Lefthook use incorrect working directory in Git worktree #1077

@praseodym

Description

@praseodym

🔧 Summary

Git commands called from Lefthook use incorrect working directory in Git worktree

Lefthook version

1.12.1

Steps to reproduce

  1. Create a new Git repository with an empty directory subdir in it
  2. Configure Lefthook using
assert_lefthook_installed: true

pre-commit:
  commands:
    date:
      root: "subdir/"
      run: >
        pwd &&
        date > test.txt &&
        git add -v test.txt
  1. touch subdir/a; git add subdir
  2. Commit and observe that test.txt is created in subdir
  3. Create a git worktree using git worktree add ../lefthook-test-worktree
  4. cd ../lefthook-test-worktree; touch subdir/b; git add subdir
  5. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions