upgrade.sh 8.3 KB

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