git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: pclouds@gmail.com
Cc: git@vger.kernel.org, szeder.dev@gmail.com,
	Junio C Hamano <gitster@pobox.com>
Subject: [PATCH v2 5/8] completion: add --option completion for most builtin commands
Date: Sat, 24 Mar 2018 21:35:22 +0100	[thread overview]
Message-ID: <20180324203525.24159-6-pclouds@gmail.com> (raw)
In-Reply-To: <20180324203525.24159-1-pclouds@gmail.com>

Many builtin commands use parseopt which supports expose the option
list via --git-completion-helper but do not have explicit support in
git-completion.bash. This patch detects those commands and uses
__gitcomp_builtin for option completion.

This does not pollute the command name completion though. "git <tab>"
will show you the same set as before. This only kicks in when you type
the correct command name.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 contrib/completion/git-completion.bash | 28 ++++++++++++++++++++++++++
 t/t9902-completion.sh                  |  6 ++++++
 2 files changed, 34 insertions(+)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index b3a9ecfad0..a90b0e8db4 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -3034,12 +3034,40 @@ _git_worktree ()
 	fi
 }
 
+__git_complete_common () {
+	local command="$1"
+
+	case "$cur" in
+	--*)
+		__gitcomp_builtin "$command"
+		;;
+	esac
+}
+
+__git_cmds_with_parseopt_helper=
+__git_support_parseopt_helper () {
+	test -n "$__git_cmds_with_parseopt_helper" ||
+		__git_cmds_with_parseopt_helper="$(__git --list-parseopt-builtins)"
+
+	case " $__git_cmds_with_parseopt_helper " in
+	*" $1 "*)
+		return 0
+		;;
+	*)
+		return 1
+		;;
+	esac
+}
+
 __git_complete_command () {
 	local command="$1"
 	local completion_func="_git_${command//-/_}"
 	if declare -f $completion_func >/dev/null 2>/dev/null; then
 		$completion_func
 		return 0
+	elif __git_support_parseopt_helper "$command"; then
+		__git_complete_common "$command"
+		return 0
 	else
 		return 1
 	fi
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index e6485feb0a..d0a1e4c988 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -1454,6 +1454,12 @@ test_expect_success 'completion used <cmd> completion for alias: !f() { : git <c
 	EOF
 '
 
+test_expect_success 'completion without explicit _git_xxx function' '
+	test_completion "git version --" <<-\EOF
+	--build-options Z
+	EOF
+'
+
 test_expect_failure 'complete with tilde expansion' '
 	git init tmp && cd tmp &&
 	test_when_finished "cd .. && rm -rf tmp" &&
-- 
2.17.0.rc0.348.gd5a49e0b6f


  parent reply	other threads:[~2018-03-24 20:38 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-21 19:30 [PATCH] completion: add option completion for most builtin commands Nguyễn Thái Ngọc Duy
2018-03-21 20:59 ` Junio C Hamano
2018-03-22  5:12   ` Duy Nguyen
2018-03-22 17:11     ` Junio C Hamano
2018-03-22 17:35       ` Duy Nguyen
2018-03-22 17:56         ` Junio C Hamano
2018-03-22 18:01           ` Duy Nguyen
2018-03-24 13:38           ` Duy Nguyen
2018-03-24 20:35 ` [PATCH v2 0/8] " Nguyễn Thái Ngọc Duy
2018-03-24 20:35   ` [PATCH v2 1/8] git.c: move cmd_struct declaration up Nguyễn Thái Ngọc Duy
2018-03-24 20:35   ` [PATCH v2 2/8] git.c: add hidden option --list-parseopt-builtins Nguyễn Thái Ngọc Duy
2018-03-24 20:35   ` [PATCH v2 3/8] completion: mention the oldest version we need to support Nguyễn Thái Ngọc Duy
2018-03-24 20:35   ` [PATCH v2 4/8] completion: factor out _git_xxx calling code Nguyễn Thái Ngọc Duy
2018-03-24 20:35   ` Nguyễn Thái Ngọc Duy [this message]
2018-03-25  0:59     ` [PATCH v2 5/8] completion: add --option completion for most builtin commands Eric Sunshine
2018-03-24 20:35   ` [PATCH v2 6/8] completion: delete option-only completion commands Nguyễn Thái Ngọc Duy
2018-03-24 20:35   ` [PATCH v2 7/8] completion: use __gitcomp_builtin in _git_ls_tree Nguyễn Thái Ngọc Duy
2018-03-24 20:35   ` [PATCH v2 8/8] completion: use __gitcomp_builtin in _git_cherry Nguyễn Thái Ngọc Duy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180324203525.24159-6-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=szeder.dev@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).