git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/3] completion: put matching ctags symbol names directly into COMPREPLY

From
SZEDER Gábor <szeder.dev@gmail.com>
Date
Mar 23, 2017, 15:38 UTC
Message-ID
<20170323153839.24283-2-szeder.dev@gmail.com>
In-Reply-To
<20170323153839.24283-1-szeder.dev@gmail.com>

The one-liner awk script in __git_match_ctag() listing ctags symbol names for 'git grep <TAB>' is already smart enough to list only symbol names matching the current word to be completed.

Extend this helper function to accept prefix and suffix parameters to be prepended and appended, respectively, to each listed symbol name in the awk script, so its output won't require any additional processing or filtering in the completion script before being handed over to Bash. Use the faster __gitcomp_direct() helper instead of __gitcomp_nl() to fill the fully processed matching symbol names into Bash's COMPREPLY array.

Right after 'git grep <TAB>' in current git.git with 14k+ symbol names in the tag file, best of five:

  Before:
    $ time __gitcomp_nl "$(__git_match_ctag "" tags)"
    real    0m0.178s
    user    0m0.176s
    sys     0m0.000s
  After:
    $ time __gitcomp_direct "$(__git_match_ctag "" tags "" " ")"
    real    0m0.058s
    user    0m0.048s
    sys     0m0.008s
Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
Notes:
    It's still just a simple linear search through the tags file, so there
    are no miracles: it's still hopelessly, unusably slow e.g. in the
    Linux repository.
 contrib/completion/git-completion.bash | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 69eabc2bc..5cacb0e2e 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1518,8 +1518,15 @@ _git_gitk ()
 	_gitk
 }
 
-__git_match_ctag() {
-	awk "/^${1//\//\\/}/ { print \$1 }" "$2"
+# Lists matching symbol names from a tag (as in ctags) file.
+# 1: List symbol names matching this word.
+# 2: The tag file to list symbol names from.
+# 3: A prefix to be added to each listed symbol name (optional).
+# 4: A suffix to be appended to each listed symbol name (optional).
+__git_match_ctag () {
+	awk -v pfx="${3-}" -v sfx="${4-}" "
+		/^${1//\//\\/}/ { print pfx \$1 sfx }
+		" "$2"
 }
 
 _git_grep ()
@@ -1548,7 +1555,7 @@ _git_grep ()
 	case "$cword,$prev" in
 	2,*|*,-*)
 		if test -r tags; then
-			__gitcomp_nl "$(__git_match_ctag "$cur" tags)"
+			__gitcomp_direct "$(__git_match_ctag "$cur" tags "" " ")"
 			return
 		fi
 		;;
-- 
2.12.1.498.gded7ead47
Previous: SZEDER GáborNext: Jeff King
Message 2 of 9 in “completion: updates to ctags symbol names”
  1. 0/3 completion: updates to ctags symbol namesSZEDER Gábor, Mar 23, 2017
  2. 1/3 completion: put matching ctags symbol names directly into COMPREPLYSZEDER Gábor, Mar 23, 2017
  3. Jeff KingMar 24, 2017
  4. 2/3 completion: extract completing ctags symbol names into helper functionSZEDER Gábor, Mar 23, 2017
  5. Jeff KingMar 24, 2017
  6. 3/3 completion: offer ctags symbol names for 'git log -S', '-G' and '-L:'SZEDER Gábor, Mar 23, 2017
  7. Jeff KingMar 24, 2017
  8. SZEDER GáborMar 30, 2017
  9. Jeff KingApr 1, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.