[Intel-gfx] [PATCH] qf: Introduce subcommand and aliases

Rodrigo Vivi rodrigo.vivi at intel.com
Tue Aug 22 18:17:55 UTC 2017


Let's start refreshing qf a bit by introducing
subcommand and aliases like dim.

The goal is to have an standardized qf and dim
where both have same style, documentation and
also that is in a format that we can easily
introduce to make check.

Actually all new code here is a simple copy
from dim directly with s/dim/qf.

This patch also introduce qf_usage,
qf_list_commands qf_list_aliases and already
 move qf_help from the case parse $1 to a
subcommand.

v2: Keep default behaviour as git command executed on
    quilt patches directory instead of qf_usage.

Cc: Daniel Vetter <daniel.vetter at ffwll.ch>
Cc: Jani Nikula <jani.nikula at intel.com>
Cc: Joonas Lahtinen <joonas.lahtinen at linux.intel.com>
Signed-off-by: Rodrigo Vivi <rodrigo.vivi at intel.com>
---
 Makefile |   5 +++
 qf       | 109 +++++++++++++++++++++++++++++++++++++++++++++------------------
 qf.rst   |  16 +++++++++-
 3 files changed, 99 insertions(+), 31 deletions(-)

diff --git a/Makefile b/Makefile
index adf26126e27c..7cb22d2b1e06 100644
--- a/Makefile
+++ b/Makefile
@@ -51,6 +51,11 @@ mancheck:
 			echo "$@: $$cmd not documented"; \
 		fi \
 	done
+	@for cmd in $$(./qf list-commands); do \
+		if ! grep -q "^$$cmd" qf.rst; then \
+			echo "$@: $$cmd not documented"; \
+		fi \
+	done
 	rst2man --strict --no-raw dim.rst >/dev/null
 	rst2man --strict --no-raw qf.rst >/dev/null
 
diff --git a/qf b/qf
index 1f056f90ef70..8e7d8c26e68f 100755
--- a/qf
+++ b/qf
@@ -169,28 +169,6 @@ function quilt_clean_check
 	fi
 }
 
-function qf_help
-{
-	manpage=$DIM_PREFIX/maintainer-tools/qf.rst
-	if [ ! -e "$manpage" ]; then
-		manpage=$(dirname $(readlink -f $0))/qf.rst
-		if [ ! -e "$manpage" ]; then
-			echo "Can't find the man page."
-			exit 1
-		fi
-	fi
-
-	if hash rst2man 2>/dev/null; then
-		renderer=rst2man
-		pager="man -l -"
-	else
-		renderer=cat
-		pager=${PAGER:-cat}
-	fi
-
-	$renderer < $manpage | $pager
-}
-
 case "$1" in
 	setup)
 		cd `git rev-parse --show-toplevel`
@@ -480,12 +458,83 @@ case "$1" in
 		shift
 		gitk "$@"
 		;;
-	help)
-		qf_help
-		;;
-	*)
-		cd_toplevel
-		cd patches
-		git "$@"
-		;;
 esac
+
+qf=$(basename $0)
+
+# first positional argument is the subcommand
+if [ -n "$HELP" ] || [ "$#" = "0" ]; then
+    subcommand="usage"
+else
+    subcommand="$1"
+    shift
+fi
+
+function qf_help
+{
+	manpage=$DIM_PREFIX/maintainer-tools/qf.rst
+	if [ ! -e "$manpage" ]; then
+		manpage=$(dirname $(readlink -f $0))/qf.rst
+		if [ ! -e "$manpage" ]; then
+			echo "Can't find the man page."
+			exit 1
+		fi
+	fi
+
+	if hash rst2man 2>/dev/null; then
+		renderer=rst2man
+		pager="man -l -"
+	else
+		renderer=cat
+		pager=${PAGER:-cat}
+	fi
+
+	$renderer < $manpage | $pager
+}
+
+function qf_list_commands
+{
+	declare -F | grep -o " qf_[a-zA-Z_]*" | sed 's/^ qf_//;s/_/-/g'
+}
+
+function qf_list_aliases
+{
+	# use posix mode to omit functions in set output
+	( set -o posix; set ) | grep "^qf_alias_[a-zA-Z0-9_]*=" |\
+		sed 's/^qf_alias_//;s/=/\t/;s/_/-/g'
+}
+
+function qf_usage
+{
+	echo "usage: $qf SUBCOMMAND [ARGUMENTS]"
+	echo
+	echo "The available subcommands are:"
+	if hash column 2>/dev/null; then
+		qf_list_commands | column -c 72 | sed 's/^/\t/'
+	else
+		qf_list_commands | sed 's/^/\t/'
+	fi
+	echo
+	echo "See '$qf help' for more information."
+}
+
+# qf subcommand aliases (with bash 4.3+)
+if ! declare -n subcmd=qf_alias_${subcommand//-/_} &> /dev/null || \
+		test -z "${subcmd:-}"; then
+	subcmd="$subcommand"
+fi
+
+# look up the function by the subcommand name
+subcmd_func=qf_${subcmd//-/_}
+if ! declare -f $subcmd_func >/dev/null; then
+	echo "Using qf as git command on quilt patches directory."
+	cd_toplevel
+	cd patches
+	git "$@"
+	exit $?
+fi
+
+# throw away to not confuse list-aliases
+unset subcmd
+
+$subcmd_func "$@"
diff --git a/qf.rst b/qf.rst
index 902b0d377f41..10447dded391 100644
--- a/qf.rst
+++ b/qf.rst
@@ -95,6 +95,16 @@ $ qf git bisect new|old
 COMMANDS
 ========
 
+list-aliases
+------------
+List all aliases for the subcommand names.
+
+See \$qf_alias_<alias> under ENVIRONMENT below on how to define aliases.
+
+list-commands
+-------------
+List all subcommand names, including aliases.
+
 setup [*branch-name*]
 ---------------------
 Sets up a git repository for this quilt worflow script by
@@ -233,10 +243,14 @@ help
 ----
 This help text here
 
+usage
+-----
+Short form usage help listing all subcommands.
+
 all other subcommands - IMPORTANT
 ---------------------------------
 Any other subcommands are executed directly in the quilt patches
-directory as git commans. When using quilt flow in scripts it is
+directory as git commands. When using quilt flow in scripts it is
 import to use the explicit forwarding to avoid clashes with
 furture extensions.
 
-- 
2.13.2



More information about the Intel-gfx mailing list