Skip to content

Commit

Permalink
Changed the way how the prefixes are compared in "gitStatusParser()" …
Browse files Browse the repository at this point in the history
…function.
  • Loading branch information
Kamil Piechaczek committed Aug 7, 2017
1 parent 54ae9d7 commit 92f66f6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/utils/gitstatusparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const DELETE_STAGED_SYMBOL = 'D ';
const DELETE_NOT_STAGED_SYMBOL = ' D';
const MODIFIED_STAGED_SYMBOL = 'M ';
const MODIFIED_NOT_STAGED_SYMBOL = ' M';
const MODIFIED_STAGED_AND_NOT_STAGED_SYMBOL = 'MM';
const RENAMED_STAGED_SYMBOL = 'R ';
const ADDED_STAGED_SYMBOL = 'A ';
const UNTRACKED_SYMBOL = '??';
Expand All @@ -33,12 +34,18 @@ module.exports = function gitStatusParser( response ) {

const branch = branchData.split( '...' )[ 0 ].match( /## (.*)$/ )[ 1 ];
const added = findFiles( [ ADDED_STAGED_SYMBOL ] );
const modified = findFiles( [ MODIFIED_STAGED_SYMBOL, MODIFIED_NOT_STAGED_SYMBOL ] );
const modified = findFiles( [ MODIFIED_STAGED_SYMBOL, MODIFIED_NOT_STAGED_SYMBOL, MODIFIED_STAGED_AND_NOT_STAGED_SYMBOL ] );
const deleted = findFiles( [ DELETE_STAGED_SYMBOL, DELETE_NOT_STAGED_SYMBOL ] );
const renamed = findFiles( [ RENAMED_STAGED_SYMBOL ] );
const unmerged = findFiles( UNMERGED_SYMBOLS );
const untracked = findFiles( [ UNTRACKED_SYMBOL ] );
const staged = findFiles( [ ADDED_STAGED_SYMBOL, DELETE_STAGED_SYMBOL, MODIFIED_STAGED_SYMBOL, RENAMED_STAGED_SYMBOL ] );
const staged = findFiles( [
ADDED_STAGED_SYMBOL,
DELETE_STAGED_SYMBOL,
MODIFIED_STAGED_SYMBOL,
MODIFIED_STAGED_AND_NOT_STAGED_SYMBOL,
RENAMED_STAGED_SYMBOL
] );

let behind = branchData.match( /behind (\d+)/ );
let ahead = branchData.match( /ahead (\d+)/ );
Expand All @@ -65,10 +72,8 @@ module.exports = function gitStatusParser( response ) {
};

function findFiles( prefixes ) {
const prefixesAsRegExp = new RegExp( `^${ prefixes.join( '|' ).replace( /\?/g, '\\?' ) } ` );

return responseAsArray
.filter( line => prefixes.some( prefix => line.trim().startsWith( prefix ) ) )
.map( line => line.replace( prefixesAsRegExp, '' ).trim() );
.filter( line => prefixes.some( prefix => prefix === line.substring( 0, 2 ) ) )
.map( line => line.slice( 2 ).trim() );
}
};
22 changes: 22 additions & 0 deletions tests/utils/gitstatusparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,27 @@ describe( 'utils', () => {
'tests/utils/helper.js',
] );
} );

it( 'returns a list with modified files (staged and not staged)', () => {
const gitStatusResponse = [
'## master...origin/master',
' M lib/index.js', // modified, not staged
'MM lib/tasks/logger.js', // modified, a part of the changes is staged
'M tests/index.js', // modified, the whole file is staged
].join( '\n' );

const status = gitStatusParser( gitStatusResponse );

expect( status.staged ).to.deep.equal( [
'lib/tasks/logger.js',
'tests/index.js'
] );

expect( status.modified ).to.deep.equal( [
'lib/index.js',
'lib/tasks/logger.js',
'tests/index.js'
] );
} );
} );
} );

0 comments on commit 92f66f6

Please sign in to comment.