2017-08-02 17:24:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2018-03-28 14:52:20 +00:00
|
|
|
"path/filepath"
|
2018-05-31 19:31:45 +00:00
|
|
|
"strings"
|
2018-08-18 05:41:16 +00:00
|
|
|
"sync"
|
2018-08-18 03:06:27 +00:00
|
|
|
|
2019-04-23 15:53:20 +00:00
|
|
|
alpm "github.com/Jguer/go-alpm"
|
2019-10-05 16:35:46 +00:00
|
|
|
"github.com/Jguer/yay/v9/pkg/types"
|
2017-08-02 17:24:03 +00:00
|
|
|
)
|
|
|
|
|
2018-03-28 14:52:20 +00:00
|
|
|
// Decide what download method to use:
|
|
|
|
// Use the config option when the destination does not already exits
|
2018-11-30 09:41:30 +00:00
|
|
|
// If .git exists in the destination use git
|
2018-03-28 14:52:20 +00:00
|
|
|
// Otherwise use a tarrball
|
|
|
|
func shouldUseGit(path string) bool {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return config.GitClone
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat(filepath.Join(path, ".git"))
|
2018-05-31 19:31:45 +00:00
|
|
|
return err == nil || os.IsExist(err)
|
2018-03-28 14:52:20 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
func downloadFile(path string, url string) (err error) {
|
|
|
|
// Create the file
|
|
|
|
out, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
// Get the data
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// Writer the body to file
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-10 00:57:17 +00:00
|
|
|
func gitHasDiff(path string, name string) (bool, error) {
|
2018-07-19 17:37:28 +00:00
|
|
|
stdout, stderr, err := capture(passToGit(filepath.Join(path, name), "rev-parse", "HEAD", "HEAD@{upstream}"))
|
2018-05-31 19:31:45 +00:00
|
|
|
if err != nil {
|
2018-06-10 00:57:17 +00:00
|
|
|
return false, fmt.Errorf("%s%s", stderr, err)
|
2018-05-31 19:31:45 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 17:37:28 +00:00
|
|
|
lines := strings.Split(stdout, "\n")
|
|
|
|
head := lines[0]
|
|
|
|
upstream := lines[1]
|
2018-06-10 00:57:17 +00:00
|
|
|
|
|
|
|
return head != upstream, nil
|
2018-05-31 19:31:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-20 17:18:53 +00:00
|
|
|
// TODO: yay-next passes args through the header, use that to unify ABS and AUR
|
|
|
|
func gitDownloadABS(url string, path string, name string) (bool, error) {
|
|
|
|
_, err := os.Stat(filepath.Join(path, name))
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
cmd := passToGit(path, "clone", "--no-progress", "--single-branch",
|
|
|
|
"-b", "packages/"+name, url, name)
|
|
|
|
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
|
|
|
|
_, stderr, err := capture(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("error cloning %s: %s", name, stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return false, fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := passToGit(filepath.Join(path, name), "fetch")
|
|
|
|
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
|
|
|
|
_, stderr, err := capture(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("error fetching %s: %s", name, stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2018-06-10 02:41:25 +00:00
|
|
|
func gitDownload(url string, path string, name string) (bool, error) {
|
2018-03-28 14:52:20 +00:00
|
|
|
_, err := os.Stat(filepath.Join(path, name, ".git"))
|
|
|
|
if os.IsNotExist(err) {
|
2018-08-02 22:07:36 +00:00
|
|
|
cmd := passToGit(path, "clone", "--no-progress", url, name)
|
|
|
|
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
|
|
|
|
_, stderr, err := capture(cmd)
|
2018-03-28 14:52:20 +00:00
|
|
|
if err != nil {
|
2018-08-29 19:19:31 +00:00
|
|
|
return false, fmt.Errorf("error cloning %s: %s", name, stderr)
|
2018-03-28 14:52:20 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 02:41:25 +00:00
|
|
|
return true, nil
|
2018-03-28 14:52:20 +00:00
|
|
|
} else if err != nil {
|
2018-06-10 02:41:25 +00:00
|
|
|
return false, fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
|
2018-03-28 14:52:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 22:07:36 +00:00
|
|
|
cmd := passToGit(filepath.Join(path, name), "fetch")
|
|
|
|
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
|
|
|
|
_, stderr, err := capture(cmd)
|
2018-03-28 14:52:20 +00:00
|
|
|
if err != nil {
|
2018-08-02 15:11:37 +00:00
|
|
|
return false, fmt.Errorf("error fetching %s: %s", name, stderr)
|
2018-03-28 14:52:20 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 02:41:25 +00:00
|
|
|
return false, nil
|
2018-06-09 23:42:58 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 13:14:16 +00:00
|
|
|
func gitMerge(path string, name string) error {
|
2018-08-02 15:11:37 +00:00
|
|
|
_, stderr, err := capture(passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD"))
|
2018-03-28 14:52:20 +00:00
|
|
|
if err != nil {
|
2018-08-02 15:11:37 +00:00
|
|
|
return fmt.Errorf("error resetting %s: %s", name, stderr)
|
2018-03-28 14:52:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 15:11:37 +00:00
|
|
|
_, stderr, err = capture(passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff"))
|
2018-03-28 14:52:20 +00:00
|
|
|
if err != nil {
|
2018-08-02 15:11:37 +00:00
|
|
|
return fmt.Errorf("error merging %s: %s", name, stderr)
|
2018-03-28 14:52:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
// DownloadAndUnpack downloads url tgz and extracts to path.
|
2018-08-18 05:41:16 +00:00
|
|
|
func downloadAndUnpack(url string, path string) error {
|
|
|
|
err := os.MkdirAll(path, 0755)
|
2017-08-02 17:24:03 +00:00
|
|
|
if err != nil {
|
2018-08-18 05:41:16 +00:00
|
|
|
return err
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
2018-05-14 17:33:48 +00:00
|
|
|
fileName := filepath.Base(url)
|
2017-08-02 17:24:03 +00:00
|
|
|
|
2018-04-17 00:32:31 +00:00
|
|
|
tarLocation := filepath.Join(path, fileName)
|
2017-08-02 17:24:03 +00:00
|
|
|
defer os.Remove(tarLocation)
|
|
|
|
|
|
|
|
err = downloadFile(tarLocation, url)
|
|
|
|
if err != nil {
|
2018-08-18 05:41:16 +00:00
|
|
|
return err
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
2018-08-18 05:41:16 +00:00
|
|
|
_, stderr, err := capture(exec.Command(config.TarBin, "-xf", tarLocation, "-C", path))
|
2017-08-02 17:24:03 +00:00
|
|
|
if err != nil {
|
2018-08-18 05:41:16 +00:00
|
|
|
return fmt.Errorf("%s", stderr)
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
2018-08-18 05:41:16 +00:00
|
|
|
return nil
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
2018-02-21 08:41:25 +00:00
|
|
|
func getPkgbuilds(pkgs []string) error {
|
2018-06-01 04:04:15 +00:00
|
|
|
missing := false
|
2017-08-02 17:24:03 +00:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
2018-02-17 17:44:30 +00:00
|
|
|
return err
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 04:04:15 +00:00
|
|
|
pkgs = removeInvalidTargets(pkgs)
|
|
|
|
aur, repo, err := packageSlices(pkgs)
|
2018-08-18 03:06:27 +00:00
|
|
|
|
2019-04-23 16:03:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-18 03:06:27 +00:00
|
|
|
for n := range aur {
|
2019-02-04 16:56:02 +00:00
|
|
|
_, pkg := splitDBFromName(aur[n])
|
2018-08-18 03:06:27 +00:00
|
|
|
aur[n] = pkg
|
|
|
|
}
|
|
|
|
|
2018-08-17 23:16:04 +00:00
|
|
|
info, err := aurInfoPrint(aur)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-01 04:04:15 +00:00
|
|
|
|
|
|
|
if len(repo) > 0 {
|
|
|
|
missing, err = getPkgbuildsfromABS(repo, wd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(aur) > 0 {
|
2018-08-30 14:30:52 +00:00
|
|
|
allBases := getBases(info)
|
|
|
|
bases := make([]Base, 0)
|
|
|
|
|
|
|
|
for _, base := range allBases {
|
|
|
|
name := base.Pkgbase()
|
|
|
|
_, err = os.Stat(filepath.Join(wd, name))
|
2019-03-07 20:02:44 +00:00
|
|
|
switch {
|
|
|
|
case err != nil && !os.IsNotExist(err):
|
2018-11-14 12:14:41 +00:00
|
|
|
fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
|
2018-08-30 14:30:52 +00:00
|
|
|
continue
|
2019-03-07 20:02:44 +00:00
|
|
|
case os.IsNotExist(err), cmdArgs.existsArg("f", "force"), shouldUseGit(filepath.Join(wd, name)):
|
2018-08-30 14:30:52 +00:00
|
|
|
if err = os.RemoveAll(filepath.Join(wd, name)); err != nil {
|
2018-11-14 12:14:41 +00:00
|
|
|
fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
|
2018-08-30 14:30:52 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-03-07 20:02:44 +00:00
|
|
|
default:
|
2018-08-30 14:30:52 +00:00
|
|
|
fmt.Printf("%s %s %s\n", yellow(smallArrow), cyan(name), "already downloaded -- use -f to overwrite")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
bases = append(bases, base)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = downloadPkgbuilds(bases, nil, wd); err != nil {
|
2018-06-01 04:04:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-08-17 23:16:04 +00:00
|
|
|
|
|
|
|
missing = missing || len(aur) != len(info)
|
2018-06-01 04:04:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if missing {
|
|
|
|
err = fmt.Errorf("")
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
2018-02-17 17:44:30 +00:00
|
|
|
return err
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPkgbuild downloads pkgbuild from the ABS.
|
2018-08-18 03:06:27 +00:00
|
|
|
func getPkgbuildsfromABS(pkgs []string, path string) (bool, error) {
|
2018-08-18 05:41:16 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
var mux sync.Mutex
|
2019-10-05 16:35:46 +00:00
|
|
|
var errs types.MultiError
|
2018-08-18 05:41:16 +00:00
|
|
|
names := make(map[string]string)
|
|
|
|
missing := make([]string, 0)
|
|
|
|
downloaded := 0
|
|
|
|
|
2019-02-04 16:56:02 +00:00
|
|
|
dbList, err := alpmHandle.SyncDBs()
|
2017-08-02 17:24:03 +00:00
|
|
|
if err != nil {
|
2018-08-18 05:41:16 +00:00
|
|
|
return false, err
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
2018-08-18 05:41:16 +00:00
|
|
|
for _, pkgN := range pkgs {
|
2018-08-18 03:06:27 +00:00
|
|
|
var pkg *alpm.Package
|
|
|
|
var err error
|
|
|
|
var url string
|
2019-02-04 16:56:02 +00:00
|
|
|
pkgDB, name := splitDBFromName(pkgN)
|
2018-06-01 04:04:15 +00:00
|
|
|
|
2019-02-04 16:56:02 +00:00
|
|
|
if pkgDB != "" {
|
|
|
|
if db, err := alpmHandle.SyncDBByName(pkgDB); err == nil {
|
2019-02-26 11:41:35 +00:00
|
|
|
pkg = db.Pkg(name)
|
2018-06-01 04:04:15 +00:00
|
|
|
}
|
2018-08-18 03:06:27 +00:00
|
|
|
} else {
|
2019-02-04 16:56:02 +00:00
|
|
|
dbList.ForEach(func(db alpm.DB) error {
|
2019-02-14 20:44:39 +00:00
|
|
|
if pkg = db.Pkg(name); pkg != nil {
|
2018-08-18 03:06:27 +00:00
|
|
|
return fmt.Errorf("")
|
2018-05-14 17:33:48 +00:00
|
|
|
}
|
2018-08-18 03:06:27 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2018-05-14 17:33:48 +00:00
|
|
|
|
2018-08-18 03:06:27 +00:00
|
|
|
if pkg == nil {
|
2018-08-18 05:41:16 +00:00
|
|
|
missing = append(missing, name)
|
2018-08-18 03:06:27 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-02-17 17:44:30 +00:00
|
|
|
|
2018-08-18 03:06:27 +00:00
|
|
|
name = pkg.Base()
|
|
|
|
if name == "" {
|
|
|
|
name = pkg.Name()
|
|
|
|
}
|
2018-05-14 17:33:48 +00:00
|
|
|
|
2019-09-20 17:18:53 +00:00
|
|
|
// TODO: Check existence with ls-remote
|
|
|
|
// https://git.archlinux.org/svntogit/packages.git
|
2018-08-18 03:06:27 +00:00
|
|
|
switch pkg.DB().Name() {
|
|
|
|
case "core", "extra", "testing":
|
2019-09-20 17:18:53 +00:00
|
|
|
url = "https://git.archlinux.org/svntogit/packages.git"
|
2018-08-18 03:06:27 +00:00
|
|
|
case "community", "multilib", "community-testing", "multilib-testing":
|
2019-09-20 17:18:53 +00:00
|
|
|
url = "https://git.archlinux.org/svntogit/community.git"
|
2018-08-18 03:06:27 +00:00
|
|
|
default:
|
2018-08-18 05:41:16 +00:00
|
|
|
missing = append(missing, name)
|
2018-08-18 03:06:27 +00:00
|
|
|
continue
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
2018-02-17 17:44:30 +00:00
|
|
|
|
2018-08-30 14:30:52 +00:00
|
|
|
_, err = os.Stat(filepath.Join(path, name))
|
2019-03-07 20:02:44 +00:00
|
|
|
switch {
|
|
|
|
case err != nil && !os.IsNotExist(err):
|
2018-11-14 12:14:41 +00:00
|
|
|
fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
|
2018-08-18 05:41:16 +00:00
|
|
|
continue
|
2019-03-07 20:02:44 +00:00
|
|
|
case os.IsNotExist(err), cmdArgs.existsArg("f", "force"):
|
2018-08-30 14:30:52 +00:00
|
|
|
if err = os.RemoveAll(filepath.Join(path, name)); err != nil {
|
2018-11-14 12:14:41 +00:00
|
|
|
fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
|
2018-08-30 14:30:52 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-03-07 20:02:44 +00:00
|
|
|
default:
|
2018-08-30 14:30:52 +00:00
|
|
|
fmt.Printf("%s %s %s\n", yellow(smallArrow), cyan(name), "already downloaded -- use -f to overwrite")
|
|
|
|
continue
|
2018-08-18 05:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
names[name] = url
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(missing) != 0 {
|
|
|
|
fmt.Println(yellow(bold(smallArrow)), "Missing ABS packages: ", cyan(strings.Join(missing, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
download := func(pkg string, url string) {
|
|
|
|
defer wg.Done()
|
2019-09-29 15:42:34 +00:00
|
|
|
if _, err := gitDownloadABS(url, config.BuildDir, pkg); err != nil {
|
2018-08-18 05:41:16 +00:00
|
|
|
errs.Add(fmt.Errorf("%s Failed to get pkgbuild: %s: %s", bold(red(arrow)), bold(cyan(pkg)), bold(red(err.Error()))))
|
|
|
|
return
|
2018-08-18 03:06:27 +00:00
|
|
|
}
|
2018-02-17 17:44:30 +00:00
|
|
|
|
2019-09-29 15:42:34 +00:00
|
|
|
_, stderr, err := capture(exec.Command("ln", "-s", filepath.Join(config.BuildDir, pkg, "trunk"), filepath.Join(path, pkg)))
|
2018-08-18 05:41:16 +00:00
|
|
|
mux.Lock()
|
|
|
|
downloaded++
|
2018-08-18 03:06:27 +00:00
|
|
|
if err != nil {
|
2019-09-20 17:18:53 +00:00
|
|
|
errs.Add(fmt.Errorf("%s Failed to link %s: %s", bold(red(arrow)), bold(cyan(pkg)), bold(red(stderr))))
|
2018-08-18 03:06:27 +00:00
|
|
|
} else {
|
2018-08-18 05:41:16 +00:00
|
|
|
fmt.Printf(bold(cyan("::"))+" Downloaded PKGBUILD from ABS (%d/%d): %s\n", downloaded, len(names), cyan(pkg))
|
2018-08-18 03:06:27 +00:00
|
|
|
}
|
2018-08-18 05:41:16 +00:00
|
|
|
mux.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-09-04 17:28:36 +00:00
|
|
|
count := 0
|
2018-08-18 05:41:16 +00:00
|
|
|
for name, url := range names {
|
|
|
|
wg.Add(1)
|
|
|
|
go download(name, url)
|
2018-09-04 17:28:36 +00:00
|
|
|
count++
|
|
|
|
if count%25 == 0 {
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2018-05-14 17:33:48 +00:00
|
|
|
}
|
|
|
|
|
2018-08-18 05:41:16 +00:00
|
|
|
wg.Wait()
|
2019-09-29 15:42:34 +00:00
|
|
|
errs.Add(os.RemoveAll(filepath.Join(config.BuildDir, "packages")))
|
2018-08-18 05:41:16 +00:00
|
|
|
return len(missing) != 0, errs.Return()
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|