Skip to content

Commit 5ed9c1f

Browse files
committed
Add completion script for dgit
This script autocompletes the most common subcommands, but only provides arguments autocompletion for only two of them, import-dsc and rpush, for practical reasons. For dgit options, it provides two-level completion for the most popular options, excluding rarely used or dangerous ("use when you know what you’re doing") options. The two-levels work in a way to not overwhelm the user with options when they Tab twice, so only some but not all arguments to e.g. --quilt= are shown: $ dgit -- --baredebian --force-import-gitapply-no-absurd --baredebian+git --force-reusing-version --baredebian+tarball --force-unrepresentable --build-products-dir= --force-unsupported-source-format --clean --force-uploading-binaries --clean=dpkg-source --force-uploading-source-only --clean=git --gbp --clean=git-ff --include-dirty --damp-run --new --delayed= --no-rm-on-error --deliberately-fresh-repo --no-sign --deliberately-include-questionable-history --overwrite --deliberately-not-fast-forward --package --dep14tag --quilt --distro= --quilt=auto --dpm --quilt=linear --dry-run --rm-old-changes --force-changes-origs-exactly --save-dgit-view= --force-dsc-changes-mismatch --split-view= --force-import-dsc-with-dgit-field --upstream-commitish= --force-import-gitapply-absurd However, when the user types --quilt=, the rest of them is shown: $ dgit --quilt= auto baredebian+git dpm linear nofix unapplied baredebian baredebian+tarball gbp nocheck smash For --upstream-commitish= the script autocompletes some tags, starting with tags with the matching version, and the rest sorted by version: $ dgit --upstream-commitish=upstream/1. upstream/1.11.0 upstream/1.6.0 For options accepting both = and space (--quilt, --clean, --package) the completion is performed for both versions; the list of all options above shows the "spaced" (--clean) version and a couple of commonly used versions with = and an argument. For options only accepting =, the option ends with = to allow the user to start typing the argument directly after that. Signed-off-by: Andrej Shadura <andrew.shadura@collabora.co.uk>
1 parent b6a9ed5 commit 5ed9c1f

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

