2017-08-02 17:24:03 +00:00
|
|
|
package main
|
|
|
|
|
2018-04-16 19:26:18 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-10-05 17:39:31 +00:00
|
|
|
|
2020-05-04 07:24:32 +00:00
|
|
|
"github.com/leonelquinteros/gotext"
|
|
|
|
|
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-07-05 00:45:23 +00:00
|
|
|
"github.com/Jguer/yay/v10/pkg/settings"
|
2020-06-13 17:29:50 +00:00
|
|
|
"github.com/Jguer/yay/v10/pkg/stringset"
|
|
|
|
"github.com/Jguer/yay/v10/pkg/text"
|
2018-04-16 19:26:18 +00:00
|
|
|
)
|
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
// GetPkgbuild gets the pkgbuild of the package 'pkg' trying the ABS first and then the AUR trying the ABS first and then the AUR.
|
|
|
|
|
|
|
|
// RemovePackage removes package from VCS information
|
|
|
|
func removeVCSPackage(pkgs []string) {
|
2018-03-05 22:15:34 +00:00
|
|
|
updated := false
|
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
for _, pkgName := range pkgs {
|
2018-07-31 08:42:17 +00:00
|
|
|
if _, ok := savedInfo[pkgName]; ok {
|
2018-03-05 22:15:34 +00:00
|
|
|
delete(savedInfo, pkgName)
|
|
|
|
updated = true
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 22:15:34 +00:00
|
|
|
if updated {
|
2020-07-05 01:26:00 +00:00
|
|
|
err := saveVCSInfo(config.Runtime.VCSPath)
|
2019-10-13 11:15:51 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2018-03-05 22:15:34 +00:00
|
|
|
}
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-14 16:11:47 +00:00
|
|
|
// CleanDependencies removes all dangling dependencies in system
|
2020-07-08 01:31:35 +00:00
|
|
|
func cleanDependencies(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle, removeOptional bool) error {
|
2020-07-05 14:58:35 +00:00
|
|
|
hanging, err := hangingPackages(removeOptional, alpmHandle)
|
2017-08-02 17:24:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(hanging) != 0 {
|
2020-07-08 01:31:35 +00:00
|
|
|
return cleanRemove(cmdArgs, hanging)
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
2018-07-31 08:42:17 +00:00
|
|
|
return nil
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CleanRemove sends a full removal command to pacman with the pkgName slice
|
2020-07-08 01:31:35 +00:00
|
|
|
func cleanRemove(cmdArgs *settings.Arguments, pkgNames []string) error {
|
2018-01-04 00:59:57 +00:00
|
|
|
if len(pkgNames) == 0 {
|
2017-08-02 17:24:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-01-19 14:51:18 +00:00
|
|
|
|
2020-07-05 00:45:23 +00:00
|
|
|
arguments := cmdArgs.CopyGlobal()
|
|
|
|
_ = arguments.AddArg("R")
|
|
|
|
arguments.AddTarget(pkgNames...)
|
2018-07-31 08:42:17 +00:00
|
|
|
|
|
|
|
return show(passToPacman(arguments))
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
2018-04-16 19:26:18 +00:00
|
|
|
|
2020-07-08 01:22:01 +00:00
|
|
|
func syncClean(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle) error {
|
2018-04-16 19:26:18 +00:00
|
|
|
keepInstalled := false
|
|
|
|
keepCurrent := false
|
|
|
|
|
2020-07-08 01:22:01 +00:00
|
|
|
_, removeAll, _ := cmdArgs.GetArg("c", "clean")
|
2018-04-16 19:26:18 +00:00
|
|
|
|
2020-07-05 13:34:27 +00:00
|
|
|
for _, v := range config.Runtime.PacmanConf.CleanMethod {
|
2018-04-16 19:26:18 +00:00
|
|
|
if v == "KeepInstalled" {
|
|
|
|
keepInstalled = true
|
|
|
|
} else if v == "KeepCurrent" {
|
|
|
|
keepCurrent = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 00:45:23 +00:00
|
|
|
if config.Runtime.Mode == settings.ModeRepo || config.Runtime.Mode == settings.ModeAny {
|
2020-07-08 01:22:01 +00:00
|
|
|
if err := show(passToPacman(cmdArgs)); err != nil {
|
2018-07-24 01:49:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 00:45:23 +00:00
|
|
|
if !(config.Runtime.Mode == settings.ModeAUR || config.Runtime.Mode == settings.ModeAny) {
|
2018-07-24 01:49:45 +00:00
|
|
|
return nil
|
2018-04-16 19:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var question string
|
|
|
|
if removeAll {
|
2020-05-04 07:24:32 +00:00
|
|
|
question = gotext.Get("Do you want to remove ALL AUR packages from cache?")
|
2018-04-16 19:26:18 +00:00
|
|
|
} else {
|
2020-05-04 07:24:32 +00:00
|
|
|
question = gotext.Get("Do you want to remove all other AUR packages from cache?")
|
2018-04-16 19:26:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 07:24:32 +00:00
|
|
|
fmt.Println(gotext.Get("\nBuild directory:"), config.BuildDir)
|
2018-04-16 19:26:18 +00:00
|
|
|
|
2020-07-10 22:48:30 +00:00
|
|
|
if text.ContinueTask(question, true, config.NoConfirm) {
|
2020-07-05 14:58:35 +00:00
|
|
|
if err := cleanAUR(keepInstalled, keepCurrent, removeAll, alpmHandle); err != nil {
|
2020-05-02 14:17:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-04-16 19:26:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 14:17:20 +00:00
|
|
|
if removeAll {
|
|
|
|
return nil
|
2018-04-16 19:26:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 22:48:30 +00:00
|
|
|
if text.ContinueTask(gotext.Get("Do you want to remove ALL untracked AUR files?"), true, config.NoConfirm) {
|
2018-07-31 08:42:17 +00:00
|
|
|
return cleanUntracked()
|
2018-04-16 19:26:18 +00:00
|
|
|
}
|
|
|
|
|
2018-07-31 08:42:17 +00:00
|
|
|
return nil
|
2018-04-16 19:26:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 14:58:35 +00:00
|
|
|
func cleanAUR(keepInstalled, keepCurrent, removeAll bool, alpmHandle *alpm.Handle) error {
|
2020-05-04 07:24:32 +00:00
|
|
|
fmt.Println(gotext.Get("removing AUR packages from cache..."))
|
2018-04-16 19:26:18 +00:00
|
|
|
|
2019-10-16 21:25:40 +00:00
|
|
|
installedBases := make(stringset.StringSet)
|
|
|
|
inAURBases := make(stringset.StringSet)
|
2018-04-16 19:26:18 +00:00
|
|
|
|
2020-07-26 23:24:28 +00:00
|
|
|
remotePackages, _, err := query.GetRemotePackages(alpmHandle)
|
2018-04-16 19:26:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(config.BuildDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cachedPackages := make([]string, 0, len(files))
|
|
|
|
for _, file := range files {
|
|
|
|
if !file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
cachedPackages = append(cachedPackages, file.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Most people probably don't use keep current and that is the only
|
|
|
|
// case where this is needed.
|
2018-05-12 16:16:48 +00:00
|
|
|
// Querying the AUR is slow and needs internet so don't do it if we
|
2018-04-16 19:26:18 +00:00
|
|
|
// don't need to.
|
|
|
|
if keepCurrent {
|
2020-07-10 00:36:45 +00:00
|
|
|
info, errInfo := query.AURInfo(cachedPackages, &query.AURWarnings{}, config.RequestSplitN)
|
2020-05-02 14:17:20 +00:00
|
|
|
if errInfo != nil {
|
|
|
|
return errInfo
|
2018-04-16 19:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, pkg := range info {
|
2019-10-05 17:39:31 +00:00
|
|
|
inAURBases.Set(pkg.PackageBase)
|
2018-04-16 19:26:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pkg := range remotePackages {
|
2018-05-07 23:43:49 +00:00
|
|
|
if pkg.Base() != "" {
|
2019-10-05 17:39:31 +00:00
|
|
|
installedBases.Set(pkg.Base())
|
2018-05-07 23:43:49 +00:00
|
|
|
} else {
|
2019-10-05 17:39:31 +00:00
|
|
|
installedBases.Set(pkg.Name())
|
2018-05-07 23:43:49 +00:00
|
|
|
}
|
2018-04-16 19:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if !file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !removeAll {
|
2019-10-05 17:39:31 +00:00
|
|
|
if keepInstalled && installedBases.Get(file.Name()) {
|
2018-04-16 19:26:18 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-10-05 17:39:31 +00:00
|
|
|
if keepCurrent && inAURBases.Get(file.Name()) {
|
2018-04-16 19:26:18 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.RemoveAll(filepath.Join(config.BuildDir, file.Name()))
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanUntracked() error {
|
2020-05-04 07:24:32 +00:00
|
|
|
fmt.Println(gotext.Get("removing untracked AUR files from cache..."))
|
2018-04-16 19:26:18 +00:00
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(config.BuildDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if !file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := filepath.Join(config.BuildDir, file.Name())
|
2020-06-26 22:57:10 +00:00
|
|
|
if isGitRepository(dir) {
|
|
|
|
if err := show(passToGit(dir, "clean", "-fx")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-16 19:26:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-27 13:10:36 +00:00
|
|
|
|
2020-06-26 22:57:10 +00:00
|
|
|
func isGitRepository(dir string) bool {
|
|
|
|
_, err := os.Stat(filepath.Join(dir, ".git"))
|
|
|
|
return !os.IsNotExist(err)
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:36:45 +00:00
|
|
|
func cleanAfter(bases []dep.Base) {
|
2020-05-08 16:13:51 +00:00
|
|
|
fmt.Println(gotext.Get("removing untracked AUR files from cache..."))
|
2018-09-27 13:10:36 +00:00
|
|
|
|
|
|
|
for i, base := range bases {
|
|
|
|
dir := filepath.Join(config.BuildDir, base.Pkgbase())
|
2020-07-01 07:14:54 +00:00
|
|
|
if !isGitRepository(dir) {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-27 13:10:36 +00:00
|
|
|
|
2020-05-04 07:24:32 +00:00
|
|
|
text.OperationInfoln(gotext.Get("Cleaning (%d/%d): %s", i+1, len(bases), cyan(dir)))
|
|
|
|
|
2019-11-04 04:20:50 +00:00
|
|
|
_, stderr, err := capture(passToGit(dir, "reset", "--hard", "HEAD"))
|
|
|
|
if err != nil {
|
2020-05-04 07:24:32 +00:00
|
|
|
text.Errorln(gotext.Get("error resetting %s: %s", base.String(), stderr))
|
2019-11-04 04:20:50 +00:00
|
|
|
}
|
2018-09-27 13:10:36 +00:00
|
|
|
|
2020-07-01 07:14:54 +00:00
|
|
|
if err := show(passToGit(dir, "clean", "-fx", "--exclude='*.pkg.*'")); err != nil {
|
2019-11-04 04:20:50 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2018-09-27 13:10:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:36:45 +00:00
|
|
|
func cleanBuilds(bases []dep.Base) {
|
2018-09-27 13:10:36 +00:00
|
|
|
for i, base := range bases {
|
|
|
|
dir := filepath.Join(config.BuildDir, base.Pkgbase())
|
2020-05-04 07:24:32 +00:00
|
|
|
text.OperationInfoln(gotext.Get("Deleting (%d/%d): %s", i+1, len(bases), cyan(dir)))
|
2018-09-27 13:10:36 +00:00
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
2018-11-14 12:14:41 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2018-09-27 13:10:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|