Repository: okonet/lint-staged
Pull request #827 (2020-04-03 04:29:42)

fix: no longer include untracked files in backup stash
Since v10.1 having untracked files in the backup stash causes revert to fail, because the stash will not apply with the same files already on disk.

Fixes https://github.com/okonet/lint-staged/issues/826

iiroj: This happens because when reverting, we call `git reset`, which doesn't touch untracked files. It's now safe to simply not have untracked files in the stash, because nothing touches them (unless tasks do something).
codecov[bot]: # [Codecov](https://codecov.io/gh/okonet/lint-staged/pull/827?src=pr&el=h1) Report
> Merging [#827](https://codecov.io/gh/okonet/lint-staged/pull/827?src=pr&el=desc) into [master](https://codecov.io/gh/okonet/lint-staged/commit/226ccdb7727c9837fa04528047ced89466c4b833?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/827/graphs/tree.svg?width=650&token=LSoEgsjk4O&height=150&src=pr)](https://codecov.io/gh/okonet/lint-staged/pull/827?src=pr&el=tree)

```diff
@@          Coverage Diff          @@
##           master   #827   +/-   ##
=====================================
  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/827?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [lib/gitWorkflow.js](https://codecov.io/gh/okonet/lint-staged/pull/827/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/827?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/827?src=pr&el=footer). Last update [226ccdb...fc56d28](https://codecov.io/gh/okonet/lint-staged/pull/827?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

iiroj: @okonet Travis integration is broken, but you can see that it passed here: https://travis-ci.org/github/okonet/lint-staged/builds/670434608

I'm unable to merge because the button is disabled.
okonet: Yeah I don’t know why it’s not working anymore 
iiroj: @okonet  I rebased this on master, since the release job seems to be working. It didn't generate a release because all commits are of `ci` type.

https://github.com/okonet/lint-staged/runs/561725417?check_suite_focus=true#step:5:59

```bash
[8:47:40 AM] [semantic-release] › ℹ  There are no relevant changes, so no new version is released.
```
okonet: So we merge this one and expect a release? Right?
iiroj: @okonet Yeah, that should be what's happening since these are `fix` types.

Patch:
diff --git a/lib/gitWorkflow.js b/lib/gitWorkflow.js
index 321b137..2d2822d 100644
--- a/lib/gitWorkflow.js
+++ b/lib/gitWorkflow.js
@@ -193,8 +193,8 @@ class GitWorkflow {
       // Manually check and backup if necessary
       await this.backupMergeStatus()
 
-      // Save stash of entire original state, including unstaged and untracked changes
-      await this.execGit(['stash', 'save', '--include-untracked', STASH])
+      // Save stash of original state
+      await this.execGit(['stash', 'save', STASH])
       await this.execGit(['stash', 'apply', '--quiet', '--index', await this.getBackupStash()])
 
       // Restore meta information about ongoing git merge, cleared by `git stash`
diff --git a/lib/runAll.js b/lib/runAll.js
index 328efa5..ef6ca67 100644
--- a/lib/runAll.js
+++ b/lib/runAll.js
@@ -254,7 +254,7 @@ const runAll = async (
 
     > git stash list
     stash@{0}: On master: automatic lint-staged backup
-    > git stash pop stash@{0}\n`)
+    > git stash apply --index stash@{0}\n`)
       }
     }
 


Test patch:
diff --git a/test/runAll.unmocked.2.spec.js b/test/runAll.unmocked.2.spec.js
index 86aea65..b20fb34 100644
--- a/test/runAll.unmocked.2.spec.js
+++ b/test/runAll.unmocked.2.spec.js
@@ -124,7 +124,7 @@ describe('runAll', () => {
 
           > git stash list
           stash@{0}: On master: automatic lint-staged backup
-          > git stash pop stash@{0}
+          > git stash apply --index stash@{0}
       "
     `)
   })
diff --git a/test/runAll.unmocked.spec.js b/test/runAll.unmocked.spec.js
index 1f78eec..3a7a822 100644
--- a/test/runAll.unmocked.spec.js
+++ b/test/runAll.unmocked.spec.js
@@ -428,7 +428,7 @@ describe('runAll', () => {
 
           > git stash list
           stash@{0}: On master: automatic lint-staged backup
-          > git stash pop stash@{0}
+          > git stash apply --index stash@{0}
       "
     `)
 
@@ -608,6 +608,29 @@ describe('runAll', () => {
     expect(Buffer.from(await readFile('binary'), 'binary').toString()).toEqual('Hello, World!')
   })
 
+  it('should keep untracked files when taks fails', async () => {
+    // Stage unfixable file
+    await appendFile('test.js', testJsFileUnfixable)
+    await execGit(['add', 'test.js'])
+
+    // Add untracked files
+    await appendFile('test-untracked.js', testJsFilePretty)
+    await appendFile('.gitattributes', 'binary\n')
+    await writeFile('binary', Buffer.from('Hello, World!', 'binary'))
+
+    // Run lint-staged with `prettier --list-different` and commit pretty file
+    await expect(
+      gitCommit({ config: { '*.js': 'prettier --list-different' } })
+    ).rejects.toThrowError()
+
+    // Something was wrong so the repo is returned to original state
+    expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('1')
+    expect(await execGit(['log', '-1', '--pretty=%B'])).toMatch('initial commit')
+    expect(await readFile('test.js')).toEqual(testJsFileUnfixable)
+    expect(await readFile('test-untracked.js')).toEqual(testJsFilePretty)
+    expect(Buffer.from(await readFile('binary'), 'binary').toString()).toEqual('Hello, World!')
+  })
+
   it('should work when amending previous commit with unstaged changes', async () => {
     // Edit file from previous commit
     await appendFile('README.md', '\n## Amended\n')