Repository: okonet/lint-staged
Pull request #906 (2020-08-18 23:40:40)

fix: disambiguate stash reference
### Problem
https://github.com/okonet/lint-staged/issues/905

When you have a branch named `stash`, you receive the following error from `lint-staged`.

```
warning: refname 'stash' is ambiguous.
error: refname 'stash' is ambiguous
warning: refname 'stash' is ambiguous.
'--quiet stash@{0}' is not a stash reference 
```

### Options
1. Stashes are stored at `refs/stash` and we could update this line with `refs/` to disambiguate
https://github.com/okonet/lint-staged/blob/b078324d5e911ec5e667736b2c552af32f475751/lib/gitWorkflow.js#L102

2. Convert the call from `git stash list` to `git log -g --pretty="%gD %H %gs" refs/stash` which returns the disambiguated stash refs (We'd need to introduce additional error handling as this command fails when there is no stash)

### The Solution
I went with the simpler option of updating the string to `refs/stash`
I've added an integration test to verify this behavior.

philipbjorge: Sweet -- There are integration tests!
I can't promise I'll have the time to update them, but I'm glad to see they're there as this seemed useless to unit test.
philipbjorge: @okonet -- Are you able to assist here? I've been unable to get a passing build on your CI even without any changes in my branch.

Locally, things look good and it also appears to be passing on Appveyor.

![Screenshot 2020-08-23 14:21:32](https://user-images.githubusercontent.com/918891/90989002-086f2b80-e54c-11ea-82f7-78510e4308ca.png)

https://ci.appveyor.com/project/okonet/lint-staged/builds/34808885

okonet: It looks like the snapshot is failing and I'm wondering why:

```diff
    - Merge branch 'branch-b'
    + Merge branch 'branch-b' into master
```

Looks like a different version of git maybe?
iiroj: That's probably it. Maybe the snapshot can be losened to assert it only contains the necessary bits.
philipbjorge: @iiroj @okonet --
Is this something you'd like me to take a stab at fixing or is this something y'all would like to handle? It looks like this is affecting some other PRs as well.
iiroj: @philipbjorge @okonet I updated dependencies and fixed the offending snapshot in https://github.com/okonet/lint-staged/pull/901
codecov[bot]: # [Codecov](https://codecov.io/gh/okonet/lint-staged/pull/906?src=pr&el=h1) Report
> Merging [#906](https://codecov.io/gh/okonet/lint-staged/pull/906?src=pr&el=desc) into [master](https://codecov.io/gh/okonet/lint-staged/commit/10b3218212e8aa5ae51747c70b1a25d9665ee6ab?el=desc) will **not change** coverage.
> The diff coverage is `100.00%`.

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

```diff
@@            Coverage Diff            @@
##            master      #906   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           17        17           
  Lines          602       602           
  Branches       142       142           
=========================================
  Hits           602       602           
```


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

------

[Continue to review full report at Codecov](https://codecov.io/gh/okonet/lint-staged/pull/906?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/906?src=pr&el=footer). Last update [10b3218...762902f](https://codecov.io/gh/okonet/lint-staged/pull/906?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/lib/gitWorkflow.js b/lib/gitWorkflow.js
index f049010..62a01e1 100644
--- a/lib/gitWorkflow.js
+++ b/lib/gitWorkflow.js
@@ -100,7 +100,7 @@ class GitWorkflow {
       ctx.errors.add(GetBackupStashError)
       throw new Error('lint-staged automatic backup is missing!')
     }
-    return `stash@{${index}}`
+    return `refs/stash@{${index}}`
   }
 
   /**


Test patch:
diff --git a/test/integration.test.js b/test/integration.test.js
index 33f4f06..6be62a9 100644
--- a/test/integration.test.js
+++ b/test/integration.test.js
@@ -1032,6 +1032,27 @@ describe('lint-staged', () => {
     expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('2')
     expect(await readFile('--looks-like-flag.js')).toEqual(testJsFilePretty)
   })
+
+  it('should work when a branch named stash exists', async () => {
+    // create a new branch called stash
+    await execGit(['branch', 'stash'])
+
+    // Stage multiple ugly files
+    await appendFile('test.js', testJsFileUgly)
+    await execGit(['add', 'test.js'])
+
+    await appendFile('test2.js', testJsFileUgly)
+    await execGit(['add', 'test2.js'])
+
+    // Run lint-staged with `prettier --write` and commit pretty file
+    await gitCommit(fixJsConfig)
+
+    // Nothing is wrong, so a new commit is created and file is pretty
+    expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('2')
+    expect(await execGit(['log', '-1', '--pretty=%B'])).toMatch('test')
+    expect(await readFile('test.js')).toEqual(testJsFilePretty)
+    expect(await readFile('test2.js')).toEqual(testJsFilePretty)
+  })
 })
 
 describe('lintStaged', () => {