1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
package submitqueue
import (
"sort"
"github.com/tweag/gerrit-queue/gerrit"
log "github.com/sirupsen/logrus"
)
// AssembleSeries consumes a list of `Changeset`, and groups them together to series
//
// As we have no control over the order of the passed changesets,
// we maintain two lookup tables,
// mapLeafToSerie, which allows to lookup a serie by its leaf commit id,
// to append to an existing serie
// and mapParentToSeries, which allows to lookup all series having a certain parent commit id,
// to prepend to any of the existing series
// if we can't find anything, we create a new series
func AssembleSeries(changesets []*gerrit.Changeset) ([]*Serie, error) {
series := make([]*Serie, 0)
mapLeafToSerie := make(map[string]*Serie, 0)
for _, changeset := range changesets {
logger := log.WithFields(log.Fields{
"changeset": changeset.String(),
})
logger.Debug("creating initial serie")
serie := &Serie{
ChangeSets: []*gerrit.Changeset{changeset},
}
series = append(series, serie)
mapLeafToSerie[changeset.CommitID] = serie
}
// Combine series using a fixpoint approach, with a max iteration count.
log.Debug("glueing together phase")
for i := 1; i < 100; i++ {
didUpdate := false
log.Debugf("at iteration %d", i)
for _, serie := range series {
logger := log.WithField("serie", serie.String())
parentCommitIDs, err := serie.GetParentCommitIDs()
if err != nil {
return series, err
}
if len(parentCommitIDs) != 1 {
// We can't append merge commits to other series
logger.Infof("No single parent, skipping.")
continue
}
parentCommitID := parentCommitIDs[0]
logger.Debug("Looking for a predecessor.")
// if there's another serie that has this parent as a leaf, glue together
if otherSerie, ok := mapLeafToSerie[parentCommitID]; ok {
if otherSerie == serie {
continue
}
logger := logger.WithField("otherSerie", otherSerie)
myLeafCommitID, err := serie.GetLeafCommitID()
if err != nil {
return series, err
}
// append our changesets to the other serie
logger.Debug("Splicing together.")
otherSerie.ChangeSets = append(otherSerie.ChangeSets, serie.ChangeSets...)
delete(mapLeafToSerie, parentCommitID)
mapLeafToSerie[myLeafCommitID] = otherSerie
// orphan our serie
serie.ChangeSets = []*gerrit.Changeset{}
// remove the orphaned serie from the lookup table
delete(mapLeafToSerie, myLeafCommitID)
didUpdate = true
} else {
logger.Debug("Not found.")
}
}
series = removeOrphanedSeries(series)
if !didUpdate {
log.Infof("converged after %d iterations", i)
break
}
}
// Check integrity, just to be on the safe side.
for _, serie := range series {
logger := log.WithFields(log.Fields{
"serie": serie.String(),
})
logger.Debugf("checking integrity")
err := serie.CheckIntegrity()
if err != nil {
logger.Errorf("checking integrity failed: %s", err)
}
}
return series, nil
}
// removeOrphanedSeries removes all empty series (that contain zero changesets)
func removeOrphanedSeries(series []*Serie) []*Serie {
newSeries := []*Serie{}
for _, serie := range series {
if len(serie.ChangeSets) != 0 {
newSeries = append(newSeries, serie)
}
}
return newSeries
}
// SortSeries sorts a list of series by the number of changesets in each serie, descending
func SortSeries(series []*Serie) []*Serie {
newSeries := make([]*Serie, len(series))
copy(newSeries, series)
sort.Slice(newSeries, func(i, j int) bool {
// the weight depends on the amount of changesets series changeset size
return len(series[i].ChangeSets) > len(series[j].ChangeSets)
})
return newSeries
}
|