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

[PATCH 49/51] completion: bash: rename _get_comp_words_by_ref()

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Aug 30, 2022, 09:31 UTC
Message-ID
<20220830093138.1581538-50-felipe.contreras@gmail.com>
In-Reply-To
<20220830093138.1581538-1-felipe.contreras@gmail.com>

It's only used in one place, rename it, and use it even if bash-completion's more inefficient version of _get_comp_words_by_ref() is available.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/completion/git-completion.bash | 186 ++++++++++++-------------
 t/t9902-completion.sh                  |   2 +-
 2 files changed, 94 insertions(+), 94 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 395a356106..77a475e272 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -312,98 +312,6 @@ __git_dequote ()
 	done
 }
 
-# The following function is based on code from:
-#
-#   bash_completion - programmable completion functions for bash 3.2+
-#
-#   Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
-#             © 2009-2010, Bash Completion Maintainers
-#                     <bash-completion-devel@lists.alioth.debian.org>
-#
-#   This program is free software; you can redistribute it and/or modify
-#   it under the terms of the GNU General Public License as published by
-#   the Free Software Foundation; either version 2, or (at your option)
-#   any later version.
-#
-#   This program is distributed in the hope that it will be useful,
-#   but WITHOUT ANY WARRANTY; without even the implied warranty of
-#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#   GNU General Public License for more details.
-#
-#   You should have received a copy of the GNU General Public License
-#   along with this program; if not, see <http://www.gnu.org/licenses/>.
-#
-#   The latest version of this software can be obtained here:
-#
-#   http://bash-completion.alioth.debian.org/
-#
-#   RELEASE: 2.x
-
-# This function reorganizes the words on the command line to be processed by
-# the rest of the script.
-#
-# This is roughly equivalent to going back in time and setting
-# COMP_WORDBREAKS to exclude '=' and ':'.  The intent is to
-# make option types like --date=<type> and <rev>:<path> easy to
-# recognize by treating each shell word as a single token.
-#
-# It is best not to set COMP_WORDBREAKS directly because the value is
-# shared with other completion scripts.  By the time the completion
-# function gets called, COMP_WORDS has already been populated so local
-# changes to COMP_WORDBREAKS have no effect.
-
-if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
-_get_comp_words_by_ref ()
-{
-	local exclude i j first
-
-	# Which word separators to exclude?
-	exclude="${COMP_WORDBREAKS//[^=:]}"
-	cword=$COMP_CWORD
-	if [ -n "$exclude" ]; then
-		# List of word completion separators has shrunk;
-		# re-assemble words to complete.
-		for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
-			# Append each nonempty word consisting of just
-			# word separator characters to the current word.
-			first=t
-			while
-				[ $i -gt 0 ] &&
-				[ -n "${COMP_WORDS[$i]}" ] &&
-				# word consists of excluded word separators
-				[ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
-			do
-				# Attach to the previous token,
-				# unless the previous token is the command name.
-				if [ $j -ge 2 ] && [ -n "$first" ]; then
-					((j--))
-				fi
-				first=
-				words[$j]=${words[j]}${COMP_WORDS[i]}
-				if [ $i = $COMP_CWORD ]; then
-					cword=$j
-				fi
-				if (($i < ${#COMP_WORDS[@]} - 1)); then
-					((i++))
-				else
-					# Done.
-					break 2
-				fi
-			done
-			words[$j]=${words[j]}${COMP_WORDS[i]}
-			if [ $i = $COMP_CWORD ]; then
-				cword=$j
-			fi
-		done
-	else
-		words=("${COMP_WORDS[@]}")
-	fi
-
-	cur=${words[cword]}
-	prev=${words[cword-1]}
-}
-fi
-
 # Clear the variables caching builtins' options when (re-)sourcing
 # the completion script.
 if [[ -n ${ZSH_VERSION-} ]]; then
@@ -3477,10 +3385,102 @@ if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
 	return
 fi
 
+# The following function is based on code from:
+#
+#   bash_completion - programmable completion functions for bash 3.2+
+#
+#   Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
+#             © 2009-2010, Bash Completion Maintainers
+#                     <bash-completion-devel@lists.alioth.debian.org>
+#
+#   This program is free software; you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#   the Free Software Foundation; either version 2, or (at your option)
+#   any later version.
+#
+#   This program is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License
+#   along with this program; if not, see <http://www.gnu.org/licenses/>.
+#
+#   The latest version of this software can be obtained here:
+#
+#   http://bash-completion.alioth.debian.org/
+#
+#   RELEASE: 2.x
+
+# This function reorganizes the words on the command line to be processed by
+# the rest of the script.
+#
+# This is roughly equivalent to going back in time and setting
+# COMP_WORDBREAKS to exclude '=' and ':'.  The intent is to
+# make option types like --date=<type> and <rev>:<path> easy to
+# recognize by treating each shell word as a single token.
+#
+# It is best not to set COMP_WORDBREAKS directly because the value is
+# shared with other completion scripts.  By the time the completion
+# function gets called, COMP_WORDS has already been populated so local
+# changes to COMP_WORDBREAKS have no effect.
+
+if ! type __git_get_comp_words_by_ref >/dev/null 2>&1; then
+__git_get_comp_words_by_ref ()
+{
+	local exclude i j first
+
+	# Which word separators to exclude?
+	exclude="${COMP_WORDBREAKS//[^=:]}"
+	cword=$COMP_CWORD
+	if [ -n "$exclude" ]; then
+		# List of word completion separators has shrunk;
+		# re-assemble words to complete.
+		for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
+			# Append each nonempty word consisting of just
+			# word separator characters to the current word.
+			first=t
+			while
+				[ $i -gt 0 ] &&
+				[ -n "${COMP_WORDS[$i]}" ] &&
+				# word consists of excluded word separators
+				[ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
+			do
+				# Attach to the previous token,
+				# unless the previous token is the command name.
+				if [ $j -ge 2 ] && [ -n "$first" ]; then
+					((j--))
+				fi
+				first=
+				words[$j]=${words[j]}${COMP_WORDS[i]}
+				if [ $i = $COMP_CWORD ]; then
+					cword=$j
+				fi
+				if (($i < ${#COMP_WORDS[@]} - 1)); then
+					((i++))
+				else
+					# Done.
+					break 2
+				fi
+			done
+			words[$j]=${words[j]}${COMP_WORDS[i]}
+			if [ $i = $COMP_CWORD ]; then
+				cword=$j
+			fi
+		done
+	else
+		words=("${COMP_WORDS[@]}")
+	fi
+
+	cur=${words[cword]}
+	prev=${words[cword-1]}
+}
+fi
+
 __git_func_wrap ()
 {
 	local cur words cword prev __git_cmd_idx=0
-	_get_comp_words_by_ref -n =: cur words cword prev
+	__git_get_comp_words_by_ref
 	$1
 }
 
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index fc09bafa07..fff8451716 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -38,7 +38,7 @@ GIT_TESTING_PORCELAIN_COMMAND_LIST='add checkout rebase'
 # We don't need this function to actually join words or do anything special.
 # Also, it's cleaner to avoid touching bash's internal completion variables.
 # So let's override it with a minimal version for testing purposes.
-_get_comp_words_by_ref ()
+__git_get_comp_words_by_ref ()
 {
 	cword=$_cword
 	cur=${_words[cword]}
-- 
2.37.2.351.g9bf691b78c.dirty
Previous: Felipe ContrerasNext: Felipe Contreras
Message 50 of 52 in “completion: revamp”
  1. 00/51 completion: revampFelipe Contreras, Aug 30, 2022
  2. 01/51 test: completion add test for __git_cmd_idxFelipe Contreras, Aug 30, 2022
  3. 03/51 completion: fix __git_cmd_idx regression for zshFelipe Contreras, Aug 30, 2022
  4. 02/51 test: add zsh completion testsFelipe Contreras, Aug 30, 2022
  5. 04/51 completion: bash: trivial cleanupFelipe Contreras, Aug 30, 2022
  6. 05/51 completion: zsh: add higher-priority locationFelipe Contreras, Aug 30, 2022
  7. 09/51 completion: prompt: use generic colorsFelipe Contreras, Aug 30, 2022
  8. 07/51 zsh: simplify realpath dirname idiomFelipe Contreras, Aug 30, 2022
  9. 10/51 completion: fix for suboptions with valueFelipe Contreras, Aug 30, 2022
  10. 06/51 zsh: resolve symlink of scriptFelipe Contreras, Aug 30, 2022
  11. 08/51 test: reset environment variablesFelipe Contreras, Aug 30, 2022
  12. 11/51 completion: zsh: trivial improvementFelipe Contreras, Aug 30, 2022
  13. 12/51 completion: bash: do not modify COMP_WORDBREAKSFelipe Contreras, Aug 30, 2022
  14. 13/51 test: completion: fix currently typed wordsFelipe Contreras, Aug 30, 2022
  15. 14/51 test: completion: switch __gitcomp_nl prefix testFelipe Contreras, Aug 30, 2022
  16. 15/51 test: completion: add run_func() helperFelipe Contreras, Aug 30, 2022
  17. 16/51 completion: bash: remove non-append functionalityFelipe Contreras, Aug 30, 2022
  18. 17/51 completion: bash: get rid of _append() functionsFelipe Contreras, Aug 30, 2022
  19. 18/51 completion: bash: get rid of any non-append codeFelipe Contreras, Aug 30, 2022
  20. 19/51 completion: zsh: fix options with argumentsFelipe Contreras, Aug 30, 2022
  21. 20/51 completion: zsh: expand --git-dir file argumentFelipe Contreras, Aug 30, 2022
  22. 21/51 completion: zsh: add support for general -C optsFelipe Contreras, Aug 30, 2022
  23. 23/51 completion: zsh: add support for general -c optsFelipe Contreras, Aug 30, 2022
  24. 24/51 completion: zsh: fix extra space on foo=Felipe Contreras, Aug 30, 2022
  25. 22/51 completion: zsh: fix for undefined completionsFelipe Contreras, Aug 30, 2022
  26. 28/51 completion: simplify equal suffix checkFelipe Contreras, Aug 30, 2022
  27. 30/51 completion: simplify __gitcompFelipe Contreras, Aug 30, 2022
  28. 32/51 completion: improve __gitcomp suffix codeFelipe Contreras, Aug 30, 2022
  29. 33/51 test: completion: add missing testFelipe Contreras, Aug 30, 2022
  30. 25/51 completion: zsh: add excluded optionsFelipe Contreras, Aug 30, 2022
  31. 27/51 completion: factor out check in __gitcompFelipe Contreras, Aug 30, 2022
  32. 26/51 completion: zsh: always set compsetFelipe Contreras, Aug 30, 2022
  33. 29/51 completion: refactor __gitcompFelipe Contreras, Aug 30, 2022
  34. 31/51 completion: bash: change suffix check in __gitcompFelipe Contreras, Aug 30, 2022
  35. 36/51 completion: add __gitcomp_optsFelipe Contreras, Aug 30, 2022
  36. 37/51 completion: bash: cleanup __gitcomp* invocationsFelipe Contreras, Aug 30, 2022
  37. 34/51 completion: bash: simplify config_variable_nameFelipe Contreras, Aug 30, 2022
  38. 35/51 completion: bash: improve __gitcomp descriptionFelipe Contreras, Aug 30, 2022
  39. 38/51 completion: bash: shuffle __gitcomp functionsFelipe Contreras, Aug 30, 2022
  40. 39/51 completion: zsh: simplify __gitcomp_directFelipe Contreras, Aug 30, 2022
  41. 42/51 completion: zsh: add elements individually in __gitcomp_optsFelipe Contreras, Aug 30, 2022
  42. 43/51 completion: zsh: add __gitcompadd helperFelipe Contreras, Aug 30, 2022
  43. 40/51 completion: zsh: shuffle __gitcomp* functionsFelipe Contreras, Aug 30, 2022
  44. 45/51 completion: bash: simplify _get_comp_words_by_ref()Felipe Contreras, Aug 30, 2022
  45. 41/51 completion: zsh: fix direct quotingFelipe Contreras, Aug 30, 2022
  46. 44/51 completion: zsh: add correct removable suffixFelipe Contreras, Aug 30, 2022
  47. 48/51 completion: bash: trivial cleanupFelipe Contreras, Aug 30, 2022
  48. 47/51 completion: bash: cleanup _get_comp_words_by_ref()Felipe Contreras, Aug 30, 2022
  49. 46/51 completion: bash: refactor _get_comp_words_by_ref()Felipe Contreras, Aug 30, 2022
  50. 49/51 completion: bash: rename _get_comp_words_by_ref()Felipe Contreras, Aug 30, 2022
  51. 50/51 zsh: remove versionFelipe Contreras, Aug 30, 2022
  52. 51/51 completion: bash: trivial grammar fixFelipe Contreras, Aug 30, 2022

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.