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"
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
saveVCSInfo()
|
|
|
|
}
|
2017-08-02 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-14 16:11:47 +00:00
|
|
|
// CleanDependencies removes all dangling dependencies in system
|
2018-03-23 23:45:46 +00:00
|
|
|
func cleanDependencies(removeOptional bool) error {
|
|
|
|
hanging, err := hangingPackages(removeOptional)
|
2017-08-02 17:24:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(hanging) != 0 {
|
2018-07-31 08:42:17 +00:00
|
|
|
return cleanRemove(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
|
2018-01-04 00:59:57 +00:00
|
|
|
func cleanRemove(pkgNames []string) (err error) {
|
|
|
|
if len(pkgNames) == 0 {
|
2017-08-02 17:24:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-01-19 14:51:18 +00:00
|
|
|
|
2018-01-04 00:59:57 +00:00
|
|
|
arguments := makeArguments()
|
2018-02-08 22:51:43 +00:00
|
|
|
arguments.addArg("R")
|
2018-01-04 00:59:57 +00:00
|
|
|
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
|
|
|
|
|
|
|
func syncClean(parser *arguments) error {
|
2018-07-24 01:49:45 +00:00
|
|
|
var err error
|
2018-04-16 19:26:18 +00:00
|
|
|
keepInstalled := false
|
|
|
|
keepCurrent := false
|
|
|
|
|
|
|
|
_, removeAll, _ := parser.getArg("c", "clean")
|
|
|
|
|
2018-09-04 14:47:22 +00:00
|
|
|
for _, v := range pacmanConf.CleanMethod {
|
2018-04-16 19:26:18 +00:00
|
|
|
if v == "KeepInstalled" {
|
|
|
|
keepInstalled = true
|
|
|
|
} else if v == "KeepCurrent" {
|
|
|
|
keepCurrent = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-14 19:18:59 +00:00
|
|
|
if mode == modeRepo || mode == modeAny {
|
2018-07-31 08:42:17 +00:00
|
|
|
if err = show(passToPacman(parser)); err != nil {
|
2018-07-24 01:49:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-14 19:18:59 +00:00
|
|
|
if !(mode == modeAUR || mode == modeAny) {
|
2018-07-24 01:49:45 +00:00
|
|
|
return nil
|
2018-04-16 19:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var question string
|
|
|
|
if removeAll {
|
|
|
|
question = "Do you want to remove ALL AUR packages from cache?"
|
|
|
|
} else {
|
|
|
|
question = "Do you want to remove all other AUR packages from cache?"
|
|
|
|
}
|
|
|
|
|
2018-07-31 08:42:17 +00:00
|
|
|
fmt.Printf("\nBuild directory: %s\n", config.BuildDir)
|
2018-04-16 19:26:18 +00:00
|
|
|
|
2018-07-21 23:46:55 +00:00
|
|
|
if continueTask(question, true) {
|
2018-04-16 19:26:18 +00:00
|
|
|
err = cleanAUR(keepInstalled, keepCurrent, removeAll)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil || removeAll {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-21 23:46:55 +00:00
|
|
|
if continueTask("Do you want to remove ALL untracked AUR files?", true) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func cleanAUR(keepInstalled, keepCurrent, removeAll bool) error {
|
|
|
|
fmt.Println("removing AUR packages from cache...")
|
|
|
|
|
|
|
|
installedBases := make(stringSet)
|
|
|
|
inAURBases := make(stringSet)
|
|
|
|
|
|
|
|
_, remotePackages, _, _, err := filterPackages()
|
|
|
|
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 {
|
|
|
|
info, err := aurInfo(cachedPackages, &aurWarnings{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pkg := range info {
|
|
|
|
inAURBases.set(pkg.PackageBase)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pkg := range remotePackages {
|
2018-05-07 23:43:49 +00:00
|
|
|
if pkg.Base() != "" {
|
|
|
|
installedBases.set(pkg.Base())
|
|
|
|
} else {
|
|
|
|
installedBases.set(pkg.Name())
|
|
|
|
}
|
2018-04-16 19:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if !file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !removeAll {
|
|
|
|
if keepInstalled && installedBases.get(file.Name()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if keepCurrent && inAURBases.get(file.Name()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.RemoveAll(filepath.Join(config.BuildDir, file.Name()))
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanUntracked() error {
|
|
|
|
fmt.Println("removing Untracked AUR files from cache...")
|
|
|
|
|
|
|
|
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())
|
|
|
|
if shouldUseGit(dir) {
|
2018-09-27 13:10:36 +00:00
|
|
|
if err := show(passToGit(dir, "clean", "-fx")); err != nil {
|
2018-04-16 19:26:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-27 13:10:36 +00:00
|
|
|
|
|
|
|
func cleanAfter(bases []Base) {
|
|
|
|
fmt.Println("removing Untracked AUR files from cache...")
|
|
|
|
|
|
|
|
for i, base := range bases {
|
|
|
|
dir := filepath.Join(config.BuildDir, base.Pkgbase())
|
|
|
|
|
|
|
|
if shouldUseGit(dir) {
|
|
|
|
fmt.Printf(bold(cyan("::")+" Cleaning (%d/%d): %s\n"), i+1, len(bases), cyan(dir))
|
|
|
|
_, stderr, err := capture(passToGit(dir, "reset", "--hard", "HEAD"))
|
|
|
|
if err != nil {
|
2018-11-14 12:14:41 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "error resetting %s: %s", base.String(), stderr)
|
2018-09-27 13:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
show(passToGit(dir, "clean", "-fx"))
|
|
|
|
} else {
|
|
|
|
fmt.Printf(bold(cyan("::")+" Deleting (%d/%d): %s\n"), i+1, len(bases), cyan(dir))
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanBuilds(bases []Base) {
|
|
|
|
for i, base := range bases {
|
|
|
|
dir := filepath.Join(config.BuildDir, base.Pkgbase())
|
|
|
|
fmt.Printf(bold(cyan("::")+" Deleting (%d/%d): %s\n"), i+1, len(bases), cyan(dir))
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|