2019-12-02 10:00:32 +01:00
|
|
|
package gerrit
|
2019-11-18 15:40:46 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
2019-12-02 10:00:32 +01:00
|
|
|
"github.com/apex/log"
|
2019-11-18 15:40:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// AssembleSeries consumes a list of `Changeset`, and groups them together to series
|
|
|
|
//
|
2019-12-02 10:00:32 +01:00
|
|
|
// We initially put every Changeset in its own Serie
|
|
|
|
//
|
2019-11-18 15:40:46 +01:00
|
|
|
// As we have no control over the order of the passed changesets,
|
2019-12-02 10:00:32 +01:00
|
|
|
// we maintain a lookup table, mapLeafToSerie,
|
|
|
|
// which allows to lookup a serie by its leaf commit id
|
|
|
|
// We concat series in a fixpoint approach
|
|
|
|
// because both appending and prepending is much more complex.
|
|
|
|
// Concatenation moves changesets of the later changeset in the previous one
|
|
|
|
// in a cleanup phase, we remove orphaned series (those without any changesets inside)
|
|
|
|
// afterwards, we do an integrity check, just to be on the safe side.
|
2019-12-03 23:54:41 +01:00
|
|
|
func AssembleSeries(changesets []*Changeset, logger *log.Logger) ([]*Serie, error) {
|
2019-11-18 15:40:46 +01:00
|
|
|
series := make([]*Serie, 0)
|
|
|
|
mapLeafToSerie := make(map[string]*Serie, 0)
|
|
|
|
|
|
|
|
for _, changeset := range changesets {
|
2019-12-03 23:54:41 +01:00
|
|
|
l := logger.WithField("changeset", changeset.String())
|
2019-11-18 15:40:46 +01:00
|
|
|
|
2019-12-03 23:54:41 +01:00
|
|
|
l.Debug("creating initial serie")
|
2019-11-18 15:40:46 +01:00
|
|
|
serie := &Serie{
|
2019-12-02 10:00:32 +01:00
|
|
|
ChangeSets: []*Changeset{changeset},
|
2019-11-18 15:40:46 +01:00
|
|
|
}
|
|
|
|
series = append(series, serie)
|
|
|
|
mapLeafToSerie[changeset.CommitID] = serie
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combine series using a fixpoint approach, with a max iteration count.
|
2019-12-03 23:54:41 +01:00
|
|
|
logger.Debug("glueing together phase")
|
2019-11-18 15:40:46 +01:00
|
|
|
for i := 1; i < 100; i++ {
|
|
|
|
didUpdate := false
|
2019-12-03 23:54:41 +01:00
|
|
|
logger.Debugf("at iteration %d", i)
|
|
|
|
for j, serie := range series {
|
|
|
|
l := logger.WithFields(log.Fields{
|
|
|
|
"i": i,
|
|
|
|
"j": j,
|
|
|
|
"serie": serie.String(),
|
|
|
|
})
|
2019-11-18 15:40:46 +01:00
|
|
|
parentCommitIDs, err := serie.GetParentCommitIDs()
|
|
|
|
if err != nil {
|
|
|
|
return series, err
|
|
|
|
}
|
|
|
|
if len(parentCommitIDs) != 1 {
|
|
|
|
// We can't append merge commits to other series
|
2019-12-03 23:54:41 +01:00
|
|
|
l.Infof("No single parent, skipping.")
|
2019-11-18 15:40:46 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
parentCommitID := parentCommitIDs[0]
|
2019-12-03 23:54:41 +01:00
|
|
|
l.Debug("Looking for a predecessor.")
|
2019-11-18 15:40:46 +01:00
|
|
|
// if there's another serie that has this parent as a leaf, glue together
|
|
|
|
if otherSerie, ok := mapLeafToSerie[parentCommitID]; ok {
|
|
|
|
if otherSerie == serie {
|
|
|
|
continue
|
|
|
|
}
|
2019-12-03 23:54:41 +01:00
|
|
|
l = l.WithField("otherSerie", otherSerie)
|
2019-11-18 15:40:46 +01:00
|
|
|
|
|
|
|
myLeafCommitID, err := serie.GetLeafCommitID()
|
|
|
|
if err != nil {
|
|
|
|
return series, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// append our changesets to the other serie
|
2019-12-03 23:54:41 +01:00
|
|
|
l.Debug("Splicing together.")
|
2019-11-18 15:40:46 +01:00
|
|
|
otherSerie.ChangeSets = append(otherSerie.ChangeSets, serie.ChangeSets...)
|
|
|
|
|
|
|
|
delete(mapLeafToSerie, parentCommitID)
|
|
|
|
mapLeafToSerie[myLeafCommitID] = otherSerie
|
|
|
|
|
|
|
|
// orphan our serie
|
2019-12-02 10:00:32 +01:00
|
|
|
serie.ChangeSets = []*Changeset{}
|
2019-11-18 15:40:46 +01:00
|
|
|
// remove the orphaned serie from the lookup table
|
|
|
|
delete(mapLeafToSerie, myLeafCommitID)
|
|
|
|
|
|
|
|
didUpdate = true
|
|
|
|
} else {
|
2019-12-03 23:54:41 +01:00
|
|
|
l.Debug("Not found.")
|
2019-11-18 15:40:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
series = removeOrphanedSeries(series)
|
|
|
|
if !didUpdate {
|
2019-12-03 23:54:41 +01:00
|
|
|
logger.Infof("converged after %d iterations", i)
|
2019-11-18 15:40:46 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check integrity, just to be on the safe side.
|
|
|
|
for _, serie := range series {
|
2019-12-03 23:54:41 +01:00
|
|
|
l := logger.WithField("serie", serie.String())
|
|
|
|
l.Debugf("checking integrity")
|
2019-11-18 15:40:46 +01:00
|
|
|
err := serie.CheckIntegrity()
|
|
|
|
if err != nil {
|
2019-12-03 23:54:41 +01:00
|
|
|
l.Errorf("checking integrity failed: %s", err)
|
2019-11-18 15:40:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|