cli.zsh 5.0 KB

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