浏览代码

refactor(diff): move diff functions to dedicated file

jguer 3 年之前
父节点
当前提交
1c96fd2d9d
共有 1 个文件被更改,包括 70 次插入48 次删除
  1. 70 48
      download.go

+ 70 - 48
download.go

@@ -2,51 +2,58 @@ package main
 
 import (
 	"fmt"
-	"os"
 	"path/filepath"
 	"strings"
 
 	"github.com/leonelquinteros/gotext"
+
+	"github.com/Jguer/yay/v10/pkg/dep"
+	"github.com/Jguer/yay/v10/pkg/multierror"
+	"github.com/Jguer/yay/v10/pkg/text"
 )
 
 const gitDiffRefName = "AUR_SEEN"
 
-// Update the YAY_DIFF_REVIEW ref to HEAD. We use this ref to determine which diff were
-// reviewed by the user
-func gitUpdateSeenRef(path, name string) error {
-	_, stderr, err := config.Runtime.CmdRunner.Capture(
-		config.Runtime.CmdBuilder.BuildGitCmd(
-			filepath.Join(path, name), "update-ref", gitDiffRefName, "HEAD"), 0)
-	if err != nil {
-		return fmt.Errorf("%s %s", stderr, err)
-	}
-	return nil
-}
-
-// Return wether or not we have reviewed a diff yet. It checks for the existence of
-// YAY_DIFF_REVIEW in the git ref-list
-func gitHasLastSeenRef(path, name string) bool {
-	_, _, err := config.Runtime.CmdRunner.Capture(
-		config.Runtime.CmdBuilder.BuildGitCmd(
-			filepath.Join(path, name), "rev-parse", "--quiet", "--verify", gitDiffRefName), 0)
-	return err == nil
-}
-
-// Returns the last reviewed hash. If YAY_DIFF_REVIEW exists it will return this hash.
-// If it does not it will return empty tree as no diff have been reviewed yet.
-func getLastSeenHash(path, name string) (string, error) {
-	if gitHasLastSeenRef(path, name) {
-		stdout, stderr, err := config.Runtime.CmdRunner.Capture(
-			config.Runtime.CmdBuilder.BuildGitCmd(
-				filepath.Join(path, name), "rev-parse", gitDiffRefName), 0)
+func showPkgbuildDiffs(bases []dep.Base, cloned map[string]bool) error {
+	var errMulti multierror.MultiError
+	for _, base := range bases {
+		pkg := base.Pkgbase()
+		dir := filepath.Join(config.BuildDir, pkg)
+		start, err := getLastSeenHash(config.BuildDir, pkg)
 		if err != nil {
-			return "", fmt.Errorf("%s %s", stderr, err)
+			errMulti.Add(err)
+			continue
 		}
 
-		lines := strings.Split(stdout, "\n")
-		return lines[0], nil
+		if cloned[pkg] {
+			start = gitEmptyTree
+		} else {
+			hasDiff, err := gitHasDiff(config.BuildDir, pkg)
+			if err != nil {
+				errMulti.Add(err)
+				continue
+			}
+
+			if !hasDiff {
+				text.Warnln(gotext.Get("%s: No changes -- skipping", text.Cyan(base.String())))
+				continue
+			}
+		}
+
+		args := []string{
+			"diff",
+			start + "..HEAD@{upstream}", "--src-prefix",
+			dir + "/", "--dst-prefix", dir + "/", "--", ".", ":(exclude).SRCINFO",
+		}
+		if text.UseColor {
+			args = append(args, "--color=always")
+		} else {
+			args = append(args, "--color=never")
+		}
+		_ = config.Runtime.CmdRunner.Show(config.Runtime.CmdBuilder.BuildGitCmd(dir, args...))
 	}
-	return gitEmptyTree, nil
+
+	return errMulti.Return()
 }
 
 // Check whether or not a diff exists between the last reviewed diff and
@@ -69,27 +76,42 @@ func gitHasDiff(path, name string) (bool, error) {
 	return true, nil
 }
 
-func gitDownload(url, path, name string) (bool, error) {
-	_, err := os.Stat(filepath.Join(path, name, ".git"))
-	if os.IsNotExist(err) {
-		cmd := config.Runtime.CmdBuilder.BuildGitCmd(path, "clone", "--no-progress", url, name)
-		_, stderr, errCapture := config.Runtime.CmdRunner.Capture(cmd, 0)
-		if errCapture != nil {
-			return false, fmt.Errorf(gotext.Get("error cloning %s: %s", name, stderr))
+// Return wether or not we have reviewed a diff yet. It checks for the existence of
+// YAY_DIFF_REVIEW in the git ref-list
+func gitHasLastSeenRef(path, name string) bool {
+	_, _, err := config.Runtime.CmdRunner.Capture(
+		config.Runtime.CmdBuilder.BuildGitCmd(
+			filepath.Join(path, name), "rev-parse", "--quiet", "--verify", gitDiffRefName), 0)
+	return err == nil
+}
+
+// Returns the last reviewed hash. If YAY_DIFF_REVIEW exists it will return this hash.
+// If it does not it will return empty tree as no diff have been reviewed yet.
+func getLastSeenHash(path, name string) (string, error) {
+	if gitHasLastSeenRef(path, name) {
+		stdout, stderr, err := config.Runtime.CmdRunner.Capture(
+			config.Runtime.CmdBuilder.BuildGitCmd(
+				filepath.Join(path, name), "rev-parse", gitDiffRefName), 0)
+		if err != nil {
+			return "", fmt.Errorf("%s %s", stderr, err)
 		}
 
-		return true, nil
-	} else if err != nil {
-		return false, fmt.Errorf(gotext.Get("error reading %s", filepath.Join(path, name, ".git")))
+		lines := strings.Split(stdout, "\n")
+		return lines[0], nil
 	}
+	return gitEmptyTree, nil
+}
 
-	cmd := config.Runtime.CmdBuilder.BuildGitCmd(filepath.Join(path, name), "fetch")
-	_, stderr, err := config.Runtime.CmdRunner.Capture(cmd, 0)
+// Update the YAY_DIFF_REVIEW ref to HEAD. We use this ref to determine which diff were
+// reviewed by the user
+func gitUpdateSeenRef(path, name string) error {
+	_, stderr, err := config.Runtime.CmdRunner.Capture(
+		config.Runtime.CmdBuilder.BuildGitCmd(
+			filepath.Join(path, name), "update-ref", gitDiffRefName, "HEAD"), 0)
 	if err != nil {
-		return false, fmt.Errorf(gotext.Get("error fetching %s: %s", name, stderr))
+		return fmt.Errorf("%s %s", stderr, err)
 	}
-
-	return false, nil
+	return nil
 }
 
 func gitMerge(path, name string) error {