upgrade.sh 8.6 KB

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