diff --git a/lib/gitWorkflow.js b/lib/gitWorkflow.js index c2127246f..128a96b13 100644 --- a/lib/gitWorkflow.js +++ b/lib/gitWorkflow.js @@ -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. @@ -82,6 +82,7 @@ class GitWorkflow { this.unstagedDiff = await this.execGit([ 'diff', + '--binary', '--unified=0', '--no-color', '--no-ext-diff', diff --git a/test/runAll.unmocked.spec.js b/test/runAll.unmocked.spec.js index 84d66a56a..affb64255 100644 --- a/test/runAll.unmocked.spec.js +++ b/test/runAll.unmocked.spec.js @@ -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) + }) })