Skip to content

Commit 1702020

Browse files
authored
Merge pull request #358 from derrickstolee/version-compare
Use signed version comparisons before recommending upgrade
2 parents 22c22e0 + 5f7b932 commit 1702020

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

git-extra/git-update-git-for-windows

+39-20
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,43 @@ version_compare () {
3131
test $a1 -le $b1 || { echo 1; return; }
3232
test $b1 -le $a1 || { echo -1; return; }
3333

34-
# Get the next character
35-
a1="$(expr "$a" : '^\(.\)')"; a="${a#$a1}"
36-
b1="$(expr "$b" : '^\(.\)')"; b="${b#$b1}"
37-
38-
test "x$a1" = "x$b1" || {
39-
if test . = "$b1"
40-
then
41-
echo -1
42-
else
43-
echo 1
44-
fi
45-
return
46-
}
34+
# Skip non-numeric prefixes
35+
a1="$(expr "$a" : '^\([^0-9]\+\)')"; a="${a#$a1}"
36+
b1="$(expr "$b" : '^\([^0-9]\+\)')"; b="${b#$b1}"
4737

48-
test . = "$a1" || { echo 0; return; }
38+
case "$a1,$b1" in
39+
-rc,-rc) ;; # both are -rc versions
40+
-rc,*) echo -1; return;;
41+
*,-rc) echo 1; return;;
42+
esac
4943
done
5044
}
5145

46+
test "--test-version-compare" != "$*" || {
47+
test_version_compare () {
48+
result="$(version_compare "$1" "$2")"
49+
test "$3" = "$result" || {
50+
echo "version_compare $1 $2 returned $result instead of $3" >&2
51+
exit 1
52+
}
53+
54+
result2="$(version_compare "$2" "$1")"
55+
test "$result2" = "$((-$3))" || {
56+
echo "version_compare $2 $1 returned $result2 instead of $((-$3))" >&2
57+
exit 1
58+
}
59+
}
60+
61+
test_version_compare 2.32.0.windows.1 2.32.1.windows.1 -1
62+
test_version_compare 2.32.1.windows.1 2.32.0.windows.1 1
63+
test_version_compare 2.32.1.vfs.0.0 2.32.0.windows.1 1
64+
test_version_compare 2.32.1.vfs.0.0 2.32.0.vfs.0.0 1
65+
test_version_compare 2.32.0.vfs.0.1 2.32.0.vfs.0.2 -1
66+
test_version_compare 2.32.0-rc0.windows.1 2.31.1.windows.1 1
67+
test_version_compare 2.32.0-rc2.windows.1 2.32.0.windows.1 -1
68+
exit 0
69+
}
70+
5271
# Counts how many Bash instances are running, apart from the current one (if
5372
# any: `git update-git-for-windows` might have been called from a CMD window,
5473
# in which case no Git Bash might be running at all).
@@ -229,16 +248,16 @@ update_git_for_windows () {
229248
set_recently_seen "$latest"
230249
return
231250
fi
232-
test -n "$testing" ||
233-
case "$version" in
234-
*.rc[0-9]*)
235-
# Do not downgrade from -rc versions to the latest stable one
251+
if ! test -n "$testing"
252+
then
253+
# We are not testing and we don't have exact equality,
254+
# so do a careful comparison and look to see if the
255+
# latest release is strictly newer than ours.
236256
if test 0 -lt "$(version_compare "$version" "$latest")"
237257
then
238258
return
239259
fi
240-
;;
241-
esac
260+
fi
242261

243262
echo "Update $latest is available" >&2
244263
releases=$(http_get $releases_url/latest) || return

0 commit comments

Comments
 (0)