completions/dgit

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# bash completion for dgit -*- shell-script -*-
2+
3+
# Author: Andrej Shadura <andrewsh@debian.org>
4+
5+
# This file is in public domain when available or, alternatively, can be
6+
# redistributed either under the terms of CC0 1.0 Universal license or
7+
# under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation; either version 2, or (at your option)
9+
# any later version.
10+
11+
# SPDX-License-Identifier: CC0-1.0 OR GPL-2.0-or-later
12+
13+
_dgit()
14+
{
15+
local cur prev words cword i _options special _prefix
16+
_init_completion -n = || return
17+
18+
local clean_opts=(git{,\,always} git-ff{,\,always} check{,\,ignores} none
19+
dpkg-source{,\,no-check,\,all-check} dpkg-source-d{,\,no-check,\,all-check})
20+
local clean_popular_opts=(git git-ff dpkg-source)
21+
local standalone_quilt_opts=(gbp dpm baredebian{,+git,+tarball})
22+
local quilt_opts=(linear auto smash nofix nocheck unapplied ${standalone_quilt_opts[@]})
23+
local quilt_popular_opts=(auto linear)
24+
local force_opts=(unrepresentable unsupported-source-format dsc-changes-mismatch
25+
changes-origs-exactly uploading-binaries uploading-source-only
26+
reusing-version import-gitapply-absurd import-gitapply-no-absurd
27+
import-dsc-with-dgit-field)
28+
local buildcommands="build sbuild gbp-build build-source pbuilder cowbuilder"
29+
local subcommands="clone fetch pull $buildcommands
30+
push push-source rpush setup-new-tree setup-useremail
31+
setup-mergechangelogs setup-gitattributes import-dsc update-vcs-git"
32+
33+
for word in ${words[@]}
34+
do
35+
if [[ $word == @(${subcommands// /|}) ]]
36+
then
37+
special=$word
38+
break
39+
fi
40+
done
41+
42+
if [ -n "$special" ] && [[ $special == @(${buildcommands// /|}) ]]
43+
then
44+
COMPREPLY=()
45+
return 0
46+
fi
47+
48+
case "$prev" in
49+
--clean)
50+
COMPREPLY=($(compgen -W "${clean_opts[*]}" -- "$cur"))
51+
return 0
52+
;;
53+
--quilt)
54+
COMPREPLY=($(compgen -W "${quilt_opts[*]}" -- "$cur"))
55+
return 0
56+
;;
57+
--build-products-dir)
58+
COMPREPLY=($(compgen -o filenames -d -- "$cur"))
59+
return 0
60+
;;
61+
-p|--package)
62+
COMPREPLY=()
63+
return 0
64+
;;
65+
-k)
66+
COMPREPLY=($(compgen -W "$(gpg --list-secret-keys --with-colons | grep ^sec | cut -d: -f5)" -- "$cur"))
67+
return 0
68+
;;
69+
import-dsc)
70+
COMPREPLY=($(compgen -o filenames -f -G '../*.dsc' -- "$cur"))
71+
return 0
72+
;;
73+
rpush)
74+
_known_hosts_real -c -a -- "$cur"
75+
compopt -o nospace
76+
return 0
77+
;;
78+
esac
79+
80+
case "$cur" in
81+
--clean=*)
82+
COMPREPLY=($(compgen -W "${clean_opts[*]}" -- "${cur#*=}"))
83+
return 0
84+
;;
85+
--quilt=*)
86+
COMPREPLY=($(compgen -W "${quilt_opts[*]}" -- "${cur#*=}"))
87+
return 0
88+
;;
89+
--split-view=*)
90+
COMPREPLY=($(compgen -W "auto always never" -- "${cur#*=}"))
91+
return 0
92+
;;
93+
--build-products-dir=*)
94+
COMPREPLY=($(compgen -o filenames -d -- "${cur#*=}"))
95+
return 0
96+
;;
97+
--upstream-commitish=*)
98+
local version=$( (dpkg-parsechangelog -SVersion 2>/dev/null || true) | sed -e 's/-[^-]*$//' -e 's/^[0-9]*://')
99+
local thesetags=$([ -n "$version" ] && git tag --list "$version" "v$version" "upstream/$version" 2>/dev/null || true)
100+
local othertags=$( (git tag --list --sort version:refname '[0-9]*' 'v[0-9]*' 'upstream/[0-9]*' 2>/dev/null || true) | grep -v -F "$version")
101+
COMPREPLY=($(compgen -W "$thesetags $othertags" -- "${cur#*=}"))
102+
compopt -o nosort
103+
return 0
104+
;;
105+
--*=*)
106+
COMPREPLY=()
107+
return 0
108+
;;
109+
esac
110+
111+
if [[ "$cur" == -* ]]; then
112+
_options="--dry-run --damp-run --no-sign --new --include-dirty"
113+
_options+=" --overwrite --delayed= --save-dgit-view="
114+
_options+=" --deliberately-not-fast-forward"
115+
_options+=" --deliberately-include-questionable-history"
116+
_options+=" --deliberately-fresh-repo"
117+
_options+=" --distro= --split-view= --upstream-commitish="
118+
_options+=" --rm-old-changes --build-products-dir="
119+
_options+=" --package -p --dep14tag --no-rm-on-error -k"
120+
_options+=" --clean ${clean_popular_opts[*]/#/--clean=}"
121+
_options+=" ${standalone_quilt_opts[*]/#/--}"
122+
_options+=" --quilt ${quilt_popular_opts[*]/#/--quilt=}"
123+
_options+=" ${force_opts[*]/#/--force-}"
124+
COMPREPLY=($(compgen -W "${_options}" -- "$cur"))
125+
# tell bash to not put a space afrer options ending with =
126+
# so that the user can type the argument directly after it
127+
if [ ${#COMPREPLY[@]} = 1 ] && [[ "${COMPREPLY[0]}" == *= ]]
128+
then
129+
compopt -o nospace
130+
fi
131+
else
132+
if [ -z "$special" ]
133+
then
134+
COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))
135+
else
136+
# if nothing explicitly handled these commands above,
137+
# just don’t autocomplete at all
138+
COMPREPLY=()
139+
fi
140+
fi
141+
142+
return 0
143+
} &&
144+
complete -F _dgit dgit
145+
146+
# ex: filetype=sh

0 commit comments

Comments
 (0)