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