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