@@ -12,7 +12,6 @@ import (
12
12
"os/signal"
13
13
"path/filepath"
14
14
"regexp"
15
- "sort"
16
15
"strings"
17
16
"text/tabwriter"
18
17
"time"
@@ -315,7 +314,7 @@ func detectNewCommits(ctx context.Context, logger *logrus.Entry, stagingDir, cen
315
314
return nil , fmt .Errorf ("failed to walk %s: %w" , stagingDir , err )
316
315
}
317
316
318
- var commits [] commit
317
+ commits := map [ string ][] commit {}
319
318
for repo , lastCommit := range lastCommits {
320
319
var remote string
321
320
switch mode {
@@ -365,7 +364,10 @@ func detectNewCommits(ctx context.Context, logger *logrus.Entry, stagingDir, cen
365
364
if err != nil {
366
365
return nil , fmt .Errorf ("invalid time %s: %w" , parts [1 ], err )
367
366
}
368
- commits = append (commits , commit {
367
+ if _ , ok := commits [repo ]; ! ok {
368
+ commits [repo ] = []commit {}
369
+ }
370
+ commits [repo ] = append (commits [repo ], commit {
369
371
Hash : parts [0 ],
370
372
Date : committedTime ,
371
373
Author : parts [2 ],
@@ -375,10 +377,43 @@ func detectNewCommits(ctx context.Context, logger *logrus.Entry, stagingDir, cen
375
377
}
376
378
}
377
379
}
378
- sort .Slice (commits , func (i , j int ) bool {
379
- return commits [i ].Date .Before (commits [j ].Date )
380
- })
381
- return commits , nil
380
+ // we would like to intertwine the commits from each upstream repository by date, while
381
+ // keeping the order of commits from any one repository in the order they were committed in
382
+ var orderedCommits []commit
383
+ indices := map [string ]int {}
384
+ for repo := range commits {
385
+ indices [repo ] = 0
386
+ }
387
+ for {
388
+ // find which repo's commit stack we should pop off to get the next earliest commit
389
+ nextTime := time .Now ()
390
+ var nextRepo string
391
+ for repo , index := range indices {
392
+ if commits [repo ][index ].Date .Before (nextTime ) {
393
+ nextTime = commits [repo ][index ].Date
394
+ nextRepo = repo
395
+ }
396
+ }
397
+
398
+ // pop the commit, add it to our list and do housekeeping for our index records
399
+ orderedCommits = append (orderedCommits , commits [nextRepo ][indices [nextRepo ]])
400
+ if indices [nextRepo ] == len (commits [nextRepo ])- 1 {
401
+ delete (indices , nextRepo )
402
+ } else {
403
+ indices [nextRepo ] += 1
404
+ }
405
+
406
+ if len (indices ) == 0 {
407
+ break
408
+ }
409
+ }
410
+
411
+ // our ordered list is descending, but we need to cherry-pick from the oldest first
412
+ var reversedCommits []commit
413
+ for i := range orderedCommits {
414
+ reversedCommits = append (reversedCommits , orderedCommits [len (orderedCommits )- i - 1 ])
415
+ }
416
+ return reversedCommits , nil
382
417
}
383
418
384
419
func isCommitMissing (ctx context.Context , logger * logrus.Entry , stagingDir string , c commit ) (bool , error ) {
0 commit comments