Skip to content

Commit

Permalink
Merge pull request #1 from Ajnasz/list-staged-files
Browse files Browse the repository at this point in the history
Add staged option to be able to list files that are staged
  • Loading branch information
szabolcs-szilagyi committed Jul 2, 2018
2 parents 0886cbb + 39c94a2 commit 0a3a3c8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ Current Working Directory - this will tell the method, in which directory it sho
## groups [object]
For each key defined in the groups, the lib will try to find matching files. The value for they hey should either be a *method* or a *regexp*.

## staged [boolean]
List only files staged for next commit

## Result
It will be an object with list of files at the keys. For example I got the following, when ran the [above example](#example-usage) against this repo:
```
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ function formatExtraGroups (groups) {
module.exports = function gitFiles (options) {
options = options || {};

var files = execSync('git ls-files', {
var cmd = options.staged ?
'git diff --diff-filter ACMR --cached --name-only' :
'git ls-files';

var files = execSync(cmd, {
encoding: 'utf8',
cwd: options && options.cwd
})
Expand Down
15 changes: 15 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ function setup () {
exec('echo haha > some.test.js');
exec('echo hoho > some.js');
exec('git init');
exec('git config user.email foo@example.com');
exec('git config user.name "Gigiri Tikiri"');
exec('git add .');
exec('git commit --no-gpg-sign -am test');
exec('echo bello > staged.txt');
exec('git add .');
exec('echo foobar > baz.txt');
}

function teardown () {
Expand Down Expand Up @@ -112,3 +115,15 @@ describe('git-files', function () {
expect(custom.nothingInHere).to.have.lengthOf(0);
});
});

describe('git-staged-files', function () {
it('should list only files that are staged for the next commit', function () {
var custom = gitFiles({
cwd: TEMP_DIR,
staged: true,
});

expect(custom.all).to.not.have.members(['baz.txt']);
expect(custom.all).to.have.members(['staged.txt']);
});
});

0 comments on commit 0a3a3c8

Please sign in to comment.