@@ -747,6 +747,117 @@ Repository.prototype.mergeBranches = function(to, from, signature) {
747747 } ) ;
748748} ;
749749
750+ /**
751+ * Goes through a rebase's rebase operations and commits them if there are
752+ * no merge conflicts
753+ *
754+ * @param {Repository } repository The repository that the rebase is being
755+ * performed in
756+ * @param {Rebase } rebase The current rebase being performed
757+ * @param {Signature } signature Identity of the one performing the rebase
758+ * @return {Int|Index } An error code for an unsuccesful rebase or an index for
759+ * a rebase with conflicts
760+ */
761+ function performRebase ( repository , rebase , signature ) {
762+ return rebase . next ( )
763+ . then ( function ( rebaseOperation ) {
764+ return repository . openIndex ( )
765+ . then ( function ( index ) {
766+ if ( index . hasConflicts ( ) ) {
767+ throw index ;
768+ }
769+
770+ if ( rebaseOperation ) {
771+ rebase . commit ( null , signature ) ;
772+
773+ return performRebase ( repository , rebase , signature ) ;
774+ }
775+
776+ return rebase . finish ( signature ) ;
777+ } ) ;
778+ } ) ;
779+ }
780+
781+ /**
782+ * Rebases a branch onto another branch
783+ *
784+ * @param {String } branch
785+ * @param {String } upstream
786+ * @param {String } onto
787+ * @param {Signature } signature Identity of the one performing the rebase
788+ * @return {Oid|Index } A commit id for a succesful merge or an index for a
789+ * rebase with conflicts
790+ */
791+ Repository . prototype . rebaseBranches = function (
792+ branch ,
793+ upstream ,
794+ onto ,
795+ signature )
796+ {
797+ var repo = this ;
798+
799+ signature = signature || repo . defaultSignature ( ) ;
800+
801+ return Promise . all ( [
802+ repo . getReference ( branch ) ,
803+ upstream ? repo . getReference ( upstream ) : null ,
804+ onto ? repo . getReference ( upstream ) : null
805+ ] )
806+ . then ( function ( refs ) {
807+ return Promise . all ( [
808+ NodeGit . AnnotatedCommit . fromRef ( repo , refs [ 0 ] ) ,
809+ upstream ? NodeGit . AnnotatedCommit . fromRef ( repo , refs [ 1 ] ) : null ,
810+ onto ? NodeGit . AnnotatedCommit . fromRef ( repo , refs [ 2 ] ) : null
811+ ] ) ;
812+ } )
813+ . then ( function ( annotatedCommits ) {
814+ return NodeGit . Rebase . init ( repo , annotatedCommits [ 0 ] , annotatedCommits [ 1 ] ,
815+ annotatedCommits [ 2 ] , signature , null ) ;
816+ } )
817+ . then ( function ( rebase ) {
818+ return performRebase ( repo , rebase , signature ) ;
819+ } )
820+ . then ( function ( error ) {
821+ if ( error ) {
822+ throw error ;
823+ }
824+
825+ return repo . getBranchCommit ( "HEAD" ) ;
826+ } ) ;
827+ } ;
828+
829+ /**
830+ * Continues an existing rebase
831+ *
832+ * @param {Signature } signature Identity of the one performing the rebase
833+ * @return {Oid|Index } A commit id for a succesful merge or an index for a
834+ * rebase with conflicts
835+ */
836+ Repository . prototype . continueRebase = function ( signature ) {
837+ var repo = this ;
838+
839+ return repo . openIndex ( )
840+ . then ( function ( index ) {
841+ if ( index . hasConflicts ( ) ) {
842+ throw index ;
843+ }
844+
845+ return NodeGit . Rebase . open ( repo ) ;
846+ } )
847+ . then ( function ( rebase ) {
848+ rebase . commit ( null , signature ) ;
849+
850+ return performRebase ( repo , rebase , signature ) ;
851+ } )
852+ . then ( function ( error ) {
853+ if ( error ) {
854+ throw error ;
855+ }
856+
857+ return repo . getBranchCommit ( "HEAD" ) ;
858+ } ) ;
859+ } ;
860+
750861// Override Repository.initExt to normalize initoptions
751862var initExt = Repository . initExt ;
752863Repository . initExt = function ( repo_path , opts ) {
0 commit comments