_hub 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #compdef hub
  2. # Zsh will source this file when attempting to autoload the "_hub" function,
  3. # typically on the first attempt to complete the hub command. We define two new
  4. # setup helper routines (one for the zsh-distributed version, one for the
  5. # git-distributed, bash-based version). Then we redefine the "_hub" function to
  6. # call "_git" after some other interception.
  7. #
  8. # This is pretty fragile, if you think about it. Any number of implementation
  9. # changes in the "_git" scripts could cause problems down the road. It would be
  10. # better if the stock git completions were just a bit more permissive about how
  11. # it allowed third-party commands to be added.
  12. (( $+functions[__hub_setup_zsh_fns] )) ||
  13. __hub_setup_zsh_fns () {
  14. (( $+functions[_git-alias] )) ||
  15. _git-alias () {
  16. _arguments \
  17. '-s[output shell script suitable for eval]' \
  18. '1::shell:(zsh bash csh)'
  19. }
  20. (( $+functions[_git-browse] )) ||
  21. _git-browse () {
  22. _arguments \
  23. '-u[output the URL]' \
  24. '2::subpage:(wiki commits issues)'
  25. }
  26. (( $+functions[_git-compare] )) ||
  27. _git-compare () {
  28. _arguments \
  29. '-u[output the URL]' \
  30. ':[start...]end range:'
  31. }
  32. (( $+functions[_git-create] )) ||
  33. _git-create () {
  34. _arguments \
  35. '::name (REPOSITORY or ORGANIZATION/REPOSITORY):' \
  36. '-p[make repository private]' \
  37. '-d[description]:description' \
  38. '-h[home page]:repository home page URL:_urls'
  39. }
  40. (( $+functions[_git-fork] )) ||
  41. _git-fork () {
  42. _arguments \
  43. '--no-remote[do not add a remote for the new fork]'
  44. }
  45. (( $+functions[_git-pull-request] )) ||
  46. _git-pull-request () {
  47. _arguments \
  48. '-f[force (skip check for local commits)]' \
  49. '-b[base]:base ("branch", "owner\:branch", "owner/repo\:branch"):' \
  50. '-h[head]:head ("branch", "owner\:branch", "owner/repo\:branch"):' \
  51. - set1 \
  52. '-m[message]' \
  53. '-F[file]' \
  54. '-a[user]' \
  55. '-M[milestone]' \
  56. '-l[labels]' \
  57. - set2 \
  58. '-i[issue]:issue number:' \
  59. - set3 \
  60. '::issue-url:_urls'
  61. }
  62. # stash the "real" command for later
  63. functions[_hub_orig_git_commands]=$functions[_git_commands]
  64. # Replace it with our own wrapper.
  65. declare -f _git_commands >& /dev/null && unfunction _git_commands
  66. _git_commands () {
  67. local ret=1
  68. # call the original routine
  69. _call_function ret _hub_orig_git_commands
  70. # Effectively "append" our hub commands to the behavior of the original
  71. # _git_commands function. Using this wrapper function approach ensures
  72. # that we only offer the user the hub subcommands when the user is
  73. # actually trying to complete subcommands.
  74. hub_commands=(
  75. alias:'show shell instructions for wrapping git'
  76. pull-request:'open a pull request on GitHub'
  77. fork:'fork origin repo on GitHub'
  78. create:'create new repo on GitHub for the current project'
  79. browse:'browse the project on GitHub'
  80. compare:'open GitHub compare view'
  81. ci-status:'lookup commit in GitHub Status API'
  82. )
  83. _describe -t hub-commands 'hub command' hub_commands && ret=0
  84. return ret
  85. }
  86. }
  87. (( $+functions[__hub_setup_bash_fns] )) ||
  88. __hub_setup_bash_fns () {
  89. # TODO more bash-style fns needed here to complete subcommand args. They take
  90. # the form "_git_CMD" where "CMD" is something like "pull-request".
  91. # Duplicate and rename the 'list_all_commands' function
  92. eval "$(declare -f __git_list_all_commands | \
  93. sed 's/__git_list_all_commands/__git_list_all_commands_without_hub/')"
  94. # Wrap the 'list_all_commands' function with extra hub commands
  95. __git_list_all_commands() {
  96. cat <<-EOF
  97. alias
  98. pull-request
  99. fork
  100. create
  101. browse
  102. compare
  103. ci-status
  104. EOF
  105. __git_list_all_commands_without_hub
  106. }
  107. # Ensure cached commands are cleared
  108. __git_all_commands=""
  109. }
  110. # redefine _hub to a much smaller function in the steady state
  111. _hub () {
  112. # only attempt to intercept the normal "_git" helper functions once
  113. (( $+__hub_func_replacement_done )) ||
  114. () {
  115. # At this stage in the shell's execution the "_git" function has not yet
  116. # been autoloaded, so the "_git_commands" or "__git_list_all_commands"
  117. # functions will not be defined. Call it now (with a bogus no-op service
  118. # to prevent premature completion) so that we can wrap them.
  119. if declare -f _git >& /dev/null ; then
  120. _hub_noop () { __hub_zsh_provided=1 } # zsh-provided will call this one
  121. __hub_noop_main () { __hub_git_provided=1 } # git-provided will call this one
  122. local service=hub_noop
  123. _git
  124. unfunction _hub_noop
  125. unfunction __hub_noop_main
  126. service=git
  127. fi
  128. if (( $__hub_zsh_provided )) ; then
  129. __hub_setup_zsh_fns
  130. elif (( $__hub_git_provided )) ; then
  131. __hub_setup_bash_fns
  132. fi
  133. __hub_func_replacement_done=1
  134. }
  135. # Now perform the actual completion, allowing the "_git" function to call our
  136. # replacement "_git_commands" function as needed. Both versions expect
  137. # service=git or they will call nonexistent routines or end up in an infinite
  138. # loop.
  139. service=git
  140. declare -f _git >& /dev/null && _git
  141. }
  142. # make sure we actually attempt to complete on the first "tab" from the user
  143. _hub