Skip to content

Commit 25a31f8

Browse files
tpavlicgitster
authored andcommitted
bash-completion: Support running when set -u is enabled
Under "set -u" semantics, it is an error to access undefined variables. Some user environments may enable this setting in the interactive shell. In any context where the completion functions access an undefined variable, accessing a default empty string (aka "${1-}" instead of "$1") is a reasonable way to code the function, as it silences the undefined variable error while still supplying an empty string. In this patch, functions that should always take an argument still use $1. Functions that have optional arguments use ${1-}. Signed-off-by: Ted Pavlic <[email protected]> Acked-by: Shawn O. Pearce <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b32acd2 commit 25a31f8

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

contrib/completion/git-completion.bash

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ esac
5252

5353
__gitdir ()
5454
{
55-
if [ -z "$1" ]; then
55+
if [ -z "${1-}" ]; then
5656
if [ -n "$__git_dir" ]; then
5757
echo "$__git_dir"
5858
elif [ -d .git ]; then
@@ -111,7 +111,7 @@ __git_ps1 ()
111111
fi
112112
fi
113113

114-
if [ -n "$1" ]; then
114+
if [ -n "${1-}" ]; then
115115
printf "$1" "${b##refs/heads/}$r"
116116
else
117117
printf " (%s)" "${b##refs/heads/}$r"
@@ -143,22 +143,22 @@ __gitcomp ()
143143
;;
144144
*)
145145
local IFS=$'\n'
146-
COMPREPLY=($(compgen -P "$2" \
147-
-W "$(__gitcomp_1 "$1" "$4")" \
146+
COMPREPLY=($(compgen -P "${2-}" \
147+
-W "$(__gitcomp_1 "${1-}" "${4-}")" \
148148
-- "$cur"))
149149
;;
150150
esac
151151
}
152152

153153
__git_heads ()
154154
{
155-
local cmd i is_hash=y dir="$(__gitdir "$1")"
155+
local cmd i is_hash=y dir="$(__gitdir "${1-}")"
156156
if [ -d "$dir" ]; then
157157
git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
158158
refs/heads
159159
return
160160
fi
161-
for i in $(git ls-remote "$1" 2>/dev/null); do
161+
for i in $(git ls-remote "${1-}" 2>/dev/null); do
162162
case "$is_hash,$i" in
163163
y,*) is_hash=n ;;
164164
n,*^{}) is_hash=y ;;
@@ -170,13 +170,13 @@ __git_heads ()
170170

171171
__git_tags ()
172172
{
173-
local cmd i is_hash=y dir="$(__gitdir "$1")"
173+
local cmd i is_hash=y dir="$(__gitdir "${1-}")"
174174
if [ -d "$dir" ]; then
175175
git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
176176
refs/tags
177177
return
178178
fi
179-
for i in $(git ls-remote "$1" 2>/dev/null); do
179+
for i in $(git ls-remote "${1-}" 2>/dev/null); do
180180
case "$is_hash,$i" in
181181
y,*) is_hash=n ;;
182182
n,*^{}) is_hash=y ;;
@@ -188,7 +188,7 @@ __git_tags ()
188188

189189
__git_refs ()
190190
{
191-
local i is_hash=y dir="$(__gitdir "$1")"
191+
local i is_hash=y dir="$(__gitdir "${1-}")"
192192
local cur="${COMP_WORDS[COMP_CWORD]}" format refs
193193
if [ -d "$dir" ]; then
194194
case "$cur" in

0 commit comments

Comments
 (0)