Update 13.05.2022
This commit is contained in:
@@ -36,11 +36,11 @@ function current_epoch() {
|
||||
|
||||
function is_update_available() {
|
||||
local branch
|
||||
branch=${"$(cd -q "$ZSH"; git config --local oh-my-zsh.branch)":-master}
|
||||
branch=${"$(builtin cd -q "$ZSH"; git config --local oh-my-zsh.branch)":-master}
|
||||
|
||||
local remote remote_url remote_repo
|
||||
remote=${"$(cd -q "$ZSH"; git config --local oh-my-zsh.remote)":-origin}
|
||||
remote_url=$(cd -q "$ZSH"; git config remote.$remote.url)
|
||||
remote=${"$(builtin cd -q "$ZSH"; git config --local oh-my-zsh.remote)":-origin}
|
||||
remote_url=$(builtin cd -q "$ZSH"; git config remote.$remote.url)
|
||||
|
||||
local repo
|
||||
case "$remote_url" in
|
||||
@@ -58,25 +58,34 @@ function is_update_available() {
|
||||
|
||||
# Get local HEAD. If this fails assume there are updates
|
||||
local local_head
|
||||
local_head=$(cd -q "$ZSH"; git rev-parse $branch 2>/dev/null) || return 0
|
||||
local_head=$(builtin cd -q "$ZSH"; git rev-parse $branch 2>/dev/null) || return 0
|
||||
|
||||
# Get remote HEAD. If no suitable command is found assume there are updates
|
||||
# On any other error, skip the update (connection may be down)
|
||||
local remote_head
|
||||
remote_head=$(
|
||||
if (( ${+commands[curl]} )); then
|
||||
curl -fsSL -H 'Accept: application/vnd.github.v3.sha' $api_url 2>/dev/null
|
||||
curl -m 2 -fsSL -H 'Accept: application/vnd.github.v3.sha' $api_url 2>/dev/null
|
||||
elif (( ${+commands[wget]} )); then
|
||||
wget -O- --header='Accept: application/vnd.github.v3.sha' $api_url 2>/dev/null
|
||||
wget -T 2 -O- --header='Accept: application/vnd.github.v3.sha' $api_url 2>/dev/null
|
||||
elif (( ${+commands[fetch]} )); then
|
||||
HTTP_ACCEPT='Accept: application/vnd.github.v3.sha' fetch -o - $api_url 2>/dev/null
|
||||
HTTP_ACCEPT='Accept: application/vnd.github.v3.sha' fetch -T 2 -o - $api_url 2>/dev/null
|
||||
else
|
||||
exit 0
|
||||
fi
|
||||
) || return 1
|
||||
|
||||
# Compare local and remote HEADs
|
||||
[[ "$local_head" != "$remote_head" ]]
|
||||
# Compare local and remote HEADs (if they're equal there are no updates)
|
||||
[[ "$local_head" != "$remote_head" ]] || return 1
|
||||
|
||||
# If local and remote HEADs don't match, check if there's a common ancestor
|
||||
# If the merge-base call fails, $remote_head might not be downloaded so assume there are updates
|
||||
local base
|
||||
base=$(builtin cd -q "$ZSH"; git merge-base $local_head $remote_head 2>/dev/null) || return 0
|
||||
|
||||
# If the common ancestor ($base) is not $remote_head,
|
||||
# the local HEAD is older than the remote HEAD
|
||||
[[ $base != $remote_head ]]
|
||||
}
|
||||
|
||||
function update_last_updated_file() {
|
||||
@@ -89,6 +98,31 @@ function update_ohmyzsh() {
|
||||
fi
|
||||
}
|
||||
|
||||
function has_typed_input() {
|
||||
# Created by Philippe Troin <phil@fifi.org>
|
||||
# https://zsh.org/mla/users/2022/msg00062.html
|
||||
emulate -L zsh
|
||||
zmodload zsh/zselect
|
||||
|
||||
# Back up stty settings prior to disabling canonical mode
|
||||
# Consider that no input can be typed if stty fails
|
||||
# (this might happen if stdin is not a terminal)
|
||||
local termios
|
||||
termios=$(stty --save 2>/dev/null) || return 1
|
||||
{
|
||||
# Disable canonical mode so that typed input counts
|
||||
# regardless of whether Enter was pressed
|
||||
stty -icanon
|
||||
|
||||
# Poll stdin (fd 0) for data ready to be read
|
||||
zselect -t 0 -r 0
|
||||
return $?
|
||||
} always {
|
||||
# Restore stty settings
|
||||
stty $termios
|
||||
}
|
||||
}
|
||||
|
||||
() {
|
||||
emulate -L zsh
|
||||
|
||||
@@ -136,7 +170,7 @@ function update_ohmyzsh() {
|
||||
fi
|
||||
|
||||
# Test if Oh My Zsh directory is a git repository
|
||||
if ! (cd -q "$ZSH" && LANG= git rev-parse &>/dev/null); then
|
||||
if ! (builtin cd -q "$ZSH" && LANG= git rev-parse &>/dev/null); then
|
||||
echo >&2 "[oh-my-zsh] Can't update: not a git repository."
|
||||
return
|
||||
fi
|
||||
@@ -146,26 +180,29 @@ function update_ohmyzsh() {
|
||||
return
|
||||
fi
|
||||
|
||||
# Ask for confirmation before updating unless in auto mode
|
||||
# If in reminder mode or user has typed input, show reminder and exit
|
||||
if [[ "$update_mode" = reminder ]] || has_typed_input; then
|
||||
printf '\r\e[0K' # move cursor to first column and clear whole line
|
||||
echo "[oh-my-zsh] It's time to update! You can do that by running \`omz update\`"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Don't ask for confirmation before updating if in auto mode
|
||||
if [[ "$update_mode" = auto ]]; then
|
||||
update_ohmyzsh
|
||||
elif [[ "$update_mode" = reminder ]]; then
|
||||
echo "[oh-my-zsh] It's time to update! You can do that by running \`omz update\`"
|
||||
else
|
||||
# input sink to swallow all characters typed before the prompt
|
||||
# and add a newline if there wasn't one after characters typed
|
||||
while read -t -k 1 option; do true; done
|
||||
[[ "$option" != ($'\n'|"") ]] && echo
|
||||
|
||||
echo -n "[oh-my-zsh] Would you like to update? [Y/n] "
|
||||
read -r -k 1 option
|
||||
[[ "$option" != $'\n' ]] && echo
|
||||
case "$option" in
|
||||
[yY$'\n']) update_ohmyzsh ;;
|
||||
[nN]) update_last_updated_file ;&
|
||||
*) echo "[oh-my-zsh] You can update manually by running \`omz update\`" ;;
|
||||
esac
|
||||
return $?
|
||||
fi
|
||||
|
||||
# Ask for confirmation and only update on 'y', 'Y' or Enter
|
||||
# Otherwise just show a reminder for how to update
|
||||
echo -n "[oh-my-zsh] Would you like to update? [Y/n] "
|
||||
read -r -k 1 option
|
||||
[[ "$option" = $'\n' ]] || echo
|
||||
case "$option" in
|
||||
[yY$'\n']) update_ohmyzsh ;;
|
||||
[nN]) update_last_updated_file ;&
|
||||
*) echo "[oh-my-zsh] You can update manually by running \`omz update\`" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
unset update_mode
|
||||
|
||||
Reference in New Issue
Block a user