git-exec-and-restage
git-exec-and-restage copied to clipboard
[Feature Request] Running git-exec-and-restage within a subdirectory
This issue is related to https://github.com/okonet/lint-staged/pull/65, if git-exec-and-restage
is installed within a subdirectory, it will report error like
Not a git repository
To compare two paths outside a working tree:
usage: git diff [--no-index] <path> <path>
Error: Exited with status 129
A gitDir
option is in need to support subdirectory, and we will use the resolved location to set cwd
in each spawn
const gitDir = path.resolve(config.gitDir) || process.cwd();
await spawn(
"git",
[
"diff-index",
"--cached",
"--name-only",
"--diff-filter=ACDMRTUXB",
revision,
"--"
],
{
encoding: "utf8",
cwd: gitDir
}
)
However we don't have a config system, so it depends on the owner to design the config. If there is not any preference, cosmiconfig will be a good fit.
Thank you.
A workaround using lint-staged
is to create a run-script that change to Git root directory before git-exec-and-restage
:
// `.lintstagedrc` in subdir
{
"gitDir": "../",
"linters": {
"*.{ts,tsx}": ["precommit-prettier"]
}
}
// `package.json` in subdir
{
// ...
"scripts": {
"precommit": "lint-staged",
"precommit-prettier": "cd .. && git-exec-and-restage prettier --write --"
}
}
Ugly but it works for me for now.
This should probably just work from anywhere inside a Git repo - I don't see a need for config really. Thoughts?