From 07bcf041c00bb583cddb1b41cdea9f9d221d7d1b Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Fri, 1 May 2020 19:02:29 +0200 Subject: core: add basic Oh My Zsh CLI (#8876) --- oh-my-zsh.sh | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'oh-my-zsh.sh') diff --git a/oh-my-zsh.sh b/oh-my-zsh.sh index 30259372c..bfb538423 100644 --- a/oh-my-zsh.sh +++ b/oh-my-zsh.sh @@ -73,6 +73,11 @@ else fi +# Load core files +for core_file ($ZSH/core/*.zsh); do + source "$core_file" +done + # Load all of the config files in ~/oh-my-zsh that end in .zsh # TIP: Add files you don't want in git to .gitignore for config_file ($ZSH/lib/*.zsh); do -- cgit v1.2.3-70-g09d2 From 173d4ca68f1ff4b04e9f3fd783244c309d848092 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Fri, 1 May 2020 19:15:14 +0200 Subject: core: move core folder to lib, for now --- core/cli.zsh | 195 ----------------------------------------------------------- lib/cli.zsh | 195 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ oh-my-zsh.sh | 5 -- 3 files changed, 195 insertions(+), 200 deletions(-) delete mode 100644 core/cli.zsh create mode 100644 lib/cli.zsh (limited to 'oh-my-zsh.sh') diff --git a/core/cli.zsh b/core/cli.zsh deleted file mode 100644 index 8c0fabb82..000000000 --- a/core/cli.zsh +++ /dev/null @@ -1,195 +0,0 @@ -#!/usr/bin/env zsh - -function omz { - [[ $# -gt 0 ]] || { - _omz::help - return 1 - } - - local command="$1" - shift - - # Subcommand functions start with _ so that they don't - # appear as completion entries when looking for `omz` - (( $+functions[_omz::$command] )) || { - _omz::help - return 1 - } - - _omz::$command "$@" -} - -function _omz { - local -a cmds subcmds - cmds=( - 'help:Usage information' - 'pr:Commands for Oh My Zsh Pull Requests' - ) - - if (( CURRENT == 2 )); then - _describe 'command' cmds - elif (( CURRENT == 3 )); then - case "$words[2]" in - pr) subcmds=( 'test:Test a Pull Request' 'clean:Delete all Pull Request branches' ) - _describe 'command' subcmds ;; - esac - fi - - return 0 -} - -compdef _omz omz - - -function _omz::help { - cat < [options] - -Available commands: - - help Print this help message - pr Commands for Oh My Zsh Pull Requests - -EOF -} - -function _omz::log { - # if promptsubst is set, a message with `` or $() - # will be run even if quoted due to `print -P` - setopt localoptions nopromptsubst - - # $1 = info|warn|error|debug - # $@ = text - - local logtype=$1 - local logname=${${functrace[1]#_}%:*} - shift - - # Don't print anything if debug is not active - if [[ $logtype = debug && -z $_OMZ_DEBUG ]]; then - return - fi - - # Choose coloring based on log type - case "$logtype" in - prompt) print -Pn "%S%F{blue}$logname%f%s: $@" ;; - debug) print -P "%F{white}$logname%f: $@" ;; - info) print -P "%F{green}$logname%f: $@" ;; - warn) print -P "%S%F{yellow}$logname%f%s: $@" ;; - error) print -P "%S%F{red}$logname%f%s: $@" ;; - esac >&2 -} - -function _omz::pr { - (( $# > 0 && $+functions[_omz::pr::$1] )) || { - cat < [options] - -Available commands: - - clean Delete all PR branches (ohmyzsh/pull-*) - test Fetch PR #NUMBER and rebase against master - -EOF - return 1 - } - - local command="$1" - shift - - _omz::pr::$command "$@" -} - -function _omz::pr::clean { - ( - set -e - cd -q "$ZSH" - - _omz::log info "removing all Oh My Zsh Pull Request branches..." - command git branch --list 'ohmyzsh/pull-*' | while read branch; do - command git branch -D "$branch" - done - ) -} - -function _omz::pr::test { - # Allow $1 to be a URL to the pull request - if [[ "$1" = https://* ]]; then - 1="${1:t}" - fi - - # Check the input - if ! [[ -n "$1" && "$1" =~ ^[[:digit:]]+$ ]]; then - echo >&2 "Usage: omz pr test " - return 1 - fi - - # Save current git HEAD - local branch - branch=$(cd -q "$ZSH"; git symbolic-ref --short HEAD) || { - _omz::log error "error when getting the current git branch. Aborting..." - return 1 - } - - - # Fetch PR onto ohmyzsh/pull- branch and rebase against master - # If any of these operations fail, undo the changes made - ( - set -e - cd -q "$ZSH" - - # Get the ohmyzsh git remote - command git remote -v | while read remote url _; do - case "$url" in - https://github.com/ohmyzsh/ohmyzsh(|.git)) found=1; break ;; - git@github.com:ohmyzsh/ohmyzsh(|.git)) found=1; break ;; - esac - done - - (( $found )) || { - _omz::log error "could not found the ohmyzsh git remote. Aborting..." - return 1 - } - - # Fetch pull request head - _omz::log info "fetching PR #$1 to ohmyzsh/pull-$1..." - command git fetch -f "$remote" refs/pull/$1/head:ohmyzsh/pull-$1 || { - _omz::log error "error when trying to fetch PR #$1." - return 1 - } - - # Rebase pull request branch against the current master - _omz::log info "rebasing PR #$1..." - command git rebase master ohmyzsh/pull-$1 || { - command git rebase --abort &>/dev/null - _omz::log warn "could not rebase PR #$1 on top of master." - _omz::log warn "you might not see the latest stable changes." - _omz::log info "run \`zsh\` to test the changes." - return 1 - } - - _omz::log info "fetch of PR #${1} successful." - ) - - # If there was an error, abort running zsh to test the PR - [[ $? -eq 0 ]] || return 1 - - # Run zsh to test the changes - _omz::log info "running \`zsh\` to test the changes. Run \`exit\` to go back." - command zsh -l - - # After testing, go back to the previous HEAD if the user wants - _omz::log prompt "do you want to go back to the previous branch? [Y/n] " - read -r -k 1 - [[ "$REPLY" = [nN] ]] && return - - ( - set -e - cd -q "$ZSH" - - command git checkout "$branch" -- || { - _omz::log error "could not go back to the previous branch ('$branch')." - return 1 - } - ) -} diff --git a/lib/cli.zsh b/lib/cli.zsh new file mode 100644 index 000000000..8c0fabb82 --- /dev/null +++ b/lib/cli.zsh @@ -0,0 +1,195 @@ +#!/usr/bin/env zsh + +function omz { + [[ $# -gt 0 ]] || { + _omz::help + return 1 + } + + local command="$1" + shift + + # Subcommand functions start with _ so that they don't + # appear as completion entries when looking for `omz` + (( $+functions[_omz::$command] )) || { + _omz::help + return 1 + } + + _omz::$command "$@" +} + +function _omz { + local -a cmds subcmds + cmds=( + 'help:Usage information' + 'pr:Commands for Oh My Zsh Pull Requests' + ) + + if (( CURRENT == 2 )); then + _describe 'command' cmds + elif (( CURRENT == 3 )); then + case "$words[2]" in + pr) subcmds=( 'test:Test a Pull Request' 'clean:Delete all Pull Request branches' ) + _describe 'command' subcmds ;; + esac + fi + + return 0 +} + +compdef _omz omz + + +function _omz::help { + cat < [options] + +Available commands: + + help Print this help message + pr Commands for Oh My Zsh Pull Requests + +EOF +} + +function _omz::log { + # if promptsubst is set, a message with `` or $() + # will be run even if quoted due to `print -P` + setopt localoptions nopromptsubst + + # $1 = info|warn|error|debug + # $@ = text + + local logtype=$1 + local logname=${${functrace[1]#_}%:*} + shift + + # Don't print anything if debug is not active + if [[ $logtype = debug && -z $_OMZ_DEBUG ]]; then + return + fi + + # Choose coloring based on log type + case "$logtype" in + prompt) print -Pn "%S%F{blue}$logname%f%s: $@" ;; + debug) print -P "%F{white}$logname%f: $@" ;; + info) print -P "%F{green}$logname%f: $@" ;; + warn) print -P "%S%F{yellow}$logname%f%s: $@" ;; + error) print -P "%S%F{red}$logname%f%s: $@" ;; + esac >&2 +} + +function _omz::pr { + (( $# > 0 && $+functions[_omz::pr::$1] )) || { + cat < [options] + +Available commands: + + clean Delete all PR branches (ohmyzsh/pull-*) + test Fetch PR #NUMBER and rebase against master + +EOF + return 1 + } + + local command="$1" + shift + + _omz::pr::$command "$@" +} + +function _omz::pr::clean { + ( + set -e + cd -q "$ZSH" + + _omz::log info "removing all Oh My Zsh Pull Request branches..." + command git branch --list 'ohmyzsh/pull-*' | while read branch; do + command git branch -D "$branch" + done + ) +} + +function _omz::pr::test { + # Allow $1 to be a URL to the pull request + if [[ "$1" = https://* ]]; then + 1="${1:t}" + fi + + # Check the input + if ! [[ -n "$1" && "$1" =~ ^[[:digit:]]+$ ]]; then + echo >&2 "Usage: omz pr test " + return 1 + fi + + # Save current git HEAD + local branch + branch=$(cd -q "$ZSH"; git symbolic-ref --short HEAD) || { + _omz::log error "error when getting the current git branch. Aborting..." + return 1 + } + + + # Fetch PR onto ohmyzsh/pull- branch and rebase against master + # If any of these operations fail, undo the changes made + ( + set -e + cd -q "$ZSH" + + # Get the ohmyzsh git remote + command git remote -v | while read remote url _; do + case "$url" in + https://github.com/ohmyzsh/ohmyzsh(|.git)) found=1; break ;; + git@github.com:ohmyzsh/ohmyzsh(|.git)) found=1; break ;; + esac + done + + (( $found )) || { + _omz::log error "could not found the ohmyzsh git remote. Aborting..." + return 1 + } + + # Fetch pull request head + _omz::log info "fetching PR #$1 to ohmyzsh/pull-$1..." + command git fetch -f "$remote" refs/pull/$1/head:ohmyzsh/pull-$1 || { + _omz::log error "error when trying to fetch PR #$1." + return 1 + } + + # Rebase pull request branch against the current master + _omz::log info "rebasing PR #$1..." + command git rebase master ohmyzsh/pull-$1 || { + command git rebase --abort &>/dev/null + _omz::log warn "could not rebase PR #$1 on top of master." + _omz::log warn "you might not see the latest stable changes." + _omz::log info "run \`zsh\` to test the changes." + return 1 + } + + _omz::log info "fetch of PR #${1} successful." + ) + + # If there was an error, abort running zsh to test the PR + [[ $? -eq 0 ]] || return 1 + + # Run zsh to test the changes + _omz::log info "running \`zsh\` to test the changes. Run \`exit\` to go back." + command zsh -l + + # After testing, go back to the previous HEAD if the user wants + _omz::log prompt "do you want to go back to the previous branch? [Y/n] " + read -r -k 1 + [[ "$REPLY" = [nN] ]] && return + + ( + set -e + cd -q "$ZSH" + + command git checkout "$branch" -- || { + _omz::log error "could not go back to the previous branch ('$branch')." + return 1 + } + ) +} diff --git a/oh-my-zsh.sh b/oh-my-zsh.sh index bfb538423..30259372c 100644 --- a/oh-my-zsh.sh +++ b/oh-my-zsh.sh @@ -73,11 +73,6 @@ else fi -# Load core files -for core_file ($ZSH/core/*.zsh); do - source "$core_file" -done - # Load all of the config files in ~/oh-my-zsh that end in .zsh # TIP: Add files you don't want in git to .gitignore for config_file ($ZSH/lib/*.zsh); do -- cgit v1.2.3-70-g09d2 From dd1a72696f162fd5285a104b5f63970deb66e8b0 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Mon, 4 May 2020 16:51:16 +0200 Subject: core: refresh zcompdump cache file in init script (#8878) --- oh-my-zsh.sh | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'oh-my-zsh.sh') diff --git a/oh-my-zsh.sh b/oh-my-zsh.sh index 30259372c..092099325 100644 --- a/oh-my-zsh.sh +++ b/oh-my-zsh.sh @@ -61,6 +61,18 @@ if [ -z "$ZSH_COMPDUMP" ]; then ZSH_COMPDUMP="${ZDOTDIR:-${HOME}}/.zcompdump-${SHORT_HOST}-${ZSH_VERSION}" fi +# Construct zcompdump OMZ metadata +zcompdump_metadata="\ +#omz revision: $(cd -q "$ZSH"; git rev-parse HEAD 2>/dev/null) +#omz fpath: $fpath\ +" + +# Delete the zcompdump file if OMZ zcompdump metadata changed +if ! cmp -s <(command grep '^#omz' "$ZSH_COMPDUMP" 2>/dev/null) <<< "$zcompdump_metadata"; then + command rm -f "$ZSH_COMPDUMP" + zcompdump_refresh=1 +fi + if [[ $ZSH_DISABLE_COMPFIX != true ]]; then source $ZSH/lib/compfix.zsh # If completion insecurities exist, warn the user @@ -72,6 +84,13 @@ else compinit -u -C -d "${ZSH_COMPDUMP}" fi +# Append zcompdump metadata if missing +if (( $zcompdump_refresh )); then + echo "\n$zcompdump_metadata" >>! "$ZSH_COMPDUMP" +fi + +unset zcompdump_metadata zcompdump_refresh + # Load all of the config files in ~/oh-my-zsh that end in .zsh # TIP: Add files you don't want in git to .gitignore -- cgit v1.2.3-70-g09d2 From ff987384cf721e7e0812636dea8100b485968bb2 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Tue, 5 May 2020 12:13:45 +0200 Subject: core: fix cmp invocation in BSD systems BSD cmp doesn't have the option of reading from stdin, so use process substitution instead. --- oh-my-zsh.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'oh-my-zsh.sh') diff --git a/oh-my-zsh.sh b/oh-my-zsh.sh index 092099325..285caf668 100644 --- a/oh-my-zsh.sh +++ b/oh-my-zsh.sh @@ -68,7 +68,7 @@ zcompdump_metadata="\ " # Delete the zcompdump file if OMZ zcompdump metadata changed -if ! cmp -s <(command grep '^#omz' "$ZSH_COMPDUMP" 2>/dev/null) <<< "$zcompdump_metadata"; then +if ! cmp -s <(command grep '^#omz' "$ZSH_COMPDUMP" 2>/dev/null) <(echo "$zcompdump_metadata"); then command rm -f "$ZSH_COMPDUMP" zcompdump_refresh=1 fi -- cgit v1.2.3-70-g09d2 From b876198575cbf23d589ddc8da6b22254d3d5358b Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Thu, 7 May 2020 11:25:35 +0200 Subject: init: silence zcompdump metadata write for invalid ZSH_COMPDUMP filenames If `$SHORT_HOST` contains invalid filename characters, the compinit call doesn't error, but the zcompdump metadata write does. Use `tee` instead so we can silence the error. See https://github.com/ohmyzsh/ohmyzsh/commit/dd1a72696f162fd5285a104b5f63970deb66e8b0#commitcomment-38984764 --- oh-my-zsh.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'oh-my-zsh.sh') diff --git a/oh-my-zsh.sh b/oh-my-zsh.sh index 285caf668..6c61725cd 100644 --- a/oh-my-zsh.sh +++ b/oh-my-zsh.sh @@ -86,7 +86,7 @@ fi # Append zcompdump metadata if missing if (( $zcompdump_refresh )); then - echo "\n$zcompdump_metadata" >>! "$ZSH_COMPDUMP" + echo "\n$zcompdump_metadata" | tee -a "$ZSH_COMPDUMP" &>/dev/null fi unset zcompdump_metadata zcompdump_refresh -- cgit v1.2.3-70-g09d2