usmanyunusov / nano-staged

Tiny tool to run commands for modified, staged, and committed files in a GIT repository.
https://npm.im/nano-staged
MIT License
468 stars 14 forks source link

Request: Support for git worktrees #26

Closed Mitsunee closed 2 years ago

Mitsunee commented 2 years ago

Clear and concise description of the problem

Currently git worktree is not supported. The following error appears:

Screenshot_20220415_122454

Suggested solution

before attempting to read .git as a directory stat it to determine if it is a worktree (where .git is a file containing a path) or not (where it is a directory)

Alternative

No response

Additional context

To recreate create a new project:

mkdir test-project-master
cd test-project
git init
yarn init -y && yarn add nano-staged prettier

Then add a config such as this one to package.json

  "nano-staged": {
    "**/*.js": [
      "prettier -w"
    ],
  }

Now commit the initial setup, then create a new worktree and attempt to make a commit there

git add -A
git commit -m "Initial setup"

git worktree add ../test-project-dev -b dev-branch # creates new worktree with its own branch
# note that a branch cannot be checked out by multiple worktrees,
# thus a new branch is required
cd ../test-project-dev
yarn # install in the new worktree

touch a-file.js
git add a-file.js
git commit -m "added a file"

You should now get the error as in the current working directory .git is a file describing the location of its location in the .git directory of the main tree. For more information on worktrees see git-worktree

usmanyunusov commented 2 years ago

@Mitsunee, fixed in PR #27.

Release 0.7 version.

Mitsunee commented 2 years ago

Just tested it, works great now, thank you :)