_hub 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. sync:'update local branches from upstream'
  83. )
  84. _describe -t hub-commands 'hub command' hub_commands && ret=0
  85. return ret
  86. }
  87. }
  88. (( $+functions[__hub_setup_bash_fns] )) ||
  89. __hub_setup_bash_fns () {
  90. # TODO more bash-style fns needed here to complete subcommand args. They take
  91. # the form "_git_CMD" where "CMD" is something like "pull-request".
  92. # Duplicate and rename the 'list_all_commands' function
  93. eval "$(declare -f __git_list_all_commands | \
  94. sed 's/__git_list_all_commands/__git_list_all_commands_without_hub/')"
  95. # Wrap the 'list_all_commands' function with extra hub commands
  96. __git_list_all_commands() {
  97. cat <<-EOF
  98. alias
  99. pull-request
  100. fork
  101. create
  102. browse
  103. compare
  104. ci-status
  105. sync
  106. EOF
  107. __git_list_all_commands_without_hub
  108. }
  109. # Ensure cached commands are cleared
  110. __git_all_commands=""
  111. }
  112. # redefine _hub to a much smaller function in the steady state
  113. _hub () {
  114. # only attempt to intercept the normal "_git" helper functions once
  115. (( $+__hub_func_replacement_done )) ||
  116. () {
  117. # At this stage in the shell's execution the "_git" function has not yet
  118. # been autoloaded, so the "_git_commands" or "__git_list_all_commands"
  119. # functions will not be defined. Call it now (with a bogus no-op service
  120. # to prevent premature completion) so that we can wrap them.
  121. if declare -f _git >& /dev/null ; then
  122. _hub_noop () { __hub_zsh_provided=1 } # zsh-provided will call this one
  123. __hub_noop_main () { __hub_git_provided=1 } # git-provided will call this one
  124. local service=hub_noop
  125. _git
  126. unfunction _hub_noop
  127. unfunction __hub_noop_main
  128. service=git
  129. fi
  130. if (( $__hub_zsh_provided )) ; then
  131. __hub_setup_zsh_fns
  132. elif (( $__hub_git_provided )) ; then
  133. __hub_setup_bash_fns
  134. fi
  135. __hub_func_replacement_done=1
  136. }
  137. # Now perform the actual completion, allowing the "_git" function to call our
  138. # replacement "_git_commands" function as needed. Both versions expect
  139. # service=git or they will call nonexistent routines or end up in an infinite
  140. # loop.
  141. service=git
  142. declare -f _git >& /dev/null && _git
  143. }
  144. # make sure we actually attempt to complete on the first "tab" from the user
  145. _hub