2021-10-11 00:48:33 +02:00
|
|
|
// file dedicated to diff menu
|
2021-10-11 22:22:03 +02:00
|
|
|
package menus
|
2017-08-02 19:24:03 +02:00
|
|
|
|
|
|
|
import (
|
2021-08-12 18:56:23 +02:00
|
|
|
"context"
|
2017-08-02 19:24:03 +02:00
|
|
|
"fmt"
|
2022-11-20 01:51:55 +01:00
|
|
|
"io"
|
2022-08-14 00:56:23 +02:00
|
|
|
"os"
|
2018-05-31 21:31:45 +02:00
|
|
|
"strings"
|
2018-08-18 05:06:27 +02:00
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
mapset "github.com/deckarep/golang-set/v2"
|
2020-05-04 09:24:32 +02:00
|
|
|
"github.com/leonelquinteros/gotext"
|
2021-08-06 09:40:37 +02:00
|
|
|
|
2021-09-08 22:28:08 +02:00
|
|
|
"github.com/Jguer/yay/v11/pkg/multierror"
|
2021-10-11 00:48:33 +02:00
|
|
|
"github.com/Jguer/yay/v11/pkg/settings"
|
2021-10-11 22:22:03 +02:00
|
|
|
"github.com/Jguer/yay/v11/pkg/settings/exe"
|
2021-09-08 22:28:08 +02:00
|
|
|
"github.com/Jguer/yay/v11/pkg/text"
|
2017-08-02 19:24:03 +02:00
|
|
|
)
|
|
|
|
|
2021-10-11 22:22:03 +02:00
|
|
|
const (
|
|
|
|
gitEmptyTree = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
|
|
|
|
gitDiffRefName = "AUR_SEEN"
|
|
|
|
)
|
2019-10-14 10:56:33 +02:00
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
func showPkgbuildDiffs(ctx context.Context, cmdBuilder exe.ICmdBuilder,
|
|
|
|
pkgbuildDirs map[string]string, bases []string,
|
|
|
|
) error {
|
2021-08-06 09:40:37 +02:00
|
|
|
var errMulti multierror.MultiError
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
for _, pkg := range bases {
|
|
|
|
dir := pkgbuildDirs[pkg]
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
start, err := getLastSeenHash(ctx, cmdBuilder, dir)
|
2019-10-07 12:24:19 +02:00
|
|
|
if err != nil {
|
2021-08-06 09:40:37 +02:00
|
|
|
errMulti.Add(err)
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2021-08-06 09:40:37 +02:00
|
|
|
continue
|
2019-10-07 12:24:19 +02:00
|
|
|
}
|
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
if start != gitEmptyTree {
|
|
|
|
hasDiff, err := gitHasDiff(ctx, cmdBuilder, dir)
|
2021-08-06 09:40:37 +02:00
|
|
|
if err != nil {
|
|
|
|
errMulti.Add(err)
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2021-08-06 09:40:37 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasDiff {
|
2022-11-20 01:51:55 +01:00
|
|
|
text.Warnln(gotext.Get("%s: No changes -- skipping", text.Cyan(pkg)))
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2021-08-06 09:40:37 +02:00
|
|
|
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")
|
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2021-10-11 22:22:03 +02:00
|
|
|
_ = cmdBuilder.Show(cmdBuilder.BuildGitCmd(ctx, dir, args...))
|
2018-05-31 21:31:45 +02:00
|
|
|
}
|
2021-08-06 09:40:37 +02:00
|
|
|
|
|
|
|
return errMulti.Return()
|
2019-10-07 12:24:19 +02:00
|
|
|
}
|
2018-05-31 21:31:45 +02:00
|
|
|
|
2019-10-07 12:24:19 +02:00
|
|
|
// Check whether or not a diff exists between the last reviewed diff and
|
2021-08-11 20:13:28 +02:00
|
|
|
// HEAD@{upstream}.
|
2022-11-20 01:51:55 +01:00
|
|
|
func gitHasDiff(ctx context.Context, cmdBuilder exe.ICmdBuilder, dir string) (bool, error) {
|
|
|
|
if gitHasLastSeenRef(ctx, cmdBuilder, dir) {
|
2021-10-11 22:22:03 +02:00
|
|
|
stdout, stderr, err := cmdBuilder.Capture(
|
2022-11-20 01:51:55 +01:00
|
|
|
cmdBuilder.BuildGitCmd(ctx, dir, "rev-parse", gitDiffRefName, "HEAD@{upstream}"))
|
2019-10-07 12:24:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("%s%s", stderr, err)
|
|
|
|
}
|
2018-06-10 02:57:17 +02:00
|
|
|
|
2019-10-07 12:24:19 +02:00
|
|
|
lines := strings.Split(stdout, "\n")
|
|
|
|
lastseen := lines[0]
|
|
|
|
upstream := lines[1]
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2019-10-07 12:24:19 +02:00
|
|
|
return lastseen != upstream, nil
|
|
|
|
}
|
|
|
|
// If YAY_DIFF_REVIEW does not exists, we have never reviewed a diff for this package
|
|
|
|
// and should display it.
|
|
|
|
return true, nil
|
2018-05-31 21:31:45 +02:00
|
|
|
}
|
|
|
|
|
2021-08-06 09:40:37 +02:00
|
|
|
// Return wether or not we have reviewed a diff yet. It checks for the existence of
|
2021-08-11 20:13:28 +02:00
|
|
|
// YAY_DIFF_REVIEW in the git ref-list.
|
2022-11-20 01:51:55 +01:00
|
|
|
func gitHasLastSeenRef(ctx context.Context, cmdBuilder exe.ICmdBuilder, dir string) bool {
|
2021-10-11 22:22:03 +02:00
|
|
|
_, _, err := cmdBuilder.Capture(
|
|
|
|
cmdBuilder.BuildGitCmd(ctx,
|
2022-11-20 01:51:55 +01:00
|
|
|
dir, "rev-parse", "--quiet", "--verify", gitDiffRefName))
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2021-08-06 09:40:37 +02:00
|
|
|
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.
|
2022-11-20 01:51:55 +01:00
|
|
|
func getLastSeenHash(ctx context.Context, cmdBuilder exe.ICmdBuilder, dir string) (string, error) {
|
|
|
|
if gitHasLastSeenRef(ctx, cmdBuilder, dir) {
|
2021-10-11 22:22:03 +02:00
|
|
|
stdout, stderr, err := cmdBuilder.Capture(
|
|
|
|
cmdBuilder.BuildGitCmd(ctx,
|
2022-11-20 01:51:55 +01:00
|
|
|
dir, "rev-parse", gitDiffRefName))
|
2021-08-06 09:40:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("%s %s", stderr, err)
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
2021-08-06 09:40:37 +02:00
|
|
|
lines := strings.Split(stdout, "\n")
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2021-08-06 09:40:37 +02:00
|
|
|
return lines[0], nil
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2021-08-06 09:40:37 +02:00
|
|
|
return gitEmptyTree, nil
|
|
|
|
}
|
2018-03-28 16:52:20 +02:00
|
|
|
|
2021-08-06 09:40:37 +02:00
|
|
|
// Update the YAY_DIFF_REVIEW ref to HEAD. We use this ref to determine which diff were
|
2021-08-11 20:13:28 +02:00
|
|
|
// reviewed by the user.
|
2022-11-20 01:51:55 +01:00
|
|
|
func gitUpdateSeenRef(ctx context.Context, cmdBuilder exe.ICmdBuilder, dir string) error {
|
2021-10-11 22:22:03 +02:00
|
|
|
_, stderr, err := cmdBuilder.Capture(
|
|
|
|
cmdBuilder.BuildGitCmd(ctx,
|
2022-11-20 01:51:55 +01:00
|
|
|
dir, "update-ref", gitDiffRefName, "HEAD"))
|
2018-03-28 16:52:20 +02:00
|
|
|
if err != nil {
|
2021-08-06 09:40:37 +02:00
|
|
|
return fmt.Errorf("%s %s", stderr, err)
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2021-08-06 09:40:37 +02:00
|
|
|
return nil
|
2018-06-10 01:42:58 +02:00
|
|
|
}
|
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
func updatePkgbuildSeenRef(ctx context.Context, cmdBuilder exe.ICmdBuilder, pkgbuildDirs map[string]string, bases []string) error {
|
2021-10-11 00:48:33 +02:00
|
|
|
var errMulti multierror.MultiError
|
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
for _, pkg := range bases {
|
|
|
|
dir := pkgbuildDirs[pkg]
|
|
|
|
if err := gitUpdateSeenRef(ctx, cmdBuilder, dir); err != nil {
|
2021-10-11 00:48:33 +02:00
|
|
|
errMulti.Add(err)
|
|
|
|
}
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 00:48:33 +02:00
|
|
|
return errMulti.Return()
|
|
|
|
}
|
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
func Diff(ctx context.Context, cmdBuilder exe.ICmdBuilder, w io.Writer,
|
|
|
|
pkgbuildDirs map[string]string, diffMenuOption bool,
|
|
|
|
installed mapset.Set[string], cloned map[string]bool, noConfirm bool, diffDefaultAnswer string,
|
2022-08-14 00:56:23 +02:00
|
|
|
) error {
|
2021-10-11 00:48:33 +02:00
|
|
|
if !diffMenuOption {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
bases := make([]string, 0, len(pkgbuildDirs))
|
|
|
|
for base := range pkgbuildDirs {
|
|
|
|
bases = append(bases, base)
|
|
|
|
}
|
|
|
|
|
|
|
|
toDiff, errMenu := selectionMenu(w, pkgbuildDirs, bases, installed, gotext.Get("Diffs to show?"),
|
2021-10-11 23:08:18 +02:00
|
|
|
noConfirm, diffDefaultAnswer, nil)
|
2021-10-11 00:48:33 +02:00
|
|
|
if errMenu != nil || len(toDiff) == 0 {
|
|
|
|
return errMenu
|
|
|
|
}
|
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
if errD := showPkgbuildDiffs(ctx, cmdBuilder, pkgbuildDirs, toDiff); errD != nil {
|
|
|
|
return errD
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
if !text.ContinueTask(os.Stdin, gotext.Get("Proceed with install?"), true, false) {
|
|
|
|
return settings.ErrUserAbort{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errUpd := updatePkgbuildSeenRef(ctx, cmdBuilder, pkgbuildDirs, toDiff); errUpd != nil {
|
|
|
|
return errUpd
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DiffFn(ctx context.Context, config *settings.Configuration, w io.Writer, pkgbuildDirsByBase map[string]string) error {
|
2022-11-20 03:47:23 +01:00
|
|
|
if len(pkgbuildDirsByBase) == 0 {
|
|
|
|
return nil // no work to do
|
|
|
|
}
|
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
bases := make([]string, 0, len(pkgbuildDirsByBase))
|
|
|
|
for base := range pkgbuildDirsByBase {
|
|
|
|
bases = append(bases, base)
|
|
|
|
}
|
|
|
|
|
|
|
|
toDiff, errMenu := selectionMenu(w, pkgbuildDirsByBase, bases, mapset.NewThreadUnsafeSet[string](), gotext.Get("Diffs to show?"),
|
|
|
|
settings.NoConfirm, config.AnswerDiff, nil)
|
|
|
|
if errMenu != nil || len(toDiff) == 0 {
|
|
|
|
return errMenu
|
|
|
|
}
|
|
|
|
|
|
|
|
if errD := showPkgbuildDiffs(ctx, config.Runtime.CmdBuilder, pkgbuildDirsByBase, toDiff); errD != nil {
|
2021-10-11 00:48:33 +02:00
|
|
|
return errD
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
|
2022-08-14 00:56:23 +02:00
|
|
|
if !text.ContinueTask(os.Stdin, gotext.Get("Proceed with install?"), true, false) {
|
2021-10-11 00:48:33 +02:00
|
|
|
return settings.ErrUserAbort{}
|
|
|
|
}
|
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
if errUpd := updatePkgbuildSeenRef(ctx, config.Runtime.CmdBuilder, pkgbuildDirsByBase, toDiff); errUpd != nil {
|
2021-10-11 00:48:33 +02:00
|
|
|
return errUpd
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|