Skip to content

Commit

Permalink
fix: support binary files
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Nov 14, 2019
1 parent cfde9ca commit 7b3a334
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/gitWorkflow.js
Expand Up @@ -71,7 +71,7 @@ class GitWorkflow {
// Get diff of staged modifications. This will be applied back to the index. after stashing all changes.
// The `git stash save --keep-index` option cannot be used since it resurrects deleted files on
// git versions before v2.23.0 (https://github.com/git/git/blob/master/Documentation/RelNotes/2.23.0.txt#L322)
const stagedDiff = await this.execGit(['diff', '--cached'])
const stagedDiff = await this.execGit(['diff', '--binary', '--cached'])

// Save stash of entire original state, including unstaged and untracked changes.
// This should remove all changes from the index.
Expand All @@ -82,6 +82,7 @@ class GitWorkflow {

this.unstagedDiff = await this.execGit([
'diff',
'--binary',
'--unified=0',
'--no-color',
'--no-ext-diff',
Expand Down
17 changes: 17 additions & 0 deletions test/runAll.unmocked.spec.js
Expand Up @@ -516,4 +516,21 @@ describe('runAll', () => {
const exists = await fs.exists(readmeFile)
expect(exists).toEqual(false)
})

it('should handle binary files', async () => {
// mark test.js as binary file
await appendFile('.gitattributes', 'test.js binary\n')

// Stage pretty file
await appendFile('test.js', testJsFilePretty)
await execGit(['add', 'test.js'])

// Run lint-staged with `prettier --list-different` and commit pretty file
await gitCommit({ config: { '*.js': 'prettier --list-different' } })

// Nothing is wrong, so a new commit is created
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)
})
})

0 comments on commit 7b3a334

Please sign in to comment.