/
depCheck.go
325 lines (254 loc) · 6.86 KB
/
depCheck.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package dep
import (
"fmt"
"os"
"strings"
"sync"
"github.com/leonelquinteros/gotext"
"github.com/Jguer/yay/v11/pkg/stringset"
"github.com/Jguer/yay/v11/pkg/text"
)
func (dp *Pool) checkInnerConflict(name, conflict string, conflicts stringset.MapStringSet) {
for _, pkg := range dp.Aur {
if pkg.Name == name {
continue
}
if satisfiesAur(conflict, pkg) {
conflicts.Add(name, pkg.Name)
}
}
for _, pkg := range dp.Repo {
if pkg.Name() == name {
continue
}
if satisfiesRepo(conflict, pkg, dp.AlpmExecutor) {
conflicts.Add(name, pkg.Name())
}
}
}
func (dp *Pool) checkForwardConflict(name, conflict string, conflicts stringset.MapStringSet) {
for _, pkg := range dp.AlpmExecutor.LocalPackages() {
if pkg.Name() == name || dp.hasPackage(pkg.Name()) {
continue
}
if satisfiesRepo(conflict, pkg, dp.AlpmExecutor) {
n := pkg.Name()
if n != conflict {
n += " (" + conflict + ")"
}
conflicts.Add(name, n)
}
}
}
func (dp *Pool) checkReverseConflict(name, conflict string, conflicts stringset.MapStringSet) {
for _, pkg := range dp.Aur {
if pkg.Name == name {
continue
}
if satisfiesAur(conflict, pkg) {
if name != conflict {
name += " (" + conflict + ")"
}
conflicts.Add(pkg.Name, name)
}
}
for _, pkg := range dp.Repo {
if pkg.Name() == name {
continue
}
if satisfiesRepo(conflict, pkg, dp.AlpmExecutor) {
if name != conflict {
name += " (" + conflict + ")"
}
conflicts.Add(pkg.Name(), name)
}
}
}
func (dp *Pool) checkInnerConflicts(conflicts stringset.MapStringSet) {
for _, pkg := range dp.Aur {
for _, conflict := range pkg.Conflicts {
dp.checkInnerConflict(pkg.Name, conflict, conflicts)
}
}
for _, pkg := range dp.Repo {
for _, conflict := range dp.AlpmExecutor.PackageConflicts(pkg) {
dp.checkInnerConflict(pkg.Name(), conflict.String(), conflicts)
}
}
}
func (dp *Pool) checkForwardConflicts(conflicts stringset.MapStringSet) {
for _, pkg := range dp.Aur {
for _, conflict := range pkg.Conflicts {
dp.checkForwardConflict(pkg.Name, conflict, conflicts)
}
}
for _, pkg := range dp.Repo {
for _, conflict := range dp.AlpmExecutor.PackageConflicts(pkg) {
dp.checkForwardConflict(pkg.Name(), conflict.String(), conflicts)
}
}
}
func (dp *Pool) checkReverseConflicts(conflicts stringset.MapStringSet) {
for _, pkg := range dp.AlpmExecutor.LocalPackages() {
if dp.hasPackage(pkg.Name()) {
continue
}
for _, conflict := range dp.AlpmExecutor.PackageConflicts(pkg) {
dp.checkReverseConflict(pkg.Name(), conflict.String(), conflicts)
}
}
}
func (dp *Pool) CheckConflicts(useAsk, noConfirm, noDeps bool) (stringset.MapStringSet, error) {
conflicts := make(stringset.MapStringSet)
if noDeps {
return conflicts, nil
}
var wg sync.WaitGroup
innerConflicts := make(stringset.MapStringSet)
wg.Add(2)
text.OperationInfoln(gotext.Get("Checking for conflicts..."))
go func() {
dp.checkForwardConflicts(conflicts)
dp.checkReverseConflicts(conflicts)
wg.Done()
}()
text.OperationInfoln(gotext.Get("Checking for inner conflicts..."))
go func() {
dp.checkInnerConflicts(innerConflicts)
wg.Done()
}()
wg.Wait()
if len(innerConflicts) != 0 {
text.Errorln(gotext.Get("\nInner conflicts found:"))
for name, pkgs := range innerConflicts {
str := text.SprintError(name + ":")
for pkg := range pkgs {
str += " " + text.Cyan(pkg) + ","
}
str = strings.TrimSuffix(str, ",")
fmt.Println(str)
}
}
if len(conflicts) != 0 {
text.Errorln(gotext.Get("\nPackage conflicts found:"))
for name, pkgs := range conflicts {
str := text.SprintError(gotext.Get("Installing %s will remove:", text.Cyan(name)))
for pkg := range pkgs {
str += " " + text.Cyan(pkg) + ","
}
str = strings.TrimSuffix(str, ",")
fmt.Println(str)
}
}
// Add the inner conflicts to the conflicts
// These are used to decide what to pass --ask to (if set) or don't pass --noconfirm to
// As we have no idea what the order is yet we add every inner conflict to the slice
for name, pkgs := range innerConflicts {
conflicts[name] = make(stringset.StringSet)
for pkg := range pkgs {
conflicts[pkg] = make(stringset.StringSet)
}
}
if len(conflicts) > 0 {
if !useAsk {
if noConfirm {
return nil, fmt.Errorf(gotext.Get("package conflicts can not be resolved with noconfirm, aborting"))
}
text.Errorln(gotext.Get("Conflicting packages will have to be confirmed manually"))
}
}
return conflicts, nil
}
type missing struct {
Good stringset.StringSet
Missing map[string][][]string
}
func (dp *Pool) _checkMissing(dep string, stack []string, missing *missing, noDeps, noCheckDeps bool) {
if missing.Good.Get(dep) {
return
}
if trees, ok := missing.Missing[dep]; ok {
for _, tree := range trees {
if stringSliceEqual(tree, stack) {
return
}
}
missing.Missing[dep] = append(missing.Missing[dep], stack)
return
}
if aurPkg := dp.findSatisfierAur(dep); aurPkg != nil {
missing.Good.Set(dep)
combinedDepList := ComputeCombinedDepList(aurPkg, noDeps, noCheckDeps)
for _, deps := range combinedDepList {
for _, aurDep := range deps {
if dp.AlpmExecutor.LocalSatisfierExists(aurDep) {
missing.Good.Set(aurDep)
continue
}
dp._checkMissing(aurDep, append(stack, aurPkg.Name), missing, noDeps, noCheckDeps)
}
}
return
}
if repoPkg := dp.findSatisfierRepo(dep); repoPkg != nil {
missing.Good.Set(dep)
if noDeps {
return
}
for _, dep := range dp.AlpmExecutor.PackageDepends(repoPkg) {
if dp.AlpmExecutor.LocalSatisfierExists(dep.String()) {
missing.Good.Set(dep.String())
continue
}
dp._checkMissing(dep.String(), append(stack, repoPkg.Name()), missing, noDeps, noCheckDeps)
}
return
}
missing.Missing[dep] = [][]string{stack}
}
func stringSliceEqual(a, b []string) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
func (dp *Pool) CheckMissing(noDeps, noCheckDeps bool) error {
missing := &missing{
make(stringset.StringSet),
make(map[string][][]string),
}
for _, target := range dp.Targets {
dp._checkMissing(target.DepString(), make([]string, 0), missing, noDeps, noCheckDeps)
}
if len(missing.Missing) == 0 {
return nil
}
text.Errorln(gotext.Get("Could not find all required packages:"))
for dep, trees := range missing.Missing {
for _, tree := range trees {
fmt.Fprintf(os.Stderr, "\t%s", text.Cyan(dep))
if len(tree) == 0 {
fmt.Fprint(os.Stderr, gotext.Get(" (Target"))
} else {
fmt.Fprint(os.Stderr, gotext.Get(" (Wanted by: "))
for n := 0; n < len(tree)-1; n++ {
fmt.Fprint(os.Stderr, text.Cyan(tree[n]), " -> ")
}
fmt.Fprint(os.Stderr, text.Cyan(tree[len(tree)-1]))
}
fmt.Fprintln(os.Stderr, ")")
}
}
return fmt.Errorf("")
}