Skip to content

Commit 2b7fc19

Browse files
scopakinomyoga
andcommitted
refactor: mark deprecated config variables clearer
Move ones in the main `bash_completion` to the compat snippet. Co-authored-by: Koichi Murase <[email protected]>
1 parent 1fd456e commit 2b7fc19

File tree

7 files changed

+61
-14
lines changed

7 files changed

+61
-14
lines changed

bash_completion

+35-7
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,36 @@ _comp_deprecate_func()
6161
eval -- "$2() { $3 \"\$@\"; }"
6262
}
6363

64+
# Declare a compatibility variable name.
65+
# For bash 4.3+, a real name alias is created, allowing value changes to
66+
# "apply through" when the variables are set later. For bash versions earlier
67+
# than that, the operation is once-only; the value of the new variable
68+
# (if it's unset) is set to that of the old (if set) at call time.
69+
#
70+
# @param $1 Version of bash-completion where the deprecation occurred
71+
# @param $2 Old variable name
72+
# @param $3 New variable name
73+
# @since 2.12
74+
_comp_deprecate_var()
75+
{
76+
if (($# != 3)); then
77+
printf 'bash_completion: %s: usage: %s DEPRECATION_VERSION OLD_NAME NEW_NAME\n' "$FUNCNAME" "$FUNCNAME"
78+
return 2
79+
fi
80+
if [[ $2 != [a-zA-Z_]*([a-zA-Z_0-9]) ]]; then
81+
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "\$2: invalid variable name '$1'" >&2
82+
return 2
83+
elif [[ $3 != [a-zA-Z_]*([a-zA-Z_0-9]) ]]; then
84+
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "\$3: invalid variable name '$2'" >&2
85+
return 2
86+
fi
87+
if ((BASH_VERSINFO[0] >= 5 || BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 3)); then
88+
eval "declare -gn $2=$3"
89+
elif [[ -v $2 && ! -v $3 ]]; then
90+
printf -v "$3" %s "$2"
91+
fi
92+
}
93+
6494
# A lot of the following one-liners were taken directly from the
6595
# completion examples provided with the bash 2.04 source distribution
6696

@@ -1092,14 +1122,13 @@ _comp_compgen_filedir()
10921122
# the fallback condition with the "plus" dirs.
10931123
local _opts=(-f -X "$_xspec")
10941124
[[ $_xspec ]] && _plusdirs=(-o plusdirs)
1095-
[[ ${BASH_COMPLETION_FILEDIR_FALLBACK-${COMP_FILEDIR_FALLBACK-}} ||
1096-
! ${_plusdirs-} ]] ||
1125+
[[ ${BASH_COMPLETION_FILEDIR_FALLBACK-} || ! ${_plusdirs-} ]] ||
10971126
_opts+=("${_plusdirs[@]}")
10981127

10991128
_comp_compgen -v toks -c "$_quoted" -- "${_opts[@]}"
11001129

11011130
# Try without filter if it failed to produce anything and configured to
1102-
[[ ${BASH_COMPLETION_FILEDIR_FALLBACK-${COMP_FILEDIR_FALLBACK-}} &&
1131+
[[ ${BASH_COMPLETION_FILEDIR_FALLBACK-} &&
11031132
$_arg && ${#toks[@]} -lt 1 ]] &&
11041133
_comp_compgen -av toks -c "$_quoted" -- \
11051134
-f ${_plusdirs+"${_plusdirs[@]}"}
@@ -2626,7 +2655,7 @@ _comp_compgen_known_hosts__impl()
26262655
fi
26272656

26282657
# Add hosts reported by avahi-browse, if desired and it's available.
2629-
if [[ ${BASH_COMPLETION_KNOWN_HOSTS_WITH_AVAHI-${COMP_KNOWN_HOSTS_WITH_AVAHI-}} ]] &&
2658+
if [[ ${BASH_COMPLETION_KNOWN_HOSTS_WITH_AVAHI-} ]] &&
26302659
type avahi-browse &>/dev/null; then
26312660
# Some old versions of avahi-browse reportedly didn't have -k
26322661
# (even if mentioned in the manpage); those we do not support any more.
@@ -2644,7 +2673,7 @@ _comp_compgen_known_hosts__impl()
26442673

26452674
# Add results of normal hostname completion, unless
26462675
# `BASH_COMPLETION_KNOWN_HOSTS_WITH_HOSTFILE' is set to an empty value.
2647-
if [[ ${BASH_COMPLETION_KNOWN_HOSTS_WITH_HOSTFILE-${COMP_KNOWN_HOSTS_WITH_HOSTFILE-1}} ]]; then
2676+
if [[ ${BASH_COMPLETION_KNOWN_HOSTS_WITH_HOSTFILE-set} ]]; then
26482677
_comp_compgen -av known_hosts -- -A hostname -P "$prefix" -S "$suffix"
26492678
fi
26502679

@@ -3006,8 +3035,7 @@ _comp_compgen_filedir_xspec()
30063035
_comp_compgen -av toks -c "$quoted" -- -f -X "@(|!($xspec))"
30073036
30083037
# Try without filter if it failed to produce anything and configured to
3009-
[[ ${BASH_COMPLETION_FILEDIR_FALLBACK-${COMP_FILEDIR_FALLBACK-}} &&
3010-
${#toks[@]} -lt 1 ]] &&
3038+
[[ ${BASH_COMPLETION_FILEDIR_FALLBACK-} && ${#toks[@]} -lt 1 ]] &&
30113039
_comp_compgen -av toks -c "$quoted" -- -f
30123040
30133041
((${#toks[@]})) || return 1

bash_completion.d/000_bash_completion_compat.bash

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ _comp_deprecate_func 2.12 _longopt _comp_complete_longopt
4242
_comp_deprecate_func 2.12 _filedir_xspec _comp_complete_filedir_xspec
4343
_comp_deprecate_func 2.12 _minimal _comp_complete_minimal
4444

45+
_comp_deprecate_var 2.12 COMP_FILEDIR_FALLBACK BASH_COMPLETION_FILEDIR_FALLBACK
46+
_comp_deprecate_var 2.12 COMP_KNOWN_HOSTS_WITH_AVAHI BASH_COMPLETION_KNOWN_HOSTS_WITH_AVAHI
47+
_comp_deprecate_var 2.12 COMP_KNOWN_HOSTS_WITH_HOSTFILE BASH_COMPLETION_KNOWN_HOSTS_WITH_HOSTFILE
48+
4549
# @deprecated 2.12 Use `_comp_xspecs`
4650
declare -Ag _xspecs
4751

completions/configure

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# bash completion for configure -*- shell-script -*-
22

3+
_comp_deprecate_var 2.12 \
4+
COMP_CONFIGURE_HINTS BASH_COMPLETION_CMD_CONFIGURE_HINTS
5+
36
_comp_cmd_configure()
47
{
58
local cur prev words cword was_split comp_args
@@ -27,7 +30,7 @@ _comp_cmd_configure()
2730

2831
# if $BASH_COMPLETION_CMD_CONFIGURE_HINTS is not null, then completions of
2932
# the form --option=SETTING will include 'SETTING' as a contextual hint
30-
if [[ ${BASH_COMPLETION_CMD_CONFIGURE_HINTS-${COMP_CONFIGURE_HINTS-}} ]]; then
33+
if [[ ${BASH_COMPLETION_CMD_CONFIGURE_HINTS-} ]]; then
3134
_comp_compgen_split -- "$("$1" --help 2>&1 |
3235
_comp_awk '/^ --[A-Za-z]/ { print $1; \
3336
if ($2 ~ /--[A-Za-z]/) print $2 }' | command sed -e 's/[[,].*//g')"

completions/cvs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# cvs(1) completion -*- shell-script -*-
22

3+
_comp_deprecate_var 2.12 \
4+
COMP_CVS_REMOTE BASH_COMPLETION_CMD_CVS_REMOTE
5+
36
_comp_cmd_cvs__entries()
47
{
58
local prefix=${cur%/*}/ IFS=$'\n'
@@ -256,7 +259,7 @@ _comp_cmd_cvs()
256259
# if $BASH_COMPLETION_CMD_CVS_REMOTE is not null, 'cvs commit'
257260
# will complete on remotely checked-out files (requires
258261
# passwordless access to the remote repository
259-
if [[ ${BASH_COMPLETION_CMD_CVS_REMOTE-${COMP_CVS_REMOTE-}} ]]; then
262+
if [[ ${BASH_COMPLETION_CMD_CVS_REMOTE-} ]]; then
260263
# this is the least computationally intensive way found so
261264
# far, but other changes (something other than
262265
# changed/removed/new) may be missing

completions/iwconfig

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# iwconfig completion -*- shell-script -*-
22

3+
_comp_deprecate_var 2.12 \
4+
COMP_IWLIST_SCAN BASH_COMPLETION_CMD_IWCONFIG_SCAN
5+
36
_comp_cmd_iwconfig()
47
{
58
local cur prev words cword comp_args
@@ -13,7 +16,7 @@ _comp_cmd_iwconfig()
1316
;;
1417
essid)
1518
_comp_compgen -- -W 'on off any'
16-
if [[ ${BASH_COMPLETION_CMD_IWCONFIG_SCAN-${COMP_IWLIST_SCAN-}} ]]; then
19+
if [[ ${BASH_COMPLETION_CMD_IWCONFIG_SCAN-} ]]; then
1720
_comp_compgen -a split -- "$(iwlist "${words[1]}" scan |
1821
_comp_awk -F '\"' '/ESSID/ {print $2}')"
1922
fi
@@ -36,7 +39,7 @@ _comp_cmd_iwconfig()
3639
;;
3740
ap)
3841
_comp_compgen -- -W 'on off any'
39-
if [[ ${BASH_COMPLETION_CMD_IWCONFIG_SCAN-${COMP_IWLIST_SCAN-}} ]]; then
42+
if [[ ${BASH_COMPLETION_CMD_IWCONFIG_SCAN-} ]]; then
4043
_comp_compgen -a split -- "$(iwlist "${words[1]}" scan |
4144
_comp_awk -F ': ' '/Address/ {print $2}')"
4245
fi

completions/tar

+5-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
# `$BASH_COMPLETION_CMD_TAR_INTERNAL_PATHS` environment variable to a non-null
5757
# value *before sourcing* this completion toggles that the other way around.
5858

59+
_comp_deprecate_var 2.12 \
60+
COMP_TAR_INTERNAL_PATHS BASH_COMPLETION_CMD_TAR_INTERNAL_PATHS
61+
5962
_comp_cmd_gtar__parse_help_opt()
6063
{
6164
local opttype arg opt separator optvar
@@ -723,15 +726,15 @@ _comp_cmd_tar()
723726
$func "$@"
724727

725728
# Install real completion for subsequent completions
726-
if [[ ${BASH_COMPLETION_CMD_TAR_INTERNAL_PATHS-${COMP_TAR_INTERNAL_PATHS-}} ]]; then
729+
if [[ ${BASH_COMPLETION_CMD_TAR_INTERNAL_PATHS-} ]]; then
727730
complete -F $func -o dirnames tar
728731
else
729732
complete -F $func tar
730733
fi
731734
unset -f "$FUNCNAME"
732735
}
733736

734-
if [[ ${BASH_COMPLETION_CMD_TAR_INTERNAL_PATHS-${COMP_TAR_INTERNAL_PATHS-}} ]]; then
737+
if [[ ${BASH_COMPLETION_CMD_TAR_INTERNAL_PATHS-} ]]; then
735738
complete -F _comp_cmd_tar -o dirnames tar
736739
complete -F _comp_cmd_tar__gnu -o dirnames gtar
737740
complete -F _comp_cmd_tar__posix -o dirnames bsdtar

test/t/test_cvsps.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import pytest
22

33

4-
@pytest.mark.bashcomp(pre_cmds=("HOME=$PWD/cvs",))
4+
@pytest.mark.bashcomp(
5+
pre_cmds=("HOME=$PWD/cvs",),
6+
ignore_env=r"^[+-]COMP_CVS_REMOTE=",
7+
)
58
class TestCvsps:
69
@pytest.mark.complete("cvsps -", require_cmd=True)
710
def test_1(self, completion):

0 commit comments

Comments
 (0)