Skip to contentSkip to conversation

bash: Add space after unique command name is completed.

PATCH In review Shawn O. Pearce 2 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 4/8 bash: Add space after unique command name is completed.

Because we use the nospace option for our completion function for the main 'git' wrapper bash won't automatically add a space after a unique completion has been made by the user. This has been pointed out in the past by Linus Torvalds as an undesired behavior. I agree.

We have to use the nospace option to ensure path completion for a command such as git show works properly, but that breaks the common case of getting the space for a unique completion. So now we set IFS=$'\n' (linefeed) and add a trailing space to every possible completion option. This causes bash to insert the space when the completion is unique.

---
 contrib/completion/git-completion.bash |   26 ++++++++++++++++++++------
 1 files changed, 20 insertions(+), 6 deletions(-)
contrib/completion/git-completion.bash +20 -6
index 93f2af5..1cf576e 100755
@@ -61,6 +61,20 @@ __git_ps1 ()
 	fi
 }
 
+__gitcomp ()
+{
+	local all c s=$'\n' IFS=' '$'\t'$'\n'
+	for c in $1; do
+		case "$c" in
+		--*=*) all="$all$c$s" ;;
+		*)     all="$all$c $s" ;;
+		esac
+	done
+	IFS=$s
+	COMPREPLY=($(compgen -W "$all" -- "${COMP_WORDS[COMP_CWORD]}"))
+	return
+}
+
 __git_heads ()
 {
 	local cmd i is_hash=y dir="$(__gitdir "$1")"
@@ -787,12 +801,12 @@ _git ()
 	done
 
 	if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
-		COMPREPLY=($(compgen -W "
-			--git-dir= --version --exec-path
-			$(__git_commands)
-			$(__git_aliases)
-			" -- "${COMP_WORDS[COMP_CWORD]}"))
-		return;
+		case "${COMP_WORDS[COMP_CWORD]}" in
+		--*=*) COMPREPLY=() ;;
+		--*)   __gitcomp "--git-dir= --bare --version --exec-path" ;;
+		*)     __gitcomp "$(__git_commands) $(__git_aliases)" ;;
+		esac
+		return
 	fi
 
 	local expansion=$(__git_aliased_command "$command")
Signature · 1 line
1.5.0.rc3.22.g5057
#1 lore raw
Quoted 4 lines · Shawn O. Pearce
contrib/completion/git-completion.bash +4 -0
+__gitcomp ()
+{
+	local all c s=$'\n' IFS=' '$'\t'$'\n'
+	for c in $1; do

contrib/completion/git-completion.bash __git_ps1 () · line 77

Quoted 10 lines
contrib/completion/git-completion.bash +10 -0
+		case "$c" in
+		--*=*) all="$all$c$s" ;;
+		*)     all="$all$c $s" ;;
+		esac
+	done
+	IFS=$s
+	COMPREPLY=($(compgen -W "$all" -- "${COMP_WORDS[COMP_CWORD]}"))
+	return
+}
+

I do not understand what is going on here. Care to explain?

(1) "for c in $1": because $1 is not dquoted, it is split into word with IFS -- which IFS applies to this splitting? The local one you hand-define to the typical " \t\n" value?

(2) when this function returns, what IFS value does the caller would have? Is the "local" used to avoid screwing up the caller?

#2 lore raw
Quoted 19 lines · Junio C Hamano
Quoted 4 lines · Shawn O. Pearce
contrib/completion/git-completion.bash +4 -0
+__gitcomp ()
+{
+	local all c s=$'\n' IFS=' '$'\t'$'\n'
+	for c in $1; do

contrib/completion/git-completion.bash __git_ps1 () · line 77

Quoted 10 lines
contrib/completion/git-completion.bash +10 -0
+		case "$c" in
+		--*=*) all="$all$c$s" ;;
+		*)     all="$all$c $s" ;;
+		esac
+	done
+	IFS=$s
+	COMPREPLY=($(compgen -W "$all" -- "${COMP_WORDS[COMP_CWORD]}"))
+	return
+}
+

I do not understand what is going on here. Care to explain?

Quoted 3 lines

(1) "for c in $1": because $1 is not dquoted, it is split into word with IFS -- which IFS applies to this splitting? The local one you hand-define to the typical " \t\n" value?

Correct.

Quoted 4 lines

(2) when this function returns, what IFS value does the caller would have? Is the "local" used to avoid screwing up the caller?

Correct.

Basically at the time I'm forming 'all' I want $1 split based on normal splitting rules, as I'm using space, tag and LF within the script in some places for the first parameter to __gitcomp.

But when I run compgen I need IFS to be just LF ($s) so that $all is split only on LF and not on spaces, as spaces were added to $all after each unique option. This way the trailing space is part of the unique option, and bash will tack in the trailing space for me, despite having -o nospace enabled.

Signature · 1 line
Shawn.