889cd7acf349f1cae81c07378986157b382f7f1d

Author
Marc Cornellà <marc.cornella@live.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

refactor(updater): switch to Zsh execution and fix git remote detection logic

Diff

This diff is truncated to protect this page.

  1diff --git a/lib/cli.zsh b/lib/cli.zsh
  2index b1478a89fcf9bfcd3781567d638314a070828cc5..30790bec72c911c7231085e73461637fcdf56fe3 100644
  3--- a/lib/cli.zsh
  4+++ b/lib/cli.zsh
  5@@ -351,7 +351,7 @@ function _omz::theme::use {
  6 
  7 function _omz::update {
  8     # Run update script
  9-    env ZSH="$ZSH" sh "$ZSH/tools/upgrade.sh"
 10+    env ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh"
 11     # Update last updated file
 12     zmodload zsh/datetime
 13     echo "LAST_EPOCH=$(( EPOCHSECONDS / 60 / 60 / 24 ))" >! "${ZSH_CACHE_DIR}/.zsh-update"
 14diff --git a/lib/functions.zsh b/lib/functions.zsh
 15index 15526cd5f221b3c0ecad6488a81a77c25c41745b..f6f34e85125fe4082faec585ad86706bf3c23734 100644
 16--- a/lib/functions.zsh
 17+++ b/lib/functions.zsh
 18@@ -14,7 +14,7 @@ function upgrade_oh_my_zsh() {
 19   fi
 20 
 21   # Run update script
 22-  env ZSH="$ZSH" sh "$ZSH/tools/upgrade.sh"
 23+  env ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh"
 24   # Update last updated file
 25   zmodload zsh/datetime
 26   echo "LAST_EPOCH=$(( EPOCHSECONDS / 60 / 60 / 24 ))" >! "${ZSH_CACHE_DIR}/.zsh-update"
 27diff --git a/tools/check_for_upgrade.sh b/tools/check_for_upgrade.sh
 28index cadd5fe496a08d9f9edfe1f58170774da326a148..17925af8b81efa7630006342ecf6546c734db8e2 100644
 29--- a/tools/check_for_upgrade.sh
 30+++ b/tools/check_for_upgrade.sh
 31@@ -1,6 +1,6 @@
 32 # Migrate .zsh-update file to $ZSH_CACHE_DIR
 33 if [[ -f ~/.zsh-update && ! -f "${ZSH_CACHE_DIR}/.zsh-update" ]]; then
 34-    mv ~/.zsh-update "${ZSH_CACHE_DIR}/.zsh-update"
 35+  mv ~/.zsh-update "${ZSH_CACHE_DIR}/.zsh-update"
 36 fi
 37 
 38 # Cancel update if:
 39@@ -10,79 +10,81 @@ fi
 40 if [[ "$DISABLE_AUTO_UPDATE" = true ]] \
 41    || [[ ! -w "$ZSH" || ! -O "$ZSH" ]] \
 42    || ! command -v git &>/dev/null; then
 43-    return
 44+  return
 45 fi
 46 
 47 
 48 function current_epoch() {
 49-    zmodload zsh/datetime
 50-    echo $(( EPOCHSECONDS / 60 / 60 / 24 ))
 51+  zmodload zsh/datetime
 52+  echo $(( EPOCHSECONDS / 60 / 60 / 24 ))
 53 }
 54 
 55 function update_last_updated_file() {
 56-    echo "LAST_EPOCH=$(current_epoch)" >! "${ZSH_CACHE_DIR}/.zsh-update"
 57+  echo "LAST_EPOCH=$(current_epoch)" >! "${ZSH_CACHE_DIR}/.zsh-update"
 58 }
 59 
 60 function update_ohmyzsh() {
 61-    ZSH="$ZSH" sh "$ZSH/tools/upgrade.sh"
 62-    update_last_updated_file
 63+  ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh"
 64+  update_last_updated_file
 65 }
 66 
 67 () {
 68-    emulate -L zsh
 69+  emulate -L zsh
 70 
 71-    local epoch_target mtime option LAST_EPOCH
 72+  local epoch_target mtime option LAST_EPOCH
 73 
 74-    # Remove lock directory if older than a day
 75-    zmodload zsh/datetime
 76-    zmodload -F zsh/stat b:zstat
 77-    if mtime=$(zstat +mtime "$ZSH/log/update.lock" 2>/dev/null); then
 78-        if (( (mtime + 3600 * 24) < EPOCHSECONDS )); then
 79-            command rm -rf "$ZSH/log/update.lock"
 80-        fi
 81+  # Remove lock directory if older than a day
 82+  zmodload zsh/datetime
 83+  zmodload -F zsh/stat b:zstat
 84+  if mtime=$(zstat +mtime "$ZSH/log/update.lock" 2>/dev/null); then
 85+    if (( (mtime + 3600 * 24) < EPOCHSECONDS )); then
 86+      command rm -rf "$ZSH/log/update.lock"
 87     fi
 88+  fi
 89 
 90-    # Check for lock directory
 91-    if ! command mkdir "$ZSH/log/update.lock" 2>/dev/null; then
 92-        return
 93-    fi
 94+  # Check for lock directory
 95+  if ! command mkdir "$ZSH/log/update.lock" 2>/dev/null; then
 96+    return
 97+  fi
 98 
 99-    # Remove lock directory on exit. `return 1` is important for when trapping a SIGINT:
100-    #  The return status from the function is handled specially. If it is zero, the signal is
101-    #  assumed to have been handled, and execution continues normally. Otherwise, the shell
102-    #  will behave as interrupted except that the return status of the trap is retained.
103-    trap "command rm -rf '$ZSH/log/update.lock'; return 1" EXIT INT QUIT
104+  # Remove lock directory on exit. `return 1` is important for when trapping a SIGINT:
105+  #  The return status from the function is handled specially. If it is zero, the signal is
106+  #  assumed to have been handled, and execution continues normally. Otherwise, the shell
107+  #  will behave as interrupted except that the return status of the trap is retained.
108+  trap "
109+  unset -f current_epoch update_last_updated_file update_ohmyzsh
110+  command rm -rf '$ZSH/log/update.lock'
111+  return 1
112+  " EXIT INT QUIT
113 
114-    # Create or update .zsh-update file if missing or malformed
115-    if ! source "${ZSH_CACHE_DIR}/.zsh-update" 2>/dev/null || [[ -z "$LAST_EPOCH" ]]; then
116-        update_last_updated_file
117-        return
118-    fi
119+  # Create or update .zsh-update file if missing or malformed
120+  if ! source "${ZSH_CACHE_DIR}/.zsh-update" 2>/dev/null || [[ -z "$LAST_EPOCH" ]]; then
121+    update_last_updated_file
122+    return
123+  fi
124 
125-    # Number of days before trying to update again
126-    epoch_target=${UPDATE_ZSH_DAYS:-13}
127-    # Test if enough time has passed until the next update
128-    if (( ( $(current_epoch) - $LAST_EPOCH ) < $epoch_target )); then
129-        return
130-    fi
131diff --git a/tools/upgrade.sh b/tools/upgrade.sh
132old mode 100644
133new mode 100755
134index e005519d6d4c9b0e7edcdc17a183e1820538905f..634d5c03d5ab27cab5b204d6966c79509d648bd9
135--- a/tools/upgrade.sh
136+++ b/tools/upgrade.sh
137@@ -1,13 +1,23 @@
138-# Use colors, but only if connected to a terminal, and that terminal
139-# supports them.
140+#!/usr/bin/env zsh
141+
142+cd "$ZSH"
143+
144+# Use colors, but only if connected to a terminal
145+# and that terminal supports them.
146+
147+local -a RAINBOW
148+local RED GREEN YELLOW BLUE UNDER BOLD RESET
149+
150 if [ -t 1 ]; then
151-  RB_RED=$(printf '\033[38;5;196m')
152-  RB_ORANGE=$(printf '\033[38;5;202m')
153-  RB_YELLOW=$(printf '\033[38;5;226m')
154-  RB_GREEN=$(printf '\033[38;5;082m')
155-  RB_BLUE=$(printf '\033[38;5;021m')
156-  RB_INDIGO=$(printf '\033[38;5;093m')
157-  RB_VIOLET=$(printf '\033[38;5;163m')
158+  RAINBOW=(
159+    "$(printf '\033[38;5;196m')"
160+    "$(printf '\033[38;5;202m')"
161+    "$(printf '\033[38;5;226m')"
162+    "$(printf '\033[38;5;082m')"
163+    "$(printf '\033[38;5;021m')"
164+    "$(printf '\033[38;5;093m')"
165+    "$(printf '\033[38;5;163m')"
166+  )
167 
168   RED=$(printf '\033[31m')
169   GREEN=$(printf '\033[32m')
170@@ -16,25 +26,19 @@ if [ -t 1 ]; then
171   BOLD=$(printf '\033[1m')
172   UNDER=$(printf '\033[4m')
173   RESET=$(printf '\033[m')
174-else
175-  RB_RED=""
176-  RB_ORANGE=""
177-  RB_YELLOW=""
178-  RB_GREEN=""
179-  RB_BLUE=""
180-  RB_INDIGO=""
181-  RB_VIOLET=""
182-
183-  RED=""
184-  GREEN=""
185-  YELLOW=""
186-  BLUE=""
187-  UNDER=""
188-  BOLD=""
189-  RESET=""
190 fi
191 
192-cd "$ZSH"
193+# Update upstream remote to ohmyzsh org
194+git remote -v | while read remote url _; do
195+  case "$url" in
196+  https://github.com/robbyrussell/oh-my-zsh(|.git))
197+    git remote set-url "$remote" "https://github.com/ohmyzsh/ohmyzsh.git"
198+    break ;;
199+  git@github.com:robbyrussell/oh-my-zsh(|.git))
200+    git remote set-url "$remote" "git@github.com:ohmyzsh/ohmyzsh.git"
201+    break ;;
202+  esac
203+done
204 
205 # Set git-config values known to fix git errors
206 # Line endings (#4069)
207@@ -45,30 +49,36 @@ git config fsck.zeroPaddedFilemode ignore
208 git config fetch.fsck.zeroPaddedFilemode ignore
209 git config receive.fsck.zeroPaddedFilemode ignore
210 # autostash on rebase (#7172)
211-resetAutoStash=$(git config --bool rebase.autoStash 2>&1)
212+resetAutoStash=$(git config --bool rebase.autoStash 2>/dev/null)
213 git config rebase.autoStash true
214 
215-# Update upstream remote to ohmyzsh org
216-remote=$(git remote -v | awk '/https:\/\/github\.com\/robbyrussell\/oh-my-zsh\.git/{ print $1; exit }')
217-if [ -n "$remote" ]; then
218-  git remote set-url "$remote" "https://github.com/ohmyzsh/ohmyzsh.git"
219-fi
220+local ret=0
221 
222+# Update Oh My Zsh
223 printf "${BLUE}%s${RESET}\n" "Updating Oh My Zsh"
224-if git pull --rebase --stat origin master
225-then
226-  printf '%s         %s__      %s           %s        %s       %s     %s__   %s\n' $RB_RED $RB_ORANGE $RB_YELLOW $RB_GREEN $RB_BLUE $RB_INDIGO $RB_VIOLET $RB_RESET
227-  printf '%s  ____  %s/ /_    %s ____ ___  %s__  __  %s ____  %s_____%s/ /_  %s\n' $RB_RED $RB_ORANGE $RB_YELLOW $RB_GREEN $RB_BLUE $RB_INDIGO $RB_VIOLET $RB_RESET
228-  printf '%s / __ \%s/ __ \  %s / __ `__ \%s/ / / / %s /_  / %s/ ___/%s __ \ %s\n' $RB_RED $RB_ORANGE $RB_YELLOW $RB_GREEN $RB_BLUE $RB_INDIGO $RB_VIOLET $RB_RESET
229-  printf '%s/ /_/ /%s / / / %s / / / / / /%s /_/ / %s   / /_%s(__  )%s / / / %s\n' $RB_RED $RB_ORANGE $RB_YELLOW $RB_GREEN $RB_BLUE $RB_INDIGO $RB_VIOLET $RB_RESET
230-  printf '%s\____/%s_/ /_/ %s /_/ /_/ /_/%s\__, / %s   /___/%s____/%s_/ /_/  %s\n' $RB_RED $RB_ORANGE $RB_YELLOW $RB_GREEN $RB_BLUE $RB_INDIGO $RB_VIOLET $RB_RESET
231-  printf '%s    %s        %s           %s /____/ %s       %s     %s          %s\n' $RB_RED $RB_ORANGE $RB_YELLOW $RB_GREEN $RB_BLUE $RB_INDIGO $RB_VIOLET $RB_RESET
232-  printf "${BLUE}%s\n" "Hooray! Oh My Zsh has been updated and/or is at the current version."
233-  printf "${BLUE}${BOLD}%s ${UNDER}%s${RESET}\n" "To keep up on the latest news and updates, follow us on Twitter:" "https://twitter.com/ohmyzsh"
234+last_commit=$(git rev-parse HEAD)
235+if git pull --rebase --stat origin master; then
236+  # Check if it was really updated or not