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

[PATCH v7 38/49] completion: bash: shuffle __gitcomp functions

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Apr 1, 2023, 08:46 UTC
Message-ID
<20230401084626.304356-39-felipe.contreras@gmail.com>
In-Reply-To
<20230401084626.304356-1-felipe.contreras@gmail.com>
They are the ones that actually do the completion, put them at the top.
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/completion/git-completion.bash | 270 +++++++++++++------------
 1 file changed, 139 insertions(+), 131 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index bc6f83adce..3f48b61cbe 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -65,6 +65,145 @@
 #     case insensitively, even on systems with case sensitive file systems
 #     (e.g., completing tag name "FOO" on "git checkout f<TAB>").
 
+# The following functions are meant to modify COMPREPLY, which should not be
+# modified directly.  The purpose is to localize the modifications so it's
+# easier to emulate it in Zsh. Every time a new __gitcomp* function is added,
+# the corresponding function should be added to Zsh.
+
+__gitcompadd ()
+{
+	local x i=${#COMPREPLY[@]}
+	for x in $1; do
+		if [[ "$x" == "$3"* ]]; then
+			COMPREPLY[i++]="$2$x$4"
+		fi
+	done
+}
+
+# Creates completion replies.
+# It accepts 1 to 4 arguments:
+# 1: List of possible completion words.
+# 2: A prefix to be added to each possible completion word (optional).
+# 3: Generate possible completion matches for this word (optional).
+# 4: A suffix to be appended to each possible completion word (optional).
+__gitcomp ()
+{
+	local IFS=$' \t\n'
+	__gitcompadd "$1" "${2-}" "${3-$cur}" "${4- }"
+}
+
+# Generates completion reply from newline-separated possible completion words
+# by appending a space to all of them. The result is appended to COMPREPLY.
+# It accepts 1 to 4 arguments:
+# 1: List of possible completion words, separated by a single newline.
+# 2: A prefix to be added to each possible completion word (optional).
+# 3: Generate possible completion matches for this word (optional).
+# 4: A suffix to be appended to each possible completion word instead of
+#    the default space (optional).  If specified but empty, nothing is
+#    appended.
+__gitcomp_nl ()
+{
+	local IFS=$'\n'
+	__gitcompadd "$1" "${2-}" "${3-$cur}" "${4- }"
+}
+
+# Appends prefiltered words to COMPREPLY without any additional processing.
+# Callers must take care of providing only words that match the current word
+# to be completed and adding any prefix and/or suffix (trailing space!), if
+# necessary.
+# 1: List of newline-separated matching completion words, complete with
+#    prefix and suffix.
+__gitcomp_direct ()
+{
+	local IFS=$'\n'
+
+	COMPREPLY+=($1)
+}
+
+# Generates completion reply with compgen from newline-separated possible
+# completion filenames.
+# It accepts 1 to 3 arguments:
+# 1: List of possible completion filenames, separated by a single newline.
+# 2: A directory prefix to be added to each possible completion filename
+#    (optional).
+# 3: Generate possible completion matches for this word (optional).
+__gitcomp_file ()
+{
+	local IFS=$'\n'
+
+	# XXX does not work when the directory prefix contains a tilde,
+	# since tilde expansion is not applied.
+	# This means that COMPREPLY will be empty and Bash default
+	# completion will be used.
+	__gitcompadd "$1" "${2-}" "${3-$cur}" ""
+
+	# use a hack to enable file mode in bash < 4
+	compopt -o filenames +o nospace 2>/dev/null ||
+	compgen -f /non-existing-dir/ >/dev/null ||
+	true
+}
+
+# Fills the COMPREPLY array with prefiltered paths without any additional
+# processing.
+# Callers must take care of providing only paths that match the current path
+# to be completed and adding any prefix path components, if necessary.
+# 1: List of newline-separated matching paths, complete with all prefix
+#    path components.
+__gitcomp_file_direct ()
+{
+	local IFS=$'\n'
+
+	COMPREPLY+=($1)
+
+	# use a hack to enable file mode in bash < 4
+	compopt -o filenames +o nospace 2>/dev/null ||
+	compgen -f /non-existing-dir/ >/dev/null ||
+	true
+}
+
+# Creates completion replies, reorganizing options and adding suffixes as needed.
+# It accepts 1 to 4 arguments:
+# 1: List of possible completion words.
+# 2: A prefix to be added to each possible completion word (optional).
+# 3: Generate possible completion matches for this word (optional).
+# 4: A suffix to be appended to each possible completion word (optional).
+__gitcomp_opts ()
+{
+	local cur_="${3-$cur}"
+
+	if [[ "$cur_" == *= ]]; then
+		return
+	fi
+
+	local c i=0 IFS=$' \t\n' sfx
+	for c in $1; do
+		if [[ $c == "--" ]]; then
+			if [[ "$cur_" == --no-* ]]; then
+				continue
+			fi
+
+			if [[ --no == "$cur_"* ]]; then
+				COMPREPLY[i++]="--no-... "
+			fi
+			break
+		fi
+		if [[ $c == "$cur_"* ]]; then
+			if [[ -z "${4+set}" ]]; then
+				case $c in
+				*=|*.) sfx="" ;;
+				*) sfx=" " ;;
+				esac
+			else
+				sfx="$4"
+			fi
+			COMPREPLY[i++]="${2-}$c$sfx"
+		fi
+	done
+}
+
+# __gitcomp functions end here
+# ==============================================================================
+
 # Discovers the path to the git repository taking any '--git-dir=<path>' and
 # '-C <path>' options into account and stores it in the $__git_repo_path
 # variable.
