@@ -331,19 +331,58 @@ export async function range(commit_group_map?: CommitGroupMap) {
331331}
332332
333333export function stack_order ( commit_range : CommitRange ) : CommitGroupList {
334- return [ ...commit_range . group_list ] ;
334+ const group_by_id = new Map ( commit_range . group_list . map ( ( group ) => [ group . id , group ] ) ) ;
335+ const children_by_base = new Map < string , CommitGroupList > ( ) ;
336+
337+ for ( const group of commit_range . group_list ) {
338+ if ( ! group . base || ! group_by_id . has ( group . base ) ) {
339+ continue ;
340+ }
341+
342+ const children = children_by_base . get ( group . base ) || [ ] ;
343+ children . push ( group ) ;
344+ children_by_base . set ( group . base , children ) ;
345+ }
346+
347+ const ordered_group_list : CommitGroupList = [ ] ;
348+ const visited = new Set < string > ( ) ;
349+
350+ function visit ( group : CommitGroupList [ number ] ) {
351+ if ( visited . has ( group . id ) ) {
352+ return ;
353+ }
354+
355+ visited . add ( group . id ) ;
356+ ordered_group_list . push ( group ) ;
357+
358+ for ( const child of children_by_base . get ( group . id ) || [ ] ) {
359+ visit ( child ) ;
360+ }
361+ }
362+
363+ for ( const group of commit_range . group_list ) {
364+ if ( ! group . base || ! group_by_id . has ( group . base ) ) {
365+ visit ( group ) ;
366+ }
367+ }
368+
369+ for ( const group of commit_range . group_list ) {
370+ visit ( group ) ;
371+ }
372+
373+ return ordered_group_list ;
335374}
336375
337376export function rebase_order ( commit_range : CommitRange ) : CommitGroupList {
338377 const state = Store . getState ( ) ;
339378 const actions = state . actions ;
340379
341- const reversed_group_list = stack_order ( commit_range ) . reverse ( ) ;
380+ const stack_group_list = stack_order ( commit_range ) ;
342381
343382 // order groups with group.master_base first
344383 const group_list_master : CommitGroupList = [ ] ;
345384 const group_list_others : CommitGroupList = [ ] ;
346- for ( const group of reversed_group_list ) {
385+ for ( const group of stack_group_list ) {
347386 if ( group . master_base ) {
348387 group_list_master . push ( group ) ;
349388 } else {
@@ -354,8 +393,8 @@ export function rebase_order(commit_range: CommitRange): CommitGroupList {
354393 const ordered_group_list = [ ...group_list_master , ...group_list_others ] ;
355394
356395 // detect if group list order differs
357- for ( let i = 0 ; i < reversed_group_list . length ; i ++ ) {
358- const original_group = reversed_group_list [ i ] ;
396+ for ( let i = 0 ; i < stack_group_list . length ; i ++ ) {
397+ const original_group = stack_group_list [ i ] ;
359398 const ordered_group = ordered_group_list [ i ] ;
360399 invariant ( original_group , "original_group must exist" ) ;
361400 invariant ( ordered_group , "ordered_group must exist" ) ;
0 commit comments