Skip to content

Commit

Permalink
fix: only run on staged files when using --staged (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
pshrmn authored and azz committed Jan 15, 2018
1 parent c1e7561 commit 57f09b7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
8 changes: 5 additions & 3 deletions src/__tests__/scm-git.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const mockGitFs = () => {
case 'ls-files':
return { stdout: '' };
case 'diff':
return { stdout: './foo.js\n' + './bar.md\n' };
return args[2] === '--cached'
? { stdout: './foo.js\n' }
: { stdout: './foo.js\n' + './bar.md\n' };
case 'add':
return { stdout: '' };
default:
Expand Down Expand Up @@ -149,15 +151,15 @@ describe('with git', () => {
expect(fs.readFileSync('/bar.md', 'utf8')).toEqual('formatted:# foo');
});

test('with --staged stages changed files', () => {
test('with --staged stages staged files', () => {
mockGitFs();

prettyQuick('root', { since: 'banana', staged: true });

expect(execa.sync).toHaveBeenCalledWith('git', ['add', './foo.js'], {
cwd: '/',
});
expect(execa.sync).toHaveBeenCalledWith('git', ['add', './bar.md'], {
expect(execa.sync).not.toHaveBeenCalledWith('git', ['add', './bar.md'], {
cwd: '/',
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default (
onFoundSinceRevision && onFoundSinceRevision(scm.name, revision);

const changedFiles = scm
.getChangedFiles(directory, revision)
.getChangedFiles(directory, revision, staged)
.filter(isSupportedExtension)
.filter(createIgnorer(directory));

Expand Down
31 changes: 17 additions & 14 deletions src/scms/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,23 @@ export const getSinceRevision = (directory, { staged }) => {
return runGit(directory, ['rev-parse', '--short', revision]).stdout.trim();
};

export const getChangedFiles = (directory, revision) => {
return [
...getLines(
runGit(directory, [
'diff',
'--name-only',
'--diff-filter=ACMRTUB',
revision,
])
),
...getLines(
runGit(directory, ['ls-files', '--others', '--exclude-standard'])
),
].filter(Boolean);
export const getChangedFiles = (directory, revision, staged) => {
return (staged
? getLines(runGit(directory, ['diff', '--name-only', '--cached', revision]))
: [
...getLines(
runGit(directory, [
'diff',
'--name-only',
'--diff-filter=ACMRTUB',
revision,
])
),
...getLines(
runGit(directory, ['ls-files', '--others', '--exclude-standard'])
),
]
).filter(Boolean);
};

export const stageFile = (directory, file) => {
Expand Down

0 comments on commit 57f09b7

Please sign in to comment.