From 8203420f9a9a0f8031ab16470c72e19e1bb98457 Mon Sep 17 00:00:00 2001 From: morganamilo Date: Thu, 29 Mar 2018 00:36:53 +0100 Subject: [PATCH 1/2] Add --gitflags and --[no]gitclone flags --- cmd.go | 6 ++++++ config.go | 4 ++++ parser.go | 2 ++ 3 files changed, 12 insertions(+) diff --git a/cmd.go b/cmd.go index 0d051cbae..1655b0dae 100644 --- a/cmd.go +++ b/cmd.go @@ -253,10 +253,16 @@ func handleConfig(option, value string) bool { config.AnswerUpgrade = value case "noanswerupgrade": config.AnswerUpgrade = "" + case "gitclone": + config.GitClone = true + case "nogitclone": + config.GitClone = false case "gpgflags": config.GpgFlags = value case "mflags": config.MFlags = value + case "gitflags": + config.GitFlags = value case "builddir": config.BuildDir = value case "editor": diff --git a/config.go b/config.go index dc1d26148..fac5bbc66 100644 --- a/config.go +++ b/config.go @@ -42,6 +42,7 @@ type Configuration struct { GpgFlags string `json:"gpgflags"` MFlags string `json:"mflags"` SortBy string `json:"sortby"` + GitFlags string `json:"gitflags"` RequestSplitN int `json:"requestsplitn"` SearchMode int `json:"-"` SortMode int `json:"sortmode"` @@ -50,6 +51,7 @@ type Configuration struct { NoConfirm bool `json:"-"` Devel bool `json:"devel"` CleanAfter bool `json:"cleanAfter"` + GitClone bool `json:"gitclone"` } var version = "3.373" @@ -138,6 +140,7 @@ func defaultSettings(config *Configuration) { config.PacmanConf = "/etc/pacman.conf" config.GpgFlags = "" config.MFlags = "" + config.GitFlags = "" config.SortMode = BottomUp config.SortBy = "votes" config.SudoLoop = false @@ -151,6 +154,7 @@ func defaultSettings(config *Configuration) { config.AnswerClean = "" config.AnswerEdit = "" config.AnswerUpgrade = "" + config.GitClone = true } // Editor returns the preferred system editor. diff --git a/parser.go b/parser.go index 56d61fc7b..62d3cc4f7 100644 --- a/parser.go +++ b/parser.go @@ -429,6 +429,8 @@ func hasParam(arg string) bool { return true case "gpgflags": return true + case "gitflags": + return true case "builddir": return true case "editor": From d8f7ac2cd3e599cbcb8c5a89492a4c16b51560c7 Mon Sep 17 00:00:00 2001 From: morganamilo Date: Wed, 28 Mar 2018 15:52:20 +0100 Subject: [PATCH 2/2] 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. --- cmd.go | 12 ++++++++++ download.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++-- install.go | 8 ++++++- 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/cmd.go b/cmd.go index 1655b0dae..fadf264f2 100644 --- a/cmd.go +++ b/cmd.go @@ -563,3 +563,15 @@ func passToMakepkg(dir string, args ...string) (err error) { } return } + +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 +} diff --git a/download.go b/download.go index 72a71822f..406c01ed4 100644 --- a/download.go +++ b/download.go @@ -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(green(arrow)), bold(green("Downloaded")), bold(magenta(pkg.Name)), bold(green("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 diff --git a/install.go b/install.go index cb928e2d5..fc9bb436c 100644 --- a/install.go +++ b/install.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "os/exec" + "path/filepath" "strconv" "strings" @@ -556,7 +557,12 @@ func downloadPkgBuilds(pkgs []*rpc.Pkg, targets stringSet, bases map[string][]*r fmt.Printf(str, k+1, len(pkgs), 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 }