Skip to contentSkip to conversation

completion: require bash completion package and use it

PATCH In review Jani Nikula 1 participant 9 messages Feb 18, 2016 – Feb 19, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/8 completion: require bash completion package and use it

The bash completion package makes life a whole lot easier than using the builtin bash completion features. It's quite likely anyone using completion in bash already has it installed.

---
 bash_completion | 62 ++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 35 insertions(+), 27 deletions(-)
bash_completion +35 -27
index e44e5fc844b4..6a3a88cc80f8 100644
@@ -11,7 +11,21 @@ dim ()
 
 _dim ()
 {
-	local cur cmds opts i
+	local args arg cur prev words cword split
+	local cmds
+
+	# require bash-completion with _init_completion
+	type -t _init_completion >/dev/null 2>&1 || return
+
+	_init_completion || return
+
+	COMPREPLY=()
+
+	# arg = subcommand
+	_get_first_arg
+
+	# args = number of arguments
+	_count_args
 
 	if [ -f ~/linux/drm-intel-rerere/nightly.conf ] ; then
 		local nightly_branches=`(source ~/linux/drm-intel-rerere/nightly.conf ; echo $nightly_branches) | \
@@ -35,27 +49,21 @@ _dim ()
 	cmds="$cmds create-branch remove-branch create-workdir for-each-workdirs fw"
 	cmds="$cmds tag-next checker"
 
-	opts="-d -f -i"
-
-	i=1
-
-	COMPREPLY=()   # Array variable storing the possible completions.
-	cur=${COMP_WORDS[COMP_CWORD]}
-
-	for comp in "${COMP_WORDS[@]}" ; do
-		for opt in $opts ; do
-			if [[ $opt = $comp ]] ; then
-				i=$((i+1))
-			fi
-		done
-	done
-
-	if [[ $COMP_CWORD == "$i" ]] ; then
-		COMPREPLY=( $( compgen -W "$cmds $opts" -- $cur ) )
+	if [ -z "${arg}" ]; then
+		# top level completion
+		case "${cur}" in
+			-*)
+				local opts="-d -f -i"
+				COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
+				;;
+			*)
+				COMPREPLY=( $(compgen -W "${cmds}" -- ${cur}) )
+				;;
+		esac
 		return 0
 	fi
 
-	case "${COMP_WORDS[i]}" in
+	case "${arg}" in
 		push-branch)
 			COMPREPLY=( $( compgen -W "-f $nightly_branches" -- $cur ) )
 			;;
@@ -69,7 +77,7 @@ _dim ()
 			COMPREPLY=( $( compgen -W "-s" -- $cur ) )
 			;;
 		magic-patch|mp)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -o nospace -W "-a" -- $cur ) )
 			fi
 			;;
@@ -80,34 +88,34 @@ _dim ()
 			# FIXME needs a git sha1
 			;;
 		pull-request)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -W "$nightly_branches" -- $cur ) )
-			elif [[ $COMP_CWORD == "$((i+2))" ]] ; then
+			elif [[ $args == 3 ]]; then
 				COMPREPLY=( $( compgen -W "$upstream_branches" -- $cur ) )
 			fi
 			;;
 		pull-request-next|pull-request-fixes|pull-request-next-fixes)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -W "$upstream_branches" -- $cur ) )
 			fi
 			;;
 		create-branch)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -o nospace -W "drm- topic/" -- $cur ) )
 			fi
 			;;
 		checkout|co)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -W "$nightly_branches" -- $cur ) )
 			fi
 			;;
 		remove-branch)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -W "$nightly_branches" -- $cur ) )
 			fi
 			;;
 		create-workdir)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -W "$nightly_branches all" -- $cur ) )
 			fi
 			;;
Signature · 6 lines
2.1.4

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#1 lore raw

PATCH 2/8 dim: add list-branches subcommand to list nightly branches

Helper for bash completion. Where to get the information depends on user's dim configuration.

---
 dim | 6 ++++++
 1 file changed, 6 insertions(+)
dim +6 -0
index c004bc75ca06..33ef8288a291 100755
@@ -972,6 +972,12 @@ function dim_pull_request_next_fixes
 	dim_pull_request drm-intel-next-fixes $upstream
 }
 
+# Note: used by bash completion
+function dim_list_branches
+{
+	echo $dim_branches | sed 's/ /\n/g'
+}
+
 dim_alias_ub=update-branches
 function dim_update_branches
 {
Signature · 6 lines
2.1.4

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#2 lore raw

PATCH 3/8 dim: add list-upstreams subcommand to list upstream branches

In reply to Jani Nikula

Helper for bash completion. The result depends on user's dim configuration.

---
 dim | 8 ++++++++
 1 file changed, 8 insertions(+)
dim +8 -0
index 33ef8288a291..6fb496ea4192 100755
@@ -973,6 +973,14 @@ function dim_pull_request_next_fixes
 }
 
 # Note: used by bash completion
+function dim_list_upstreams
+{
+	echo origin/master
+	echo $DIM_DRM_UPSTREAM_REMOTE/drm-next
+	echo $DIM_DRM_UPSTREAM_REMOTE/drm-fixes
+}
+
+# Note: used by bash completion
 function dim_list_branches
 {
 	echo $dim_branches | sed 's/ /\n/g'
Signature · 6 lines
2.1.4

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#3 lore raw

PATCH 4/8 completion: use the dim helpers to complete nightly and upstream branches

In reply to Jani Nikula

Use the user's configured directories and remotes via dim.

---
 bash_completion | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
bash_completion +2 -7
index 6a3a88cc80f8..f89764e3947d 100644
@@ -27,13 +27,8 @@ _dim ()
 	# args = number of arguments
 	_count_args
 
-	if [ -f ~/linux/drm-intel-rerere/nightly.conf ] ; then
-		local nightly_branches=`(source ~/linux/drm-intel-rerere/nightly.conf ; echo $nightly_branches) | \
-			xargs -n 1 echo | grep '^origin' | sed -e 's/^origin\///'`
-	else
-		local nightly_branches=""
-	fi
-	local upstream_branches="origin/master airlied/drm-next airlied/drm-fixes"
+	local nightly_branches="$(dim list-branches)"
+	local upstream_branches="$(dim list-upstreams)"
 
 	cmds="setup nightly-forget update-branches"
 	cmds="$cmds rebuild-nightly cat-to-fixup"
Signature · 6 lines
2.1.4

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#4 lore raw

PATCH 5/8 dim: add list-commands subcommand to list all subcommands

In reply to Jani Nikula

Helper for completion.

---
 dim | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
dim +7 -1
index 6fb496ea4192..2f6e6151a4b2 100755
@@ -1115,6 +1115,12 @@ function assert_branch
 	fi
 }
 
+# Note: used by bash completion
+function dim_list_commands
+{
+	declare -F | grep -o " dim_[a-zA-Z_]*" | sed 's/^ dim_//;s/_/-/g'
+}
+
 function dim_alias
 {
 	# use posix mode to omit functions in set output
@@ -1178,7 +1184,7 @@ function dim_usage
 	echo "usage: $0 [OPTIONS] SUBCOMMAND [ARGUMENTS]"
 	echo
 	echo "The available subcommands are:"
-	declare -F | grep -o " dim_[a-zA-Z_]*" | sed 's/^ dim_/\t/'
+	dim_list_commands | sed 's/^/\t/'
 	echo
 	echo "See '$0 help' for more information."
 }
Signature · 6 lines
2.1.4

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#5 lore raw

PATCH 6/8 dim: rename alias subcommand to list-aliases

In reply to Jani Nikula

Also drop leading tab and fix underscores in output. Helper for bash completion.

---
 dim | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
dim +3 -2
index 2f6e6151a4b2..1addd6f6a0e9 100755
@@ -1121,11 +1121,12 @@ function dim_list_commands
 	declare -F | grep -o " dim_[a-zA-Z_]*" | sed 's/^ dim_//;s/_/-/g'
 }
 
-function dim_alias
+# Note: used by bash completion
+function dim_list_aliases
 {
 	# use posix mode to omit functions in set output
 	( set -o posix; set ) | grep "^dim_alias_[a-zA-Z0-9_]*=" |\
-		sed 's/^dim_alias_/\t/;s/=/\t/'
+		sed 's/^dim_alias_//;s/=/\t/;s/_/-/g'
 }
 
 function dim_cat_to_fixup
Signature · 6 lines
2.1.4

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#6 lore raw

PATCH 7/8 completion: use the dim helpers to complete subcommands and aliases

In reply to Jani Nikula

Autodiscover everything, including user's configured aliases.

---
 bash_completion | 16 +---------------
 1 file changed, 1 insertion(+), 15 deletions(-)
bash_completion +1 -15
index f89764e3947d..4a9d981709a0 100644
@@ -12,7 +12,6 @@ dim ()
 _dim ()
 {
 	local args arg cur prev words cword split
-	local cmds
 
 	# require bash-completion with _init_completion
 	type -t _init_completion >/dev/null 2>&1 || return
@@ -30,20 +29,6 @@ _dim ()
 	local nightly_branches="$(dim list-branches)"
 	local upstream_branches="$(dim list-upstreams)"
 
-	cmds="setup nightly-forget update-branches"
-	cmds="$cmds rebuild-nightly cat-to-fixup"
-	cmds="$cmds push-queued pq push-fixes pf push-next-fixes pnf push-branch"
-	cmds="$cmds checkout co conq cof conf"
-	cmds="$cmds apply-branch ab sob apply-queued aq apply-fixes af apply-next-fixes anf"
-	cmds="$cmds magic-patch mp cd"
-	cmds="$cmds magic-rebase-resolve mrr"
-	cmds="$cmds apply-igt ai"
-	cmds="$cmds apply-resolved ar tc fixes check-patch cp cherry-pick"
-	cmds="$cmds pull-request pull-request-fixes pull-request-next pull-request-next-fixes"
-	cmds="$cmds update-next"
-	cmds="$cmds create-branch remove-branch create-workdir for-each-workdirs fw"
-	cmds="$cmds tag-next checker"
-
 	if [ -z "${arg}" ]; then
 		# top level completion
 		case "${cur}" in
@@ -52,6 +37,7 @@ _dim ()
 				COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
 				;;
 			*)
+				local cmds="$(dim list-commands) $(dim list-aliases | sed 's/\t.*//')"
 				COMPREPLY=( $(compgen -W "${cmds}" -- ${cur}) )
 				;;
 		esac
Signature · 6 lines
2.1.4

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#7 lore raw

PATCH 8/8 completion: complete aliases like the actual command

In reply to Jani Nikula

Map aliases to the actual commands. No need to know all the aliases.

---
 bash_completion | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
bash_completion +12 -6
index 4a9d981709a0..9f659b4ebcce 100644
@@ -44,20 +44,26 @@ _dim ()
 		return 0
 	fi
 
+	# complete aliases like the actual command
+	local aliasref=$(dim list-aliases | sed -n "s/^${arg}\t\(.*\)/\1/p")
+	if [[ -n "$aliasref" ]]; then
+		arg="$aliasref"
+	fi
+
 	case "${arg}" in
 		push-branch)
 			COMPREPLY=( $( compgen -W "-f $nightly_branches" -- $cur ) )
 			;;
-		push-queued|pq|push-fixes|pf|push-next-fixes|pnf)
+		push-queued|push-fixes|push-next-fixes)
 			COMPREPLY=( $( compgen -W "-f" -- $cur ) )
 			;;
-		apply-branch|ab|sob)
+		apply-branch)
 			COMPREPLY=( $( compgen -W "-s $nightly_branches" -- $cur ) )
 			;;
-		apply-queued|aq|apply-fixes|af|apply-next-fixes|anf)
+		apply-queued|apply-fixes|apply-next-fixes)
 			COMPREPLY=( $( compgen -W "-s" -- $cur ) )
 			;;
-		magic-patch|mp)
+		magic-patch)
 			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -o nospace -W "-a" -- $cur ) )
 			fi
@@ -65,7 +71,7 @@ _dim ()
 		tc|fixes)
 			# FIXME needs a git sha1
 			;;
-		check-patch|cp)
+		checkpatch)
 			# FIXME needs a git sha1
 			;;
 		pull-request)
@@ -85,7 +91,7 @@ _dim ()
 				COMPREPLY=( $( compgen -o nospace -W "drm- topic/" -- $cur ) )
 			fi
 			;;
-		checkout|co)
+		checkout)
 			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -W "$nightly_branches" -- $cur ) )
 			fi
Signature · 6 lines
2.1.4

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#8 lore raw
In reply to Jani Nikula
Quoted 3 lines · Jani Nikula

The bash completion package makes life a whole lot easier than using the builtin bash completion features. It's quite likely anyone using completion in bash already has it installed.

I boldly went ahead and pushed the lot. Please scream if
https://xkcd.com/1172/.

BR,
Jani.

Quoted 124 lines
---
 bash_completion | 62 ++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 35 insertions(+), 27 deletions(-)
bash_completion +35 -27
index e44e5fc844b4..6a3a88cc80f8 100644
@@ -11,7 +11,21 @@ dim ()
 
 _dim ()
 {
-	local cur cmds opts i
+	local args arg cur prev words cword split
+	local cmds
+
+	# require bash-completion with _init_completion
+	type -t _init_completion >/dev/null 2>&1 || return
+
+	_init_completion || return
+
+	COMPREPLY=()
+
+	# arg = subcommand
+	_get_first_arg
+
+	# args = number of arguments
+	_count_args
 
 	if [ -f ~/linux/drm-intel-rerere/nightly.conf ] ; then
 		local nightly_branches=`(source ~/linux/drm-intel-rerere/nightly.conf ; echo $nightly_branches) | \
@@ -35,27 +49,21 @@ _dim ()
 	cmds="$cmds create-branch remove-branch create-workdir for-each-workdirs fw"
 	cmds="$cmds tag-next checker"
 
-	opts="-d -f -i"
-
-	i=1
-
-	COMPREPLY=()   # Array variable storing the possible completions.
-	cur=${COMP_WORDS[COMP_CWORD]}
-
-	for comp in "${COMP_WORDS[@]}" ; do
-		for opt in $opts ; do
-			if [[ $opt = $comp ]] ; then
-				i=$((i+1))
-			fi
-		done
-	done
-
-	if [[ $COMP_CWORD == "$i" ]] ; then
-		COMPREPLY=( $( compgen -W "$cmds $opts" -- $cur ) )
+	if [ -z "${arg}" ]; then
+		# top level completion
+		case "${cur}" in
+			-*)
+				local opts="-d -f -i"
+				COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
+				;;
+			*)
+				COMPREPLY=( $(compgen -W "${cmds}" -- ${cur}) )
+				;;
+		esac
 		return 0
 	fi
 
