upgrade.sh 7.6 KB

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