upgrade.sh 6.5 KB

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