@@ -852,17 +852,28 @@ static int rebase_next_inmemory(
852852 git_tree * current_tree = NULL , * head_tree = NULL , * parent_tree = NULL ;
853853 git_rebase_operation * operation ;
854854 git_index * index = NULL ;
855+ unsigned int parent_count ;
855856 int error ;
856857
857858 * out = NULL ;
858859
859860 operation = git_array_get (rebase -> operations , rebase -> current );
860861
861862 if ((error = git_commit_lookup (& current_commit , rebase -> repo , & operation -> id )) < 0 ||
862- (error = git_commit_tree (& current_tree , current_commit )) < 0 ||
863- (error = git_commit_parent (& parent_commit , current_commit , 0 )) < 0 ||
864- (error = git_commit_tree (& parent_tree , parent_commit )) < 0 ||
865- (error = git_commit_tree (& head_tree , rebase -> last_commit )) < 0 ||
863+ (error = git_commit_tree (& current_tree , current_commit )) < 0 )
864+ goto done ;
865+
866+ if ((parent_count = git_commit_parentcount (current_commit )) > 1 ) {
867+ giterr_set (GITERR_REBASE , "Cannot rebase a merge commit" );
868+ error = -1 ;
869+ goto done ;
870+ } else if (parent_count ) {
871+ if ((error = git_commit_parent (& parent_commit , current_commit , 0 )) < 0 ||
872+ (error = git_commit_tree (& parent_tree , parent_commit )) < 0 )
873+ goto done ;
874+ }
875+
876+ if ((error = git_commit_tree (& head_tree , rebase -> last_commit )) < 0 ||
866877 (error = git_merge_trees (& index , rebase -> repo , parent_tree , head_tree , current_tree , & rebase -> options .merge_options )) < 0 )
867878 goto done ;
868879
0 commit comments