git-exec-and-restage icon indicating copy to clipboard operation
git-exec-and-restage copied to clipboard

[Feature Request] Running git-exec-and-restage within a subdirectory

Open JLHwung opened this issue 7 years ago • 2 comments

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.

JLHwung avatar Jun 30 '17 06:06 JLHwung

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.

frenic avatar Sep 18 '17 13:09 frenic

This should probably just work from anywhere inside a Git repo - I don't see a need for config really. Thoughts?

motiz88 avatar May 20 '18 08:05 motiz88