3c777ebf1b24826363bea6bc29afa9ba038b10e8

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

Message

update: refactor and fix logic in check_for_upgrade.sh (#8939)

Diff

This diff is truncated to protect this page.

  1diff --git a/oh-my-zsh.sh b/oh-my-zsh.sh
  2index 4025f002588b148047720dab2c8d843f16094e65..4b4191c01469b7265c7a68b4ecb8c8d45703e601 100644
  3--- a/oh-my-zsh.sh
  4+++ b/oh-my-zsh.sh
  5@@ -4,11 +4,6 @@ if [[ -z "$ZSH_CACHE_DIR" ]]; then
  6   ZSH_CACHE_DIR="$ZSH/cache"
  7 fi
  8 
  9-# Migrate .zsh-update file to $ZSH_CACHE_DIR
 10-if [ -f ~/.zsh-update ] && [ ! -f ${ZSH_CACHE_DIR}/.zsh-update ]; then
 11-    mv ~/.zsh-update ${ZSH_CACHE_DIR}/.zsh-update
 12-fi
 13-
 14 # Check for updates on initial load...
 15 if [ "$DISABLE_AUTO_UPDATE" != "true" ]; then
 16   env ZSH=$ZSH ZSH_CACHE_DIR=$ZSH_CACHE_DIR DISABLE_UPDATE_PROMPT=$DISABLE_UPDATE_PROMPT zsh -f $ZSH/tools/check_for_upgrade.sh
 17diff --git a/tools/check_for_upgrade.sh b/tools/check_for_upgrade.sh
 18index c8dedcf77d0fcfb0ace9c759925ab220fa0b3a64..872f5e820ecfebdd63d95a5051936d67c7444baf 100644
 19--- a/tools/check_for_upgrade.sh
 20+++ b/tools/check_for_upgrade.sh
 21@@ -1,62 +1,88 @@
 22-#!/usr/bin/env zsh
 23+# Migrate .zsh-update file to $ZSH_CACHE_DIR
 24+if [[ -f ~/.zsh-update && ! -f "${ZSH_CACHE_DIR}/.zsh-update" ]]; then
 25+    mv ~/.zsh-update "${ZSH_CACHE_DIR}/.zsh-update"
 26+fi
 27+
 28+# Cancel update if:
 29+# - the automatic update is disabled.
 30+# - the current user doesn't have write permissions nor owns the $ZSH directory.
 31+# - git is unavailable on the system.
 32+if [[ "$DISABLE_AUTO_UPDATE" = true ]] \
 33+   || [[ ! -w "$ZSH" || ! -O "$ZSH" ]] \
 34+   || ! command -v git &>/dev/null; then
 35+    return
 36+fi
 37 
 38-zmodload zsh/datetime
 39 
 40-function _current_epoch() {
 41-  echo $(( $EPOCHSECONDS / 60 / 60 / 24 ))
 42+function current_epoch() {
 43+    zmodload zsh/datetime
 44+    echo $(( EPOCHSECONDS / 60 / 60 / 24 ))
 45 }
 46 
 47-function _update_zsh_update() {
 48-  echo "LAST_EPOCH=$(_current_epoch)" >! ${ZSH_CACHE_DIR}/.zsh-update
 49+function update_last-updated_file() {
 50+    echo "LAST_EPOCH=$(current_epoch)" >! "${ZSH_CACHE_DIR}/.zsh-update"
 51 }
 52 
 53-function _upgrade_zsh() {
 54-  env ZSH=$ZSH sh $ZSH/tools/upgrade.sh
 55-  # update the zsh file
 56-  _update_zsh_update
 57+function update_ohmyzsh() {
 58+    ZSH="$ZSH" sh "$ZSH/tools/upgrade.sh"
 59+    update_last-updated_file
 60 }
 61 
 62-epoch_target=$UPDATE_ZSH_DAYS
 63-if [[ -z "$epoch_target" ]]; then
 64-  # Default to old behavior
 65-  epoch_target=13
 66-fi
 67+() {
 68+    emulate -L zsh
 69+
 70+    local epoch_target mtime option LAST_EPOCH
 71+
 72+    # Remove lock directory if older than a day
 73+    zmodload zsh/datetime
 74+    zmodload -F zsh/stat b:zstat
 75+    if mtime=$(zstat +mtime "$ZSH/log/update.lock" 2>/dev/null); then
 76+        if (( (mtime + 3600 * 24) < EPOCHSECONDS )); then
 77+            command rm -rf "$ZSH/log/update.lock"
 78+        fi
 79+    fi
 80 
 81-# Cancel upgrade if the current user doesn't have write permissions for the
 82-# oh-my-zsh directory.
 83-[[ -w "$ZSH" ]] || return 0
 84+    # Check for lock directory
 85+    if ! command mkdir "$ZSH/log/update.lock" 2>/dev/null; then
 86+        return
 87+    fi
 88 
 89-# Cancel upgrade if git is unavailable on the system
 90-whence git >/dev/null || return 0
 91+    # Remove lock directory on exit. `return 1` is important for when trapping a SIGINT:
 92+    #  The return status from the function is handled specially. If it is zero, the signal is
 93+    #  assumed to have been handled, and execution continues normally. Otherwise, the shell
 94+    #  will behave as interrupted except that the return status of the trap is retained.
 95+    trap "rm -rf '$ZSH/log/update.lock'; return 1" EXIT INT QUIT
 96 
 97-if mkdir "$ZSH/log/update.lock" 2>/dev/null; then
 98-  if [ -f ${ZSH_CACHE_DIR}/.zsh-update ]; then
 99-    . ${ZSH_CACHE_DIR}/.zsh-update
100+    # Create or update .zsh-update file if missing or malformed
101+    if ! source "${ZSH_CACHE_DIR}/.zsh-update" 2>/dev/null || [[ -z "$LAST_EPOCH" ]]; then
102+        update_last-updated_file
103+        return
104+    fi
105 
106-    if [[ -z "$LAST_EPOCH" ]]; then
107-      _update_zsh_update
108-      rmdir $ZSH/log/update.lock # TODO: fix later
109-      return 0
110+    # Number of days before trying to update again
111+    epoch_target=${UPDATE_ZSH_DAYS:-13}
112+    # Test if enough time has passed until the next update
113+    if (( ( $(current_epoch) - $LAST_EPOCH ) < $epoch_target )); then
114+        return
115     fi
116 
117-    epoch_diff=$(($(_current_epoch) - $LAST_EPOCH))
118-    if [ $epoch_diff -gt $epoch_target ]; then
119-      if [ "$DISABLE_UPDATE_PROMPT" = "true" ]; then
120-        _upgrade_zsh