@matheusbsbs / matheus-bagliano / commits / 25a31f8140
Fork 0

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 <ted@tedpavlic.com> Acked-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ted Pavlic committed Jan 15, 2009 at 11:02 UTC 25a31f814016891c7728fdebca056ef47ca75547
1 file changed +9 -9
contrib/completion/git-completion.bash
+9 -9
@@ -52,7 +52,7 @@ esac
52
53 __gitdir ()
54 {
55 - if [ -z "$1" ]; then
55 + if [ -z "${1-}" ]; then
56 if [ -n "$__git_dir" ]; then
57 echo "$__git_dir"
58 elif [ -d .git ]; then
@@ -111,7 +111,7 @@ __git_ps1 ()
111 fi
112 fi
113
114 - if [ -n "$1" ]; then
114 + if [ -n "${1-}" ]; then
115 printf "$1" "${b##refs/heads/}$r"
116 else
117 printf " (%s)" "${b##refs/heads/}$r"
@@ -143,8 +143,8 @@ __gitcomp ()
143 ;;
144 *)
145 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-}")" \
148 -- "$cur"))
149 ;;
150 esac
@@ -152,13 +152,13 @@ __gitcomp ()
152
153 __git_heads ()
154 {
155 - local cmd i is_hash=y dir="$(__gitdir "$1")"
155 + local cmd i is_hash=y dir="$(__gitdir "${1-}")"
156 if [ -d "$dir" ]; then
157 git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
158 refs/heads
159 return
160 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
162 case "$is_hash,$i" in
163 y,*) is_hash=n ;;
164 n,*^{}) is_hash=y ;;
@@ -170,13 +170,13 @@ __git_heads ()
170
171 __git_tags ()
172 {
173 - local cmd i is_hash=y dir="$(__gitdir "$1")"
173 + local cmd i is_hash=y dir="$(__gitdir "${1-}")"
174 if [ -d "$dir" ]; then
175 git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
176 refs/tags
177 return
178 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
180 case "$is_hash,$i" in
181 y,*) is_hash=n ;;
182 n,*^{}) is_hash=y ;;
@@ -188,7 +188,7 @@ __git_tags ()
188
189 __git_refs ()
190 {
191 - local i is_hash=y dir="$(__gitdir "$1")"
191 + local i is_hash=y dir="$(__gitdir "${1-}")"
192 local cur="${COMP_WORDS[COMP_CWORD]}" format refs
193 if [ -d "$dir" ]; then
194 case "$cur" in