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-03-05 22:15:34 +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"
|
2018-03-05 22:15:34 +00:00
|
|
|
"os/exec"
|
2017-05-01 01:34:40 +00:00
|
|
|
"strings"
|
2018-03-05 22:15:34 +00:00
|
|
|
"time"
|
2018-03-22 18:23:20 +00:00
|
|
|
|
|
|
|
rpc "github.com/mikkeloscar/aur"
|
|
|
|
gopkg "github.com/mikkeloscar/gopkgbuild"
|
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
|
2018-03-05 22:15:34 +00:00
|
|
|
type vcsInfo map[string]shaInfos
|
|
|
|
type shaInfos map[string]shaInfo
|
|
|
|
type shaInfo struct {
|
|
|
|
Protocols []string `json:"protocols"`
|
|
|
|
Brach 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
|
2017-08-02 17:24:03 +00:00
|
|
|
func createDevelDB() error {
|
2018-03-22 18:23:20 +00:00
|
|
|
infoMap := make(map[string]*rpc.Pkg)
|
|
|
|
srcinfosStale := make(map[string]*gopkg.PKGBUILD)
|
|
|
|
|
2017-08-07 13:43:25 +00:00
|
|
|
_, _, _, remoteNames, err := filterPackages()
|
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
|
|
|
|
2018-04-11 03:43:51 +00:00
|
|
|
info, err := aurInfoPrint(remoteNames)
|
2018-03-22 18:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-25 21:31:20 +00:00
|
|
|
|
2018-03-22 18:23:20 +00:00
|
|
|
for _, pkg := range info {
|
|
|
|
infoMap[pkg.Name] = pkg
|
|
|
|
}
|
|
|
|
|
|
|
|
bases := getBases(infoMap)
|
|
|
|
|
|
|
|
downloadPkgBuilds(info, sliceToStringSet(remoteNames), bases)
|
2018-04-02 18:16:49 +00:00
|
|
|
tryParsesrcinfosFile(info, srcinfosStale, bases)
|
2018-03-22 18:23:20 +00:00
|
|
|
|
|
|
|
for _, pkg := range info {
|
2018-04-02 18:16:49 +00:00
|
|
|
pkgbuild, ok := srcinfosStale[pkg.PackageBase]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2018-03-22 18:23:20 +00:00
|
|
|
|
|
|
|
for _, pkg := range bases[pkg.PackageBase] {
|
|
|
|
updateVCSData(pkg.Name, pkgbuild.Source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 23:45:58 +00:00
|
|
|
fmt.Println(bold(yellow(arrow) + bold(" GenDB finished. No packages were installed")))
|
2018-03-22 18:23:20 +00:00
|
|
|
|
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
|
2018-03-05 22:15:34 +00:00
|
|
|
func parseSource(source string) (url string, branch string, protocols []string) {
|
2017-05-09 01:09:43 +00:00
|
|
|
if !(strings.Contains(source, "git://") ||
|
|
|
|
strings.Contains(source, ".git") ||
|
|
|
|
strings.Contains(source, "git+https://")) {
|
2018-03-05 23:41:54 +00:00
|
|
|
return "", "", nil
|
2017-05-09 01:09:43 +00:00
|
|
|
}
|
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-03-05 22:15:34 +00:00
|
|
|
protocols = strings.Split(split[0], "+")
|
|
|
|
split = strings.SplitN(split[1], "#", 2)
|
|
|
|
if len(split) == 2 {
|
|
|
|
secondSplit := strings.SplitN(split[1], "=", 2)
|
|
|
|
if secondSplit[0] != "branch" {
|
|
|
|
//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-03-05 22:15:34 +00:00
|
|
|
return
|
|
|
|
}
|
2018-02-10 23:11:24 +00:00
|
|
|
|
2018-03-05 22:15:34 +00:00
|
|
|
func updateVCSData(pkgName string, sources []string) {
|
|
|
|
if savedInfo == nil {
|
|
|
|
savedInfo = make(vcsInfo)
|
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)
|
2017-05-02 10:50:11 +00:00
|
|
|
|
2018-03-05 22:15:34 +00:00
|
|
|
for _, source := range sources {
|
|
|
|
url, branch, protocols := parseSource(source)
|
|
|
|
if url == "" || branch == "" {
|
|
|
|
continue
|
2017-05-02 10:50:11 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 22:15:34 +00:00
|
|
|
commit := getCommit(url, branch, protocols)
|
|
|
|
if commit == "" {
|
|
|
|
continue
|
2017-05-02 00:06:16 +00:00
|
|
|
}
|
2018-03-05 22:15:34 +00:00
|
|
|
|
|
|
|
info[url] = shaInfo{
|
|
|
|
protocols,
|
|
|
|
branch,
|
|
|
|
commit,
|
|
|
|
}
|
|
|
|
|
|
|
|
savedInfo[pkgName] = info
|
2018-03-22 18:30:56 +00:00
|
|
|
|
2018-04-10 23:45:58 +00:00
|
|
|
fmt.Println(bold(yellow(arrow)) + " Found git repo: " + cyan(url))
|
2018-03-05 22:15:34 +00:00
|
|
|
saveVCSInfo()
|
2017-05-02 00:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 22:15:34 +00:00
|
|
|
func getCommit(url string, branch string, protocols []string) string {
|
|
|
|
for _, protocol := range protocols {
|
|
|
|
var outbuf bytes.Buffer
|
2018-02-10 23:11:24 +00:00
|
|
|
|
2018-03-07 16:29:52 +00:00
|
|
|
cmd := exec.Command(config.GitBin, "ls-remote", protocol+"://"+url, branch)
|
2018-03-05 22:15:34 +00:00
|
|
|
cmd.Stdout = &outbuf
|
2018-06-02 12:52:18 +00:00
|
|
|
cmd.Env = append(cmd.Env, "GIT_TERMINAL_PROMPT=0")
|
2018-02-10 23:11:24 +00:00
|
|
|
|
2018-03-05 22:15:34 +00:00
|
|
|
err := cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2017-05-01 01:34:40 +00:00
|
|
|
|
2018-03-05 22:15:34 +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
|
|
|
|
timer := time.AfterFunc(5*time.Second, func() {
|
|
|
|
cmd.Process.Kill()
|
|
|
|
})
|
2017-05-01 01:34:40 +00:00
|
|
|
|
2018-03-05 22:15:34 +00:00
|
|
|
err = cmd.Wait()
|
|
|
|
timer.Stop()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-04-17 16:57:02 +00:00
|
|
|
|
2018-03-05 22:15:34 +00:00
|
|
|
stdout := outbuf.String()
|
|
|
|
split := strings.Fields(stdout)
|
2018-02-27 12:06:30 +00:00
|
|
|
|
2018-03-05 22:15:34 +00:00
|
|
|
if len(split) < 2 {
|
|
|
|
continue
|
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 {
|
2018-03-08 22:20:47 +00:00
|
|
|
//used to signal we have gone through all sources and found nothing
|
|
|
|
finished := make(chan struct{})
|
|
|
|
alive := 0
|
2018-03-16 19:30:14 +00:00
|
|
|
|
2018-03-08 22:20:47 +00:00
|
|
|
//if we find an update we use this to exit early and return true
|
|
|
|
hasUpdate := make(chan struct{})
|
2018-03-08 21:34:12 +00:00
|
|
|
|
|
|
|
checkHash := func(url string, info shaInfo) {
|
2018-03-05 22:15:34 +00:00
|
|
|
hash := getCommit(url, info.Brach, 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
|
|
|
}
|
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
func saveVCSInfo() 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
|
|
|
|
}
|
2017-11-20 23:41:55 +00:00
|
|
|
in, err := os.OpenFile(vcsFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
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
|
|
|
|
}
|