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

[PATCH 31/47] completion: bash: shuffle __gitcomp functions

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Jan 1, 2021, 02:16 UTC
Message-ID
<20210101021622.798041-32-felipe.contreras@gmail.com>
In-Reply-To
<20210101021622.798041-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 9bcb484656..3fa597ce71 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -45,6 +45,145 @@
 #     When set to "1" suggest all options, including options which are
 #     typically hidden (e.g. '--allow-empty' for 'git commit').
 
+# 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.
@@ -287,81 +426,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
@@ -410,62 +474,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.30.0
Previous: Felipe ContrerasNext: Felipe Contreras
Message 30 of 48 in “completion: git-completion 1.3 patches”
  1. 00/47 completion: git-completion 1.3 patchesFelipe Contreras, Jan 1, 2021
  2. 06/47 test: completion: fix currently typed wordsFelipe Contreras, Jan 1, 2021
  3. 05/47 completion: bash: do not modify COMP_WORDBREAKSFelipe Contreras, Jan 1, 2021
  4. 15/47 completion: zsh: fix for undefined completionsFelipe Contreras, Jan 1, 2021
  5. 02/47 completion: fix for suboptions with valueFelipe Contreras, Jan 1, 2021
  6. 13/47 completion: zsh: expand --git-dir file argumentFelipe Contreras, Jan 1, 2021
  7. 01/47 completion: bash: fix prefix detection in branch.*Felipe Contreras, Jan 1, 2021
  8. 04/47 completion: bash: add correct suffix in variablesFelipe Contreras, Jan 1, 2021
  9. 03/47 completion: bash: fix for multiple dash commandsFelipe Contreras, Jan 1, 2021
  10. 07/47 test: completion: switch __gitcomp_nl prefix testFelipe Contreras, Jan 1, 2021
  11. 10/47 completion: bash: get rid of _append() functionsFelipe Contreras, Jan 1, 2021
  12. 08/47 test: completion: add run_func() helperFelipe Contreras, Jan 1, 2021
  13. 17/47 completion: zsh: fix extra space on foo=Felipe Contreras, Jan 1, 2021
  14. 23/47 completion: simplify __gitcompFelipe Contreras, Jan 1, 2021
  15. 21/47 completion: simplify equal suffix checkFelipe Contreras, Jan 1, 2021
  16. 19/47 completion: zsh: always set compsetFelipe Contreras, Jan 1, 2021
  17. 27/47 test: completion: add missing testFelipe Contreras, Jan 1, 2021
  18. 11/47 completion: bash: get rid of any non-append codeFelipe Contreras, Jan 1, 2021
  19. 25/47 completion: improve __gitcomp suffix codeFelipe Contreras, Jan 1, 2021
  20. 28/47 completion: bash: improve __gitcomp descriptionFelipe Contreras, Jan 1, 2021
  21. 12/47 completion: zsh: fix options with argumentsFelipe Contreras, Jan 1, 2021
  22. 14/47 completion: zsh: add support for general -C optsFelipe Contreras, Jan 1, 2021
  23. 16/47 completion: zsh: add support for general -c optsFelipe Contreras, Jan 1, 2021
  24. 29/47 completion: add __gitcomp_optsFelipe Contreras, Jan 1, 2021
  25. 30/47 completion: bash: cleanup __gitcomp* invocationsFelipe Contreras, Jan 1, 2021
  26. 09/47 completion: bash: remove non-append functionalityFelipe Contreras, Jan 1, 2021
  27. 33/47 completion: zsh: shuffle __gitcomp* functionsFelipe Contreras, Jan 1, 2021
  28. 35/47 completion: zsh: add elements individually in __gitcomp_optsFelipe Contreras, Jan 1, 2021
  29. 36/47 completion: zsh: add __gitcompadd helperFelipe Contreras, Jan 1, 2021
  30. 31/47 completion: bash: shuffle __gitcomp functionsFelipe Contreras, Jan 1, 2021
  31. 38/47 completion: bash: simplify _get_comp_words_by_ref()Felipe Contreras, Jan 1, 2021
  32. 37/47 completion: zsh: add correct removable suffixFelipe Contreras, Jan 1, 2021
  33. 39/47 completion: bash: refactor _get_comp_words_by_ref()Felipe Contreras, Jan 1, 2021
  34. 32/47 completion: zsh: simplify __gitcomp_directFelipe Contreras, Jan 1, 2021
  35. 40/47 completion: bash: cleanup _get_comp_words_by_ref()Felipe Contreras, Jan 1, 2021
  36. 18/47 completion: zsh: add excluded optionsFelipe Contreras, Jan 1, 2021
  37. 20/47 completion: factor out check in __gitcompFelipe Contreras, Jan 1, 2021
  38. 22/47 completion: refactor __gitcompFelipe Contreras, Jan 1, 2021
  39. 24/47 completion: bash: change suffix check in __gitcompFelipe Contreras, Jan 1, 2021
  40. 44/47 completion: bash: improve function detectionFelipe Contreras, Jan 1, 2021
  41. 26/47 completion: bash: simplify config_variable_nameFelipe Contreras, Jan 1, 2021
  42. 45/47 test: completion: add tests for __git_completeFelipe Contreras, Jan 1, 2021
  43. 47/47 completion: zsh: add higher-priority locationFelipe Contreras, Jan 1, 2021
  44. 42/47 completion: bash: rename _get_comp_words_by_ref()Felipe Contreras, Jan 1, 2021
  45. 34/47 completion: zsh: fix direct quotingFelipe Contreras, Jan 1, 2021
  46. 41/47 completion: bash: trivial cleanupFelipe Contreras, Jan 1, 2021
  47. 43/47 completion: bash: add __git_have_func helperFelipe Contreras, Jan 1, 2021
  48. 46/47 completion: add proper public __git_completeFelipe Contreras, Jan 1, 2021

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.