@@ -307,81 +446,6 @@ _get_comp_words_by_ref ()
 }
 fi
 
-# Appends prefiltered words to COMPREPLY without any additional processing.
-# Callers must take care of providing only words that match the current word
-# to be completed and adding any prefix and/or suffix (trailing space!), if
-# necessary.
-# 1: List of newline-separated matching completion words, complete with
-#    prefix and suffix.
-__gitcomp_direct ()
-{
-	local IFS=$'\n'
-
-	COMPREPLY+=($1)
-}
-
-__gitcompadd ()
-{
-	local x i=${#COMPREPLY[@]}
-	for x in $1; do
-		if [[ "$x" == "$3"* ]]; then
-			COMPREPLY[i++]="$2$x$4"
-		fi
-	done
-}
-
-# Creates completion replies.
-# It accepts 1 to 4 arguments:
-# 1: List of possible completion words.
-# 2: A prefix to be added to each possible completion word (optional).
-# 3: Generate possible completion matches for this word (optional).
-# 4: A suffix to be appended to each possible completion word (optional).
-__gitcomp ()
-{
-	local IFS=$' \t\n'
-	__gitcompadd "$1" "${2-}" "${3-$cur}" "${4- }"
-}
-
-# Creates completion replies, reorganizing options and adding suffixes as needed.
-# It accepts 1 to 4 arguments:
-# 1: List of possible completion words.
-# 2: A prefix to be added to each possible completion word (optional).
-# 3: Generate possible completion matches for this word (optional).
-# 4: A suffix to be appended to each possible completion word (optional).
-__gitcomp_opts ()
-{
-	local cur_="${3-$cur}"
-
-	if [[ "$cur_" == *= ]]; then
-		return
-	fi
-
-	local c i=0 IFS=$' \t\n' sfx
-	for c in $1; do
-		if [[ $c == "--" ]]; then
-			if [[ "$cur_" == --no-* ]]; then
-				continue
-			fi
-
-			if [[ --no == "$cur_"* ]]; then
-				COMPREPLY[i++]="--no-... "
-			fi
-			break
-		fi
-		if [[ $c == "$cur_"* ]]; then
-			if [[ -z "${4+set}" ]]; then
-				case $c in
-				*=|*.) sfx="" ;;
-				*) sfx=" " ;;
-				esac
-			else
-				sfx="$4"
-			fi
-			COMPREPLY[i++]="${2-}$c$sfx"
-		fi
-	done
-}
-
 # Clear the variables caching builtins' options when (re-)sourcing
 # the completion script.
 if [[ -n ${ZSH_VERSION-} ]]; then
