Skip to content

Commit 35da5f6

Browse files
scripts/bumper: improve sort order
While we want to use date to order commits between repositories, where there is no real order, we must not change the order of commits within a repo. Signed-off-by: Steve Kuznetsov <[email protected]>
1 parent 586ee40 commit 35da5f6

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

scripts/bumper/main.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"os/signal"
1313
"path/filepath"
1414
"regexp"
15-
"sort"
1615
"strings"
1716
"text/tabwriter"
1817
"time"
@@ -315,7 +314,7 @@ func detectNewCommits(ctx context.Context, logger *logrus.Entry, stagingDir, cen
315314
return nil, fmt.Errorf("failed to walk %s: %w", stagingDir, err)
316315
}
317316

318-
var commits []commit
317+
commits := map[string][]commit{}
319318
for repo, lastCommit := range lastCommits {
320319
var remote string
321320
switch mode {
@@ -365,7 +364,10 @@ func detectNewCommits(ctx context.Context, logger *logrus.Entry, stagingDir, cen
365364
if err != nil {
366365
return nil, fmt.Errorf("invalid time %s: %w", parts[1], err)
367366
}
368-
commits = append(commits, commit{
367+
if _, ok := commits[repo]; !ok {
368+
commits[repo] = []commit{}
369+
}
370+
commits[repo] = append(commits[repo], commit{
369371
Hash: parts[0],
370372
Date: committedTime,
371373
Author: parts[2],
@@ -375,10 +377,43 @@ func detectNewCommits(ctx context.Context, logger *logrus.Entry, stagingDir, cen
375377
}
376378
}
377379
}
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
382417
}
383418

384419
func isCommitMissing(ctx context.Context, logger *logrus.Entry, stagingDir string, c commit) (bool, error) {

0 commit comments

Comments
 (0)