Merge pull request #1049 from reyreaud-l/add-diff-review-ref

FR #994: Implement proper diff display
This commit is contained in:
J Guerreiro 2019-10-13 00:07:56 +01:00 committed by GitHub
commit f1a6c4afdc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 9 deletions

View file

@ -48,17 +48,55 @@ func downloadFile(path string, url string) (err error) {
return err
}
func gitHasDiff(path string, name string) (bool, error) {
stdout, stderr, err := capture(passToGit(filepath.Join(path, name), "rev-parse", "HEAD", "HEAD@{upstream}"))
// Update the YAY_DIFF_REVIEW ref to HEAD. We use this ref to determine which diff were
// reviewed by the user
func gitUpdateSeenRef(path string, name string) error {
_, stderr, err := capture(passToGit(filepath.Join(path, name), "update-ref", "YAY_DIFF_REVIEW", "HEAD"))
if err != nil {
return false, fmt.Errorf("%s%s", stderr, err)
return fmt.Errorf("%s%s", stderr, err)
}
return nil
}
lines := strings.Split(stdout, "\n")
head := lines[0]
upstream := lines[1]
// 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 string, name string) bool {
_, _, err := capture(passToGit(filepath.Join(path, name), "rev-parse", "--quiet", "--verify", "YAY_DIFF_REVIEW"))
return err == nil
}
return head != upstream, 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 string, name string) (string, error) {
if gitHasLastSeenRef(path, name) {
stdout, stderr, err := capture(passToGit(filepath.Join(path, name), "rev-parse", "YAY_DIFF_REVIEW"))
if err != nil {
return "", fmt.Errorf("%s%s", stderr, err)
}
lines := strings.Split(stdout, "\n")
return lines[0], nil
}
return gitEmptyTree, nil
}
// Check whether or not a diff exists between the last reviewed diff and
// HEAD@{upstream}
func gitHasDiff(path string, name string) (bool, error) {
if gitHasLastSeenRef(path, name) {
stdout, stderr, err := capture(passToGit(filepath.Join(path, name), "rev-parse", "YAY_DIFF_REVIEW", "HEAD@{upstream}"))
if err != nil {
return false, fmt.Errorf("%s%s", stderr, err)
}
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
}
// TODO: yay-next passes args through the header, use that to unify ABS and AUR

View file

@ -224,6 +224,7 @@ func install(parser *arguments) (err error) {
if !continueTask(bold(green("Proceed with install?")), true) {
return fmt.Errorf("Aborting due to user")
}
updatePkgbuildSeenRef(toDiff, cloned)
config.NoConfirm = oldValue
}
@ -676,12 +677,26 @@ func editDiffNumberMenu(bases []Base, installed types.StringSet, diff bool) ([]B
return toEdit, nil
}
func updatePkgbuildSeenRef(bases []Base, cloned types.StringSet) error {
for _, base := range bases {
pkg := base.Pkgbase()
dir := filepath.Join(config.BuildDir, pkg)
if shouldUseGit(dir) {
gitUpdateSeenRef(config.BuildDir, pkg)
}
}
return nil
}
func showPkgbuildDiffs(bases []Base, cloned types.StringSet) error {
for _, base := range bases {
pkg := base.Pkgbase()
dir := filepath.Join(config.BuildDir, pkg)
if shouldUseGit(dir) {
start := "HEAD"
start, err := getLastSeenHash(config.BuildDir, pkg)
if err != nil {
return err
}
if cloned.Get(pkg) {
start = gitEmptyTree
@ -703,7 +718,7 @@ func showPkgbuildDiffs(bases []Base, cloned types.StringSet) error {
} else {
args = append(args, "--color=never")
}
err := show(passToGit(dir, args...))
err = show(passToGit(dir, args...))
if err != nil {
return err
}