2017-08-02 19:24:03 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2018-03-28 16:52:20 +02:00
|
|
|
"path/filepath"
|
2018-05-31 21:31:45 +02:00
|
|
|
"strings"
|
2018-08-18 07:41:16 +02:00
|
|
|
"sync"
|
2018-08-18 05:06:27 +02:00
|
|
|
|
|
|
|
alpm "github.com/jguer/go-alpm"
|
2017-08-02 19:24:03 +02:00
|
|
|
)
|
|
|
|
|
2018-03-28 16:52:20 +02:00
|
|
|
// Decide what download method to use:
|
|
|
|
// Use the config option when the destination does not already exits
|
|
|
|
// If .git exists in the destination uer git
|
|
|
|
// 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 21:31:45 +02:00
|
|
|
return err == nil || os.IsExist(err)
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
2017-08-02 19:24:03 +02: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 02:57:17 +02:00
|
|
|
func gitHasDiff(path string, name string) (bool, error) {
|
2018-07-19 19:37:28 +02:00
|
|
|
stdout, stderr, err := capture(passToGit(filepath.Join(path, name), "rev-parse", "HEAD", "HEAD@{upstream}"))
|
2018-05-31 21:31:45 +02:00
|
|
|
if err != nil {
|
2018-06-10 02:57:17 +02:00
|
|
|
return false, fmt.Errorf("%s%s", stderr, err)
|
2018-05-31 21:31:45 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 19:37:28 +02:00
|
|
|
lines := strings.Split(stdout, "\n")
|
|
|
|
head := lines[0]
|
|
|
|
upstream := lines[1]
|
2018-06-10 02:57:17 +02:00
|
|
|
|
|
|
|
return head != upstream, nil
|
2018-05-31 21:31:45 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 04:41:25 +02:00
|
|
|
func gitDownload(url string, path string, name string) (bool, error) {
|
2018-03-28 16:52:20 +02:00
|
|
|
_, err := os.Stat(filepath.Join(path, name, ".git"))
|
|
|
|
if os.IsNotExist(err) {
|
2018-08-03 00:07:36 +02: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 16:52:20 +02:00
|
|
|
if err != nil {
|
2018-08-29 21:19:31 +02:00
|
|
|
return false, fmt.Errorf("error cloning %s: %s", name, stderr)
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 04:41:25 +02:00
|
|
|
return true, nil
|
2018-03-28 16:52:20 +02:00
|
|
|
} else if err != nil {
|
2018-06-10 04:41:25 +02:00
|
|
|
return false, fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 00:07:36 +02:00
|
|
|
cmd := passToGit(filepath.Join(path, name), "fetch")
|
|
|
|
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
|
|
|
|
_, stderr, err := capture(cmd)
|
2018-03-28 16:52:20 +02:00
|
|
|
if err != nil {
|
2018-08-02 17:11:37 +02:00
|
|
|
return false, fmt.Errorf("error fetching %s: %s", name, stderr)
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 04:41:25 +02:00
|
|
|
return false, nil
|
2018-06-10 01:42:58 +02:00
|
|
|
}
|
|
|
|
|
2018-07-30 15:14:16 +02:00
|
|
|
func gitMerge(path string, name string) error {
|
2018-08-02 17:11:37 +02:00
|
|
|
_, stderr, err := capture(passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD"))
|
2018-03-28 16:52:20 +02:00
|
|
|
if err != nil {
|
2018-08-02 17:11:37 +02:00
|
|
|
return fmt.Errorf("error resetting %s: %s", name, stderr)
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 17:11:37 +02:00
|
|
|
_, stderr, err = capture(passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff"))
|
2018-03-28 16:52:20 +02:00
|
|
|
if err != nil {
|
2018-08-02 17:11:37 +02:00
|
|
|
return fmt.Errorf("error merging %s: %s", name, stderr)
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-31 21:31:45 +02:00
|
|
|
func gitDiff(path string, name string) error {
|
2018-07-19 19:37:28 +02:00
|
|
|
err := show(passToGit(filepath.Join(path, name), "diff", "HEAD..HEAD@{upstream}"))
|
2018-05-31 21:31:45 +02:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-02 19:24:03 +02:00
|
|
|
// DownloadAndUnpack downloads url tgz and extracts to path.
|
2018-08-18 07:41:16 +02:00
|
|
|
func downloadAndUnpack(url string, path string) error {
|
|
|
|
err := os.MkdirAll(path, 0755)
|
2017-08-02 19:24:03 +02:00
|
|
|
if err != nil {
|
2018-08-18 07:41:16 +02:00
|
|
|
return err
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-05-14 19:33:48 +02:00
|
|
|
fileName := filepath.Base(url)
|
2017-08-02 19:24:03 +02:00
|
|
|
|
2018-04-17 02:32:31 +02:00
|
|
|
tarLocation := filepath.Join(path, fileName)
|
2017-08-02 19:24:03 +02:00
|
|
|
defer os.Remove(tarLocation)
|
|
|
|
|
|
|
|
err = downloadFile(tarLocation, url)
|
|
|
|
if err != nil {
|
2018-08-18 07:41:16 +02:00
|
|
|
return err
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-08-18 07:41:16 +02:00
|
|
|
_, stderr, err := capture(exec.Command(config.TarBin, "-xf", tarLocation, "-C", path))
|
2017-08-02 19:24:03 +02:00
|
|
|
if err != nil {
|
2018-08-18 07:41:16 +02:00
|
|
|
return fmt.Errorf("%s", stderr)
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-08-18 07:41:16 +02:00
|
|
|
return nil
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-21 09:41:25 +01:00
|
|
|
func getPkgbuilds(pkgs []string) error {
|
2018-06-01 06:04:15 +02:00
|
|
|
missing := false
|
2017-08-02 19:24:03 +02:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
2018-02-17 18:44:30 +01:00
|
|
|
return err
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-06-01 06:04:15 +02:00
|
|
|
pkgs = removeInvalidTargets(pkgs)
|
|
|
|
aur, repo, err := packageSlices(pkgs)
|
2018-08-18 05:06:27 +02:00
|
|
|
|
|
|
|
for n := range aur {
|
|
|
|
_, pkg := splitDbFromName(aur[n])
|
|
|
|
aur[n] = pkg
|
|
|
|
}
|
|
|
|
|
2018-08-18 01:16:04 +02:00
|
|
|
info, err := aurInfoPrint(aur)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-01 06:04:15 +02:00
|
|
|
|
|
|
|
if len(repo) > 0 {
|
|
|
|
missing, err = getPkgbuildsfromABS(repo, wd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(aur) > 0 {
|
2018-08-30 16:30:52 +02:00
|
|
|
allBases := getBases(info)
|
|
|
|
bases := make([]Base, 0)
|
|
|
|
|
|
|
|
for _, base := range allBases {
|
|
|
|
name := base.Pkgbase()
|
|
|
|
_, err = os.Stat(filepath.Join(wd, name))
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2018-11-14 13:14:41 +01:00
|
|
|
fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
|
2018-08-30 16:30:52 +02:00
|
|
|
continue
|
|
|
|
} else if os.IsNotExist(err) || cmdArgs.existsArg("f", "force") || shouldUseGit(filepath.Join(wd, name)) {
|
|
|
|
if err = os.RemoveAll(filepath.Join(wd, name)); err != nil {
|
2018-11-14 13:14:41 +01:00
|
|
|
fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
|
2018-08-30 16:30:52 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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 06:04:15 +02:00
|
|
|
return err
|
|
|
|
}
|
2018-08-18 01:16:04 +02:00
|
|
|
|
|
|
|
missing = missing || len(aur) != len(info)
|
2018-06-01 06:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if missing {
|
|
|
|
err = fmt.Errorf("")
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-17 18:44:30 +01:00
|
|
|
return err
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPkgbuild downloads pkgbuild from the ABS.
|
2018-08-18 05:06:27 +02:00
|
|
|
func getPkgbuildsfromABS(pkgs []string, path string) (bool, error) {
|
2018-08-18 07:41:16 +02:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
var mux sync.Mutex
|
|
|
|
var errs MultiError
|
|
|
|
names := make(map[string]string)
|
|
|
|
missing := make([]string, 0)
|
|
|
|
downloaded := 0
|
|
|
|
|
2017-10-18 04:38:19 +02:00
|
|
|
dbList, err := alpmHandle.SyncDbs()
|
2017-08-02 19:24:03 +02:00
|
|
|
if err != nil {
|
2018-08-18 07:41:16 +02:00
|
|
|
return false, err
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-08-18 07:41:16 +02:00
|
|
|
for _, pkgN := range pkgs {
|
2018-08-18 05:06:27 +02:00
|
|
|
var pkg *alpm.Package
|
|
|
|
var err error
|
|
|
|
var url string
|
2018-08-18 07:41:16 +02:00
|
|
|
pkgDb, name := splitDbFromName(pkgN)
|
2018-06-01 06:04:15 +02:00
|
|
|
|
2018-08-18 05:06:27 +02:00
|
|
|
if pkgDb != "" {
|
|
|
|
if db, err := alpmHandle.SyncDbByName(pkgDb); err == nil {
|
|
|
|
pkg, err = db.PkgByName(name)
|
2018-06-01 06:04:15 +02:00
|
|
|
}
|
2018-08-18 05:06:27 +02:00
|
|
|
} else {
|
|
|
|
dbList.ForEach(func(db alpm.Db) error {
|
|
|
|
if pkg, err = db.PkgByName(name); err == nil {
|
|
|
|
return fmt.Errorf("")
|
2018-05-14 19:33:48 +02:00
|
|
|
}
|
2018-08-18 05:06:27 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2018-05-14 19:33:48 +02:00
|
|
|
|
2018-08-18 05:06:27 +02:00
|
|
|
if pkg == nil {
|
2018-08-18 07:41:16 +02:00
|
|
|
missing = append(missing, name)
|
2018-08-18 05:06:27 +02:00
|
|
|
continue
|
|
|
|
}
|
2018-02-17 18:44:30 +01:00
|
|
|
|
2018-08-18 05:06:27 +02:00
|
|
|
name = pkg.Base()
|
|
|
|
if name == "" {
|
|
|
|
name = pkg.Name()
|
|
|
|
}
|
2018-05-14 19:33:48 +02:00
|
|
|
|
2018-08-18 05:06:27 +02:00
|
|
|
switch pkg.DB().Name() {
|
|
|
|
case "core", "extra", "testing":
|
|
|
|
url = "https://git.archlinux.org/svntogit/packages.git/snapshot/packages/" + name + ".tar.gz"
|
|
|
|
case "community", "multilib", "community-testing", "multilib-testing":
|
|
|
|
url = "https://git.archlinux.org/svntogit/community.git/snapshot/packages/" + name + ".tar.gz"
|
|
|
|
default:
|
2018-08-18 07:41:16 +02:00
|
|
|
missing = append(missing, name)
|
2018-08-18 05:06:27 +02:00
|
|
|
continue
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
2018-02-17 18:44:30 +01:00
|
|
|
|
2018-08-30 16:30:52 +02:00
|
|
|
_, err = os.Stat(filepath.Join(path, name))
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2018-11-14 13:14:41 +01:00
|
|
|
fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
|
2018-08-18 07:41:16 +02:00
|
|
|
continue
|
2018-08-30 16:30:52 +02:00
|
|
|
} else if os.IsNotExist(err) || cmdArgs.existsArg("f", "force") {
|
|
|
|
if err = os.RemoveAll(filepath.Join(path, name)); err != nil {
|
2018-11-14 13:14:41 +01:00
|
|
|
fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
|
2018-08-30 16:30:52 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%s %s %s\n", yellow(smallArrow), cyan(name), "already downloaded -- use -f to overwrite")
|
|
|
|
continue
|
2018-08-18 07:41:16 +02: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()
|
|
|
|
if err := downloadAndUnpack(url, cacheHome); err != nil {
|
|
|
|
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 05:06:27 +02:00
|
|
|
}
|
2018-02-17 18:44:30 +01:00
|
|
|
|
2018-08-18 07:41:16 +02:00
|
|
|
_, stderr, err := capture(exec.Command("mv", filepath.Join(cacheHome, "packages", pkg, "trunk"), filepath.Join(path, pkg)))
|
|
|
|
mux.Lock()
|
|
|
|
downloaded++
|
2018-08-18 05:06:27 +02:00
|
|
|
if err != nil {
|
2018-08-18 07:41:16 +02:00
|
|
|
errs.Add(fmt.Errorf("%s Failed to move %s: %s", bold(red(arrow)), bold(cyan(pkg)), bold(red(string(stderr)))))
|
2018-08-18 05:06:27 +02:00
|
|
|
} else {
|
2018-08-18 07:41:16 +02:00
|
|
|
fmt.Printf(bold(cyan("::"))+" Downloaded PKGBUILD from ABS (%d/%d): %s\n", downloaded, len(names), cyan(pkg))
|
2018-08-18 05:06:27 +02:00
|
|
|
}
|
2018-08-18 07:41:16 +02:00
|
|
|
mux.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-09-04 19:28:36 +02:00
|
|
|
count := 0
|
2018-08-18 07:41:16 +02:00
|
|
|
for name, url := range names {
|
|
|
|
wg.Add(1)
|
|
|
|
go download(name, url)
|
2018-09-04 19:28:36 +02:00
|
|
|
count++
|
|
|
|
if count%25 == 0 {
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2018-05-14 19:33:48 +02:00
|
|
|
}
|
|
|
|
|
2018-08-18 07:41:16 +02:00
|
|
|
wg.Wait()
|
|
|
|
errs.Add(os.RemoveAll(filepath.Join(cacheHome, "packages")))
|
|
|
|
return len(missing) != 0, errs.Return()
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|