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