瀏覽代碼

Merge pull request #355 from Morganamilo/fix#354

 Add support for -Sc
Morgana 7 年之前
父節點
當前提交
44ce849cb0
共有 6 個文件被更改,包括 184 次插入11 次删除
  1. 1 1
      Gopkg.lock
  2. 145 0
      clean.go
  3. 2 0
      cmd.go
  4. 13 4
      doc/yay.8
  5. 18 4
      parser.go
  6. 5 2
      vendor/github.com/jguer/go-alpm/conf.go

+ 1 - 1
Gopkg.lock

@@ -5,7 +5,7 @@
   branch = "master"
   name = "github.com/jguer/go-alpm"
   packages = ["."]
-  revision = "bc954af9b2ced79e4db54ce6ab3c6a24d769e98b"
+  revision = "11d6aadda57c8fb4f93969333cb990677d28d4f9"
 
 [[projects]]
   branch = "master"

+ 145 - 0
clean.go

@@ -1,5 +1,12 @@
 package main
 
+import (
+	"fmt"
+	"io/ioutil"
+	"os"
+	"path/filepath"
+)
+
 // 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
@@ -45,3 +52,141 @@ func cleanRemove(pkgNames []string) (err error) {
 	err = passToPacman(arguments)
 	return err
 }
+
+func syncClean(parser *arguments) error {
+	keepInstalled := false
+	keepCurrent := false
+
+	_, removeAll, _ := parser.getArg("c", "clean")
+
+	for _, v := range alpmConf.CleanMethod {
+		if v == "KeepInstalled" {
+			keepInstalled = true
+		} else if v == "KeepCurrent" {
+			keepCurrent = true
+		}
+	}
+
+	err := passToPacman(parser)
+	if err != nil {
+		return err
+	}
+
+	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?"
+	}
+
+	fmt.Println()
+	fmt.Printf("Build directory: %s\n", config.BuildDir)
+
+	if continueTask(question, "nN") {
+		err = cleanAUR(keepInstalled, keepCurrent, removeAll)
+	}
+
+	if err != nil || removeAll {
+		return err
+	}
+
+	if continueTask("Do you want to remove ALL untracked AUR files?", "nN") {
+		err = cleanUntracked()
+	}
+
+	return err
+}
+
+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.
+	// Querying the AUR is slow and needs internet so dont do it if we
+	// 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 {
+		installedBases.set(pkg.Name())
+	}
+
+	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) {
+			err = passToGit(dir, "clean", "-fx")
+			if err != nil {
+				return err
+			}
+		}
+	}
+
+	return nil
+}

+ 2 - 0
cmd.go

@@ -389,6 +389,8 @@ func handleSync() (err error) {
 		}
 
 		err = syncSearch(targets)
+	} else if cmdArgs.existsArg("c", "clean") {
+		err = syncClean(cmdArgs)
 	} else if cmdArgs.existsArg("l", "list") {
 		err = passToPacman(cmdArgs)
 	} else if cmdArgs.existsArg("c", "clean") {

+ 13 - 4
doc/yay.8

@@ -42,14 +42,23 @@ If no arguments are provided 'yay -Syu' will be performed\&.
 If no operation is selected -Y will be assumed\&.
 .SH "EXTENDED PACMAN OPERATIONS"
 .PP
-\fB\-S, -Si, -Ss, -Su, -Qu\fR
+\fB\-S, -Si, -Ss, -Su, -Sc, -Qu\fR
 .RS 4
 These operations are extended to support both AUR and repo packages\&.
 .RE
 .PP
+\fB\-Sc\fR
+.RS 4
+Yay will also clean cached AUR package and any untracked Files in the
+cache\&. Untracked files cleaning only works for packages downloaded
+using gitclone\&. Cleaning untracked files will wipe any downloaded
+sources or built packages but will keep already downloaded vcs sources\&.
+.RE
+.PP
 \fB\-R\fR
 .RS 4
 Yay will also remove cached data about devel packages\&.
+
 .RE
 .SH "YAY OPTIONS (APPLY TO -Y AND --YAY)"
 .PP
@@ -190,14 +199,14 @@ the same when parsed\&.
 .PP
 \fB\-\-answeredit <All|None|Installed|NotInstalled|...>\fR
 .RS 4
-Set a predetermined answer for the edit pkgbuild  menu question\&. This answer
+Set a predetermined answer for the edit pkgbuild menu question\&. This answer
 will be used instead of reading from standard input but will be treaded exactly
 the same when parsed\&.
 .RE
 .PP
 \fB\-\-answerupgrade\fR <Repo|^Repo|None|...>
 .RS 4
-Set a predetermined answer for the upgrade  menu question\&. This answer
+Set a predetermined answer for the upgrade menu question\&. This answer
 will be used instead of reading from standard input but will be treaded exactly
 the same\&.
 .RE
@@ -398,7 +407,7 @@ any of these commits change the package will be upgraded during a devel update.
 .RS 4
 Unless otherwise set this should be the same as \fBCACHE DIRECTORY\fR. This
 directory is used to store downloaded AUR Packages as well as any source files
-and built packages from  those packages\&.
+and built packages from those packages\&.
 .RE
 .PP
 \fBPACMAN.CONF\fR

+ 18 - 4
parser.go

@@ -217,22 +217,36 @@ func (parser *arguments) existsArg(options ...string) bool {
 }
 
 func (parser *arguments) getArg(options ...string) (arg string, double bool, exists bool) {
+	existCount := 0
+
 	for _, option := range options {
 		arg, exists = parser.options[option]
 
 		if exists {
-			_, double = parser.doubles[option]
-			return
+			existCount++
+			_, exists = parser.doubles[option]
+
+			if exists {
+				existCount++
+			}
+
 		}
 
 		arg, exists = parser.globals[option]
 
 		if exists {
-			_, double = parser.doubles[option]
-			return
+			existCount++
+			_, exists = parser.doubles[option]
+
+			if exists {
+				existCount++
+			}
+
 		}
 	}
 
+	double = existCount >= 2
+
 	return
 }
 

+ 5 - 2
vendor/github.com/jguer/go-alpm/conf.go

@@ -55,7 +55,7 @@ type PacmanConfig struct {
 	XferCommand        string
 	NoUpgrade          []string
 	NoExtract          []string
-	CleanMethod        string
+	CleanMethod        []string
 	SigLevel           SigLevel
 	LocalFileSigLevel  SigLevel
 	RemoteFileSigLevel SigLevel
@@ -256,6 +256,10 @@ lineloop:
 		}
 	}
 
+	if len(conf.CleanMethod) == 0 {
+		conf.CleanMethod = []string{"KeepInstalled"}
+	}
+
 	if len(conf.CacheDir) == 0 {
 		conf.CacheDir = []string{"/var/cache/pacman/pkg/"} //should only be set if the config does not specify this
 	}
@@ -271,7 +275,6 @@ func (conf *PacmanConfig) SetDefaults() {
 	conf.GPGDir = "/etc/pacman.d/gnupg/"
 	conf.LogFile = "/var/log/pacman.log"
 	conf.UseDelta = 0.7
-	conf.CleanMethod = "KeepInstalled"
 
 	conf.SigLevel = SigPackage | SigPackageOptional | SigDatabase | SigDatabaseOptional
 	conf.LocalFileSigLevel = SigUseDefault