@@ -430,62 +494,6 @@ __gitcomp_builtin ()
 	__gitcomp_opts "$options"
 }
 
-# Generates completion reply from newline-separated possible completion words
-# by appending a space to all of them. The result is appended to COMPREPLY.
-# It accepts 1 to 4 arguments:
-# 1: List of possible completion words, separated by a single newline.
-# 2: A prefix to be added to each possible completion word (optional).
-# 3: Generate possible completion matches for this word (optional).
-# 4: A suffix to be appended to each possible completion word instead of
-#    the default space (optional).  If specified but empty, nothing is
-#    appended.
-__gitcomp_nl ()
-{
-	local IFS=$'\n'
-	__gitcompadd "$1" "${2-}" "${3-$cur}" "${4- }"
-}
-
-# Fills the COMPREPLY array with prefiltered paths without any additional
-# processing.
-# Callers must take care of providing only paths that match the current path
-# to be completed and adding any prefix path components, if necessary.
-# 1: List of newline-separated matching paths, complete with all prefix
-#    path components.
-__gitcomp_file_direct ()
-{
-	local IFS=$'\n'
-
-	COMPREPLY+=($1)
-
-	# use a hack to enable file mode in bash < 4
-	compopt -o filenames +o nospace 2>/dev/null ||
-	compgen -f /non-existing-dir/ >/dev/null ||
-	true
-}
-
-# Generates completion reply with compgen from newline-separated possible
-# completion filenames.
-# It accepts 1 to 3 arguments:
-# 1: List of possible completion filenames, separated by a single newline.
-# 2: A directory prefix to be added to each possible completion filename
-#    (optional).
-# 3: Generate possible completion matches for this word (optional).
-__gitcomp_file ()
-{
-	local IFS=$'\n'
-
-	# XXX does not work when the directory prefix contains a tilde,
-	# since tilde expansion is not applied.
-	# This means that COMPREPLY will be empty and Bash default
-	# completion will be used.
-	__gitcompadd "$1" "${2-}" "${3-$cur}" ""
-
-	# use a hack to enable file mode in bash < 4
-	compopt -o filenames +o nospace 2>/dev/null ||
-	compgen -f /non-existing-dir/ >/dev/null ||
-	true
-}
-
 # Execute 'git ls-files', unless the --committable option is specified, in
 # which case it runs 'git diff-index' to find out the files that can be
 # committed.  It return paths relative to the directory specified in the first
