upgrade.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #!/usr/bin/env zsh
  2. set +u # disable nounset
  3. local ret=0 # exit code
  4. # Protect against running with shells other than zsh
  5. if [ -z "$ZSH_VERSION" ]; then
  6. exec zsh "$0" "$@"
  7. fi
  8. # Protect against unwanted sourcing
  9. case "$ZSH_EVAL_CONTEXT" in
  10. *:file) echo "error: this file should not be sourced" && return ;;
  11. esac
  12. cd "$ZSH"
  13. verbose_mode="default"
  14. interactive=false
  15. while getopts "v:i" opt; do
  16. case $opt in
  17. v)
  18. if [[ $OPTARG == default || $OPTARG == minimal || $OPTARG == silent ]]; then
  19. verbose_mode=$OPTARG
  20. else
  21. echo "[oh-my-zsh] update verbosity '$OPTARG' is not valid"
  22. echo "[oh-my-zsh] valid options are 'default', 'minimal' and 'silent'"
  23. fi
  24. ;;
  25. i) interactive=true ;;
  26. esac
  27. done
  28. # Use colors, but only if connected to a terminal
  29. # and that terminal supports them.
  30. # The [ -t 1 ] check only works when the function is not called from
  31. # a subshell (like in `$(...)` or `(...)`, so this hack redefines the
  32. # function at the top level to always return false when stdout is not
  33. # a tty.
  34. if [ -t 1 ]; then
  35. is_tty() {
  36. true
  37. }
  38. else
  39. is_tty() {
  40. false
  41. }
  42. fi
  43. # This function uses the logic from supports-hyperlinks[1][2], which is
  44. # made by Kat Marchán (@zkat) and licensed under the Apache License 2.0.
  45. # [1] https://github.com/zkat/supports-hyperlinks
  46. # [2] https://crates.io/crates/supports-hyperlinks
  47. #
  48. # Copyright (c) 2021 Kat Marchán
  49. #
  50. # Licensed under the Apache License, Version 2.0 (the "License");
  51. # you may not use this file except in compliance with the License.
  52. # You may obtain a copy of the License at
  53. #
  54. # http://www.apache.org/licenses/LICENSE-2.0
  55. #
  56. # Unless required by applicable law or agreed to in writing, software
  57. # distributed under the License is distributed on an "AS IS" BASIS,
  58. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  59. # See the License for the specific language governing permissions and
  60. # limitations under the License.
  61. supports_hyperlinks() {
  62. # $FORCE_HYPERLINK must be set and be non-zero (this acts as a logic bypass)
  63. if [ -n "$FORCE_HYPERLINK" ]; then
  64. [ "$FORCE_HYPERLINK" != 0 ]
  65. return $?
  66. fi
  67. # If stdout is not a tty, it doesn't support hyperlinks
  68. is_tty || return 1
  69. # DomTerm terminal emulator (domterm.org)
  70. if [ -n "$DOMTERM" ]; then
  71. return 0
  72. fi
  73. # VTE-based terminals above v0.50 (Gnome Terminal, Guake, ROXTerm, etc)
  74. if [ -n "$VTE_VERSION" ]; then
  75. [ $VTE_VERSION -ge 5000 ]
  76. return $?
  77. fi
  78. # If $TERM_PROGRAM is set, these terminals support hyperlinks
  79. case "$TERM_PROGRAM" in
  80. Hyper|iTerm.app|terminology|WezTerm) return 0 ;;
  81. esac
  82. # kitty supports hyperlinks
  83. if [ "$TERM" = xterm-kitty ]; then
  84. return 0
  85. fi
  86. # Windows Terminal also supports hyperlinks
  87. if [ -n "$WT_SESSION" ]; then
  88. return 0
  89. fi
  90. # Konsole supports hyperlinks, but it's an opt-in setting that can't be detected
  91. # https://github.com/ohmyzsh/ohmyzsh/issues/10964
  92. # if [ -n "$KONSOLE_VERSION" ]; then
  93. # return 0
  94. # fi
  95. return 1
  96. }
  97. # Adapted from code and information by Anton Kochkov (@XVilka)
  98. # Source: https://gist.github.com/XVilka/8346728
  99. supports_truecolor() {
  100. case "$COLORTERM" in
  101. truecolor|24bit) return 0 ;;
  102. esac
  103. case "$TERM" in
  104. iterm |\
  105. tmux-truecolor |\
  106. linux-truecolor |\
  107. xterm-truecolor |\
  108. screen-truecolor) return 0 ;;
  109. esac
  110. return 1
  111. }
  112. fmt_link() {
  113. # $1: text, $2: url, $3: fallback mode
  114. if supports_hyperlinks; then
  115. printf '\033]8;;%s\033\\%s\033]8;;\033\\\n' "$2" "$1"
  116. return
  117. fi
  118. case "$3" in
  119. --text) printf '%s\n' "$1" ;;
  120. --url|*) fmt_underline "$2" ;;
  121. esac
  122. }
  123. fmt_underline() {
  124. is_tty && printf '\033[4m%s\033[24m\n' "$*" || printf '%s\n' "$*"
  125. }
  126. setopt typeset_silent
  127. typeset -a RAINBOW
  128. if is_tty; then
  129. if supports_truecolor; then
  130. RAINBOW=(
  131. "$(printf '\033[38;2;255;0;0m')"
  132. "$(printf '\033[38;2;255;97;0m')"
  133. "$(printf '\033[38;2;247;255;0m')"
  134. "$(printf '\033[38;2;0;255;30m')"
  135. "$(printf '\033[38;2;77;0;255m')"
  136. "$(printf '\033[38;2;168;0;255m')"
  137. "$(printf '\033[38;2;245;0;172m')"
  138. )
  139. else
  140. RAINBOW=(
  141. "$(printf '\033[38;5;196m')"
  142. "$(printf '\033[38;5;202m')"
  143. "$(printf '\033[38;5;226m')"
  144. "$(printf '\033[38;5;082m')"
  145. "$(printf '\033[38;5;021m')"
  146. "$(printf '\033[38;5;093m')"
  147. "$(printf '\033[38;5;163m')"
  148. )
  149. fi
  150. RED=$(printf '\033[31m')
  151. GREEN=$(printf '\033[32m')
  152. YELLOW=$(printf '\033[33m')
  153. BLUE=$(printf '\033[34m')
  154. BOLD=$(printf '\033[1m')
  155. RESET=$(printf '\033[0m')
  156. fi
  157. # Update upstream remote to ohmyzsh org
  158. git remote -v | while read remote url extra; do
  159. case "$url" in
  160. git://github.com/robbyrussell/oh-my-zsh(|.git))
  161. # Update out-of-date "unauthenticated git protocol on port 9418" to https
  162. git remote set-url "$remote" "https://github.com/ohmyzsh/ohmyzsh.git" ;;
  163. https://github.com/robbyrussell/oh-my-zsh(|.git))
  164. git remote set-url "$remote" "https://github.com/ohmyzsh/ohmyzsh.git" ;;
  165. git@github.com:robbyrussell/oh-my-zsh(|.git))
  166. git remote set-url "$remote" "git@github.com:ohmyzsh/ohmyzsh.git" ;;
  167. https://github.com/ohmyzsh/ohmyzsh(|.git)) ;;
  168. git@github.com:ohmyzsh/ohmyzsh(|.git)) ;;
  169. *) continue ;;
  170. esac
  171. # If we reach this point we have found the proper ohmyzsh upstream remote. If we don't,
  172. # we'll only update from the set remote if `oh-my-zsh.remote` has been set to a remote,
  173. # as when installing from a fork.
  174. git config --local oh-my-zsh.remote "$remote"
  175. break
  176. done
  177. # Set git-config values known to fix git errors
  178. # Line endings (#4069)
  179. git config core.eol lf
  180. git config core.autocrlf false
  181. # zeroPaddedFilemode fsck errors (#4963)
  182. git config fsck.zeroPaddedFilemode ignore
  183. git config fetch.fsck.zeroPaddedFilemode ignore
  184. git config receive.fsck.zeroPaddedFilemode ignore
  185. # autostash on rebase (#7172)
  186. resetAutoStash=$(git config --bool rebase.autoStash 2>/dev/null)
  187. git config rebase.autoStash true
  188. local ret=0
  189. # repository settings
  190. remote=${"$(git config --local oh-my-zsh.remote)":-origin}
  191. branch=${"$(git config --local oh-my-zsh.branch)":-master}
  192. # repository state
  193. last_head=$(git symbolic-ref --quiet --short HEAD || git rev-parse HEAD)
  194. # checkout update branch
  195. git checkout -q "$branch" -- || exit 1
  196. # branch commit before update (used in changelog)
  197. last_commit=$(git rev-parse "$branch")
  198. # Update Oh My Zsh
  199. if [[ $verbose_mode != silent ]]; then
  200. printf "${BLUE}%s${RESET}\n" "Updating Oh My Zsh"
  201. fi
  202. if LANG= git pull --quiet --rebase $remote $branch; then
  203. # Check if it was really updated or not
  204. if [[ "$(git rev-parse HEAD)" = "$last_commit" ]]; then
  205. message="Oh My Zsh is already at the latest version."
  206. else
  207. message="Hooray! Oh My Zsh has been updated!"
  208. # Save the commit prior to updating
  209. git config oh-my-zsh.lastVersion "$last_commit"
  210. # Print changelog to the terminal
  211. if [[ $interactive == true && $verbose_mode == default ]]; then
  212. "$ZSH/tools/changelog.sh" HEAD "$last_commit"
  213. fi
  214. if [[ $verbose_mode != silent ]]; then
  215. printf "${BLUE}%s \`${BOLD}%s${RESET}${BLUE}\`${RESET}\n" "You can see the changelog with" "omz changelog"
  216. fi
  217. fi
  218. if [[ $verbose_mode == default ]]; then
  219. printf '%s %s__ %s %s %s %s %s__ %s\n' $RAINBOW $RESET
  220. printf '%s ____ %s/ /_ %s ____ ___ %s__ __ %s ____ %s_____%s/ /_ %s\n' $RAINBOW $RESET
  221. printf '%s / __ \\%s/ __ \\ %s / __ `__ \\%s/ / / / %s /_ / %s/ ___/%s __ \\ %s\n' $RAINBOW $RESET
  222. printf '%s/ /_/ /%s / / / %s / / / / / /%s /_/ / %s / /_%s(__ )%s / / / %s\n' $RAINBOW $RESET
  223. printf '%s\\____/%s_/ /_/ %s /_/ /_/ /_/%s\\__, / %s /___/%s____/%s_/ /_/ %s\n' $RAINBOW $RESET
  224. printf '%s %s %s %s /____/ %s %s %s %s\n' $RAINBOW $RESET
  225. printf '\n'
  226. printf "${BLUE}%s${RESET}\n\n" "$message"
  227. printf "${BLUE}${BOLD}%s %s${RESET}\n" "To keep up with the latest news and updates, follow us on Twitter:" "$(fmt_link @ohmyzsh https://twitter.com/ohmyzsh)"
  228. printf "${BLUE}${BOLD}%s %s${RESET}\n" "Want to get involved in the community? Join our Discord:" "$(fmt_link "Discord server" https://discord.gg/ohmyzsh)"
  229. printf "${BLUE}${BOLD}%s %s${RESET}\n" "Get your Oh My Zsh swag at:" "$(fmt_link "Planet Argon Shop" https://shop.planetargon.com/collections/oh-my-zsh)"
  230. elif [[ $verbose_mode == minimal ]]; then
  231. printf "${BLUE}%s${RESET}\n" "$message"
  232. fi
  233. else
  234. ret=$?
  235. printf "${RED}%s${RESET}\n" 'There was an error updating. Try again later?'
  236. fi
  237. # go back to HEAD previous to update
  238. git checkout -q "$last_head" --
  239. # Unset git-config values set just for the upgrade
  240. case "$resetAutoStash" in
  241. "") git config --unset rebase.autoStash ;;
  242. *) git config rebase.autoStash "$resetAutoStash" ;;
  243. esac
  244. # Exit with `1` if the update failed
  245. exit $ret