2017-08-02 19:24:03 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-03-28 16:52:20 +02:00
|
|
|
"path/filepath"
|
2018-05-31 21:31:45 +02:00
|
|
|
"strings"
|
2018-08-18 05:06:27 +02:00
|
|
|
|
2020-05-04 09:24:32 +02:00
|
|
|
"github.com/leonelquinteros/gotext"
|
2021-08-06 09:40:37 +02:00
|
|
|
|
|
|
|
"github.com/Jguer/yay/v10/pkg/dep"
|
|
|
|
"github.com/Jguer/yay/v10/pkg/multierror"
|
|
|
|
"github.com/Jguer/yay/v10/pkg/text"
|
2017-08-02 19:24:03 +02:00
|
|
|
)
|
|
|
|
|
2019-10-15 10:02:10 +02:00
|
|
|
const gitDiffRefName = "AUR_SEEN"
|
2019-10-14 10:56:33 +02:00
|
|
|
|
2021-08-06 09:40:37 +02:00
|
|
|
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)
|
2019-10-07 12:24:19 +02:00
|
|
|
if err != nil {
|
2021-08-06 09:40:37 +02:00
|
|
|
errMulti.Add(err)
|
|
|
|
continue
|
2019-10-07 12:24:19 +02:00
|
|
|
}
|
|
|
|
|
2021-08-06 09:40:37 +02:00
|
|
|
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")
|
|
|
|
}
|
2021-08-08 20:22:39 +02:00
|
|
|
_ = config.Runtime.CmdBuilder.Show(config.Runtime.CmdBuilder.BuildGitCmd(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
|
|
|
|
// HEAD@{upstream}
|
2020-05-02 16:17:20 +02:00
|
|
|
func gitHasDiff(path, name string) (bool, error) {
|
2019-10-07 12:24:19 +02:00
|
|
|
if gitHasLastSeenRef(path, name) {
|
2021-08-08 20:22:39 +02:00
|
|
|
stdout, stderr, err := config.Runtime.CmdBuilder.Capture(
|
2020-08-22 01:24:52 +02:00
|
|
|
config.Runtime.CmdBuilder.BuildGitCmd(filepath.Join(path, name), "rev-parse", gitDiffRefName, "HEAD@{upstream}"), 0)
|
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]
|
|
|
|
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
|
|
|
|
// YAY_DIFF_REVIEW in the git ref-list
|
|
|
|
func gitHasLastSeenRef(path, name string) bool {
|
2021-08-08 20:22:39 +02:00
|
|
|
_, _, err := config.Runtime.CmdBuilder.Capture(
|
2021-08-06 09:40:37 +02:00
|
|
|
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) {
|
2021-08-08 20:22:39 +02:00
|
|
|
stdout, stderr, err := config.Runtime.CmdBuilder.Capture(
|
2021-08-06 09:40:37 +02:00
|
|
|
config.Runtime.CmdBuilder.BuildGitCmd(
|
|
|
|
filepath.Join(path, name), "rev-parse", gitDiffRefName), 0)
|
|
|
|
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")
|
|
|
|
return lines[0], nil
|
2018-03-28 16:52:20 +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
|
|
|
|
// reviewed by the user
|
|
|
|
func gitUpdateSeenRef(path, name string) error {
|
2021-08-08 20:22:39 +02:00
|
|
|
_, stderr, err := config.Runtime.CmdBuilder.Capture(
|
2021-08-06 09:40:37 +02:00
|
|
|
config.Runtime.CmdBuilder.BuildGitCmd(
|
|
|
|
filepath.Join(path, name), "update-ref", gitDiffRefName, "HEAD"), 0)
|
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-06 09:40:37 +02:00
|
|
|
return nil
|
2018-06-10 01:42:58 +02:00
|
|
|
}
|
|
|
|
|
2020-05-02 16:17:20 +02:00
|
|
|
func gitMerge(path, name string) error {
|
2021-08-08 20:22:39 +02:00
|
|
|
_, stderr, err := config.Runtime.CmdBuilder.Capture(
|
2020-08-22 01:24:52 +02:00
|
|
|
config.Runtime.CmdBuilder.BuildGitCmd(
|
|
|
|
filepath.Join(path, name), "reset", "--hard", "HEAD"), 0)
|
2018-03-28 16:52:20 +02:00
|
|
|
if err != nil {
|
2020-05-04 09:24:32 +02:00
|
|
|
return fmt.Errorf(gotext.Get("error resetting %s: %s", name, stderr))
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
2021-08-08 20:22:39 +02:00
|
|
|
_, stderr, err = config.Runtime.CmdBuilder.Capture(
|
2020-08-22 01:24:52 +02:00
|
|
|
config.Runtime.CmdBuilder.BuildGitCmd(
|
|
|
|
filepath.Join(path, name), "merge", "--no-edit", "--ff"), 0)
|
2018-03-28 16:52:20 +02:00
|
|
|
if err != nil {
|
2020-05-04 09:24:32 +02:00
|
|
|
return fmt.Errorf(gotext.Get("error merging %s: %s", name, stderr))
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|