From fbdc078fa60c2c34a7f5078846902538d162fbee Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Sun, 9 Jan 2022 23:06:34 +0100 Subject: style: use 24bit colors in Oh My Zsh logo if supported --- tools/install.sh | 62 ++++++++++++++++++++++++++++++++++++++++++-------------- tools/upgrade.sh | 48 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 86 insertions(+), 24 deletions(-) (limited to 'tools') diff --git a/tools/install.sh b/tools/install.sh index 731d89a29..5009bd586 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -127,6 +127,24 @@ supports_hyperlinks() { return 1 } +# Adapted from code and information by Anton Kochkov (@XVilka) +# Source: https://gist.github.com/XVilka/8346728 +supports_truecolor() { + case "$COLORTERM" in + truecolor|24bit) return 0 ;; + esac + + case "$TERM" in + iterm |\ + tmux-truecolor |\ + linux-truecolor |\ + xterm-truecolor |\ + screen-truecolor) return 0 ;; + esac + + return 1 +} + fmt_link() { # $1: text, $2: url, $3: fallback mode if supports_hyperlinks; then @@ -155,7 +173,28 @@ fmt_error() { setup_color() { # Only use colors if connected to a terminal - if is_tty; then + if ! is_tty; then + RAINBOW="" + RED="" + GREEN="" + YELLOW="" + BLUE="" + BOLD="" + RESET="" + return + fi + + if supports_truecolor; then + RAINBOW=" + $(printf '\033[38;2;255;0;0m') + $(printf '\033[38;2;255;97;0m') + $(printf '\033[38;2;247;255;0m') + $(printf '\033[38;2;0;255;30m') + $(printf '\033[38;2;77;0;255m') + $(printf '\033[38;2;168;0;255m') + $(printf '\033[38;2;245;0;172m') + " + else RAINBOW=" $(printf '\033[38;5;196m') $(printf '\033[38;5;202m') @@ -165,21 +204,14 @@ setup_color() { $(printf '\033[38;5;093m') $(printf '\033[38;5;163m') " - RED=$(printf '\033[31m') - GREEN=$(printf '\033[32m') - YELLOW=$(printf '\033[33m') - BLUE=$(printf '\033[34m') - BOLD=$(printf '\033[1m') - RESET=$(printf '\033[m') - else - RAINBOW="" - RED="" - GREEN="" - YELLOW="" - BLUE="" - BOLD="" - RESET="" fi + + RED=$(printf '\033[31m') + GREEN=$(printf '\033[32m') + YELLOW=$(printf '\033[33m') + BLUE=$(printf '\033[34m') + BOLD=$(printf '\033[1m') + RESET=$(printf '\033[0m') } setup_ohmyzsh() { diff --git a/tools/upgrade.sh b/tools/upgrade.sh index 994ffe9c9..25381de7e 100755 --- a/tools/upgrade.sh +++ b/tools/upgrade.sh @@ -86,6 +86,24 @@ supports_hyperlinks() { return 1 } +# Adapted from code and information by Anton Kochkov (@XVilka) +# Source: https://gist.github.com/XVilka/8346728 +supports_truecolor() { + case "$COLORTERM" in + truecolor|24bit) return 0 ;; + esac + + case "$TERM" in + iterm |\ + tmux-truecolor |\ + linux-truecolor |\ + xterm-truecolor |\ + screen-truecolor) return 0 ;; + esac + + return 1 +} + fmt_link() { # $1: text, $2: url, $3: fallback mode if supports_hyperlinks; then @@ -107,15 +125,27 @@ setopt typeset_silent typeset -a RAINBOW if is_tty; then - RAINBOW=( - "$(printf '\033[38;5;196m')" - "$(printf '\033[38;5;202m')" - "$(printf '\033[38;5;226m')" - "$(printf '\033[38;5;082m')" - "$(printf '\033[38;5;021m')" - "$(printf '\033[38;5;093m')" - "$(printf '\033[38;5;163m')" - ) + if supports_truecolor; then + RAINBOW=( + "$(printf '\033[38;2;255;0;0m')" + "$(printf '\033[38;2;255;97;0m')" + "$(printf '\033[38;2;247;255;0m')" + "$(printf '\033[38;2;0;255;30m')" + "$(printf '\033[38;2;77;0;255m')" + "$(printf '\033[38;2;168;0;255m')" + "$(printf '\033[38;2;245;0;172m')" + ) + else + RAINBOW=( + "$(printf '\033[38;5;196m')" + "$(printf '\033[38;5;202m')" + "$(printf '\033[38;5;226m')" + "$(printf '\033[38;5;082m')" + "$(printf '\033[38;5;021m')" + "$(printf '\033[38;5;093m')" + "$(printf '\033[38;5;163m')" + ) + fi RED=$(printf '\033[31m') GREEN=$(printf '\033[32m') -- cgit v1.2.3-70-g09d2 From 9c84c344d762b200de7acc794b9a0e7832144e7a Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Mon, 10 Jan 2022 19:39:05 +0100 Subject: fix: disable `log.showSignature` in `git log` calls --- plugins/git/git.plugin.zsh | 4 +--- themes/Soliah.zsh-theme | 4 +--- themes/avit.zsh-theme | 2 +- themes/dogenpunk.zsh-theme | 4 +--- themes/smt.zsh-theme | 2 +- themes/wedisagree.zsh-theme | 4 +--- tools/changelog.sh | 2 +- 7 files changed, 7 insertions(+), 15 deletions(-) (limited to 'tools') diff --git a/plugins/git/git.plugin.zsh b/plugins/git/git.plugin.zsh index 648fa0a33..8f7e623ec 100644 --- a/plugins/git/git.plugin.zsh +++ b/plugins/git/git.plugin.zsh @@ -24,9 +24,7 @@ compdef _git _git_log_prettily=git-log # Warn if the current branch is a WIP function work_in_progress() { - if $(git log -n 1 2>/dev/null | grep -q -c "\-\-wip\-\-"); then - echo "WIP!!" - fi + command git -c log.showSignature=false log -n 1 2>/dev/null | grep -q -- "--wip--" && echo "WIP!!" } # Check if main exists and use instead of master diff --git a/themes/Soliah.zsh-theme b/themes/Soliah.zsh-theme index 070c54981..c3dd6af89 100644 --- a/themes/Soliah.zsh-theme +++ b/themes/Soliah.zsh-theme @@ -45,9 +45,7 @@ function rvm_gemset() { function git_time_since_commit() { if git rev-parse --git-dir > /dev/null 2>&1; then # Only proceed if there is actually a commit. - if [[ $(git log 2>&1 > /dev/null | grep -c "^fatal: bad default revision") == 0 ]]; then - # Get the last commit. - last_commit=`git log --pretty=format:'%at' -1 2> /dev/null` + if last_commit=`git -c log.showSignature=false log --pretty=format:'%at' -1 2> /dev/null`; then now=`date +%s` seconds_since_last_commit=$((now-last_commit)) diff --git a/themes/avit.zsh-theme b/themes/avit.zsh-theme index d117c4e94..f90ba331b 100644 --- a/themes/avit.zsh-theme +++ b/themes/avit.zsh-theme @@ -31,7 +31,7 @@ function _git_time_since_commit() { local last_commit now seconds_since_last_commit local minutes hours days years commit_age # Only proceed if there is actually a commit. - if last_commit=$(command git -c log.showSignatures=false log --format='%at' -1 2>/dev/null); then + if last_commit=$(command git -c log.showSignature=false log --format='%at' -1 2>/dev/null); then now=$(date +%s) seconds_since_last_commit=$((now-last_commit)) diff --git a/themes/dogenpunk.zsh-theme b/themes/dogenpunk.zsh-theme index 6a9921288..923ca74bc 100644 --- a/themes/dogenpunk.zsh-theme +++ b/themes/dogenpunk.zsh-theme @@ -37,9 +37,7 @@ ZSH_THEME_GIT_TIME_SINCE_COMMIT_NEUTRAL="%{$fg[cyan]%}" function git_time_since_commit() { if git rev-parse --git-dir > /dev/null 2>&1; then # Only proceed if there is actually a commit. - if git log -n 1 > /dev/null 2>&1; then - # Get the last commit. - last_commit=`git log --pretty=format:'%at' -1 2> /dev/null` + if last_commit=`git -c log.showSignature=false log --pretty=format:'%at' -1 2> /dev/null`; then now=`date +%s` seconds_since_last_commit=$((now-last_commit)) diff --git a/themes/smt.zsh-theme b/themes/smt.zsh-theme index 7f54472c6..52e6d9a21 100644 --- a/themes/smt.zsh-theme +++ b/themes/smt.zsh-theme @@ -40,7 +40,7 @@ function git_time_since_commit() { local last_commit seconds_since_last_commit # Only proceed if there is actually a commit - if ! last_commit=$(command git log --pretty=format:'%at' -1 2>/dev/null); then + if ! last_commit=$(command git -c log.showSignature=false log --pretty=format:'%at' -1 2>/dev/null); then echo "[$ZSH_THEME_GIT_TIME_SINCE_COMMIT_NEUTRAL~%{$reset_color%}]" return fi diff --git a/themes/wedisagree.zsh-theme b/themes/wedisagree.zsh-theme index 07006ecd9..e9e9d6ef8 100644 --- a/themes/wedisagree.zsh-theme +++ b/themes/wedisagree.zsh-theme @@ -69,9 +69,7 @@ function rvm_gemset() { function git_time_since_commit() { if git rev-parse --git-dir > /dev/null 2>&1; then # Only proceed if there is actually a commit. - if [[ $(git log 2>&1 > /dev/null | grep -c "^fatal: bad default revision") == 0 ]]; then - # Get the last commit. - last_commit=`git log --pretty=format:'%at' -1 2> /dev/null` + if last_commit=`git -c log.showSignature=false log --pretty=format:'%at' -1 2> /dev/null`; then now=`date +%s` seconds_since_last_commit=$((now-last_commit)) diff --git a/tools/changelog.sh b/tools/changelog.sh index 664f34608..86774a7ea 100755 --- a/tools/changelog.sh +++ b/tools/changelog.sh @@ -414,7 +414,7 @@ function main { # --first-parent: commits from merged branches are omitted local SEP="0mZmAgIcSeP" local -a raw_commits - raw_commits=(${(0)"$(command git log -z \ + raw_commits=(${(0)"$(command git -c log.showSignature=false log -z \ --format="%h${SEP}%D${SEP}%s${SEP}%b" --abbrev=7 \ --no-merges --first-parent $range)"}) -- cgit v1.2.3-70-g09d2 From b7a59e6d5c1a699b972a780b4a4eb4ffd89f22b3 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Tue, 11 Jan 2022 16:18:00 +0100 Subject: fix(installer): run `chsh` with sudo if user has privileges This fixes the error in Google Cloud Shell, where a password prompt appears when running `chsh` but the user (hello) does not have a password. If ran with `sudo`, the `chsh` command happens without a password prompt. --- tools/install.sh | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'tools') diff --git a/tools/install.sh b/tools/install.sh index 5009bd586..d3be1ace4 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -317,7 +317,7 @@ EOF "$YELLOW" "$RESET" read -r opt case $opt in - y*|Y*|"") echo "Changing the shell..." ;; + y*|Y*|"") ;; n*|N*) echo "Shell change skipped."; return ;; *) echo "Invalid choice. Shell change skipped."; return ;; esac @@ -355,11 +355,20 @@ EOF if [ -n "$SHELL" ]; then echo "$SHELL" > ~/.shell.pre-oh-my-zsh else - grep "^$USERNAME:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh + grep "^$USER:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh fi - # Actually change the default shell to zsh - if ! chsh -s "$zsh"; then + echo "Changing your shell to $zsh..." + + # Check if user has sudo privileges and run `chsh` or `sudo chsh` + if LANG= sudo -l -U "$USER" 2>/dev/null | grep -q "is not allowed to run"; then + chsh -s "$zsh" "$USER" # run chsh normally + else + sudo -k chsh -s "$zsh" "$USER" # -k forces the password prompt + fi + + # Check if the shell change was successful + if [ $? -ne 0 ]; then fmt_error "chsh command unsuccessful. Change your default shell manually." else export SHELL="$zsh" -- cgit v1.2.3-70-g09d2 From a0a949de56f7bc63403c521bfb1d7426843ec5b7 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Tue, 11 Jan 2022 18:56:18 +0100 Subject: fix(installer): fix `sudo` check for users with password or without privileges The previous check only worked if the user could run `sudo` without typing the password, which is almost none (I checked in Google Cloud Shell so I failed to notice this). This new check works whether the user has no sudo privileges, or if it has, whether they have to type in the password or not. It should really be easier to check if the user doesn't have privilege without having to make them type the password. --- tools/install.sh | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'tools') diff --git a/tools/install.sh b/tools/install.sh index d3be1ace4..b7498fa64 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -56,6 +56,28 @@ command_exists() { command -v "$@" >/dev/null 2>&1 } +user_can_sudo() { + # The following command has 3 parts: + # + # 1. Run `sudo` with `-v`. Does the following: + # • with privilege: asks for a password immediately. + # • without privilege: exits with error code 1 and prints the message: + # Sorry, user may not run sudo on + # + # 2. Pass `-S` to `sudo` to tell it to get the password from stdin + # instead of from a tty, and pipe `true` to `sudo`, since it doesn't + # output anything. This will make sudo exit with error code 1 and print + # the message: + # sudo: no password was provided + # + # 3. Check for the words "may not run sudo" in the output to really tell + # whether the user has privileges or not. For that we have to make sure + # to run `sudo` in the default locale (with `LANG=`) so that the message + # stays consistent regardless of the user's locale. + # + true | LANG= sudo -v -S 2>&1 | grep -q "may not run sudo" +} + # The [ -t 1 ] check only works when the function is not called from # a subshell (like in `$(...)` or `(...)`, so this hack redefines the # function at the top level to always return false when stdout is not @@ -360,8 +382,16 @@ EOF echo "Changing your shell to $zsh..." - # Check if user has sudo privileges and run `chsh` or `sudo chsh` - if LANG= sudo -l -U "$USER" 2>/dev/null | grep -q "is not allowed to run"; then + # Check if user has sudo privileges to run `chsh` with or without `sudo` + # + # This allows the call to succeed without password on systems where the + # user does not have a password but does have sudo privileges, like in + # Google Cloud Shell. + # + # On systems that don't have a user with passwordless sudo, the user will + # be prompted for the password either way, so this shouldn't cause any issues. + # + if user_can_sudo; then chsh -s "$zsh" "$USER" # run chsh normally else sudo -k chsh -s "$zsh" "$USER" # -k forces the password prompt -- cgit v1.2.3-70-g09d2 From c63ba17525b0b4344729762253c9d9e1c0823d49 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Tue, 11 Jan 2022 19:53:50 +0100 Subject: refactor(installer): simplify `user_can_sudo` check --- tools/install.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'tools') diff --git a/tools/install.sh b/tools/install.sh index b7498fa64..d3f1ee640 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -64,18 +64,18 @@ user_can_sudo() { # • without privilege: exits with error code 1 and prints the message: # Sorry, user may not run sudo on # - # 2. Pass `-S` to `sudo` to tell it to get the password from stdin - # instead of from a tty, and pipe `true` to `sudo`, since it doesn't - # output anything. This will make sudo exit with error code 1 and print - # the message: - # sudo: no password was provided + # 2. Pass `-n` to `sudo` to tell it to not ask for a password. If the + # password is not required, the command will finish with exit code 0. + # If one is required, sudo will exit with error code 1 and print the + # message: + # sudo: a password is required # # 3. Check for the words "may not run sudo" in the output to really tell # whether the user has privileges or not. For that we have to make sure # to run `sudo` in the default locale (with `LANG=`) so that the message # stays consistent regardless of the user's locale. # - true | LANG= sudo -v -S 2>&1 | grep -q "may not run sudo" + LANG= sudo -n -v 2>&1 | grep -q "may not run sudo" } # The [ -t 1 ] check only works when the function is not called from -- cgit v1.2.3-70-g09d2 From bddecfed58058910f0aeb78d9010f4f8f6d83692 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Thu, 13 Jan 2022 12:34:10 +0100 Subject: style(updater): remove statl from `git pull` --- tools/upgrade.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tools') diff --git a/tools/upgrade.sh b/tools/upgrade.sh index 25381de7e..55412062a 100755 --- a/tools/upgrade.sh +++ b/tools/upgrade.sh @@ -194,7 +194,7 @@ last_commit=$(git rev-parse "$branch") # Update Oh My Zsh printf "${BLUE}%s${RESET}\n" "Updating Oh My Zsh" -if git pull --rebase --stat $remote $branch; then +if git pull --rebase $remote $branch; then # Check if it was really updated or not if [[ "$(git rev-parse HEAD)" = "$last_commit" ]]; then message="Oh My Zsh is already at the latest version." -- cgit v1.2.3-70-g09d2 From 805427e06bc0549c7b9a4f50d3e39bbf68043f16 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Thu, 13 Jan 2022 17:28:15 +0100 Subject: fix(updater): give priority to `zstyle` settings if set (#10587) Fixes #10587 --- tools/check_for_upgrade.sh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'tools') diff --git a/tools/check_for_upgrade.sh b/tools/check_for_upgrade.sh index 293f48edf..729d8ecb5 100644 --- a/tools/check_for_upgrade.sh +++ b/tools/check_for_upgrade.sh @@ -10,11 +10,13 @@ fi # - auto: the update is performed automatically when it's time # - reminder: a reminder is shown to the user when it's time to update # - disabled: automatic update is turned off -zstyle -s ':omz:update' mode update_mode || update_mode=prompt +zstyle -s ':omz:update' mode update_mode || { + update_mode=prompt -# Support old-style settings -[[ "$DISABLE_UPDATE_PROMPT" != true ]] || update_mode=auto -[[ "$DISABLE_AUTO_UPDATE" != true ]] || update_mode=disabled + # If the mode zstyle setting is not set, support old-style settings + [[ "$DISABLE_UPDATE_PROMPT" != true ]] || update_mode=auto + [[ "$DISABLE_AUTO_UPDATE" != true ]] || update_mode=disabled +} # Cancel update if: # - the automatic update is disabled. -- cgit v1.2.3-70-g09d2 From 035c856c2cbbad2b45252ec8c065c3a9e7eefa65 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Thu, 13 Jan 2022 17:46:09 +0100 Subject: fix: get branch name first in `omz version` and changelog --- lib/cli.zsh | 7 ++++--- tools/changelog.sh | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) (limited to 'tools') diff --git a/lib/cli.zsh b/lib/cli.zsh index 0a85402df..ec59d1d44 100644 --- a/lib/cli.zsh +++ b/lib/cli.zsh @@ -791,12 +791,13 @@ function _omz::version { # Get the version name: # 1) try tag-like version - # 2) try name-rev - # 3) try branch name + # 2) try branch name + # 3) try name-rev (tag~ or branch~) local version version=$(command git describe --tags HEAD 2>/dev/null) \ + || version=$(command git symbolic-ref --quiet --short HEAD 2>/dev/null) \ || version=$(command git name-rev --no-undefined --name-only --exclude="remotes/*" HEAD 2>/dev/null) \ - || version=$(command git symbolic-ref --quiet --short HEAD 2>/dev/null) + || version="" # Get short hash for the current HEAD local commit=$(command git rev-parse --short HEAD 2>/dev/null) diff --git a/tools/changelog.sh b/tools/changelog.sh index 86774a7ea..49532a4a4 100755 --- a/tools/changelog.sh +++ b/tools/changelog.sh @@ -395,12 +395,12 @@ function main { # Get the first version name: # 1) try tag-like version, or - # 2) try name-rev, or - # 3) try branch name, or + # 2) try branch name, or + # 3) try name-rev, or # 4) try short hash version=$(command git describe --tags $until 2>/dev/null) \ - || version=$(command git name-rev --no-undefined --name-only --exclude="remotes/*" $until 2>/dev/null) \ || version=$(command git symbolic-ref --quiet --short $until 2>/dev/null) \ + || version=$(command git name-rev --no-undefined --name-only --exclude="remotes/*" $until 2>/dev/null) \ || version=$(command git rev-parse --short $until 2>/dev/null) # Get commit list from $until commit until $since commit, or until root commit if $since is unset -- cgit v1.2.3-70-g09d2 From 84931adcd465e9a3b5e38f3b416a1df2acc33801 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Fri, 21 Jan 2022 19:03:35 +0100 Subject: fix: do not call chpwd hooks in subshells --- lib/cli.zsh | 10 +++++----- tools/check_for_upgrade.sh | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) (limited to 'tools') diff --git a/lib/cli.zsh b/lib/cli.zsh index 70076bcfb..2f3f293da 100644 --- a/lib/cli.zsh +++ b/lib/cli.zsh @@ -37,7 +37,7 @@ function _omz { elif (( CURRENT == 3 )); then case "$words[2]" in changelog) local -a refs - refs=("${(@f)$(cd "$ZSH"; command git for-each-ref --format="%(refname:short):%(subject)" refs/heads refs/tags)}") + refs=("${(@f)$(builtin cd -q "$ZSH"; command git for-each-ref --format="%(refname:short):%(subject)" refs/heads refs/tags)}") _describe 'command' refs ;; plugin) subcmds=( 'disable:Disable plugin(s)' @@ -176,7 +176,7 @@ function _omz::changelog { local version=${1:-HEAD} format=${3:-"--text"} if ( - cd "$ZSH" + builtin cd -q "$ZSH" ! command git show-ref --verify refs/heads/$version && \ ! command git show-ref --verify refs/tags/$version && \ ! command git rev-parse --verify "${version}^{commit}" @@ -761,7 +761,7 @@ function _omz::theme::use { } function _omz::update { - local last_commit=$(cd "$ZSH"; git rev-parse HEAD) + local last_commit=$(builtin cd -q "$ZSH"; git rev-parse HEAD) # Run update script if [[ "$1" != --unattended ]]; then @@ -777,7 +777,7 @@ function _omz::update { command rm -rf "$ZSH/log/update.lock" # Restart the zsh session if there were changes - if [[ "$1" != --unattended && "$(cd "$ZSH"; git rev-parse HEAD)" != "$last_commit" ]]; then + if [[ "$1" != --unattended && "$(builtin cd -q "$ZSH"; git rev-parse HEAD)" != "$last_commit" ]]; then # Old zsh versions don't have ZSH_ARGZERO local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}" # Check whether to run a login shell @@ -787,7 +787,7 @@ function _omz::update { function _omz::version { ( - cd "$ZSH" + builtin cd -q "$ZSH" # Get the version name: # 1) try tag-like version diff --git a/tools/check_for_upgrade.sh b/tools/check_for_upgrade.sh index 729d8ecb5..3f6d35c3e 100644 --- a/tools/check_for_upgrade.sh +++ b/tools/check_for_upgrade.sh @@ -36,11 +36,11 @@ function current_epoch() { function is_update_available() { local branch - branch=${"$(cd "$ZSH"; git config --local oh-my-zsh.branch)":-master} + branch=${"$(cd -q "$ZSH"; git config --local oh-my-zsh.branch)":-master} local remote remote_url remote_repo - remote=${"$(cd "$ZSH"; git config --local oh-my-zsh.remote)":-origin} - remote_url=$(cd "$ZSH"; git config remote.$remote.url) + remote=${"$(cd -q "$ZSH"; git config --local oh-my-zsh.remote)":-origin} + remote_url=$(cd -q "$ZSH"; git config remote.$remote.url) local repo case "$remote_url" in @@ -58,7 +58,7 @@ function is_update_available() { # Get local HEAD. If this fails assume there are updates local local_head - local_head=$(cd "$ZSH"; git rev-parse $branch 2>/dev/null) || return 0 + local_head=$(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) @@ -136,7 +136,7 @@ function update_ohmyzsh() { fi # Test if Oh My Zsh directory is a git repository - if ! (cd "$ZSH" && LANG= git rev-parse &>/dev/null); then + if ! (cd -q "$ZSH" && LANG= git rev-parse &>/dev/null); then echo >&2 "[oh-my-zsh] Can't update: not a git repository." return fi -- cgit v1.2.3-70-g09d2 From c7221c5f257fc42ec8f6c6a6463bc8a5da25b00f Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Mon, 24 Jan 2022 15:41:40 +0100 Subject: style(installer): prefix formatting variables with `FMT_` --- tools/install.sh | 78 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 39 deletions(-) (limited to 'tools') diff --git a/tools/install.sh b/tools/install.sh index d3f1ee640..b4b90a62d 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -190,24 +190,24 @@ fmt_code() { } fmt_error() { - printf '%sError: %s%s\n' "$BOLD$RED" "$*" "$RESET" >&2 + printf '%sError: %s%s\n' "${FMT_BOLD}${FMT_RED}" "$*" "$FMT_RESET" >&2 } setup_color() { # Only use colors if connected to a terminal if ! is_tty; then - RAINBOW="" - RED="" - GREEN="" - YELLOW="" - BLUE="" - BOLD="" - RESET="" + FMT_RAINBOW="" + FMT_RED="" + FMT_GREEN="" + FMT_YELLOW="" + FMT_BLUE="" + FMT_BOLD="" + FMT_RESET="" return fi if supports_truecolor; then - RAINBOW=" + FMT_RAINBOW=" $(printf '\033[38;2;255;0;0m') $(printf '\033[38;2;255;97;0m') $(printf '\033[38;2;247;255;0m') @@ -217,7 +217,7 @@ setup_color() { $(printf '\033[38;2;245;0;172m') " else - RAINBOW=" + FMT_RAINBOW=" $(printf '\033[38;5;196m') $(printf '\033[38;5;202m') $(printf '\033[38;5;226m') @@ -228,12 +228,12 @@ setup_color() { " fi - RED=$(printf '\033[31m') - GREEN=$(printf '\033[32m') - YELLOW=$(printf '\033[33m') - BLUE=$(printf '\033[34m') - BOLD=$(printf '\033[1m') - RESET=$(printf '\033[0m') + FMT_RED=$(printf '\033[31m') + FMT_GREEN=$(printf '\033[32m') + FMT_YELLOW=$(printf '\033[33m') + FMT_BLUE=$(printf '\033[34m') + FMT_BOLD=$(printf '\033[1m') + FMT_RESET=$(printf '\033[0m') } setup_ohmyzsh() { @@ -244,7 +244,7 @@ setup_ohmyzsh() { # precedence over umasks except for filesystems mounted with option "noacl". umask g-w,o-w - echo "${BLUE}Cloning Oh My Zsh...${RESET}" + echo "${FMT_BLUE}Cloning Oh My Zsh...${FMT_RESET}" command_exists git || { fmt_error "git is not installed" @@ -276,14 +276,14 @@ setup_zshrc() { # Keep most recent old .zshrc at .zshrc.pre-oh-my-zsh, and older ones # with datestamp of installation that moved them aside, so we never actually # destroy a user's original zshrc - echo "${BLUE}Looking for an existing zsh config...${RESET}" + echo "${FMT_BLUE}Looking for an existing zsh config...${FMT_RESET}" # Must use this exact name so uninstall.sh can find it OLD_ZSHRC=~/.zshrc.pre-oh-my-zsh if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then # Skip this if the user doesn't want to replace an existing .zshrc if [ "$KEEP_ZSHRC" = yes ]; then - echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Keeping...${RESET}" + echo "${FMT_YELLOW}Found ~/.zshrc.${FMT_RESET} ${FMT_GREEN}Keeping...${FMT_RESET}" return fi if [ -e "$OLD_ZSHRC" ]; then @@ -295,14 +295,14 @@ setup_zshrc() { fi mv "$OLD_ZSHRC" "${OLD_OLD_ZSHRC}" - echo "${YELLOW}Found old ~/.zshrc.pre-oh-my-zsh." \ - "${GREEN}Backing up to ${OLD_OLD_ZSHRC}${RESET}" + echo "${FMT_YELLOW}Found old ~/.zshrc.pre-oh-my-zsh." \ + "${FMT_GREEN}Backing up to ${OLD_OLD_ZSHRC}${FMT_RESET}" fi - echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Backing up to ${OLD_ZSHRC}${RESET}" + echo "${FMT_YELLOW}Found ~/.zshrc.${FMT_RESET} ${FMT_GREEN}Backing up to ${OLD_ZSHRC}${FMT_RESET}" mv ~/.zshrc "$OLD_ZSHRC" fi - echo "${GREEN}Using the Oh My Zsh template file and adding it to ~/.zshrc.${RESET}" + echo "${FMT_GREEN}Using the Oh My Zsh template file and adding it to ~/.zshrc.${FMT_RESET}" # Replace $HOME path with '$HOME' in $ZSH variable in .zshrc file omz=$(echo "$ZSH" | sed "s|^$HOME/|\$HOME/|") @@ -327,16 +327,16 @@ setup_shell() { if ! command_exists chsh; then cat < Date: Mon, 24 Jan 2022 15:41:57 +0100 Subject: fix(installer): define `$USER` if not defined Fixes missing $USER value in ArchLinux sh (bash) --- tools/install.sh | 3 +++ 1 file changed, 3 insertions(+) (limited to 'tools') diff --git a/tools/install.sh b/tools/install.sh index b4b90a62d..9ad6f7d14 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -51,6 +51,9 @@ CHSH=${CHSH:-yes} RUNZSH=${RUNZSH:-yes} KEEP_ZSHRC=${KEEP_ZSHRC:-no} +# Sane defaults +USER=${USER:-$(whoami)} + command_exists() { command -v "$@" >/dev/null 2>&1 -- cgit v1.2.3-70-g09d2 From 788802af68af9a1adbfdb678086939c89d79d999 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Mon, 24 Jan 2022 15:45:42 +0100 Subject: fix(installer): correct check for `sudo` in shell change logic --- tools/install.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'tools') diff --git a/tools/install.sh b/tools/install.sh index 9ad6f7d14..34dca8413 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -60,6 +60,8 @@ command_exists() { } user_can_sudo() { + # Check if sudo is installed + command_exists sudo || return 1 # The following command has 3 parts: # # 1. Run `sudo` with `-v`. Does the following: @@ -78,7 +80,7 @@ user_can_sudo() { # to run `sudo` in the default locale (with `LANG=`) so that the message # stays consistent regardless of the user's locale. # - LANG= sudo -n -v 2>&1 | grep -q "may not run sudo" + ! LANG= sudo -n -v 2>&1 | grep -q "may not run sudo" } # The [ -t 1 ] check only works when the function is not called from @@ -395,9 +397,9 @@ EOF # be prompted for the password either way, so this shouldn't cause any issues. # if user_can_sudo; then - chsh -s "$zsh" "$USER" # run chsh normally - else sudo -k chsh -s "$zsh" "$USER" # -k forces the password prompt + else + chsh -s "$zsh" "$USER" # run chsh normally fi # Check if the shell change was successful -- cgit v1.2.3-70-g09d2 From fc40b53e6460560ed9b256deb87f2165f8d48f1f Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Mon, 24 Jan 2022 18:32:36 +0100 Subject: style(updater): silence `git pull` output and show errors in English --- tools/upgrade.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tools') diff --git a/tools/upgrade.sh b/tools/upgrade.sh index 55412062a..b6cb10b5a 100755 --- a/tools/upgrade.sh +++ b/tools/upgrade.sh @@ -194,7 +194,7 @@ last_commit=$(git rev-parse "$branch") # Update Oh My Zsh printf "${BLUE}%s${RESET}\n" "Updating Oh My Zsh" -if git pull --rebase $remote $branch; then +if LANG= git pull --quiet --rebase $remote $branch; then # Check if it was really updated or not if [[ "$(git rev-parse HEAD)" = "$last_commit" ]]; then message="Oh My Zsh is already at the latest version." -- cgit v1.2.3-70-g09d2 From 59c40eee8e9232f556e9d4c8c97eb1b866846af3 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Thu, 27 Jan 2022 18:01:27 +0100 Subject: fix(installer): avoid `git clone -c` to support git v1.7.1 (#10621) --- tools/install.sh | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'tools') diff --git a/tools/install.sh b/tools/install.sh index 34dca8413..2290bc1eb 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -263,13 +263,19 @@ setup_ohmyzsh() { exit 1 fi - git clone -c core.eol=lf -c core.autocrlf=false \ - -c fsck.zeroPaddedFilemode=ignore \ - -c fetch.fsck.zeroPaddedFilemode=ignore \ - -c receive.fsck.zeroPaddedFilemode=ignore \ - -c oh-my-zsh.remote=origin \ - -c oh-my-zsh.branch="$BRANCH" \ - --depth=1 --branch "$BRANCH" "$REMOTE" "$ZSH" || { + # Manual clone with git config options to support git < v1.7.2 + git init "$ZSH" && cd "$ZSH" \ + && git config core.eol lf \ + && git config core.autocrlf false \ + && git config fsck.zeroPaddedFilemode ignore \ + && git config fetch.fsck.zeroPaddedFilemode ignore \ + && git config receive.fsck.zeroPaddedFilemode ignore \ + && git config oh-my-zsh.remote origin \ + && git config oh-my-zsh.branch "$BRANCH" \ + && git remote add origin "$REMOTE" \ + && git fetch --depth=1 origin \ + && git checkout -b "$BRANCH" "origin/$BRANCH" || { + rm -rf "$ZSH" fmt_error "git clone of oh-my-zsh repo failed" exit 1 } -- cgit v1.2.3-70-g09d2 From 3c5367d272011e7bca9fa4e8f9a5f9635f938d11 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Fri, 28 Jan 2022 13:22:31 +0100 Subject: fix(changelog): don't show changelog with only ignored type commits --- tools/changelog.sh | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) (limited to 'tools') diff --git a/tools/changelog.sh b/tools/changelog.sh index 49532a4a4..6489a3cb2 100755 --- a/tools/changelog.sh +++ b/tools/changelog.sh @@ -23,8 +23,7 @@ TYPES=( test "Testing" ) -#* Types that will be displayed in their own section, -#* in the order specified here. +#* Types that will be displayed in their own section, in the order specified here. local -a MAIN_TYPES MAIN_TYPES=(feat fix perf docs) @@ -34,7 +33,8 @@ OTHER_TYPES=(refactor style other) #* Commit types that don't appear in $MAIN_TYPES nor $OTHER_TYPES #* will not be displayed and will simply be ignored. - +local -a IGNORED_TYPES +IGNORED_TYPES=(${${${(@k)TYPES}:|MAIN_TYPES}:|OTHER_TYPES}) ############################ # COMMIT PARSING UTILITIES # @@ -139,7 +139,7 @@ function parse-commit { # [BREAKING CHANGE: warning] # commits holds the commit type - commits[$hash]="$(commit:type "$subject")" + types[$hash]="$(commit:type "$subject")" # scopes holds the commit scope scopes[$hash]="$(commit:scope "$subject")" # subjects holds the commit subject @@ -164,26 +164,32 @@ function parse-commit { function display-release { # This function uses the following globals: output, version, - # commits (A), subjects (A), scopes (A), breaking (A) and reverts (A). + # types (A), subjects (A), scopes (A), breaking (A) and reverts (A). # # - output is the output format to use when formatting (raw|text|md) # - version is the version in which the commits are made - # - commits, subjects, scopes, breaking, and reverts are associative arrays + # - types, subjects, scopes, breaking, and reverts are associative arrays # with commit hashes as keys # Remove commits that were reverted local hash rhash for hash rhash in ${(kv)reverts}; do - if (( ${+commits[$rhash]} )); then + if (( ${+types[$rhash]} )); then # Remove revert commit - unset "commits[$hash]" "subjects[$hash]" "scopes[$hash]" "breaking[$hash]" + unset "types[$hash]" "subjects[$hash]" "scopes[$hash]" "breaking[$hash]" # Remove reverted commit - unset "commits[$rhash]" "subjects[$rhash]" "scopes[$rhash]" "breaking[$rhash]" + unset "types[$rhash]" "subjects[$rhash]" "scopes[$rhash]" "breaking[$rhash]" fi done + # Remove commits from ignored types unless it has breaking change information + for hash in ${(k)types[(R)${(j:|:)IGNORED_TYPES}]}; do + (( ! ${+breaking[$hash]} )) || continue + unset "types[$hash]" "subjects[$hash]" "scopes[$hash]" + done + # If no commits left skip displaying the release - if (( $#commits == 0 )); then + if (( $#types == 0 )); then return fi @@ -313,7 +319,7 @@ function display-release { local hash type="$1" local -a hashes - hashes=(${(k)commits[(R)$type]}) + hashes=(${(k)types[(R)$type]}) # If no commits found of type $type, go to next type (( $#hashes != 0 )) || return 0 @@ -330,7 +336,7 @@ function display-release { # Commits made under types considered other changes local -A changes - changes=(${(kv)commits[(R)${(j:|:)OTHER_TYPES}]}) + changes=(${(kv)types[(R)${(j:|:)OTHER_TYPES}]}) # If no commits found under "other" types, don't display anything (( $#changes != 0 )) || return 0 @@ -388,7 +394,7 @@ function main { fi # Commit classification arrays - local -A commits subjects scopes breaking reverts + local -A types subjects scopes breaking reverts local truncate=0 read_commits=0 local version tag local hash refs subject body @@ -441,7 +447,7 @@ function main { # Output previous release display-release # Reinitialize commit storage - commits=() + types=() subjects=() scopes=() breaking=() -- cgit v1.2.3-70-g09d2 From 46f5d38b1dc6a725e78bd53ca35a6299b4e91ece Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Fri, 28 Jan 2022 20:53:30 +0100 Subject: refactor(installer): use POSIX-standard's `id -u -n` to define `$USER` --- tools/install.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'tools') diff --git a/tools/install.sh b/tools/install.sh index 2290bc1eb..e64e39063 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -37,6 +37,13 @@ # set -e +# Make sure important variables exist if not already defined +# +# $USER is defined by login(1) which is not always executed (e.g. containers) +# POSIX: https://pubs.opengroup.org/onlinepubs/009695299/utilities/id.html +USER=${USER:-$(id -u -n)} + + # Track if $ZSH was provided custom_zsh=${ZSH:+yes} @@ -51,9 +58,6 @@ CHSH=${CHSH:-yes} RUNZSH=${RUNZSH:-yes} KEEP_ZSHRC=${KEEP_ZSHRC:-no} -# Sane defaults -USER=${USER:-$(whoami)} - command_exists() { command -v "$@" >/dev/null 2>&1 -- cgit v1.2.3-70-g09d2 From f0f42828fa6842af631cc3dbf45f5454ea88fa3c Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Mon, 7 Feb 2022 18:58:47 +0100 Subject: feat(updater): do not update when user already typed some characters (#9699) Fixes #9699 --- tools/check_for_upgrade.sh | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) (limited to 'tools') diff --git a/tools/check_for_upgrade.sh b/tools/check_for_upgrade.sh index 3f6d35c3e..4484df4fe 100644 --- a/tools/check_for_upgrade.sh +++ b/tools/check_for_upgrade.sh @@ -146,26 +146,35 @@ function update_ohmyzsh() { return fi - # Ask for confirmation before updating unless in auto mode + # Don't ask for confirmation before updating if in auto mode if [[ "$update_mode" = auto ]]; then update_ohmyzsh - elif [[ "$update_mode" = reminder ]]; then + return $? + fi + + # If in reminder mode show reminder and exit + if [[ "$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 0 fi + + # If user has typed input, show reminder and exit + if read -t -k 1; then + echo + echo "[oh-my-zsh] It's time to update! You can do that by running \`omz update\`" + return 0 + 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 -- cgit v1.2.3-70-g09d2 From dbd92a62ce1fc25a6819ae6d0a29dc8b8ec9a7dd Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Fri, 11 Feb 2022 10:01:48 +0100 Subject: fix(updater): do not swallow 1 character in check for user input Co-authored-by: Philippe Troin --- tools/check_for_upgrade.sh | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'tools') diff --git a/tools/check_for_upgrade.sh b/tools/check_for_upgrade.sh index 4484df4fe..33f30c85a 100644 --- a/tools/check_for_upgrade.sh +++ b/tools/check_for_upgrade.sh @@ -89,6 +89,23 @@ function update_ohmyzsh() { fi } +function has_typed_input() { + # Created by Philippe Troin + # https://zsh.org/mla/users/2022/msg00062.html + emulate -L zsh + zmodload zsh/zselect + + { + local termios=$(stty --save) + stty -icanon + + zselect -t 0 -r 0 + return $? + } always { + stty $termios + } +} + () { emulate -L zsh @@ -159,7 +176,7 @@ function update_ohmyzsh() { fi # If user has typed input, show reminder and exit - if read -t -k 1; then + if has_typed_input; then echo echo "[oh-my-zsh] It's time to update! You can do that by running \`omz update\`" return 0 -- cgit v1.2.3-70-g09d2 From 69e29378915d53655d8fa8dc181b6cf526754569 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Fri, 11 Feb 2022 19:51:52 +0100 Subject: fix(updater): fix input check on non-interactive runs Reference: https://www.zsh.org/mla/users/2022/msg00067.html --- tools/check_for_upgrade.sh | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'tools') diff --git a/tools/check_for_upgrade.sh b/tools/check_for_upgrade.sh index 33f30c85a..a36aecb84 100644 --- a/tools/check_for_upgrade.sh +++ b/tools/check_for_upgrade.sh @@ -95,13 +95,21 @@ function has_typed_input() { 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 { - local termios=$(stty --save) + # 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 } } -- cgit v1.2.3-70-g09d2