Debian Codegraph @

debian/fpm.bash-completion

raw
1 # -*- shell-script -*-
2 #
3 # Bash tab auto-completion rules for fpm
4 #
5 # WIP based on gbp
6 #
7 # Copyright (C) 2026 Alastair McKinstry <[email protected]>
8 #
9 # Distributed under the GNU General Public License, version 2.0.
10
11
12 _gbp_tags ()
13 {
14 [ -d .git ] || return 0
15 git for-each-ref --format="%(refname:short)" refs/tags
16 }
17
18
19 _gbp_remote_configs ()
20 {
21 GBP_DISABLE_GBP_CONF_DEPRECATION=true \
22 GBP_DISABLE_SECTION_DEPRECATION=true \
23 gbp create-remote-repo list | sed -ne 's/^ \+\([a-z]\+\)/\1/p'
24 }
25
26
27 _gbp_options ()
28 {
29 GBP_DISABLE_GBP_CONF_DEPRECATION=true \
30 GBP_DISABLE_SECTION_DEPRECATION=true \
31 gbp "${1}" --help | sed -ne 's/^ \+\(\(\-[a-z]\), \)\?\(\-\-[a-z\-]\+\=\?\).*/\2 \3/p'
32 }
33
34
35 _fpm_commands ()
36 {
37 echo << EOF
38 build
39 new
40 update
41 run
42 test
43 help
44 list
45 install
46 clean
47 publish
48 EOF
49 }
50
51
52 _gbp_comp ()
53 {
54 local cur="${COMP_WORDS[COMP_CWORD]}"
55 local prev="${COMP_WORDS[COMP_CWORD - 1]}"
56 local options=$1
57 local branch_opts=${2:-"--debian-branch\= --upstream-branch\= --upstream-tree\="}
58 local tag_opts=${3:-"--debian-tag\= --upstream-tag\= --upstream-vcs-tag\="}
59 local tristate_opts=${4:-"--color\="}
60 local cbdist_opts=${5:-"--git-dist\="}
61 local remote_config_opts="--remote-config\="
62 local file_opts="--postimport\= --git-builder\= --git-cleaner\= \
63 --git-export-dir\= --git-postbuild\= --git-postexport\= \
64 --git-posttag\= --git-prebuild\= --git-preexport\= --git-tarball-dir\= \
65 --posttag\= --tarball-dir\="
66 local start_opt=""
67
68 # COMPREPLY considers '=' as a word. For $prev we prefer the word before the actual "="
69 if [[ "$prev" == "=" ]]; then
70 prev="${COMP_WORDS[COMP_CWORD - 2]}"
71 elif [[ "$cur" == "=" ]]; then
72 start_opt=1
73 cur=""
74 fi
75
76 if [[ ${prev} == --* ]]; then
77 if [[ "${branch_opts}" == *$prev* && "${prev}" == --* ]]; then
78 local refs=$(_gbp_branches)
79 COMPREPLY=( $(compgen -W "$refs" -- $cur ) )
80 return 0
81 fi
82
83 if [[ "${tag_opts}" == *$prev* && "${prev}" == --* ]]; then
84 local refs=$(_gbp_tags)
85 COMPREPLY=( $(compgen -W "$refs" -- $cur ) )
86 return 0
87 fi
88
89 if [[ "${tristate_opts}" == *$prev* ]]; then
90 COMPREPLY=( $(compgen -W 'on off auto' -- $cur ) )
91 return 0
92 fi
93
94 if [[ "${cbdist_opts}" == *$prev* ]]; then
95 local BASE="/var/cache/pbuilder/base-"
96 COMPREPLY=( $( compgen -o dirnames -G "${BASE}${cur}*.cow" \
97 | sed -e "s,${BASE}\(.*\)\.cow,\1,g" ) )
98 return 0
99 fi
100
101 if [[ "${remote_config_opts}" == *$prev* ]]; then
102 local remote_configs=$(_gbp_remote_configs)
103 COMPREPLY=( $(compgen -W "$remote_configs" -- $cur ) )
104 return 0
105 fi
106
107 if [[ "${file_opts}" == *$prev* ]]; then
108 COMPREPLY=( $(compgen -f -- $cur ) )
109 return 0
110 fi
111 fi
112
113 # separate opts by tab so we can append a space to all options not ending
114 # with an equal sign
115 tab_opts=$(echo "$options" | sed -e 's/ \+/\t/g' -e 's/[^=]$/& /g')
116 type compopt &>/dev/null && compopt -o nospace
117 local IFS=$'\t\n'
118 if [ -n "$start_opt" ]; then
119 COMPREPLY="" # we don't have any good suggestions
120 else
121 COMPREPLY=($(compgen -W "$tab_opts" -- $cur))
122 fi
123 }
124
125 # check if we can find a gbp command on the commandline
126 _fpm_find_cmd_on_cmdline ()
127 {
128 local cmds="$1" # list of commands to check for
129 local word cmd c=1
130
131 while [ $c -lt $((COMP_CWORD)) ]; do
132 word="${COMP_WORDS[c]}"
133 for cmd in $cmds; do
134 if [ "$cmd" = "$word" ]; then
135 echo "$cmd"
136 return
137 fi
138 done
139 ((c++))
140 done
141 }
142
143 _gbp-buildpackage()
144 {
145 local options=$(_gbp_options buildpackage)
146 local branch_opts="--git-debian-branch\= --git-upstream-branch\= --git-upstream-tree\="
147 local tag_opts="--git-debian-tag\= --git-upstream-tag\="
148 local tristate_opts="--git-color\= --git-notify\="
149
150 _gbp_comp "$options" "$branch_opts" "$tag_opts" "$tristate_opts" \
151 "$cbdist_opts"
152 }
153
154
155 _gbp-pq ()
156 {
157 local options=$(_gbp_options pq)
158 options="$options export import rebase drop apply switch"
159 _gbp_comp "$options"
160 }
161
162
163 _gbp-create-remote-repo ()
164 {
165 local options=$(_gbp_options create-remote-repo)
166 options="$options list"
167 _gbp_comp "$options"
168 }
169
170
171 _gbp-generic-cmd()
172 {
173 local options=$(_gbp_options "${1}")
174 _gbp_comp "$options"
175 }
176
177
178 _have fpm &&
179 _fpm ()
180 {
181 local cur="${COMP_WORDS[COMP_CWORD]}"
182 local commands=$(_fpm_commands)
183 local func
184
185 command=$(_fpm_find_cmd_on_cmdline "$commands")
186 if [ -z "${command}" ]; then
187 COMPREPLY=( $(compgen -W "$commands" -- "${cur}" ) )
188 else
189 if type _gbp-"${command}" >& /dev/null; then
190 _fpm-"${command}"
191 else
192 _fpm-generic-cmd "${command}"
193 fi
194 fi
195 } && complete -F _fpm -o default fpm