Repository: okonet/lint-staged
Pull request #821 (2020-03-31 12:48:30)

fix: add `--` to `git add` command to denote pathspec starting
Without the -- denoter some paths might be interpreted as flags.

Fixes https://github.com/okonet/lint-staged/issues/820

codecov[bot]: # [Codecov](https://codecov.io/gh/okonet/lint-staged/pull/821?src=pr&el=h1) Report
> Merging [#821](https://codecov.io/gh/okonet/lint-staged/pull/821?src=pr&el=desc) into [master](https://codecov.io/gh/okonet/lint-staged/commit/8abb09f1acf4d751dde35de015593522ea399e10?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/okonet/lint-staged/pull/821/graphs/tree.svg?width=650&token=LSoEgsjk4O&height=150&src=pr)](https://codecov.io/gh/okonet/lint-staged/pull/821?src=pr&el=tree)

```diff
@@          Coverage Diff          @@
##           master   #821   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          13     13           
  Lines         514    514           
  Branches      110    110           
=====================================
  Hits          514    514
```


| [Impacted Files](https://codecov.io/gh/okonet/lint-staged/pull/821?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [lib/gitWorkflow.js](https://codecov.io/gh/okonet/lint-staged/pull/821/diff?src=pr&el=tree#diff-bGliL2dpdFdvcmtmbG93Lmpz) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/okonet/lint-staged/pull/821?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/okonet/lint-staged/pull/821?src=pr&el=footer). Last update [8abb09f...e5e97de](https://codecov.io/gh/okonet/lint-staged/pull/821?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

yoyo837: Thanks for the catch!

Patch:
diff --git a/lib/gitWorkflow.js b/lib/gitWorkflow.js
index 2540548..321b137 100644
--- a/lib/gitWorkflow.js
+++ b/lib/gitWorkflow.js
@@ -238,7 +238,7 @@ class GitWorkflow {
     await Promise.all(
       // stagedFileChunks includes staged files that lint-staged originally detected.
       // Add only these files so any 3rd-party edits to other files won't be included in the commit.
-      this.stagedFileChunks.map(stagedFiles => this.execGit(['add', ...stagedFiles]))
+      this.stagedFileChunks.map(stagedFiles => this.execGit(['add', '--', ...stagedFiles]))
     )
     debug('Done adding task modifications to index!')
 


Test patch:
diff --git a/test/runAll.unmocked.spec.js b/test/runAll.unmocked.spec.js
index f4e94fd..1f78eec 100644
--- a/test/runAll.unmocked.spec.js
+++ b/test/runAll.unmocked.spec.js
@@ -1038,6 +1038,14 @@ describe('runAll', () => {
     expect(await readFile('test.js')).toEqual(testJsFilePretty) // file was still fixed
     expect(await readFile('test2.js')).toEqual(testJsFileUnfixable)
   })
+
+  it('should handle files that begin with dash', async () => {
+    await appendFile('--looks-like-flag.js', testJsFileUgly)
+    await execGit(['add', '--', '--looks-like-flag.js'])
+    await expect(gitCommit(fixJsConfig)).resolves.toEqual(undefined)
+    expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('2')
+    expect(await readFile('--looks-like-flag.js')).toEqual(testJsFilePretty)
+  })
 })
 
 describe('runAll', () => {