git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Update bash completions to prevent unbound variable errors.

From
Ted Pavlic <ted@tedpavlic.com>
Date
Jan 12, 2009, 19:58 UTC
Message-ID
<496BA0E4.2040607@tedpavlic.com>

In bash, "set -u" gives an error when a variable is unbound. In this case, the bash completion script included in the git/contrib directory produces several errors.

The attached patch replaces things like
         if [ -z "$1" ]
with
         if [ -z "${1-}" ]

so that the unbound variable returns an empty value. Hence, the completion script will now work even "set -u" set.

Signed-off-by: Ted Pavlic <ted@tedpavlic.com>
---
  contrib/completion/git-completion.bash |   68 
++++++++++++++++----------------
  1 files changed, 34 insertions(+), 34 deletions(-)
diff --git a/contrib/completion/git-completion.bash 
b/contrib/completion/git-completion.bash
index 7b074d7..50e345f 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -52,25 +52,25 @@ esac

  __gitdir ()
  {
-	if [ -z "$1" ]; then
-		if [ -n "$__git_dir" ]; then
+	if [ -z "${1-}" ]; then
+		if [ -n "${__git_dir-}" ]; then
  			echo "$__git_dir"
  		elif [ -d .git ]; then
  			echo .git
  		else
  			git rev-parse --git-dir 2>/dev/null
  		fi
-	elif [ -d "$1/.git" ]; then
-		echo "$1/.git"
+	elif [ -d "${1-}/.git" ]; then
+		echo "${1-}/.git"
  	else
-		echo "$1"
+		echo "${1-}"
  	fi
  }

  __git_ps1 ()
  {
  	local g="$(git rev-parse --git-dir 2>/dev/null)"
-	if [ -n "$g" ]; then
+	if [ -n "${g-}" ]; then
  		local r
  		local b
  		if [ -d "$g/rebase-apply" ]
@@ -111,8 +111,8 @@ __git_ps1 ()
  			fi
  		fi

-		if [ -n "$1" ]; then
-			printf "$1" "${b##refs/heads/}$r"
+		if [ -n "${1-}" ]; then
+			printf "${1-}" "${b##refs/heads/}$r"
  		else
  			printf " (%s)" "${b##refs/heads/}$r"
  		fi
@@ -122,11 +122,11 @@ __git_ps1 ()
  __gitcomp_1 ()
  {
  	local c IFS=' '$'\t'$'\n'
-	for c in $1; do
-		case "$c$2" in
-		--*=*) printf %s$'\n' "$c$2" ;;
-		*.)    printf %s$'\n' "$c$2" ;;
-		*)     printf %s$'\n' "$c$2 " ;;
+	for c in ${1-}; do
+		case "$c${2-}" in
+		--*=*) printf %s$'\n' "$c${2-}" ;;
+		*.)    printf %s$'\n' "$c${2-}" ;;
+		*)     printf %s$'\n' "$c${2-} " ;;
  		esac
  	done
  }
@@ -135,7 +135,7 @@ __gitcomp ()
  {
  	local cur="${COMP_WORDS[COMP_CWORD]}"
  	if [ $# -gt 2 ]; then
-		cur="$3"
+		cur="${3-}"
  	fi
  	case "$cur" in
  	--*=)
@@ -143,8 +143,8 @@ __gitcomp ()
  		;;
  	*)
  		local IFS=$'\n'
