check_for_upgrade.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # Migrate .zsh-update file to $ZSH_CACHE_DIR
  2. if [[ -f ~/.zsh-update && ! -f "${ZSH_CACHE_DIR}/.zsh-update" ]]; then
  3. mv ~/.zsh-update "${ZSH_CACHE_DIR}/.zsh-update"
  4. fi
  5. # Get user's update preferences
  6. #
  7. # Supported update modes:
  8. # - prompt (default): the user is asked before updating when it's time to update
  9. # - auto: the update is performed automatically when it's time
  10. # - reminder: a reminder is shown to the user when it's time to update
  11. # - disabled: automatic update is turned off
  12. zstyle -s ':omz:update' mode update_mode || {
  13. update_mode=prompt
  14. # If the mode zstyle setting is not set, support old-style settings
  15. [[ "$DISABLE_UPDATE_PROMPT" != true ]] || update_mode=auto
  16. [[ "$DISABLE_AUTO_UPDATE" != true ]] || update_mode=disabled
  17. }
  18. # Cancel update if:
  19. # - the automatic update is disabled.
  20. # - the current user doesn't have write permissions nor owns the $ZSH directory.
  21. # - is not run from a tty
  22. # - git is unavailable on the system.
  23. if [[ "$update_mode" = disabled ]] \
  24. || [[ ! -w "$ZSH" || ! -O "$ZSH" ]] \
  25. || [[ ! -t 1 ]] \
  26. || ! command git --version 2>&1 >/dev/null; then
  27. unset update_mode
  28. return
  29. fi
  30. function current_epoch() {
  31. zmodload zsh/datetime
  32. echo $(( EPOCHSECONDS / 60 / 60 / 24 ))
  33. }
  34. function is_update_available() {
  35. local branch
  36. branch=${"$(builtin cd -q "$ZSH"; git config --local oh-my-zsh.branch)":-master}
  37. local remote remote_url remote_repo
  38. remote=${"$(builtin cd -q "$ZSH"; git config --local oh-my-zsh.remote)":-origin}
  39. remote_url=$(builtin cd -q "$ZSH"; git config remote.$remote.url)
  40. local repo
  41. case "$remote_url" in
  42. https://github.com/*) repo=${${remote_url#https://github.com/}%.git} ;;
  43. git@github.com:*) repo=${${remote_url#git@github.com:}%.git} ;;
  44. *)
  45. # If the remote is not using GitHub we can't check for updates
  46. # Let's assume there are updates
  47. return 0 ;;
  48. esac
  49. # If the remote repo is not the official one, let's assume there are updates available
  50. [[ "$repo" = ohmyzsh/ohmyzsh ]] || return 0
  51. local api_url="https://api.github.com/repos/${repo}/commits/${branch}"
  52. # Get local HEAD. If this fails assume there are updates
  53. local local_head
  54. local_head=$(builtin cd -q "$ZSH"; git rev-parse $branch 2>/dev/null) || return 0
  55. # Get remote HEAD. If no suitable command is found assume there are updates
  56. # On any other error, skip the update (connection may be down)
  57. local remote_head
  58. remote_head=$(
  59. if (( ${+commands[curl]} )); then
  60. curl --connect-timeout 2 -fsSL -H 'Accept: application/vnd.github.v3.sha' $api_url 2>/dev/null
  61. elif (( ${+commands[wget]} )); then
  62. wget -T 2 -O- --header='Accept: application/vnd.github.v3.sha' $api_url 2>/dev/null
  63. elif (( ${+commands[fetch]} )); then
  64. HTTP_ACCEPT='Accept: application/vnd.github.v3.sha' fetch -T 2 -o - $api_url 2>/dev/null
  65. else
  66. exit 0
  67. fi
  68. ) || return 1
  69. # Compare local and remote HEADs (if they're equal there are no updates)
  70. [[ "$local_head" != "$remote_head" ]] || return 1
  71. # If local and remote HEADs don't match, check if there's a common ancestor
  72. # If the merge-base call fails, $remote_head might not be downloaded so assume there are updates
  73. local base
  74. base=$(builtin cd -q "$ZSH"; git merge-base $local_head $remote_head 2>/dev/null) || return 0
  75. # If the common ancestor ($base) is not $remote_head,
  76. # the local HEAD is older than the remote HEAD
  77. [[ $base != $remote_head ]]
  78. }
  79. function update_last_updated_file() {
  80. echo "LAST_EPOCH=$(current_epoch)" >! "${ZSH_CACHE_DIR}/.zsh-update"
  81. }
  82. function update_ohmyzsh() {
  83. zstyle -s ':omz:update' verbose verbose_mode || verbose_mode=default
  84. if ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh" -i -v $verbose_mode; then
  85. update_last_updated_file
  86. fi
  87. }
  88. function has_typed_input() {
  89. # Created by Philippe Troin <phil@fifi.org>
  90. # https://zsh.org/mla/users/2022/msg00062.html
  91. emulate -L zsh
  92. zmodload zsh/zselect
  93. # Back up stty settings prior to disabling canonical mode
  94. # Consider that no input can be typed if stty fails
  95. # (this might happen if stdin is not a terminal)
  96. local termios
  97. termios=$(stty --save 2>/dev/null) || return 1
  98. {
  99. # Disable canonical mode so that typed input counts
  100. # regardless of whether Enter was pressed
  101. stty -icanon
  102. # Poll stdin (fd 0) for data ready to be read
  103. zselect -t 0 -r 0
  104. return $?
  105. } always {
  106. # Restore stty settings
  107. stty $termios
  108. }
  109. }
  110. () {
  111. emulate -L zsh
  112. local epoch_target mtime option LAST_EPOCH
  113. # Remove lock directory if older than a day
  114. zmodload zsh/datetime
  115. zmodload -F zsh/stat b:zstat
  116. if mtime=$(zstat +mtime "$ZSH/log/update.lock" 2>/dev/null); then
  117. if (( (mtime + 3600 * 24) < EPOCHSECONDS )); then
  118. command rm -rf "$ZSH/log/update.lock"
  119. fi
  120. fi
  121. # Check for lock directory
  122. if ! command mkdir "$ZSH/log/update.lock" 2>/dev/null; then
  123. return
  124. fi
  125. # Remove lock directory on exit. `return $ret` is important for when trapping a SIGINT:
  126. # The return status from the function is handled specially. If it is zero, the signal is
  127. # assumed to have been handled, and execution continues normally. Otherwise, the shell
  128. # will behave as interrupted except that the return status of the trap is retained.
  129. # This means that for a CTRL+C, the trap needs to return the same exit status so that
  130. # the shell actually exits what it's running.
  131. trap "
  132. ret=\$?
  133. unset update_mode
  134. unset -f current_epoch is_update_available update_last_updated_file update_ohmyzsh 2>/dev/null
  135. command rm -rf '$ZSH/log/update.lock'
  136. return \$ret
  137. " EXIT INT QUIT
  138. # Create or update .zsh-update file if missing or malformed
  139. if ! source "${ZSH_CACHE_DIR}/.zsh-update" 2>/dev/null || [[ -z "$LAST_EPOCH" ]]; then
  140. update_last_updated_file
  141. return
  142. fi
  143. # Number of days before trying to update again
  144. zstyle -s ':omz:update' frequency epoch_target || epoch_target=${UPDATE_ZSH_DAYS:-13}
  145. # Test if enough time has passed until the next update
  146. if (( ( $(current_epoch) - $LAST_EPOCH ) < $epoch_target )); then
  147. return
  148. fi
  149. # Test if Oh My Zsh directory is a git repository
  150. if ! (builtin cd -q "$ZSH" && LANG= git rev-parse &>/dev/null); then
  151. echo >&2 "[oh-my-zsh] Can't update: not a git repository."
  152. return
  153. fi
  154. # Check if there are updates available before proceeding
  155. if ! is_update_available; then
  156. update_last_updated_file
  157. return
  158. fi
  159. # If in reminder mode or user has typed input, show reminder and exit
  160. if [[ "$update_mode" = reminder ]] || has_typed_input; then
  161. printf '\r\e[0K' # move cursor to first column and clear whole line
  162. echo "[oh-my-zsh] It's time to update! You can do that by running \`omz update\`"
  163. return 0
  164. fi
  165. # Don't ask for confirmation before updating if in auto mode
  166. if [[ "$update_mode" = auto ]]; then
  167. update_ohmyzsh
  168. return $?
  169. fi
  170. # Ask for confirmation and only update on 'y', 'Y' or Enter
  171. # Otherwise just show a reminder for how to update
  172. echo -n "[oh-my-zsh] Would you like to update? [Y/n] "
  173. read -r -k 1 option
  174. [[ "$option" = $'\n' ]] || echo
  175. case "$option" in
  176. [yY$'\n']) update_ohmyzsh ;;
  177. [nN]) update_last_updated_file ;&
  178. *) echo "[oh-my-zsh] You can update manually by running \`omz update\`" ;;
  179. esac
  180. }
  181. unset update_mode
  182. unset -f current_epoch is_update_available update_last_updated_file update_ohmyzsh