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-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
|
2018-03-24 00:45:46 +01:00
|
|
|
func cleanDependencies(removeOptional bool) error {
|
|
|
|
hanging, err := hangingPackages(removeOptional)
|
2017-08-02 19:24:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(hanging) != 0 {
|
2018-07-31 10:42:17 +02:00
|
|
|
return cleanRemove(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
|
2019-10-13 13:15:51 +02:00
|
|
|
func cleanRemove(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-07-05 02:45:23 +02:00
|
|
|
func syncClean(parser *settings.Arguments) error {
|
2018-04-16 21:26:18 +02:00
|
|
|
keepInstalled := false
|
|
|
|
keepCurrent := false
|
|
|
|
|
2020-07-05 02:45:23 +02:00
|
|
|
_, removeAll, _ := parser.GetArg("c", "clean")
|
2018-04-16 21:26:18 +02:00
|
|
|
|
2018-09-04 16:47:22 +02:00
|
|
|
for _, v := range 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 {
|
2019-03-05 21:10:04 +01:00
|
|
|
if err := show(passToPacman(parser)); 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
|
|
|
|
2018-07-22 01:46:55 +02:00
|
|
|
if continueTask(question, true) {
|
2020-05-02 16:17:20 +02:00
|
|
|
if err := cleanAUR(keepInstalled, keepCurrent, removeAll); err != nil {
|
|
|
|
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-05-04 09:24:32 +02:00
|
|
|
if continueTask(gotext.Get("Do you want to remove ALL untracked AUR files?"), true) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func cleanAUR(keepInstalled, keepCurrent, removeAll bool) 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-07-05 02:01:08 +02:00
|
|
|
_, remotePackages, _, _, err := query.FilterPackages(alpmHandle)
|
2018-04-16 21:26:18 +02: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 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-05-02 16:17:20 +02:00
|
|
|
info, errInfo := aurInfo(cachedPackages, &aurWarnings{})
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-09-27 15:10:36 +02:00
|
|
|
func cleanAfter(bases []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-05-04 09:24:32 +02:00
|
|
|
text.OperationInfoln(gotext.Get("Cleaning (%d/%d): %s", i+1, len(bases), cyan(dir)))
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanBuilds(bases []Base) {
|
|
|
|
for i, base := range bases {
|
|
|
|
dir := filepath.Join(config.BuildDir, base.Pkgbase())
|
2020-05-04 09:24:32 +02:00
|
|
|
text.OperationInfoln(gotext.Get("Deleting (%d/%d): %s", i+1, len(bases), 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|