check_for_upgrade.sh 5.2 KB

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