Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Use internal execGit to list staged files (#624)
- Loading branch information
Showing
9 changed files
with
103 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict' | ||
|
||
const execGit = require('./execGit') | ||
|
||
module.exports = async function getStagedFiles(options) { | ||
try { | ||
const lines = await execGit(['diff', '--staged', '--diff-filter=ACM', '--name-only'], options) | ||
return lines ? lines.split('\n') : [] | ||
} catch (error) { | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import getStagedFiles from '../src/getStagedFiles' | ||
import execGit from '../src/execGit' | ||
|
||
jest.mock('../src/execGit') | ||
|
||
describe('getStagedFiles', () => { | ||
it('should return array of file names', async () => { | ||
execGit.mockImplementationOnce(async () => 'foo.js\nbar.js') | ||
const staged = await getStagedFiles() | ||
expect(staged).toEqual(['foo.js', 'bar.js']) | ||
}) | ||
|
||
it('should return empty array when no staged files', async () => { | ||
execGit.mockImplementationOnce(async () => '') | ||
const staged = await getStagedFiles() | ||
expect(staged).toEqual([]) | ||
}) | ||
|
||
it('should return null in case of error', async () => { | ||
execGit.mockImplementationOnce(async () => { | ||
throw new Error('fatal: not a git repository (or any of the parent directories): .git') | ||
}) | ||
const staged = await getStagedFiles() | ||
expect(staged).toEqual(null) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.