-
-
Notifications
You must be signed in to change notification settings - Fork 365
/
Copy pathdownload.go
321 lines (273 loc) · 8.53 KB
/
download.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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"github.com/leonelquinteros/gotext"
"github.com/pkg/errors"
alpm "github.com/Jguer/go-alpm/v2"
"github.com/Jguer/yay/v10/pkg/db"
"github.com/Jguer/yay/v10/pkg/dep"
"github.com/Jguer/yay/v10/pkg/multierror"
"github.com/Jguer/yay/v10/pkg/query"
"github.com/Jguer/yay/v10/pkg/text"
)
const gitDiffRefName = "AUR_SEEN"
// Update the YAY_DIFF_REVIEW ref to HEAD. We use this ref to determine which diff were
// reviewed by the user
func gitUpdateSeenRef(path, name string) error {
_, stderr, err := config.Runtime.CmdRunner.Capture(
config.Runtime.CmdBuilder.BuildGitCmd(
filepath.Join(path, name), "update-ref", gitDiffRefName, "HEAD"), 0)
if err != nil {
return fmt.Errorf("%s %s", stderr, err)
}
return nil
}
// Return wether or not we have reviewed a diff yet. It checks for the existence of
// YAY_DIFF_REVIEW in the git ref-list
func gitHasLastSeenRef(path, name string) bool {
_, _, err := config.Runtime.CmdRunner.Capture(
config.Runtime.CmdBuilder.BuildGitCmd(
filepath.Join(path, name), "rev-parse", "--quiet", "--verify", gitDiffRefName), 0)
return err == nil
}
// Returns the last reviewed hash. If YAY_DIFF_REVIEW exists it will return this hash.
// If it does not it will return empty tree as no diff have been reviewed yet.
func getLastSeenHash(path, name string) (string, error) {
if gitHasLastSeenRef(path, name) {
stdout, stderr, err := config.Runtime.CmdRunner.Capture(
config.Runtime.CmdBuilder.BuildGitCmd(
filepath.Join(path, name), "rev-parse", gitDiffRefName), 0)
if err != nil {
return "", fmt.Errorf("%s %s", stderr, err)
}
lines := strings.Split(stdout, "\n")
return lines[0], nil
}
return gitEmptyTree, nil
}
// Check whether or not a diff exists between the last reviewed diff and
// HEAD@{upstream}
func gitHasDiff(path, name string) (bool, error) {
if gitHasLastSeenRef(path, name) {
stdout, stderr, err := config.Runtime.CmdRunner.Capture(
config.Runtime.CmdBuilder.BuildGitCmd(filepath.Join(path, name), "rev-parse", gitDiffRefName, "HEAD@{upstream}"), 0)
if err != nil {
return false, fmt.Errorf("%s%s", stderr, err)
}
lines := strings.Split(stdout, "\n")
lastseen := lines[0]
upstream := lines[1]
return lastseen != upstream, nil
}
// If YAY_DIFF_REVIEW does not exists, we have never reviewed a diff for this package
// and should display it.
return true, nil
}
// TODO: yay-next passes args through the header, use that to unify ABS and AUR
func gitDownloadABS(url, path, name string) (bool, error) {
if err := os.MkdirAll(path, 0o755); err != nil {
return false, err
}
if _, errExist := os.Stat(filepath.Join(path, name)); os.IsNotExist(errExist) {
cmd := config.Runtime.CmdBuilder.BuildGitCmd(path, "clone", "--no-progress", "--single-branch",
"-b", "packages/"+name, url, name)
_, stderr, err := config.Runtime.CmdRunner.Capture(cmd, 0)
if err != nil {
return false, fmt.Errorf(gotext.Get("error cloning %s: %s", name, stderr))
}
return true, nil
} else if errExist != nil {
return false, fmt.Errorf(gotext.Get("error reading %s", filepath.Join(path, name, ".git")))
}
cmd := config.Runtime.CmdBuilder.BuildGitCmd(filepath.Join(path, name), "pull", "--ff-only")
_, stderr, err := config.Runtime.CmdRunner.Capture(cmd, 0)
if err != nil {
return false, fmt.Errorf(gotext.Get("error fetching %s: %s", name, stderr))
}
return true, nil
}
func gitDownload(url, path, name string) (bool, error) {
_, err := os.Stat(filepath.Join(path, name, ".git"))
if os.IsNotExist(err) {
cmd := config.Runtime.CmdBuilder.BuildGitCmd(path, "clone", "--no-progress", url, name)
_, stderr, errCapture := config.Runtime.CmdRunner.Capture(cmd, 0)
if errCapture != nil {
return false, fmt.Errorf(gotext.Get("error cloning %s: %s", name, stderr))
}
return true, nil
} else if err != nil {
return false, fmt.Errorf(gotext.Get("error reading %s", filepath.Join(path, name, ".git")))
}
cmd := config.Runtime.CmdBuilder.BuildGitCmd(filepath.Join(path, name), "fetch")
_, stderr, err := config.Runtime.CmdRunner.Capture(cmd, 0)
if err != nil {
return false, fmt.Errorf(gotext.Get("error fetching %s: %s", name, stderr))
}
return false, nil
}
func gitMerge(path, name string) error {
_, stderr, err := config.Runtime.CmdRunner.Capture(
config.Runtime.CmdBuilder.BuildGitCmd(
filepath.Join(path, name), "reset", "--hard", "HEAD"), 0)
if err != nil {
return fmt.Errorf(gotext.Get("error resetting %s: %s", name, stderr))
}
_, stderr, err = config.Runtime.CmdRunner.Capture(
config.Runtime.CmdBuilder.BuildGitCmd(
filepath.Join(path, name), "merge", "--no-edit", "--ff"), 0)
if err != nil {
return fmt.Errorf(gotext.Get("error merging %s: %s", name, stderr))
}
return nil
}
func getPkgbuilds(pkgs []string, dbExecutor db.Executor, force bool) error {
missing := false
wd, err := os.Getwd()
if err != nil {
return err
}
pkgs = query.RemoveInvalidTargets(pkgs, config.Runtime.Mode)
aur, repo := packageSlices(pkgs, dbExecutor)
for n := range aur {
_, pkg := text.SplitDBFromName(aur[n])
aur[n] = pkg
}
info, err := query.AURInfoPrint(aur, config.RequestSplitN)
if err != nil {
return err
}
if len(repo) > 0 {
missing, err = getPkgbuildsfromABS(repo, wd, dbExecutor, force)
if err != nil {
return err
}
}
if len(aur) > 0 {
allBases := dep.GetBases(info)
bases := make([]dep.Base, 0)
for _, base := range allBases {
name := base.Pkgbase()
pkgDest := filepath.Join(wd, name)
_, err = os.Stat(pkgDest)
if os.IsNotExist(err) {
bases = append(bases, base)
} else if err != nil {
text.Errorln(err)
continue
} else {
if force {
if err = os.RemoveAll(pkgDest); err != nil {
text.Errorln(err)
continue
}
bases = append(bases, base)
} else {
text.Warnln(gotext.Get("%s already exists. Use -f/--force to overwrite", pkgDest))
continue
}
}
}
if _, err = downloadPkgbuilds(bases, nil, wd); err != nil {
return err
}
missing = missing || len(aur) != len(info)
}
if missing {
err = fmt.Errorf("")
}
return err
}
// GetPkgbuild downloads pkgbuild from the ABS.
func getPkgbuildsfromABS(pkgs []string, path string, dbExecutor db.Executor, force bool) (bool, error) {
var wg sync.WaitGroup
var mux sync.Mutex
var errs multierror.MultiError
names := make(map[string]string)
missing := make([]string, 0)
downloaded := 0
for _, pkgN := range pkgs {
var pkg alpm.IPackage
var err error
var url string
pkgDB, name := text.SplitDBFromName(pkgN)
if pkgDB != "" {
pkg = dbExecutor.SatisfierFromDB(name, pkgDB)
} else {
pkg = dbExecutor.SyncSatisfier(name)
}
if pkg == nil {
missing = append(missing, name)
continue
}
name = pkg.Base()
if name == "" {
name = pkg.Name()
}
// TODO: Check existence with ls-remote
// https://git.archlinux.org/svntogit/packages.git
switch pkg.DB().Name() {
case "core", "extra", "testing":
url = "https://git.archlinux.org/svntogit/packages.git"
case "community", "multilib", "community-testing", "multilib-testing":
url = "https://git.archlinux.org/svntogit/community.git"
default:
missing = append(missing, name)
continue
}
_, err = os.Stat(filepath.Join(path, name))
switch {
case err != nil && !os.IsNotExist(err):
text.Errorln(err)
continue
case os.IsNotExist(err), force:
if err = os.RemoveAll(filepath.Join(path, name)); err != nil {
text.Errorln(err)
continue
}
default:
text.Warn(gotext.Get("%s already downloaded -- use -f to overwrite", text.Cyan(name)))
continue
}
names[name] = url
}
if len(missing) != 0 {
text.Warnln(gotext.Get("Missing ABS packages:"),
text.Cyan(strings.Join(missing, ", ")))
}
download := func(pkg string, url string) {
defer wg.Done()
if _, err := gitDownloadABS(url, config.ABSDir, pkg); err != nil {
errs.Add(errors.New(gotext.Get("failed to get pkgbuild: %s: %s", text.Cyan(pkg), err.Error())))
return
}
_, stderr, err := config.Runtime.CmdRunner.Capture(
exec.Command(
"cp", "-r",
filepath.Join(config.ABSDir, pkg, "trunk"),
filepath.Join(path, pkg)), 0)
mux.Lock()
downloaded++
if err != nil {
errs.Add(errors.New(gotext.Get("failed to link %s: %s", text.Cyan(pkg), stderr)))
} else {
fmt.Fprintln(os.Stdout, gotext.Get("(%d/%d) Downloaded PKGBUILD from ABS: %s", downloaded, len(names), text.Cyan(pkg)))
}
mux.Unlock()
}
count := 0
for name, url := range names {
wg.Add(1)
go download(name, url)
count++
if count%25 == 0 {
wg.Wait()
}
}
wg.Wait()
return len(missing) != 0, errs.Return()
}