Use git clone for pkgbuild downloading

Use git clone over tarballs for pkgbuild downloading during -S. This
option can still be toggled using the config flags.

The config option for selecting clone or tarball will be overiden if an
existing package is cached. The method used to download the package
perviously will be used regardless of the config.
This commit is contained in:
morganamilo 2018-03-28 15:52:20 +01:00 committed by Jguer
parent 6d876a738c
commit cf47746d20
3 changed files with 81 additions and 3 deletions

12
cmd.go
View file

@ -594,3 +594,15 @@ func passToMakepkgCapture(dir string, args ...string) (string, string, error) {
return stdout, stderr, err
}
func passToGit(dir string, _args ...string) (err error) {
gitflags := strings.Fields(config.GitFlags)
args := []string{"-C", dir}
args = append(args, gitflags...)
args = append(args, _args...)
cmd := exec.Command(config.GitBin, args...)
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
err = cmd.Run()
return
}

View file

@ -6,9 +6,28 @@ import (
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
)
// 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"))
if os.IsNotExist(err) {
return false
}
return true
}
func downloadFile(path string, url string) (err error) {
// Create the file
out, err := os.Create(path)
@ -29,6 +48,37 @@ func downloadFile(path string, url string) (err error) {
return err
}
func gitDownload(url string, path string, name string) error {
_, err := os.Stat(filepath.Join(path, name, ".git"))
if os.IsNotExist(err) {
err = passToGit(path, "clone", url, name)
if err != nil {
return fmt.Errorf("error cloning %s", name)
}
return nil
} else if err != nil {
return fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
}
err = passToGit(filepath.Join(path, name), "fetch")
if err != nil {
return fmt.Errorf("error fetching %s", name)
}
err = passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD")
if err != nil {
return fmt.Errorf("error reseting %s", name)
}
err = passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff")
if err != nil {
return fmt.Errorf("error merging %s", name)
}
return nil
}
// DownloadAndUnpack downloads url tgz and extracts to path.
func downloadAndUnpack(url string, path string, trim bool) (err error) {
err = os.MkdirAll(path, 0755)
@ -129,8 +179,18 @@ func getPkgbuildsfromAUR(pkgs []string, dir string) (err error) {
}
for _, pkg := range aq {
downloadAndUnpack(baseURL+aq[0].URLPath, dir, false)
fmt.Println(bold(yellow(arrow)), "Downloaded", cyan(pkg.Name), "from AUR")
var err error
if shouldUseGit(filepath.Join(dir, pkg.PackageBase)) {
err = gitDownload(baseURL+"/"+pkg.PackageBase+".git", dir, pkg.PackageBase)
} else {
err = downloadAndUnpack(baseURL+aq[0].URLPath, dir, false)
}
if err != nil {
fmt.Println(err)
} else {
fmt.Println(bold(green(arrow)), bold(green("Downloaded")), bold(magenta(pkg.Name)), bold(green("from AUR")))
}
}
return

View file

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
@ -551,7 +552,12 @@ func downloadPkgBuilds(pkgs []*rpc.Pkg, targets stringSet, bases map[string][]*r
fmt.Printf(str, k+1, len(pkgs), cyan(formatPkgbase(pkg, bases)))
err := downloadAndUnpack(baseURL+pkg.URLPath, config.BuildDir, false)
var err error
if shouldUseGit(filepath.Join(config.BuildDir, pkg.PackageBase)) {
err = gitDownload(baseURL+"/"+pkg.PackageBase+".git", config.BuildDir, pkg.PackageBase)
} else {
err = downloadAndUnpack(baseURL+pkg.URLPath, config.BuildDir, false)
}
if err != nil {
return err
}