Skip to content

Commit

Permalink
Fixing newerVersion()
Browse files Browse the repository at this point in the history
The function sometimes accepts version tags as newer versions even
though it is not the case, the whole function has been rewritten to work
as follows:

- checks every number between dots one by one
- if it is a number (or 'r' + number) and it is different, then it
  returns the comparaison of the number
- if it is the same number, then it ignores and iterate to the next
  number
- if it is not a number but a commit hash, it returns true if is
  different. In all cases, commit hashes must be placed at the end of
  version tags so a complete return would work as intended

A series of tests has been done on this function and can be run online,
with comparaison with the previous implementation:
https://go.dev/play/p/QUm8zPl_Ngw

Fixes ericm#31
  • Loading branch information
Ballasi committed Mar 17, 2022
1 parent 169183f commit fe7f085
Showing 1 changed file with 36 additions and 43 deletions.
79 changes: 36 additions & 43 deletions update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -158,57 +159,49 @@ func AurUpdate() error {
func newerVersion(oldVersion, newVersion string) bool {
oldVer := strings.Split(oldVersion, "-")
newVer := strings.Split(newVersion, "-")
regex := "^r?[0-9]+$" // true if is number, false if commit hash

// check if version tag (1.2.3-1)
if len(oldVer) > 1 && len(newVer) > 1 {
if len(oldVer[0]) > 7 {
return oldVersion != newVersion // Likely commit hashed
}
// For rXX
rSplitO := strings.SplitAfter(oldVer[0], "r")
rSplitN := strings.SplitAfter(newVer[0], "r")
if len(rSplitO) > 0 && len(rSplitN) > 0 {
oldVer[0] = rSplitO[0]
newVer[0] = rSplitN[0]
}
// Get rel
if oldVer[0] == newVer[0] {
relOld, _ := strconv.Atoi(oldVer[1])
relNew, _ := strconv.Atoi(newVer[1])
if len(rSplitO) > 1 && len(rSplitN) > 1 {
rSplitOld, _ := strconv.Atoi(rSplitO[1])
rSplitNew, _ := strconv.Atoi(rSplitN[1])
if rSplitOld < rSplitNew {
return true
}
}
if relOld < relNew {
old := strings.Split(oldVer[0], ".")
new := strings.Split(newVer[0], ".")

lenOld := len(old)

// iterating through each member of the version tag
for i := 0; i < len(new); i++ {
if i == lenOld {
return true
}
return false
}
// Get version diff
dotOld := strings.Split(oldVer[0], ".")
dotNew := strings.Split(newVer[0], ".")
var m int
if len(dotOld) > len(dotNew) {
m = len(dotNew)
} else {
m = len(dotOld)
}
for i := 0; i < m; i++ {
// Get ints
oldNum, errOld := strconv.Atoi(dotOld[i])
newNum, errNew := strconv.Atoi(dotNew[i])
if errOld == nil && errNew == nil {
if newNum > oldNum {
return true

match_old, _ := regexp.MatchString(regex, old[i])
match_new, _ := regexp.MatchString(regex, new[i])

if match_old && match_new { // is number
slice_old := 0
slice_new := 0

// removing "r" if needed
if strings.HasPrefix(old[i], "r") {
slice_old = 1
}
} else {
if dotNew[i] > dotOld[i] {
return true
if strings.HasPrefix(new[i], "r") {
slice_new = 1
}

// string has already been checked to be a number
number_old, _ := strconv.Atoi(old[i][slice_old:])
number_new, _ := strconv.Atoi(new[i][slice_new:])

if number_old != number_new {
return number_old < number_new
}
} else { // is commit hash
return old[i] != new[i]
}
}
}

// version tag isn't right, don't do anything
return false
}

0 comments on commit fe7f085

Please sign in to comment.