check_for_upgrade.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. # - git is unavailable on the system.
  22. if [[ "$update_mode" = disabled ]] \
  23. || [[ ! -w "$ZSH" || ! -O "$ZSH" ]] \
  24. || ! command -v git &>/dev/null; then
  25. unset update_mode
  26. return
  27. fi
  28. function current_epoch() {
  29. zmodload zsh/datetime
  30. echo $(( EPOCHSECONDS / 60 / 60 / 24 ))
  31. }
  32. function is_update_available() {
  33. local branch
  34. branch=${"$(cd -q "$ZSH"; git config --local oh-my-zsh.branch)":-master}
  35. local remote remote_url remote_repo
  36. remote=${"$(cd -q "$ZSH"; git config --local oh-my-zsh.remote)":-origin}
  37. remote_url=$(cd -q "$ZSH"; git config remote.$remote.url)
  38. local repo
  39. case "$remote_url" in
  40. https://github.com/*) repo=${${remote_url#https://github.com/}%.git} ;;
  41. git@github.com:*) repo=${${remote_url#git@github.com:}%.git} ;;
  42. *)
  43. # If the remote is not using GitHub we can't check for updates
  44. # Let's assume there are updates
  45. return 0 ;;
  46. esac
  47. # If the remote repo is not the official one, let's assume there are updates available
  48. [[ "$repo" = ohmyzsh/ohmyzsh ]] || return 0
  49. local api_url="https://api.github.com/repos/${repo}/commits/${branch}"
  50. # Get local HEAD. If this fails assume there are updates
  51. local local_head
  52. local_head=$(cd -q "$ZSH"; git rev-parse $branch 2>/dev/null) || return 0
  53. # Get remote HEAD. If no suitable command is found assume there are updates
  54. # On any other error, skip the update (connection may be down)
  55. local remote_head
  56. remote_head=$(
  57. if (( ${+commands[curl]} )); then
  58. curl -fsSL -H 'Accept: application/vnd.github.v3.sha' $api_url 2>/dev/null
  59. elif (( ${+commands[wget]} )); then
  60. wget -O- --header='Accept: application/vnd.github.v3.sha' $api_url 2>/dev/null
  61. elif (( ${+commands[fetch]} )); then
  62. HTTP_ACCEPT='Accept: application/vnd.github.v3.sha' fetch -o - $api_url 2>/dev/null
  63. else
  64. exit 0
  65. fi
  66. ) || return 1
  67. # Compare local and remote HEADs
  68. [[ "$local_head" != "$remote_head" ]]
  69. }
  70. function update_last_updated_file() {
  71. echo "LAST_EPOCH=$(current_epoch)" >! "${ZSH_CACHE_DIR}/.zsh-update"
  72. }
  73. function update_ohmyzsh() {
  74. if ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh" --interactive; then
  75. update_last_updated_file
  76. fi
  77. }
  78. () {
  79. emulate -L zsh
  80. local epoch_target mtime option LAST_EPOCH
  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. # Check for lock directory
  90. if ! command mkdir "$ZSH/log/update.lock" 2>/dev/null; then
  91. return
  92. fi
  93. # Remove lock directory on exit. `return $ret` is important for when trapping a SIGINT:
  94. # The return status from the function is handled specially. If it is zero, the signal is
  95. # assumed to have been handled, and execution continues normally. Otherwise, the shell
  96. # will behave as interrupted except that the return status of the trap is retained.
  97. # This means that for a CTRL+C, the trap needs to return the same exit status so that
  98. # the shell actually exits what it's running.
  99. trap "
  100. ret=\$?
  101. unset update_mode
  102. unset -f current_epoch is_update_available update_last_updated_file update_ohmyzsh 2>/dev/null
  103. command rm -rf '$ZSH/log/update.lock'
  104. return \$ret
  105. " EXIT INT QUIT
  106. # Create or update .zsh-update file if missing or malformed
  107. if ! source "${ZSH_CACHE_DIR}/.zsh-update" 2>/dev/null || [[ -z "$LAST_EPOCH" ]]; then
  108. update_last_updated_file
  109. return
  110. fi
  111. # Number of days before trying to update again
  112. zstyle -s ':omz:update' frequency epoch_target || epoch_target=${UPDATE_ZSH_DAYS:-13}
  113. # Test if enough time has passed until the next update
  114. if (( ( $(current_epoch) - $LAST_EPOCH ) < $epoch_target )); then
  115. return
  116. fi
  117. # Test if Oh My Zsh directory is a git repository
  118. if ! (cd -q "$ZSH" && LANG= git rev-parse &>/dev/null); then
  119. echo >&2 "[oh-my-zsh] Can't update: not a git repository."
  120. return
  121. fi
  122. # Check if there are updates available before proceeding
  123. if ! is_update_available; then
  124. return
  125. fi
  126. # Ask for confirmation before updating unless in auto mode
  127. if [[ "$update_mode" = auto ]]; then
  128. update_ohmyzsh
  129. elif [[ "$update_mode" = reminder ]]; then
  130. echo "[oh-my-zsh] It's time to update! You can do that by running \`omz update\`"
  131. else
  132. # input sink to swallow all characters typed before the prompt
  133. # and add a newline if there wasn't one after characters typed
  134. while read -t -k 1 option; do true; done
  135. [[ "$option" != ($'\n'|"") ]] && echo
  136. echo -n "[oh-my-zsh] Would you like to update? [Y/n] "
  137. read -r -k 1 option
  138. [[ "$option" != $'\n' ]] && echo
  139. case "$option" in
  140. [yY$'\n']) update_ohmyzsh ;;
  141. [nN]) update_last_updated_file ;&
  142. *) echo "[oh-my-zsh] You can update manually by running \`omz update\`" ;;
  143. esac
  144. fi
  145. }
  146. unset update_mode
  147. unset -f current_epoch is_update_available update_last_updated_file update_ohmyzsh