-	case "${COMP_WORDS[i]}" in
+	case "${arg}" in
 		push-branch)
 			COMPREPLY=( $( compgen -W "-f $nightly_branches" -- $cur ) )
 			;;
@@ -69,7 +77,7 @@ _dim ()
 			COMPREPLY=( $( compgen -W "-s" -- $cur ) )
 			;;
 		magic-patch|mp)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -o nospace -W "-a" -- $cur ) )
 			fi
 			;;
@@ -80,34 +88,34 @@ _dim ()
 			# FIXME needs a git sha1
 			;;
 		pull-request)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -W "$nightly_branches" -- $cur ) )
-			elif [[ $COMP_CWORD == "$((i+2))" ]] ; then
+			elif [[ $args == 3 ]]; then
 				COMPREPLY=( $( compgen -W "$upstream_branches" -- $cur ) )
 			fi
 			;;
 		pull-request-next|pull-request-fixes|pull-request-next-fixes)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -W "$upstream_branches" -- $cur ) )
 			fi
 			;;
 		create-branch)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -o nospace -W "drm- topic/" -- $cur ) )
 			fi
 			;;
 		checkout|co)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -W "$nightly_branches" -- $cur ) )
 			fi
 			;;
 		remove-branch)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -W "$nightly_branches" -- $cur ) )
 			fi
 			;;
 		create-workdir)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -W "$nightly_branches all" -- $cur ) )
 			fi
 			;;
Signature · 5 lines
Jani Nikula, Intel Open Source Technology Center
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx