upgrade.sh 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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|vscode) return 0 ;;
  81. esac
  82. # These termcap entries support hyperlinks
  83. case "$TERM" in
  84. xterm-kitty|alacritty|alacritty-direct) return 0 ;;
  85. esac
  86. # xfce4-terminal supports hyperlinks
  87. if [ "$COLORTERM" = "xfce4-terminal" ]; then
  88. return 0
  89. fi
  90. # Windows Terminal also supports hyperlinks
  91. if [ -n "$WT_SESSION" ]; then
  92. return 0
  93. fi
  94. # Konsole supports hyperlinks, but it's an opt-in setting that can't be detected
  95. # https://github.com/ohmyzsh/ohmyzsh/issues/10964
  96. # if [ -n "$KONSOLE_VERSION" ]; then
  97. # return 0
  98. # fi
  99. return 1
  100. }
  101. # Adapted from code and information by Anton Kochkov (@XVilka)
  102. # Source: https://gist.github.com/XVilka/8346728
  103. supports_truecolor() {
  104. case "$COLORTERM" in
  105. truecolor|24bit) return 0 ;;
  106. esac
  107. case "$TERM" in
  108. iterm |\
  109. tmux-truecolor |\
  110. linux-truecolor |\
  111. xterm-truecolor |\
  112. screen-truecolor) return 0 ;;
  113. esac
  114. return 1
  115. }
  116. fmt_link() {
  117. # $1: text, $2: url, $3: fallback mode
  118. if supports_hyperlinks; then
  119. printf '\033]8;;%s\033\\%s\033]8;;\033\\\n' "$2" "$1"
  120. return
  121. fi
  122. case "$3" in
  123. --text) printf '%s\n' "$1" ;;
  124. --url|*) fmt_underline "$2" ;;
  125. esac
  126. }
  127. fmt_underline() {
  128. is_tty && printf '\033[4m%s\033[24m\n' "$*" || printf '%s\n' "$*"
  129. }
  130. setopt typeset_silent
  131. typeset -a RAINBOW
  132. if is_tty; then
  133. if supports_truecolor; then
  134. RAINBOW=(
  135. "$(printf '\033[38;2;255;0;0m')"
  136. "$(printf '\033[38;2;255;97;0m')"
  137. "$(printf '\033[38;2;247;255;0m')"
  138. "$(printf '\033[38;2;0;255;30m')"
  139. "$(printf '\033[38;2;77;0;255m')"
  140. "$(printf '\033[38;2;168;0;255m')"
  141. "$(printf '\033[38;2;245;0;172m')"
  142. )
  143. else
  144. RAINBOW=(
  145. "$(printf '\033[38;5;196m')"
  146. "$(printf '\033[38;5;202m')"
  147. "$(printf '\033[38;5;226m')"
  148. "$(printf '\033[38;5;082m')"
  149. "$(printf '\033[38;5;021m')"
  150. "$(printf '\033[38;5;093m')"
  151. "$(printf '\033[38;5;163m')"
  152. )
  153. fi
  154. RED=$(printf '\033[31m')
  155. GREEN=$(printf '\033[32m')
  156. YELLOW=$(printf '\033[33m')
  157. BLUE=$(printf '\033[34m')
  158. BOLD=$(printf '\033[1m')
  159. RESET=$(printf '\033[0m')
  160. fi
  161. # Update upstream remote to ohmyzsh org
  162. git remote -v | while read remote url extra; do
  163. case "$url" in
  164. git://github.com/robbyrussell/oh-my-zsh(|.git))
  165. # Update out-of-date "unauthenticated git protocol on port 9418" to https
  166. git remote set-url "$remote" "https://github.com/ohmyzsh/ohmyzsh.git" ;;
  167. https://github.com/robbyrussell/oh-my-zsh(|.git))
  168. git remote set-url "$remote" "https://github.com/ohmyzsh/ohmyzsh.git" ;;
  169. git@github.com:robbyrussell/oh-my-zsh(|.git))
  170. git remote set-url "$remote" "git@github.com:ohmyzsh/ohmyzsh.git" ;;
  171. https://github.com/ohmyzsh/ohmyzsh(|.git)) ;;
  172. git@github.com:ohmyzsh/ohmyzsh(|.git)) ;;
  173. *) continue ;;
  174. esac
  175. # If we reach this point we have found the proper ohmyzsh upstream remote. If we don't,
  176. # we'll only update from the set remote if `oh-my-zsh.remote` has been set to a remote,
  177. # as when installing from a fork.
  178. git config --local oh-my-zsh.remote "$remote"
  179. break
  180. done
  181. # Set git-config values known to fix git errors
  182. # Line endings (#4069)
  183. git config core.eol lf
  184. git config core.autocrlf false
  185. # zeroPaddedFilemode fsck errors (#4963)
  186. git config fsck.zeroPaddedFilemode ignore
  187. git config fetch.fsck.zeroPaddedFilemode ignore
  188. git config receive.fsck.zeroPaddedFilemode ignore
  189. # autostash on rebase (#7172)
  190. resetAutoStash=$(git config --bool rebase.autoStash 2>/dev/null)
  191. git config rebase.autoStash true
  192. local ret=0
  193. # repository settings
  194. remote=${"$(git config --local oh-my-zsh.remote)":-origin}
  195. branch=${"$(git config --local oh-my-zsh.branch)":-master}
  196. # repository state
  197. last_head=$(git symbolic-ref --quiet --short HEAD || git rev-parse HEAD)
  198. # checkout update branch
  199. git checkout -q "$branch" -- || exit 1
  200. # branch commit before update (used in changelog)
  201. last_commit=$(git rev-parse "$branch")
  202. # Update Oh My Zsh
  203. if [[ $verbose_mode != silent ]]; then
  204. printf "${BLUE}%s${RESET}\n" "Updating Oh My Zsh"
  205. fi
  206. if LANG= git pull --quiet --rebase $remote $branch; then
  207. # Check if it was really updated or not
  208. if [[ "$(git rev-parse HEAD)" = "$last_commit" ]]; then
  209. message="Oh My Zsh is already at the latest version."
  210. else
  211. message="Hooray! Oh My Zsh has been updated!"
  212. # Save the commit prior to updating
  213. git config oh-my-zsh.lastVersion "$last_commit"
  214. # Print changelog to the terminal
  215. if [[ $interactive == true && $verbose_mode == default ]]; then
  216. "$ZSH/tools/changelog.sh" HEAD "$last_commit"
  217. fi
  218. if [[ $verbose_mode != silent ]]; then
  219. printf "${BLUE}%s \`${BOLD}%s${RESET}${BLUE}\`${RESET}\n" "You can see the changelog with" "omz changelog"
  220. fi
  221. fi
  222. if [[ $verbose_mode == default ]]; then
  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 '%s / __ \\%s/ __ \\ %s / __ `__ \\%s/ / / / %s /_ / %s/ ___/%s __ \\ %s\n' $RAINBOW $RESET
  226. printf '%s/ /_/ /%s / / / %s / / / / / /%s /_/ / %s / /_%s(__ )%s / / / %s\n' $RAINBOW $RESET
  227. printf '%s\\____/%s_/ /_/ %s /_/ /_/ /_/%s\\__, / %s /___/%s____/%s_/ /_/ %s\n' $RAINBOW $RESET
  228. printf '%s %s %s %s /____/ %s %s %s %s\n' $RAINBOW $RESET
  229. printf '\n'
  230. printf "${BLUE}%s${RESET}\n\n" "$message"
  231. 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)"
  232. 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)"
  233. 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)"
  234. elif [[ $verbose_mode == minimal ]]; then
  235. printf "${BLUE}%s${RESET}\n" "$message"
  236. fi
  237. else
  238. ret=$?
  239. printf "${RED}%s${RESET}\n" 'There was an error updating. Try again later?'
  240. fi
  241. # go back to HEAD previous to update
  242. git checkout -q "$last_head" --
  243. # Unset git-config values set just for the upgrade
  244. case "$resetAutoStash" in
  245. "") git config --unset rebase.autoStash ;;
  246. *) git config rebase.autoStash "$resetAutoStash" ;;
  247. esac
  248. # Exit with `1` if the update failed
  249. exit $ret