gbp.completion 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # -*- shell-script -*-
  2. #
  3. # Bash tab auto-completion rules for git-buildpackage.
  4. #
  5. # Copyright (C) 2010 Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com>
  6. # Copyright (C) 2010,2013,2015 Guido Guenther <agx@sigxcpu.org>
  7. #
  8. # Distributed under the GNU General Public License, version 2.0.
  9. _gbp_branches ()
  10. {
  11. [ -d .git ] || return 0
  12. git for-each-ref --format="%(refname:short)" refs/heads
  13. }
  14. _gbp_tags ()
  15. {
  16. [ -d .git ] || return 0
  17. git for-each-ref --format="%(refname:short)" refs/tags
  18. }
  19. _gbp_options ()
  20. {
  21. GBP_DISABLE_SECTION_DEPRECTATION=true \
  22. gbp "${1}" --help | sed -ne 's/^ \+\(\(\-[a-z]\), \)\?\(\-\-[a-z\-]\+\=\?\).*/\2 \3/p'
  23. }
  24. _gbp_commands ()
  25. {
  26. gbp --list-cmds | sed -ne 's/^ \+\([a-z\-]\+\) \-.*/\1/p'
  27. }
  28. _gbp_comp ()
  29. {
  30. local cur="${COMP_WORDS[COMP_CWORD]}"
  31. local prev="${COMP_WORDS[COMP_CWORD - 1]}"
  32. local options=$1
  33. local branch_opts=${2:-"--debian-branch\= --upstream-branch\="}
  34. local tag_opts=${3:-"--debian-tag\= --upstream-tag\="}
  35. local tristate_opts=${4:-"--color\="}
  36. local cbdist_opts=${5:-"--git-dist\="}
  37. # COMPREPLY considers '=' as a word. For $prev we prefer the word before the actual "="
  38. if [[ "$prev" == "=" ]]; then
  39. prev="${COMP_WORDS[COMP_CWORD - 2]}"
  40. elif [[ "$cur" == "=" ]]; then
  41. cur=""
  42. fi
  43. if [[ "${branch_opts}" == *$prev* ]]; then
  44. local refs=$(_gbp_branches)
  45. COMPREPLY=( $(compgen -W "$refs" -- $cur ) )
  46. return 0
  47. fi
  48. if [[ "${tag_opts}" == *$prev* ]]; then
  49. local refs=$(_gbp_tags)
  50. COMPREPLY=( $(compgen -W "$refs" -- $cur ) )
  51. return 0
  52. fi
  53. if [[ "${tristate_opts}" == *$prev* ]]; then
  54. COMPREPLY=( $(compgen -W 'on off auto' -- $cur ) )
  55. return 0
  56. fi
  57. if [[ "${cbdist_opts}" == *$prev* ]]; then
  58. local BASE="/var/cache/pbuilder/base-"
  59. COMPREPLY=( $( compgen -o dirnames -G "${BASE}${cur}*.cow" \
  60. | sed -e "s,${BASE}\(.*\)\.cow,\1,g" ) )
  61. return 0
  62. fi
  63. # separate opts by tab so we can append a space to all options not ending
  64. # with an equal sign
  65. tab_opts=$(echo "$options" | sed -e 's/ \+/\t/g' -e 's/[^=]$/& /g')
  66. type compopt &>/dev/null && compopt -o nospace
  67. local IFS=$'\t\n'
  68. COMPREPLY=($(compgen -W "$tab_opts" -- $cur))
  69. }
  70. # check if we can find a gbp command on the commandline
  71. _gbp_find_cmd_on_cmdline ()
  72. {
  73. local cmds="$1" # list of commands to check for
  74. local word cmd c=1
  75. while [ $c -lt $((COMP_CWORD)) ]; do
  76. word="${COMP_WORDS[c]}"
  77. for cmd in $cmds; do
  78. if [ "$cmd" = "$word" ]; then
  79. echo "$cmd"
  80. return
  81. fi
  82. done
  83. ((c++))
  84. done
  85. }
  86. _gbp-buildpackage()
  87. {
  88. local options=$(_gbp_options buildpackage)
  89. local branch_opts="--git-debian-branch\= --git-upstream-branch\= --git-upstream-tree\="
  90. local tag_opts="--git-debian-tag\= --git-upstream-tag\="
  91. local tristate_opts="--git-color\= --git-notify\="
  92. _gbp_comp "$options" "$branch_opts" "$tag_opts" "$tristate_opts" \
  93. "$cbdist_opts"
  94. }
  95. _gbp-pq ()
  96. {
  97. local options=$(_gbp_options pq)
  98. options="$options export import rebase drop apply switch"
  99. _gbp_comp "$options"
  100. }
  101. _gbp-generic-cmd()
  102. {
  103. local options=$(_gbp_options "${1}")
  104. _gbp_comp "$options"
  105. }
  106. _have gbp &&
  107. _gbp ()
  108. {
  109. local cur="${COMP_WORDS[COMP_CWORD]}"
  110. local commands=$(_gbp_commands)
  111. local func
  112. command=$(_gbp_find_cmd_on_cmdline "$commands")
  113. if [ -z "${command}" ]; then
  114. COMPREPLY=( $(compgen -W "$commands" -- "${cur}" ) )
  115. else
  116. if type _gbp-"${command}" >& /dev/null; then
  117. _gbp-"${command}"
  118. else
  119. _gbp-generic-cmd "${command}"
  120. fi
  121. fi
  122. } && complete -F _gbp -o default gbp