This repository was archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathoutput_sort_utils.go
277 lines (220 loc) · 6.82 KB
/
output_sort_utils.go
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
Copyright 2018 Google, Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package util
import (
"sort"
pkgutil "github.com/GoogleContainerTools/container-diff/pkg/util"
)
var SortSize bool
type packageBy func(p1, p2 *PackageOutput) bool
func (by packageBy) Sort(packages []PackageOutput) {
ps := &packageSorter{
packages: packages,
by: by,
}
sort.Sort(ps)
}
type packageSorter struct {
packages []PackageOutput
by func(p1, p2 *PackageOutput) bool
}
func (s *packageSorter) Len() int {
return len(s.packages)
}
func (s *packageSorter) Less(i, j int) bool {
return s.by(&s.packages[i], &s.packages[j])
}
func (s *packageSorter) Swap(i, j int) {
s.packages[i], s.packages[j] = s.packages[j], s.packages[i]
}
// If packages have the same name, means they exist where multiple version of the same package are allowed,
// so sort by version. If they have the same version, then sort by size.
var packageNameSort = func(p1, p2 *PackageOutput) bool {
if p1.Name == p2.Name {
if p1.Version == p2.Version {
return p1.Size > p2.Size
}
return p1.Version < p2.Version
}
return p1.Name < p2.Name
}
// If packages have the same size, sort by name. If they are two versions of the same package, sort by version.
var packageSizeSort = func(p1, p2 *PackageOutput) bool {
if p1.Size == p2.Size {
if p1.Name == p2.Name {
return p1.Version < p2.Version
}
return p1.Name < p2.Name
}
return p1.Size > p2.Size
}
type singleInfoBy func(a, b *Info) bool
func (by singleInfoBy) Sort(packageDiffs []Info) {
ss := &singleVersionInfoSorter{
packageDiffs: packageDiffs,
by: by,
}
sort.Sort(ss)
}
type singleVersionInfoSorter struct {
packageDiffs []Info
by func(a, b *Info) bool
}
func (s *singleVersionInfoSorter) Len() int {
return len(s.packageDiffs)
}
func (s *singleVersionInfoSorter) Less(i, j int) bool {
return s.by(&s.packageDiffs[i], &s.packageDiffs[j])
}
func (s *singleVersionInfoSorter) Swap(i, j int) {
s.packageDiffs[i], s.packageDiffs[j] = s.packageDiffs[j], s.packageDiffs[i]
}
var singleInfoNameSort = func(a, b *Info) bool {
return a.Package < b.Package
}
// Sorts MultiVersionInfos by package instance with the largest size in the first image, in descending order
var singleInfoSizeSort = func(a, b *Info) bool {
return a.Info1.Size > b.Info1.Size
}
type multiInfoBy func(a, b *MultiVersionInfo) bool
func (by multiInfoBy) Sort(packageDiffs []MultiVersionInfo) {
ms := &multiVersionInfoSorter{
packageDiffs: packageDiffs,
by: by,
}
sort.Sort(ms)
}
type multiVersionInfoSorter struct {
packageDiffs []MultiVersionInfo
by func(a, b *MultiVersionInfo) bool
}
func (s *multiVersionInfoSorter) Len() int {
return len(s.packageDiffs)
}
func (s *multiVersionInfoSorter) Less(i, j int) bool {
return s.by(&s.packageDiffs[i], &s.packageDiffs[j])
}
func (s *multiVersionInfoSorter) Swap(i, j int) {
s.packageDiffs[i], s.packageDiffs[j] = s.packageDiffs[j], s.packageDiffs[i]
}
var multiInfoNameSort = func(a, b *MultiVersionInfo) bool {
return a.Package < b.Package
}
// Sorts MultiVersionInfos by package instance with the largest size in the first image, in descending order
var multiInfoSizeSort = func(a, b *MultiVersionInfo) bool {
aInfo1 := a.Info1
bInfo1 := b.Info1
// For each package, sorts the infos of the first image's instances of that package in descending order
sort.Sort(packageInfoBySize(aInfo1))
sort.Sort(packageInfoBySize(bInfo1))
// Compares the largest size instances of each package in the first image
return aInfo1[0].Size > bInfo1[0].Size
}
type packageInfoBySize []PackageInfo
func (infos packageInfoBySize) Len() int {
return len(infos)
}
func (infos packageInfoBySize) Swap(i, j int) {
infos[i], infos[j] = infos[j], infos[i]
}
func (infos packageInfoBySize) Less(i, j int) bool {
if infos[i].Size == infos[j].Size {
return infos[i].Version < infos[j].Version
}
return infos[i].Size > infos[j].Size
}
type packageInfoByVersion []PackageInfo
func (infos packageInfoByVersion) Len() int {
return len(infos)
}
func (infos packageInfoByVersion) Swap(i, j int) {
infos[i], infos[j] = infos[j], infos[i]
}
func (infos packageInfoByVersion) Less(i, j int) bool {
if infos[i].Version == infos[j].Version {
return infos[i].Size > infos[j].Size
}
return infos[i].Version < infos[j].Version
}
type directoryBy func(e1, e2 *pkgutil.DirectoryEntry) bool
func (by directoryBy) Sort(entries []pkgutil.DirectoryEntry) {
ds := &directorySorter{
entries: entries,
by: by,
}
sort.Sort(ds)
}
type directorySorter struct {
entries []pkgutil.DirectoryEntry
by func(p1, p2 *pkgutil.DirectoryEntry) bool
}
func (s *directorySorter) Len() int {
return len(s.entries)
}
func (s *directorySorter) Less(i, j int) bool {
return s.by(&s.entries[i], &s.entries[j])
}
func (s *directorySorter) Swap(i, j int) {
s.entries[i], s.entries[j] = s.entries[j], s.entries[i]
}
var directoryNameSort = func(e1, e2 *pkgutil.DirectoryEntry) bool {
return e1.Name < e2.Name
}
// If directory entries have the same size, sort by name.
var directorySizeSort = func(e1, e2 *pkgutil.DirectoryEntry) bool {
if e1.Size == e2.Size {
return e1.Name < e2.Name
}
return e1.Size > e2.Size
}
func sortDirDiff(diff DirDiff) DirDiff {
adds, dels, mods := diff.Adds, diff.Dels, diff.Mods
if SortSize {
directoryBy(directorySizeSort).Sort(adds)
directoryBy(directorySizeSort).Sort(dels)
entryDiffBy(entryDiffSizeSort).Sort(mods)
} else {
directoryBy(directoryNameSort).Sort(adds)
directoryBy(directoryNameSort).Sort(dels)
entryDiffBy(entryDiffSizeSort).Sort(mods)
}
return DirDiff{adds, dels, mods}
}
type entryDiffBy func(a, b *EntryDiff) bool
func (by entryDiffBy) Sort(entryDiffs []EntryDiff) {
ds := &entryDiffSorter{
entryDiffs: entryDiffs,
by: by,
}
sort.Sort(ds)
}
type entryDiffSorter struct {
entryDiffs []EntryDiff
by func(a, b *EntryDiff) bool
}
func (s *entryDiffSorter) Len() int {
return len(s.entryDiffs)
}
func (s *entryDiffSorter) Less(i, j int) bool {
return s.by(&s.entryDiffs[i], &s.entryDiffs[j])
}
func (s *entryDiffSorter) Swap(i, j int) {
s.entryDiffs[i], s.entryDiffs[j] = s.entryDiffs[j], s.entryDiffs[i]
}
var entryDiffNameSort = func(a, b *EntryDiff) bool {
return a.Name < b.Name
}
// Sorts by size of the files in the first image, in descending order
var entryDiffSizeSort = func(a, b *EntryDiff) bool {
return a.Size1 > b.Size1
}