-		COMPREPLY=($(compgen -P "$2" \
-			-W "$(__gitcomp_1 "$1" "$4")" \
+		COMPREPLY=($(compgen -P "${2-}" \
+			-W "$(__gitcomp_1 "${1-}" "${4-}")" \
  			-- "$cur"))
  		;;
  	esac
@@ -152,13 +152,13 @@ __gitcomp ()

  __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 ;;
@@ -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 ;;
@@ -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
@@ -221,7 +221,7 @@ __git_refs ()
  __git_refs2 ()
  {
  	local i
-	for i in $(__git_refs "$1"); do
+	for i in $(__git_refs "${1-}"); do
  		echo "$i:$i"
  	done
  }
@@ -229,11 +229,11 @@ __git_refs2 ()
  __git_refs_remotes ()
  {
  	local cmd i is_hash=y
-	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
  		n,refs/heads/*)
  			is_hash=y
-			echo "$i:refs/remotes/$1/${i#refs/heads/}"
+			echo "$i:refs/remotes/${1-}/${i#refs/heads/}"
  			;;
  		y,*) is_hash=n ;;
  		n,*^{}) is_hash=y ;;
@@ -264,7 +264,7 @@ __git_remotes ()

  __git_merge_strategies ()
  {
-	if [ -n "$__git_merge_strategylist" ]; then
+	if [ -n "${__git_merge_strategylist-}" ]; then
  		echo "$__git_merge_strategylist"
  		return
  	fi
@@ -350,7 +350,7 @@ __git_complete_revlist ()

  __git_all_commands ()
  {
-	if [ -n "$__git_all_commandlist" ]; then
+	if [ -n "${__git_all_commandlist-}" ]; then
  		echo "$__git_all_commandlist"
  		return
  	fi
@@ -368,7 +368,7 @@ __git_all_commandlist="$(__git_all_commands 
2>/dev/null)"

  __git_porcelain_commands ()
  {
-	if [ -n "$__git_porcelain_commandlist" ]; then
+	if [ -n "${__git_porcelain_commandlist-}" ]; then
  		echo "$__git_porcelain_commandlist"
  		return
  	fi
@@ -473,7 +473,7 @@ __git_aliases ()
  __git_aliased_command ()
  {
  	local word cmdline=$(git --git-dir="$(__gitdir)" \
-		config --get "alias.$1")
+		config --get "alias.${1-}")
  	for word in $cmdline; do
  		if [ "${word##-*}" ]; then
  			echo $word
@@ -488,7 +488,7 @@ __git_find_subcommand ()

  	while [ $c -lt $COMP_CWORD ]; do
  		word="${COMP_WORDS[c]}"
-		for subcommand in $1; do
+		for subcommand in ${1-}; do
  			if [ "$subcommand" = "$word" ]; then
  				echo "$subcommand"
  				return
@@ -599,7 +599,7 @@ _git_bisect ()

  	local subcommands="start bad good skip reset visualize replay log run"
  	local subcommand="$(__git_find_subcommand "$subcommands")"
-	if [ -z "$subcommand" ]; then
+	if [ -z "${subcommand-}" ]; then
  		__gitcomp "$subcommands"
  		return
  	fi
@@ -1371,7 +1371,7 @@ _git_remote ()
  {
  	local subcommands="add rm show prune update"
  	local subcommand="$(__git_find_subcommand "$subcommands")"
-	if [ -z "$subcommand" ]; then
+	if [ -z "${subcommand-}" ]; then
  		__gitcomp "$subcommands"
  		return
  	fi
@@ -1500,7 +1500,7 @@ _git_stash ()
  {
  	local subcommands='save list show apply clear drop pop create branch'
  	local subcommand="$(__git_find_subcommand "$subcommands")"
-	if [ -z "$subcommand" ]; then
+	if [ -z "${subcommand-}" ]; then
  		__gitcomp "$subcommands"
  	else
  		local cur="${COMP_WORDS[COMP_CWORD]}"
@@ -1552,7 +1552,7 @@ _git_svn ()
  		proplist show-ignore show-externals
  		"
  	local subcommand="$(__git_find_subcommand "$subcommands")"
-	if [ -z "$subcommand" ]; then
+	if [ -z "${subcommand-}" ]; then
  		__gitcomp "$subcommands"
  	else
  		local remote_opts="--username= --config-dir= --no-auth-cache"
@@ -1672,7 +1672,7 @@ _git ()
  		c=$((++c))
  	done

-	if [ -z "$command" ]; then
+	if [ -z "${command-}" ]; then
  		case "${COMP_WORDS[COMP_CWORD]}" in
  		--*=*) COMPREPLY=() ;;
  		--*)   __gitcomp "
-- 
1.6.1.87.g15624
Next: Boyd Stephen Smith Jr.
Message 1 of 14 in “Update bash completions to prevent unbound variable errors.”
  1. Update bash completions to prevent unbound variable errors.Ted Pavlic, Jan 12, 2009
  2. Boyd Stephen Smith Jr.Jan 12, 2009
  3. Adeodato SimóJan 12, 2009
  4. Boyd Stephen Smith Jr.Jan 12, 2009
  5. Shawn O. PearceJan 12, 2009
  6. Boyd Stephen Smith Jr.Jan 12, 2009
  7. Ted PavlicJan 12, 2009
  8. Ted PavlicJan 12, 2009
  9. Shawn O. PearceJan 12, 2009
  10. Ted PavlicJan 12, 2009
  11. Adeodato SimóJan 12, 2009
  12. Ted PavlicJan 12, 2009
  13. Adeodato SimóJan 12, 2009
  14. Adeodato SimóJan 12, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.