Skip to content

Commit

Permalink
bash-completion: Support running when set -u is enabled
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Ted Pavlic authored and Junio C Hamano committed Jan 15, 2009
1 parent b32acd2 commit 25a31f8
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions contrib/completion/git-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ esac

__gitdir ()
{
if [ -z "$1" ]; then
if [ -z "${1-}" ]; then
if [ -n "$__git_dir" ]; then
echo "$__git_dir"
elif [ -d .git ]; then
Expand Down Expand Up @@ -111,7 +111,7 @@ __git_ps1 ()
fi
fi

if [ -n "$1" ]; then
if [ -n "${1-}" ]; then
printf "$1" "${b##refs/heads/}$r"
else
printf " (%s)" "${b##refs/heads/}$r"
Expand Down Expand Up @@ -143,22 +143,22 @@ __gitcomp ()
;;
*)
local IFS=$'\n'
COMPREPLY=($(compgen -P "$2" \
-W "$(__gitcomp_1 "$1" "$4")" \
COMPREPLY=($(compgen -P "${2-}" \
-W "$(__gitcomp_1 "${1-}" "${4-}")" \
-- "$cur"))
;;
esac
}

__git_heads ()
{
local cmd i is_hash=y dir="$(__gitdir "$1")"
local cmd i is_hash=y dir="$(__gitdir "${1-}")"
if [ -d "$dir" ]; then
git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
refs/heads
return
fi
for i in $(git ls-remote "$1" 2>/dev/null); do
for i in $(git ls-remote "${1-}" 2>/dev/null); do
case "$is_hash,$i" in
y,*) is_hash=n ;;
n,*^{}) is_hash=y ;;
Expand All @@ -170,13 +170,13 @@ __git_heads ()

__git_tags ()
{
local cmd i is_hash=y dir="$(__gitdir "$1")"
local cmd i is_hash=y dir="$(__gitdir "${1-}")"
if [ -d "$dir" ]; then
git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
refs/tags
return
fi
for i in $(git ls-remote "$1" 2>/dev/null); do
for i in $(git ls-remote "${1-}" 2>/dev/null); do
case "$is_hash,$i" in
y,*) is_hash=n ;;
n,*^{}) is_hash=y ;;
Expand All @@ -188,7 +188,7 @@ __git_tags ()

__git_refs ()
{
local i is_hash=y dir="$(__gitdir "$1")"
local i is_hash=y dir="$(__gitdir "${1-}")"
local cur="${COMP_WORDS[COMP_CWORD]}" format refs
if [ -d "$dir" ]; then
case "$cur" in
Expand Down

0 comments on commit 25a31f8

Please sign in to comment.