From f271fb03ddd3dbb0c87d49a9483ef7376052b242 Mon Sep 17 00:00:00 2001 From: Kamil Piechaczek Date: Mon, 13 Feb 2017 15:08:17 +0100 Subject: [PATCH] Fix: Prevent to checking out to non-existing branch. --- lib/commands/update.js | 19 ++++++++-------- tests/commands/update.js | 49 +++++++++++++++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/lib/commands/update.js b/lib/commands/update.js index 83a784e..bc88da9 100644 --- a/lib/commands/update.js +++ b/lib/commands/update.js @@ -60,13 +60,16 @@ module.exports = { log.concat( response.logs ); } ) .then( () => { - return execCommand.execute( getExecData( `git checkout ${ data.repository.branch }` ) ); + return execCommand.execute( getExecData( `git checkout ${ data.repository.branch }` ) ) + .catch( ( response ) => { + throw new Error( response.logs.error[ 0 ].replace( /^error\: /, '' ) ); + } ); } ) .then( ( response ) => { log.concat( response.logs ); } ) .then( () => { - return execCommand.execute( getExecData( 'git branch -a' ) ); + return execCommand.execute( getExecData( 'git branch' ) ); } ) .then( ( response ) => { const stdout = response.logs.info.join( '\n' ).trim(); @@ -79,14 +82,10 @@ module.exports = { return resolve( { logs: log.all() } ); } - const isRemoteBranchAvailableRegexp = new RegExp( `remotes\\\/origin\\\/${ data.repository.branch }` ); - - // Check whether the remote branch is available. - if ( !stdout.match( isRemoteBranchAvailableRegexp ) ) { - throw new Error( `Branch "${ data.repository.branch }" is not available on server.` ); - } - - return execCommand.execute( getExecData( `git pull origin ${ data.repository.branch }` ) ); + return execCommand.execute( getExecData( `git pull origin ${ data.repository.branch }` ) ) + .catch( ( response ) => { + throw new Error( response.logs.error[ 0 ] ); + } ); } ) .then( ( response ) => { log.concat( response.logs ); diff --git a/tests/commands/update.js b/tests/commands/update.js index e674739..b45539a 100644 --- a/tests/commands/update.js +++ b/tests/commands/update.js @@ -113,7 +113,7 @@ describe( 'commands/update', () => { expect( exec.getCall( 0 ).args[ 0 ].arguments[ 0 ] ).to.equal( 'git status -s' ); expect( exec.getCall( 1 ).args[ 0 ].arguments[ 0 ] ).to.equal( 'git fetch' ); expect( exec.getCall( 2 ).args[ 0 ].arguments[ 0 ] ).to.equal( 'git checkout master' ); - expect( exec.getCall( 3 ).args[ 0 ].arguments[ 0 ] ).to.equal( 'git branch -a' ); + expect( exec.getCall( 3 ).args[ 0 ].arguments[ 0 ] ).to.equal( 'git branch' ); expect( exec.getCall( 4 ).args[ 0 ].arguments[ 0 ] ).to.equal( 'git pull origin master' ); expect( response.logs.info ).to.deep.equal( [ @@ -125,7 +125,7 @@ describe( 'commands/update', () => { } ); } ); - it( 'aborts if package has uncommmitted changes', () => { + it( 'aborts if package has uncommitted changes', () => { stubs.fs.existsSync.returns( true ); const exec = stubs.execCommand.execute; @@ -186,7 +186,7 @@ describe( 'commands/update', () => { } ); } ); - it( 'aborts if a remote branch does not exist anymore', () => { + it( 'aborts if user wants to pull changes from non-existing branch', () => { stubs.fs.existsSync.returns( true ); data.repository.branch = 'develop'; @@ -206,7 +206,11 @@ describe( 'commands/update', () => { } ) ); exec.onCall( 3 ).returns( Promise.resolve( { - logs: getCommandLogs( '* develop\n master\n remotes/origin/master' ) + logs: getCommandLogs( '* develop' ) + } ) ); + + exec.onCall( 4 ).returns( Promise.reject( { + logs: getCommandLogs( 'fatal: Couldn\'t find remote ref develop', true ) } ) ); return updateCommand.execute( data ) @@ -219,10 +223,43 @@ describe( 'commands/update', () => { 'Already on \'develop\'.' ] ); - const errMsg = 'Error: Branch "develop" is not available on server.'; + const errMsg = 'Error: fatal: Couldn\'t find remote ref develop'; + expect( response.logs.error[ 0 ].split( '\n' )[ 0 ] ).to.equal( errMsg ); + + expect( exec.callCount ).to.equal( 5 ); + } + ); + } ); + + it( 'aborts if user wants to check out to non-existing branch', () => { + stubs.fs.existsSync.returns( true ); + + data.repository.branch = 'non-existing-branch'; + + const exec = stubs.execCommand.execute; + + exec.onCall( 0 ).returns( Promise.resolve( { + logs: getCommandLogs( '' ) + } ) ); + + exec.onCall( 1 ).returns( Promise.resolve( { + logs: getCommandLogs( '' ) + } ) ); + + exec.onCall( 2 ).returns( Promise.reject( { + logs: getCommandLogs( 'error: pathspec \'ggdfgd\' did not match any file(s) known to git.', true ), + } ) ); + + return updateCommand.execute( data ) + .then( + () => { + throw new Error( 'Supposed to be rejected.' ); + }, + ( response ) => { + const errMsg = 'Error: pathspec \'ggdfgd\' did not match any file(s) known to git.'; expect( response.logs.error[ 0 ].split( '\n' )[ 0 ] ).to.equal( errMsg ); - expect( exec.callCount ).to.equal( 4 ); + expect( exec.callCount ).to.equal( 3 ); } ); } );