Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git directory is not correctly resolved if GIT_WORK_TREE is set to relative path #886

Closed
alexrinass opened this issue Jun 11, 2020 · 6 comments · Fixed by #887
Closed

Git directory is not correctly resolved if GIT_WORK_TREE is set to relative path #886

alexrinass opened this issue Jun 11, 2020 · 6 comments · Fixed by #887
Labels

Comments

@alexrinass
Copy link
Contributor

Description

Resolving the git directory will fail if the environment variable GIT_WORK_TREE is set to a relative path and lint-staged is run from a subdirectory of a Git repository.

This is because while GIT_DIR is unset in resolveGitRepo.js, GIT_WORK_TREE is not.

GIT_WORK_TREE can be used to set the worktree directory for a Git repo. GIT_DIR specifies only the .git directory. git rev-parse --show-toplevel will return the Git worktree directory. If it is set to a relative path, it won't work when git commands are called from a subdirectory, as . will then translate to the subdirectory.

In resolveGitRepo.js, you need to add delete process.env.GIT_WORK_TREE and then everything works as expected.

Why set GIT_WORK_TREE to a relative path?

lint-staged is usually called from hook-scripts and if you call git --work-tree=/ABSOLUTE/PATH/TO/REPO --git-dir=/ABSOLUTE/PATH/TO/REPO/.git commit from the worktree root, Git will set GIT_WORK_TREE to .. GIT_DIR will remain an absolute path.

Steps to reproduce

  1. Create a Git repo test-repo
  2. Make a subdirectory (e.g. my_app), where your JS app lives and which contains package.json with lint-staged config
  3. Modify a file and stage it
  4. Run lint-staged from the subdirectory – THIS WORKS
  5. Go back to your root Git working directory and set export GIT_WORK_TREE=.
  6. Run lint-staged from the subdirectory – this will now fail to detect the git directory, see error below.

Debug Logs

The Git repo resides at /Users/USER/Development/test-repo/. lint-staged resolves it to the subdirectory /Users/USER/Development/test-repo/my_app.

expand to view
  lint-staged:resolveGitRepo Resolving git repo from `/Users/USER/Development/test-repo/my_app` +0ms
  lint-staged:resolveGitRepo Unset GIT_DIR (was `undefined`) +0ms
  lint-staged:git Running git command [ 'rev-parse', '--show-toplevel' ] +0ms
  lint-staged:resolveGitRepo Failed to resolve git repo with error: [Error: ENOENT: no such file or directory, lstat '/Users/USER/Development/test-repo/my_app/.git'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'lstat',
  path: '/Users/USER/Development/test-repo/my_app/.git'
} +11ms
✖ Current directory is not a git directory!

Environment

  • OS: macOS 10.15
  • Node.js: v13.3.0
  • lint-staged: 10.2.9
@iiroj
Copy link
Member

iiroj commented Jun 11, 2020

Thanks for the issue! Would you like to submit a PR as well? Otherwise, I can open one during the weekend or so.

@iiroj
Copy link
Member

iiroj commented Jun 11, 2020

@alexrinass
Copy link
Contributor Author

I guess that would work but wouldn't it be more suitable here?

https://github.com/okonet/lint-staged/blob/master/lib/resolveGitRepo.js#L37

@iiroj
Copy link
Member

iiroj commented Jun 11, 2020

You are correct! Maybe all those could be collected in one place.

@alexrinass
Copy link
Contributor Author

I created a pull request with the fix: #887.

@github-actions
Copy link
Contributor

🎉 This issue has been resolved in version 10.2.10 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging a pull request may close this issue.

2 participants