-- 
2.33.0
Previous: Felipe ContrerasNext: Felipe Contreras
Message 39 of 50 in “completion: git-completion 2.0 patches”
  1. 00/49 completion: git-completion 2.0 patchesFelipe Contreras, Apr 1, 2023
  2. 02/49 completion: bash: trivial cleanupFelipe Contreras, Apr 1, 2023
  3. 01/49 test: completion add test for __git_cmd_idxFelipe Contreras, Apr 1, 2023
  4. 03/49 zsh: remove versionFelipe Contreras, Apr 1, 2023
  5. 04/49 completion: bash: trivial grammar fixFelipe Contreras, Apr 1, 2023
  6. 05/49 completion: zsh: add higher-priority locationFelipe Contreras, Apr 1, 2023
  7. 06/49 zsh: resolve symlink of scriptFelipe Contreras, Apr 1, 2023
  8. 07/49 zsh: simplify realpath dirname idiomFelipe Contreras, Apr 1, 2023
  9. 08/49 test: completion: use global configFelipe Contreras, Apr 1, 2023
  10. 09/49 completion: fix __git_cmd_idx regression for zshFelipe Contreras, Apr 1, 2023
  11. 10/49 completion: fix for suboptions with valueFelipe Contreras, Apr 1, 2023
  12. 11/49 completion: zsh: trivial improvementFelipe Contreras, Apr 1, 2023
  13. 12/49 completion: bash: do not modify COMP_WORDBREAKSFelipe Contreras, Apr 1, 2023
  14. 13/49 test: completion: fix currently typed wordsFelipe Contreras, Apr 1, 2023
  15. 14/49 test: completion: switch __gitcomp_nl prefix testFelipe Contreras, Apr 1, 2023
  16. 15/49 test: completion: add run_func() helperFelipe Contreras, Apr 1, 2023
  17. 16/49 completion: bash: remove non-append functionalityFelipe Contreras, Apr 1, 2023
  18. 17/49 completion: bash: get rid of _append() functionsFelipe Contreras, Apr 1, 2023
  19. 18/49 completion: bash: get rid of any non-append codeFelipe Contreras, Apr 1, 2023
  20. 19/49 completion: zsh: fix options with argumentsFelipe Contreras, Apr 1, 2023
  21. 20/49 completion: zsh: expand --git-dir file argumentFelipe Contreras, Apr 1, 2023
  22. 21/49 completion: zsh: add support for general -C optsFelipe Contreras, Apr 1, 2023
  23. 22/49 completion: zsh: fix for undefined completionsFelipe Contreras, Apr 1, 2023
  24. 23/49 completion: zsh: add support for general -c optsFelipe Contreras, Apr 1, 2023
  25. 24/49 completion: zsh: fix extra space on foo=Felipe Contreras, Apr 1, 2023
  26. 25/49 completion: zsh: add excluded optionsFelipe Contreras, Apr 1, 2023
  27. 26/49 completion: zsh: always set compsetFelipe Contreras, Apr 1, 2023
  28. 27/49 completion: factor out check in __gitcompFelipe Contreras, Apr 1, 2023
  29. 28/49 completion: simplify equal suffix checkFelipe Contreras, Apr 1, 2023
  30. 29/49 completion: refactor __gitcompFelipe Contreras, Apr 1, 2023
  31. 30/49 completion: simplify __gitcompFelipe Contreras, Apr 1, 2023
  32. 31/49 completion: bash: change suffix check in __gitcompFelipe Contreras, Apr 1, 2023
  33. 32/49 completion: improve __gitcomp suffix codeFelipe Contreras, Apr 1, 2023
  34. 33/49 test: completion: add missing testFelipe Contreras, Apr 1, 2023
  35. 34/49 completion: bash: simplify config_variable_nameFelipe Contreras, Apr 1, 2023
  36. 35/49 completion: bash: improve __gitcomp descriptionFelipe Contreras, Apr 1, 2023
  37. 36/49 completion: add __gitcomp_optsFelipe Contreras, Apr 1, 2023
  38. 37/49 completion: bash: cleanup __gitcomp* invocationsFelipe Contreras, Apr 1, 2023
  39. 38/49 completion: bash: shuffle __gitcomp functionsFelipe Contreras, Apr 1, 2023
  40. 39/49 completion: zsh: simplify __gitcomp_directFelipe Contreras, Apr 1, 2023
  41. 40/49 completion: zsh: shuffle __gitcomp* functionsFelipe Contreras, Apr 1, 2023
  42. 41/49 completion: zsh: fix direct quotingFelipe Contreras, Apr 1, 2023
  43. 42/49 completion: zsh: add elements individually in __gitcomp_optsFelipe Contreras, Apr 1, 2023
  44. 43/49 completion: zsh: add __gitcompadd helperFelipe Contreras, Apr 1, 2023
  45. 44/49 completion: zsh: add correct removable suffixFelipe Contreras, Apr 1, 2023
  46. 45/49 completion: bash: simplify _get_comp_words_by_ref()Felipe Contreras, Apr 1, 2023
  47. 46/49 completion: bash: refactor _get_comp_words_by_ref()Felipe Contreras, Apr 1, 2023
  48. 47/49 completion: bash: cleanup _get_comp_words_by_ref()Felipe Contreras, Apr 1, 2023
  49. 48/49 completion: bash: trivial cleanupFelipe Contreras, Apr 1, 2023
  50. 49/49 completion: bash: rename _get_comp_words_by_ref()Felipe Contreras, Apr 1, 2023

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.