git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Duy Nguyen <pclouds@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: "Git Mailing List" <git@vger.kernel.org>,
	"SZEDER Gábor" <szeder.dev@gmail.com>
Subject: Re: [PATCH] completion: add option completion for most builtin commands
Date: Thu, 22 Mar 2018 18:35:54 +0100	[thread overview]
Message-ID: <20180322173553.GA20984@duynguyen.home> (raw)
In-Reply-To: <xmqqy3ikkpba.fsf@gitster-ct.c.googlers.com>

On Thu, Mar 22, 2018 at 10:11:53AM -0700, Junio C Hamano wrote:
> Duy Nguyen <pclouds@gmail.com> writes:
> 
> >> And that pattern repeats throughout the patch.  I wonder if we can
> >> express the same a lot more concisely by updating the caller that
> >> calls these command specific helpers?
> >
> > Yeah. I almost went to just generate and eval these functions. But we
> > still need to keep a list of "bultin with --git-completion-helper"
> > support somewhere, and people may want to complete arguments without
> > double dashes (e.g. read-tree should take a ref...) which can't be
> > helped by --git-completion-helper.
> 
> Hmph, I actually did not have 'eval' in mind.
> 
> Rather, I was wondering if it is cleaner to update __git_main where
> it computes $completion_func by mangling $command and then calls
> it---instead call __gitcomp_builtin directly when the $command
> appears in such a "list of builtins that knows --completion-helper
> and no custom completion".

Ah. Something like this? Seems to work fine and definitely not as ugly
as eval.

-- 8< --
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 6da95b8095..960e26e09d 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -3032,6 +3032,32 @@ _git_worktree ()
 	fi
 }
 
+__git_main_with_parseopt_helper='
+	blame cat-file check-attr check-ignore
+	check-mailmap checkout-index column count-objects fast-export
+	hash-object index-pack interpret-trailers merge-file mktree
+	pack-objects pack-refs prune prune-packed read-tree repack
+	send-pack show-ref stripspace symbolic-ref update-index
+	update-ref verify-commit verify-tag write-tree
+'
+__git_complete_command() {
+	local command="$1"
+	local completion_func="_git_${command//-/_}"
+	if declare -f $completion_func >/dev/null 2>/dev/null; then
+		$completion_func
+	elif echo $__git_main_with_parseopt_helper | git grep -w "$command" >/dev/null; then
+		case "$cur" in
+		--*)
+			__gitcomp_builtin "$command"
+			return 0
+			;;
+		esac
+		return 0
+	else
+		return 1
+	fi
+}
+
 __git_main ()
 {
 	local i c=1 command __git_dir __git_repo_path
@@ -3091,14 +3117,12 @@ __git_main ()
 		return
 	fi
 
-	local completion_func="_git_${command//-/_}"
-	declare -f $completion_func >/dev/null 2>/dev/null && $completion_func && return
+	__git_complete_command "$command" && return
 
 	local expansion=$(__git_aliased_command "$command")
 	if [ -n "$expansion" ]; then
 		words[1]=$expansion
-		completion_func="_git_${expansion//-/_}"
-		declare -f $completion_func >/dev/null 2>/dev/null && $completion_func
+		__git_complete_command "$expansion"
 	fi
 }
 
-- 8< --


  reply	other threads:[~2018-03-22 17:36 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 [this message]
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   ` [PATCH v2 5/8] completion: add --option completion for most builtin commands Nguyễn Thái Ngọc Duy
2018-03-25  0:59     ` 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=20180322173553.GA20984@duynguyen.home \
    --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).