cli.zsh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/usr/bin/env zsh
  2. function omz {
  3. [[ $# -gt 0 ]] || {
  4. _omz::help
  5. return 1
  6. }
  7. local command="$1"
  8. shift
  9. # Subcommand functions start with _ so that they don't
  10. # appear as completion entries when looking for `omz`
  11. (( $+functions[_omz::$command] )) || {
  12. _omz::help
  13. return 1
  14. }
  15. _omz::$command "$@"
  16. }
  17. function _omz {
  18. local -a cmds subcmds
  19. cmds=(
  20. 'help:Usage information'
  21. 'update:Update Oh My Zsh'
  22. 'pr:Commands for Oh My Zsh Pull Requests'
  23. )
  24. if (( CURRENT == 2 )); then
  25. _describe 'command' cmds
  26. elif (( CURRENT == 3 )); then
  27. case "$words[2]" in
  28. pr) subcmds=( 'test:Test a Pull Request' 'clean:Delete all Pull Request branches' )
  29. _describe 'command' subcmds ;;
  30. esac
  31. fi
  32. return 0
  33. }
  34. compdef _omz omz
  35. function _omz::help {
  36. cat <<EOF
  37. Usage: omz <command> [options]
  38. Available commands:
  39. help Print this help message
  40. update Update Oh My Zsh
  41. pr <command> Commands for Oh My Zsh Pull Requests
  42. EOF
  43. }
  44. function _omz::log {
  45. # if promptsubst is set, a message with `` or $()
  46. # will be run even if quoted due to `print -P`
  47. setopt localoptions nopromptsubst
  48. # $1 = info|warn|error|debug
  49. # $@ = text
  50. local logtype=$1
  51. local logname=${${functrace[1]#_}%:*}
  52. shift
  53. # Don't print anything if debug is not active
  54. if [[ $logtype = debug && -z $_OMZ_DEBUG ]]; then
  55. return
  56. fi
  57. # Choose coloring based on log type
  58. case "$logtype" in
  59. prompt) print -Pn "%S%F{blue}$logname%f%s: $@" ;;
  60. debug) print -P "%F{white}$logname%f: $@" ;;
  61. info) print -P "%F{green}$logname%f: $@" ;;
  62. warn) print -P "%S%F{yellow}$logname%f%s: $@" ;;
  63. error) print -P "%S%F{red}$logname%f%s: $@" ;;
  64. esac >&2
  65. }
  66. function _omz::pr {
  67. (( $# > 0 && $+functions[_omz::pr::$1] )) || {
  68. cat <<EOF
  69. Usage: omz pr <command> [options]
  70. Available commands:
  71. clean Delete all PR branches (ohmyzsh/pull-*)
  72. test <PR_number_or_URL> Fetch PR #NUMBER and rebase against master
  73. EOF
  74. return 1
  75. }
  76. local command="$1"
  77. shift
  78. _omz::pr::$command "$@"
  79. }
  80. function _omz::pr::clean {
  81. (
  82. set -e
  83. builtin cd -q "$ZSH"
  84. _omz::log info "removing all Oh My Zsh Pull Request branches..."
  85. command git branch --list 'ohmyzsh/pull-*' | while read branch; do
  86. command git branch -D "$branch"
  87. done
  88. )
  89. }
  90. function _omz::pr::test {
  91. # Allow $1 to be a URL to the pull request
  92. if [[ "$1" = https://* ]]; then
  93. 1="${1:t}"
  94. fi
  95. # Check the input
  96. if ! [[ -n "$1" && "$1" =~ ^[[:digit:]]+$ ]]; then
  97. echo >&2 "Usage: omz pr test <PR_NUMBER_or_URL>"
  98. return 1
  99. fi
  100. # Save current git HEAD
  101. local branch
  102. branch=$(builtin cd -q "$ZSH"; git symbolic-ref --short HEAD) || {
  103. _omz::log error "error when getting the current git branch. Aborting..."
  104. return 1
  105. }
  106. # Fetch PR onto ohmyzsh/pull-<PR_NUMBER> branch and rebase against master
  107. # If any of these operations fail, undo the changes made
  108. (
  109. set -e
  110. builtin cd -q "$ZSH"
  111. # Get the ohmyzsh git remote
  112. command git remote -v | while read remote url _; do
  113. case "$url" in
  114. https://github.com/ohmyzsh/ohmyzsh(|.git)) found=1; break ;;
  115. git@github.com:ohmyzsh/ohmyzsh(|.git)) found=1; break ;;
  116. esac
  117. done
  118. (( $found )) || {
  119. _omz::log error "could not found the ohmyzsh git remote. Aborting..."
  120. return 1
  121. }
  122. # Fetch pull request head
  123. _omz::log info "fetching PR #$1 to ohmyzsh/pull-$1..."
  124. command git fetch -f "$remote" refs/pull/$1/head:ohmyzsh/pull-$1 || {
  125. _omz::log error "error when trying to fetch PR #$1."
  126. return 1
  127. }
  128. # Rebase pull request branch against the current master
  129. _omz::log info "rebasing PR #$1..."
  130. command git rebase master ohmyzsh/pull-$1 || {
  131. command git rebase --abort &>/dev/null
  132. _omz::log warn "could not rebase PR #$1 on top of master."
  133. _omz::log warn "you might not see the latest stable changes."
  134. _omz::log info "run \`zsh\` to test the changes."
  135. return 1
  136. }
  137. _omz::log info "fetch of PR #${1} successful."
  138. )
  139. # If there was an error, abort running zsh to test the PR
  140. [[ $? -eq 0 ]] || return 1
  141. # Run zsh to test the changes
  142. _omz::log info "running \`zsh\` to test the changes. Run \`exit\` to go back."
  143. command zsh -l
  144. # After testing, go back to the previous HEAD if the user wants
  145. _omz::log prompt "do you want to go back to the previous branch? [Y/n] "
  146. read -r -k 1
  147. # If no newline entered, add a newline
  148. [[ "$REPLY" != $'\n' ]] && echo
  149. # If NO selected, do nothing else
  150. [[ "$REPLY" = [nN] ]] && return
  151. (
  152. set -e
  153. builtin cd -q "$ZSH"
  154. command git checkout "$branch" -- || {
  155. _omz::log error "could not go back to the previous branch ('$branch')."
  156. return 1
  157. }
  158. )
  159. }
  160. function _omz::update {
  161. # Run update script
  162. env ZSH="$ZSH" sh "$ZSH/tools/upgrade.sh"
  163. # Update last updated file
  164. zmodload zsh/datetime
  165. echo "LAST_EPOCH=$(( EPOCHSECONDS / 60 / 60 / 24 ))" >! "${ZSH_CACHE_DIR}/.zsh-update"
  166. # Remove update lock if it exists
  167. command rm -rf "$ZSH/log/update.lock"
  168. }