check_for_upgrade.sh 9.5 KB

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