2017-08-02 17:24:03 +00:00
|
|
|
package main
|
2017-05-01 01:23:03 +00:00
|
|
|
|
2017-05-01 01:34:40 +00:00
|
|
|
import (
|
2018-07-20 17:59:12 +00:00
|
|
|
"bytes"
|
2017-05-01 01:34:40 +00:00
|
|
|
"encoding/json"
|
2018-03-22 18:23:20 +00:00
|
|
|
"fmt"
|
2017-05-02 00:06:16 +00:00
|
|
|
"os"
|
2017-05-01 01:34:40 +00:00
|
|
|
"strings"
|
2018-08-02 22:30:48 +00:00
|
|
|
"sync"
|
2018-03-05 22:15:34 +00:00
|
|
|
"time"
|
2018-03-22 18:23:20 +00:00
|
|
|
|
2018-07-16 14:28:18 +00:00
|
|
|
gosrc "github.com/Morganamilo/go-srcinfo"
|
2020-05-04 07:24:32 +00:00
|
|
|
"github.com/leonelquinteros/gotext"
|
2020-05-02 14:17:20 +00:00
|
|
|
|
2020-07-05 14:58:35 +00:00
|
|
|
"github.com/Jguer/go-alpm"
|
|
|
|
|
2020-07-10 00:36:45 +00:00
|
|
|
"github.com/Jguer/yay/v10/pkg/dep"
|
2020-07-05 00:01:08 +00:00
|
|
|
"github.com/Jguer/yay/v10/pkg/query"
|
2020-06-13 17:29:50 +00:00
|
|
|
"github.com/Jguer/yay/v10/pkg/stringset"
|
|
|
|
"github.com/Jguer/yay/v10/pkg/text"
|
2017-05-01 01:34:40 +00:00
|
|
|
)
|
2017-05-01 01:23:03 +00:00
|
|
|
|
2017-05-02 00:06:16 +00:00
|
|
|
// Info contains the last commit sha of a repo
|
2020-07-08 01:40:50 +00:00
|
|
|
type (
|
|
|
|
vcsInfo map[string]shaInfos
|
|
|
|
shaInfos map[string]shaInfo
|
|
|
|
shaInfo struct {
|
|
|
|
Protocols []string `json:"protocols"`
|
|
|
|
Branch string `json:"branch"`
|
|
|
|
SHA string `json:"sha"`
|
|
|
|
}
|
|
|
|
)
|
2018-02-10 23:11:24 +00:00
|
|
|
|
2017-10-14 16:11:47 +00:00
|
|
|
// createDevelDB forces yay to create a DB of the existing development packages
|
2020-07-05 14:58:35 +00:00
|
|
|
func createDevelDB(vcsFilePath string, alpmHandle *alpm.Handle) error {
|
2018-08-02 22:30:48 +00:00
|
|
|
var mux sync.Mutex
|
|
|
|
var wg sync.WaitGroup
|
2018-03-22 18:23:20 +00:00
|
|
|
|
2020-07-26 23:24:28 +00:00
|
|
|
_, remoteNames, err := query.GetPackageNamesBySource(alpmHandle)
|
2017-05-02 00:06:16 +00:00
|
|
|
if err != nil {
|
2017-08-02 17:24:03 +00:00
|
|
|
return err
|
2017-05-02 00:06:16 +00:00
|
|
|
}
|
2017-08-02 17:24:03 +00:00
|
|
|
|
2020-07-10 00:36:45 +00:00
|
|
|
info, err := query.AURInfoPrint(remoteNames, config.RequestSplitN)
|
2018-03-22 18:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-25 21:31:20 +00:00
|
|
|
|
2020-07-10 00:36:45 +00:00
|
|
|
bases := dep.GetBases(info)
|
2019-10-16 21:58:55 +00:00
|
|
|
toSkip := pkgbuildsToSkip(bases, stringset.FromSlice(remoteNames))
|
2019-10-13 11:15:51 +00:00
|
|
|
_, err = downloadPkgbuilds(bases, toSkip, config.BuildDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
srcinfos, err := parseSrcinfoFiles(bases, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-22 18:23:20 +00:00
|
|
|
|
2020-05-02 14:17:20 +00:00
|
|
|
for i := range srcinfos {
|
|
|
|
for iP := range srcinfos[i].Packages {
|
2018-08-02 22:30:48 +00:00
|
|
|
wg.Add(1)
|
2020-07-05 01:26:00 +00:00
|
|
|
go updateVCSData(vcsFilePath, srcinfos[i].Packages[iP].Pkgname, srcinfos[i].Source, &mux, &wg)
|
2018-03-22 18:23:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 22:30:48 +00:00
|
|
|
wg.Wait()
|
2020-05-04 07:24:32 +00:00
|
|
|
text.OperationInfoln(gotext.Get("GenDB finished. No packages were installed"))
|
2017-08-02 17:24:03 +00:00
|
|
|
return err
|
2017-05-01 01:23:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 23:53:03 +00:00
|
|
|
// parseSource returns the git url, default branch and protocols it supports
|
2020-05-02 14:17:20 +00:00
|
|
|
func parseSource(source string) (url, branch string, protocols []string) {
|
2018-03-05 22:15:34 +00:00
|
|
|
split := strings.Split(source, "::")
|
|
|
|
source = split[len(split)-1]
|
|
|
|
split = strings.SplitN(source, "://", 2)
|
2017-05-01 01:34:40 +00:00
|
|
|
|
2018-03-05 22:15:34 +00:00
|
|
|
if len(split) != 2 {
|
2018-03-05 23:41:54 +00:00
|
|
|
return "", "", nil
|
2018-02-10 23:11:24 +00:00
|
|
|
}
|
2018-10-09 00:12:10 +00:00
|
|
|
protocols = strings.SplitN(split[0], "+", 2)
|
2018-08-02 21:55:48 +00:00
|
|
|
|
|
|
|
git := false
|
|
|
|
for _, protocol := range protocols {
|
|
|
|
if protocol == "git" {
|
|
|
|
git = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 00:12:10 +00:00
|
|
|
protocols = protocols[len(protocols)-1:]
|
|
|
|
|
2018-08-02 21:55:48 +00:00
|
|
|
if !git {
|
|
|
|
return "", "", nil
|
|
|
|
}
|
|
|
|
|
2018-03-05 22:15:34 +00:00
|
|
|
split = strings.SplitN(split[1], "#", 2)
|
|
|
|
if len(split) == 2 {
|
|
|
|
secondSplit := strings.SplitN(split[1], "=", 2)
|
|
|
|
if secondSplit[0] != "branch" {
|
2020-05-02 14:17:20 +00:00
|
|
|
// source has #commit= or #tag= which makes them not vcs
|
|
|
|
// packages because they reference a specific point
|
2018-03-05 23:41:54 +00:00
|
|
|
return "", "", nil
|
2018-03-05 22:15:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(secondSplit) == 2 {
|
|
|
|
url = split[0]
|
|
|
|
branch = secondSplit[1]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
url = split[0]
|
|
|
|
branch = "HEAD"
|
2018-02-10 23:11:24 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 14:29:54 +00:00
|
|
|
url = strings.Split(url, "?")[0]
|
2018-07-05 00:55:12 +00:00
|
|
|
branch = strings.Split(branch, "?")[0]
|
2018-07-04 14:29:54 +00:00
|
|
|
|
2020-05-02 14:17:20 +00:00
|
|
|
return url, branch, protocols
|
2018-03-05 22:15:34 +00:00
|
|
|
}
|
2018-02-10 23:11:24 +00:00
|
|
|
|
2020-07-05 01:26:00 +00:00
|
|
|
func updateVCSData(vcsFilePath, pkgName string, sources []gosrc.ArchString, mux sync.Locker, wg *sync.WaitGroup) {
|
2018-08-02 22:30:48 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
2018-03-05 22:15:34 +00:00
|
|
|
if savedInfo == nil {
|
2018-08-02 22:30:48 +00:00
|
|
|
mux.Lock()
|
2018-03-05 22:15:34 +00:00
|
|
|
savedInfo = make(vcsInfo)
|
2018-08-02 22:30:48 +00:00
|
|
|
mux.Unlock()
|
2017-05-02 10:50:11 +00:00
|
|
|
}
|
2017-05-02 00:06:16 +00:00
|
|
|
|
2018-03-05 22:15:34 +00:00
|
|
|
info := make(shaInfos)
|
2018-08-02 22:30:48 +00:00
|
|
|
checkSource := func(source gosrc.ArchString) {
|
|
|
|
defer wg.Done()
|
2018-07-16 14:28:18 +00:00
|
|
|
url, branch, protocols := parseSource(source.Value)
|
2018-03-05 22:15:34 +00:00
|
|
|
if url == "" || branch == "" {
|
2018-08-02 22:30:48 +00:00
|
|
|
return
|
2017-05-02 10:50:11 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 22:15:34 +00:00
|
|
|
commit := getCommit(url, branch, protocols)
|
|
|
|
if commit == "" {
|
2018-08-02 22:30:48 +00:00
|
|
|
return
|
2017-05-02 00:06:16 +00:00
|
|
|
}
|
2018-03-05 22:15:34 +00:00
|
|
|
|
2018-08-09 16:01:29 +00:00
|
|
|
mux.Lock()
|
2018-03-05 22:15:34 +00:00
|
|
|
info[url] = shaInfo{
|
|
|
|
protocols,
|
|
|
|
branch,
|
|
|
|
commit,
|
|
|
|
}
|
|
|
|
|
|
|
|
savedInfo[pkgName] = info
|
2020-05-04 07:24:32 +00:00
|
|
|
text.Warnln(gotext.Get("Found git repo: %s", cyan(url)))
|
2020-07-05 01:26:00 +00:00
|
|
|
err := saveVCSInfo(vcsFilePath)
|
2019-10-13 11:15:51 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2018-08-02 22:30:48 +00:00
|
|
|
mux.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, source := range sources {
|
|
|
|
wg.Add(1)
|
|
|
|
go checkSource(source)
|
2017-05-02 00:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 14:17:20 +00:00
|
|
|
func getCommit(url, branch string, protocols []string) string {
|
2018-10-09 00:12:10 +00:00
|
|
|
if len(protocols) > 0 {
|
|
|
|
protocol := protocols[len(protocols)-1]
|
2018-07-20 17:59:12 +00:00
|
|
|
var outbuf bytes.Buffer
|
|
|
|
|
|
|
|
cmd := passToGit("", "ls-remote", protocol+"://"+url, branch)
|
|
|
|
cmd.Stdout = &outbuf
|
2018-07-19 19:52:30 +00:00
|
|
|
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
|
2018-07-20 17:59:12 +00:00
|
|
|
|
|
|
|
err := cmd.Start()
|
2018-03-05 22:15:34 +00:00
|
|
|
if err != nil {
|
2018-10-09 00:12:10 +00:00
|
|
|
return ""
|
2018-03-05 22:15:34 +00:00
|
|
|
}
|
2017-05-01 01:34:40 +00:00
|
|
|
|
2020-05-02 14:17:20 +00:00
|
|
|
// for some reason
|
|
|
|
// git://bitbucket.org/volumesoffun/polyvox.git` hangs on my
|
|
|
|
// machine but using http:// instead of git does not hang.
|
|
|
|
// Introduce a time out so this can not hang
|
2018-03-05 22:15:34 +00:00
|
|
|
timer := time.AfterFunc(5*time.Second, func() {
|
2019-10-13 11:15:51 +00:00
|
|
|
err = cmd.Process.Kill()
|
|
|
|
if err != nil {
|
2020-05-04 07:24:32 +00:00
|
|
|
text.Errorln(err)
|
2019-10-13 11:15:51 +00:00
|
|
|
}
|
2018-03-05 22:15:34 +00:00
|
|
|
})
|
2017-05-01 01:34:40 +00:00
|
|
|
|
2018-03-05 22:15:34 +00:00
|
|
|
err = cmd.Wait()
|
|
|
|
timer.Stop()
|
|
|
|
if err != nil {
|
2018-10-09 00:12:10 +00:00
|
|
|
return ""
|
2018-03-05 22:15:34 +00:00
|
|
|
}
|
2018-04-17 16:57:02 +00:00
|
|
|
|
2018-07-20 17:59:12 +00:00
|
|
|
stdout := outbuf.String()
|
2018-03-05 22:15:34 +00:00
|
|
|
split := strings.Fields(stdout)
|
2018-02-27 12:06:30 +00:00
|
|
|
|
2018-03-05 22:15:34 +00:00
|
|
|
if len(split) < 2 {
|
2018-10-09 00:12:10 +00:00
|
|
|
return ""
|
2017-05-02 00:06:16 +00:00
|
|
|
}
|
2018-03-05 22:15:34 +00:00
|
|
|
|
|
|
|
commit := split[0]
|
|
|
|
return commit
|
2017-05-02 00:06:16 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 22:15:34 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (infos shaInfos) needsUpdate() bool {
|
2020-05-02 14:17:20 +00:00
|
|
|
// used to signal we have gone through all sources and found nothing
|
2018-03-08 22:20:47 +00:00
|
|
|
finished := make(chan struct{})
|
|
|
|
alive := 0
|
2018-03-16 19:30:14 +00:00
|
|
|
|
2020-05-02 14:17:20 +00:00
|
|
|
// if we find an update we use this to exit early and return true
|
2018-03-08 22:20:47 +00:00
|
|
|
hasUpdate := make(chan struct{})
|
2018-03-08 21:34:12 +00:00
|
|
|
|
|
|
|
checkHash := func(url string, info shaInfo) {
|
2018-06-04 19:12:26 +00:00
|
|
|
hash := getCommit(url, info.Branch, info.Protocols)
|
2018-03-07 16:29:52 +00:00
|
|
|
if hash != "" && hash != info.SHA {
|
2018-03-08 22:20:47 +00:00
|
|
|
hasUpdate <- struct{}{}
|
|
|
|
} else {
|
|
|
|
finished <- struct{}{}
|
2018-03-05 22:15:34 +00:00
|
|
|
}
|
2018-02-27 12:06:30 +00:00
|
|
|
}
|
|
|
|
|
2018-03-08 21:34:12 +00:00
|
|
|
for url, info := range infos {
|
2018-03-08 22:20:47 +00:00
|
|
|
alive++
|
2018-03-08 21:34:12 +00:00
|
|
|
go checkHash(url, info)
|
|
|
|
}
|
|
|
|
|
2018-03-08 22:20:47 +00:00
|
|
|
for {
|
|
|
|
select {
|
2018-03-16 19:30:14 +00:00
|
|
|
case <-hasUpdate:
|
2018-03-08 22:20:47 +00:00
|
|
|
return true
|
2018-03-16 19:30:14 +00:00
|
|
|
case <-finished:
|
2018-03-08 22:20:47 +00:00
|
|
|
alive--
|
|
|
|
if alive == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-05 22:15:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 01:26:00 +00:00
|
|
|
func saveVCSInfo(vcsFilePath string) error {
|
2017-08-04 09:26:53 +00:00
|
|
|
marshalledinfo, err := json.MarshalIndent(savedInfo, "", "\t")
|
2017-05-09 13:44:34 +00:00
|
|
|
if err != nil || string(marshalledinfo) == "null" {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-08 01:40:50 +00:00
|
|
|
in, err := os.OpenFile(vcsFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
|
2017-05-02 10:50:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
_, err = in.Write(marshalledinfo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = in.Sync()
|
|
|
|
return err
|
|
|
|
}
|