From 49386cad6def77fb335278b7ee641212c41d545b Mon Sep 17 00:00:00 2001 From: Eric Nielsen <4120606+ericbn@users.noreply.github.com> Date: Fri, 16 Jun 2023 20:24:33 -0500 Subject: [PATCH] Make forced check-version asynchronous (again) as it was before a34b8dab64214216ffb27c00474d7c8314b1c98e. Don't make zimfw hang in the foreground waiting for git to connect to GitHub, mainly because the waiting can take too long in case of connection issues or no network. Also set git timeout to 30 seconds so background check also does hang for too long, just it case. Fixes #514 --- src/stage2/50_zimfw_check_version.zsh.erb | 27 ++++++++++++++------ src/stage2/80_zimfw.zsh.erb | 2 +- zimfw.zsh | 31 +++++++++++++++-------- 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/stage2/50_zimfw_check_version.zsh.erb b/src/stage2/50_zimfw_check_version.zsh.erb index 963d73a..ed3cf25 100644 --- a/src/stage2/50_zimfw_check_version.zsh.erb +++ b/src/stage2/50_zimfw_check_version.zsh.erb @@ -1,14 +1,25 @@ _zimfw_check_version() { - if [[ ${1} -ne 0 ]]; then + if (( ${1} )); then # Get latest version (get all `v*` tags from repo, delete `*v` from beginning, # sort in descending `O`rder `n`umerically, and get the `[1]` first) - local tags - tags=$(command git ls-remote --tags --refs https://github.com/zimfw/zimfw.git 'v*') || return 1 - >! ${_zversion_target} <<<${${(On)${(f)tags}##*v}[1]} || return 1 + if (( ${2} )); then + # background check + if [[ -w ${_zversion_target:h} ]]; then + print -R ${${(On)${(f)"$(GIT_HTTP_LOW_SPEED_LIMIT=1000 GIT_HTTP_LOW_SPEED_TIME=30 command git ls-remote --tags --refs \ + https://github.com/zimfw/zimfw.git 'v*' 2>/dev/null)"}##*v}[1]} >! ${_zversion_target} &! + fi + else + # foreground check + local tags + tags=$(command git ls-remote --tags --refs https://github.com/zimfw/zimfw.git 'v*') || return 1 + >! ${_zversion_target} <<<${${(On)${(f)tags}##*v}[1]} || return 1 + fi fi - local -r zlatest_version=$(<${_zversion_target}) - if [[ -n ${zlatest_version} && ${_zversion} != ${zlatest_version} ]]; then - _zimfw_print -u2 -PR "%F{yellow}Latest zimfw version is %B${zlatest_version}%b. You're using version %B${_zversion}%b. Run %Bzimfw upgrade%b to upgrade.%f" - return 4 + if [[ -f ${_zversion_target} ]]; then + local -r zlatest_version=$(<${_zversion_target}) + if [[ -n ${zlatest_version} && ${_zversion} != ${zlatest_version} ]]; then + _zimfw_print -u2 -PR "%F{yellow}Latest zimfw version is %B${zlatest_version}%b. You're using version %B${_zversion}%b. Run %Bzimfw upgrade%b to upgrade.%f" + return 4 + fi fi } diff --git a/src/stage2/80_zimfw.zsh.erb b/src/stage2/80_zimfw.zsh.erb index c6410b2..59e9e2a 100644 --- a/src/stage2/80_zimfw.zsh.erb +++ b/src/stage2/80_zimfw.zsh.erb @@ -49,7 +49,7 @@ Options: if ! zstyle -t ':zim' disable-version-check && [[ ${1} != check-version ]]; then # If .latest_version does not exist or was not modified in the last 30 days [[ -f ${_zversion_target}(#qNm-30) ]]; local -r zversion_check_force=${?} - _zimfw_check_version ${zversion_check_force} + _zimfw_check_version ${zversion_check_force} 1 fi local _zrestartmsg=' Restart your terminal for changes to take effect.' diff --git a/zimfw.zsh b/zimfw.zsh index eb5251d..6096785 100644 --- a/zimfw.zsh +++ b/zimfw.zsh @@ -386,17 +386,28 @@ _zimfw_check_dumpfile() { } _zimfw_check_version() { - if [[ ${1} -ne 0 ]]; then + if (( ${1} )); then # Get latest version (get all `v*` tags from repo, delete `*v` from beginning, # sort in descending `O`rder `n`umerically, and get the `[1]` first) - local tags - tags=$(command git ls-remote --tags --refs https://github.com/zimfw/zimfw.git 'v*') || return 1 - >! ${_zversion_target} <<<${${(On)${(f)tags}##*v}[1]} || return 1 + if (( ${2} )); then + # background check + if [[ -w ${_zversion_target:h} ]]; then + print -R ${${(On)${(f)"$(GIT_HTTP_LOW_SPEED_LIMIT=1000 GIT_HTTP_LOW_SPEED_TIME=30 command git ls-remote --tags --refs \ + https://github.com/zimfw/zimfw.git 'v*' 2>/dev/null)"}##*v}[1]} >! ${_zversion_target} &! + fi + else + # foreground check + local tags + tags=$(command git ls-remote --tags --refs https://github.com/zimfw/zimfw.git 'v*') || return 1 + >! ${_zversion_target} <<<${${(On)${(f)tags}##*v}[1]} || return 1 + fi fi - local -r zlatest_version=$(<${_zversion_target}) - if [[ -n ${zlatest_version} && ${_zversion} != ${zlatest_version} ]]; then - _zimfw_print -u2 -PR "%F{yellow}Latest zimfw version is %B${zlatest_version}%b. You're using version %B${_zversion}%b. Run %Bzimfw upgrade%b to upgrade.%f" - return 4 + if [[ -f ${_zversion_target} ]]; then + local -r zlatest_version=$(<${_zversion_target}) + if [[ -n ${zlatest_version} && ${_zversion} != ${zlatest_version} ]]; then + _zimfw_print -u2 -PR "%F{yellow}Latest zimfw version is %B${zlatest_version}%b. You're using version %B${_zversion}%b. Run %Bzimfw upgrade%b to upgrade.%f" + return 4 + fi fi } @@ -435,7 +446,7 @@ _zimfw_compile() { } _zimfw_info() { - print -R 'zimfw version: '${_zversion}' (built at 2023-06-15 14:32:20 UTC, previous commit is f9648e0)' + print -R 'zimfw version: '${_zversion}' (built at 2023-06-17 01:23:46 UTC, previous commit is 0566b9b)' local zparam for zparam in LANG ${(Mk)parameters:#LC_*} OSTYPE TERM TERM_PROGRAM TERM_PROGRAM_VERSION ZIM_HOME ZSH_VERSION; do print -R ${(r.22....:.)zparam}${(P)zparam} @@ -885,7 +896,7 @@ Options: if ! zstyle -t ':zim' disable-version-check && [[ ${1} != check-version ]]; then # If .latest_version does not exist or was not modified in the last 30 days [[ -f ${_zversion_target}(#qNm-30) ]]; local -r zversion_check_force=${?} - _zimfw_check_version ${zversion_check_force} + _zimfw_check_version ${zversion_check_force} 1 fi local _zrestartmsg=' Restart your terminal for changes to take effect.'