evilmartians / lefthook

Fast and powerful Git hooks manager for any type of projects.
MIT License
4.75k stars 211 forks source link

Difference of git root directory durring a pre commit hook #214

Open ryuran opened 3 years ago

ryuran commented 3 years ago

Hello,

@arkweid/lefthook@0.7.6 git version 2.32.0

I spoted a difference when the script is run by lefthook or during a real commit with this config:

pre-commit:
  commands:
    test:
      root: "packages/sub/"
      run: |
        pwd
        git rev-parse --show-toplevel
        echo {staged_files}

On a npx lefthook run pre-commit (I think this one is correct):

/gitRootDirectory/packages/sub
/gitRootDirectory
./file.ts ./otherFile.tsx

On a real commit:

/gitRootDirectory/packages/sub
/gitRootDirectory/packages/sub
./file.ts ./otherFile.tsx
Envek commented 3 years ago

Git always sets working directory to repository root on hooks invocation:

Before Git invokes a hook, it changes its working directory to either $GIT_DIR in a bare repository or the root of the working tree in a non-bare repository.

See https://git-scm.com/docs/githooks

Because of that lefthook run hook command behavior is wrong and should be changed to match git behavior.

Envek commented 3 years ago

By the way:

On a real commit:

/gitRootDirectory/packages/sub
/gitRootDirectory/packages/sub

@ryuran Isn't it a typo? It should be /gitRootDirectory for both pwd and git rev-parse --show-toplevel I believe

ryuran commented 3 years ago

No it's not a typo. I use root option for a monorepo. But I encounter some issue with git diff or git apply into the hook. It seems to mess up because of this working directory change.

shamilovtim commented 1 year ago

Yeah I can confirm monorepo support is messed up. The lefthook.yml supports the root: directive but does nothing to propagate it to the git hooks. Because the git hooks avoid doing anything with the root: directive monorepo support doesn't work.

mrexox commented 1 month ago

Have you tested this on latest lefthook versions? Does the issue persist on lefthook 1.7.*?