upgrade.sh 7.4 KB

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