upgrade.sh 8.9 KB

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