From 7a2cb106258aa7a18bcd53e45df96c4871a03d5e Mon Sep 17 00:00:00 2001
From: Marc Cornellà <hello@mcornella.com>
Date: Fri, 5 Nov 2021 19:47:29 +0100
Subject: fix(updater): stop update if connection unavailable

---
 tools/check_for_upgrade.sh | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

(limited to 'tools/check_for_upgrade.sh')

diff --git a/tools/check_for_upgrade.sh b/tools/check_for_upgrade.sh
index 157b0cce2..8264762b6 100644
--- a/tools/check_for_upgrade.sh
+++ b/tools/check_for_upgrade.sh
@@ -54,14 +54,27 @@ function is_update_available() {
   [[ "$repo" = ohmyzsh/ohmyzsh ]] || return 0
   local api_url="https://api.github.com/repos/${repo}/commits/${branch}"
 
-  # Get local and remote HEADs and compare them. If we can't get either assume there are updates
-  local local_head remote_head
+  # Get local HEAD. If this fails assume there are updates
+  local local_head
   local_head=$(git -C "$ZSH" rev-parse $branch 2>/dev/null) || return 0
 
-  remote_head=$(curl -fsSL -H 'Accept: application/vnd.github.v3.sha' $api_url 2>/dev/null) \
-  || remote_head=$(wget -O- --header='Accept: application/vnd.github.v3.sha' $api_url 2>/dev/null) \
-  || remote_head=$(HTTP_ACCEPT='Accept: application/vnd.github.v3.sha' fetch -o - $api_url 2>/dev/null) \
-  || return 0
+  # Get remote HEAD. If we can't get it assume there are updates unless there is no connection:
+  # - curl: 6 (could not resolve) or 7 (could not connect)
+  # - wget: 4 (network unreachable)
+  # - fetch: 1 (no route to host)
+  local remote_head ret
+  remote_head=$(
+    curl -fsSL -H 'Accept: application/vnd.github.v3.sha' $api_url 2>/dev/null || {
+      [[ $? -eq 6 || $? -eq 7 ]] && exit 1
+    } || wget -O- --header='Accept: application/vnd.github.v3.sha' $api_url 2>/dev/null || {
+      [[ $? -eq 4 ]] && exit 1
+    } || HTTP_ACCEPT='Accept: application/vnd.github.v3.sha' fetch -o - $api_url 2>/dev/null || {
+      [[ $? -eq 1 ]] && exit 1
+    } || exit 0
+  )
+
+  # If can't fetch remote HEAD, return exit code
+  ret=$?; [[ -n "$remote_head" ]] || return $ret
 
   # Compare local and remote HEADs
   [[ "$local_head" != "$remote_head" ]]
-- 
cgit v1.2.3-70-g09d2