Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly handle git stash when using MSYS2 #1178

Merged
merged 3 commits into from Jun 24, 2022
Merged

fix: correctly handle git stash when using MSYS2 #1178

merged 3 commits into from Jun 24, 2022

Conversation

HitoriSensei
Copy link
Contributor

@HitoriSensei HitoriSensei commented Jun 24, 2022

This simple patch fixes #1121 by detecting MSYS2 shell via env variables set by MSYS2 (https://github.com/msys2/MSYS2-packages/blob/master/filesystem/msys2_shell.cmd) and adds escaped braces if MSYS2 is found.

@codecov
Copy link

codecov bot commented Jun 24, 2022

Codecov Report

Merging #1178 (0d7d7a6) into master (1a5a66a) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            master     #1178   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           26        26           
  Lines          733       735    +2     
  Branches       197       198    +1     
=========================================
+ Hits           733       735    +2     
Impacted Files Coverage Δ
lib/gitWorkflow.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1a5a66a...0d7d7a6. Read the comment docs.

@iiroj
Copy link
Collaborator

iiroj commented Jun 24, 2022

Thank you for the PR!

Please rename the commit as something like fix: correctly handle git stash when using MSYS2.

Also, this needs at least an unit test, let me figure out how to do it.

@iiroj
Copy link
Collaborator

iiroj commented Jun 24, 2022

Something like this will properly test the gitWorkflow.getBackupStash method:

import { execGit } from '../../lib/execGit.js'
import { GitWorkflow, STASH } from '../../lib/gitWorkflow.js'
import { getInitialState } from '../../lib/state.js'
import { GetBackupStashError } from '../../lib/symbols'

jest.mock('../../lib/execGit.js', () => ({
  execGit: jest.fn(async () => ''),
}))

describe('gitWorkflow', () => {
  const options = { gitConfigDir: '.' }

  describe('getBackupStash', () => {
    it('should throw when stash not found', async () => {
      const gitWorkflow = new GitWorkflow(options)
      const ctx = getInitialState()

      await expect(gitWorkflow.getBackupStash(ctx)).rejects.toThrowError(
        'lint-staged automatic backup is missing!'
      )

      expect(ctx.errors.has(GetBackupStashError)).toEqual(true)
    })

    it('should throw when stash not found even when other stashes are', async () => {
      const gitWorkflow = new GitWorkflow(options)
      const ctx = getInitialState()

      execGit.mockResolvedValueOnce('stash@{0}: some random stuff')

      await expect(gitWorkflow.getBackupStash(ctx)).rejects.toThrowError(
        'lint-staged automatic backup is missing!'
      )

      expect(ctx.errors.has(GetBackupStashError)).toEqual(true)
    })

    it('should return ref to the backup stash', async () => {
      const gitWorkflow = new GitWorkflow(options)
      const ctx = getInitialState()

      execGit.mockResolvedValueOnce(
        [
          'stash@{0}: some random stuff',
          `stash@{1}: ${STASH}`,
          'stash@{2}: other random stuff',
        ].join('\n')
      )

      await expect(gitWorkflow.getBackupStash(ctx)).resolves.toEqual('refs/stash@{1}')
    })

    it('should return escaped ref to the backup stash when using MSYS2', async () => {
      const gitWorkflow = new GitWorkflow(options)
      const ctx = getInitialState()

      process.env.MSYSTEM = 'MSYS'

      execGit.mockResolvedValueOnce(
        [
          'stash@{0}: some random stuff',
          `stash@{1}: ${STASH}`,
          'stash@{2}: other random stuff',
        ].join('\n')
      )

      await expect(gitWorkflow.getBackupStash(ctx)).resolves.toEqual('refs/stash@\\{1\\}')

      delete process.env.MSYSTEM
    })
  })
})

lib/gitWorkflow.js Outdated Show resolved Hide resolved
@HitoriSensei HitoriSensei changed the title fix: https://github.com/okonet/lint-staged/issues/1121 fix: correctly handle git stash when using MSYS2 Jun 24, 2022
iiroj
iiroj approved these changes Jun 24, 2022
@iiroj iiroj merged commit 0d627a5 into okonet:master Jun 24, 2022
16 checks passed
@github-actions
Copy link

github-actions bot commented Jun 24, 2022

🎉 This PR is included in version 13.0.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants