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

[PATCH v2 42/45] completion: bash: refactor _get_comp_words_by_ref()

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Jun 18, 2021, 18:25 UTC
Message-ID
<20210618182518.697912-43-felipe.contreras@gmail.com>
In-Reply-To
<20210618182518.697912-1-felipe.contreras@gmail.com>

We don't need a separate function to do what we already know we want to do.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/completion/git-completion.bash | 93 +++++++++++---------------
 1 file changed, 39 insertions(+), 54 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 2c0b869aba..9976009951 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -334,21 +334,11 @@ __git_dequote ()
 #
 #   RELEASE: 2.x
 
-# This function can be used to access a tokenized list of words
-# on the command line:
-#
-#	__git_reassemble_comp_words_by_ref '=:'
-#	if test "${words_[cword_-1]}" = -w
-#	then
-#		...
-#	fi
-#
-# The argument should be a collection of characters from the list of
-# word completion separators (COMP_WORDBREAKS) to treat as ordinary
-# characters.
+# 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 those characters.  The intent is to
+# 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.
 #
@@ -356,60 +346,55 @@ __git_dequote ()
 # 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.
-#
-# Output: words_, cword_, cur_.
 
-__git_reassemble_comp_words_by_ref()
+if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
+_get_comp_words_by_ref ()
 {
+	local words_ cword_
 	local exclude i j first
+
 	# Which word separators to exclude?
-	exclude="${1//[^$COMP_WORDBREAKS]}"
+	exclude="${COMP_WORDBREAKS//[^=:]}"
 	cword_=$COMP_CWORD
 	if [ -z "$exclude" ]; then
 		words_=("${COMP_WORDS[@]}")
-		return
-	fi
-	# 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=
+	else
+		# 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
-			if (($i < ${#COMP_WORDS[@]} - 1)); then
-				((i++))
-			else
-				# Done.
-				return
-			fi
 		done
-		words_[$j]=${words_[j]}${COMP_WORDS[i]}
-		if [ $i = $COMP_CWORD ]; then
-			cword_=$j
-		fi
-	done
-}
+	fi
 
-if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
-_get_comp_words_by_ref ()
-{
-	local words_ cword_
-	__git_reassemble_comp_words_by_ref "=:"
 	cword=$cword_
 	cur=${words_[cword]}
 	prev=${words_[cword-1]}
-- 
2.32.0
Previous: Felipe ContrerasNext: Felipe Contreras
Message 44 of 48 in “completion: git-completion 1.3.3 patches”
  1. 00/45 completion: git-completion 1.3.3 patchesFelipe Contreras, Jun 18, 2021
  2. 01/45 completion: fix __git_cmd_idx regressionFelipe Contreras, Jun 18, 2021
  3. completion: fix __git_cmd_idx regressionFelipe Contreras, Jun 18, 2021
  4. Felipe ContrerasJun 18, 2021
  5. 02/45 completion: bash: fix prefix detection in branch.*Felipe Contreras, Jun 18, 2021
  6. 03/45 completion: fix for suboptions with valueFelipe Contreras, Jun 18, 2021
  7. 04/45 completion: bash: fix for multiple dash commandsFelipe Contreras, Jun 18, 2021
  8. 05/45 completion: bash: add correct suffix in variablesFelipe Contreras, Jun 18, 2021
  9. 06/45 completion: zsh: add higher-priority locationFelipe Contreras, Jun 18, 2021
  10. 07/45 completion: zsh: trivial improvementFelipe Contreras, Jun 18, 2021
  11. 08/45 completion: bash: do not modify COMP_WORDBREAKSFelipe Contreras, Jun 18, 2021
  12. 09/45 test: completion: fix currently typed wordsFelipe Contreras, Jun 18, 2021
  13. 10/45 test: completion: switch __gitcomp_nl prefix testFelipe Contreras, Jun 18, 2021
  14. 11/45 test: completion: add run_func() helperFelipe Contreras, Jun 18, 2021
  15. 12/45 completion: bash: remove non-append functionalityFelipe Contreras, Jun 18, 2021
  16. 13/45 completion: bash: get rid of _append() functionsFelipe Contreras, Jun 18, 2021
  17. 14/45 completion: bash: get rid of any non-append codeFelipe Contreras, Jun 18, 2021
  18. 15/45 completion: zsh: fix options with argumentsFelipe Contreras, Jun 18, 2021
  19. 16/45 completion: zsh: expand --git-dir file argumentFelipe Contreras, Jun 18, 2021
  20. 17/45 completion: zsh: add support for general -C optsFelipe Contreras, Jun 18, 2021
  21. 18/45 completion: zsh: fix for undefined completionsFelipe Contreras, Jun 18, 2021
  22. 19/45 completion: zsh: add support for general -c optsFelipe Contreras, Jun 18, 2021
  23. 20/45 completion: zsh: fix extra space on foo=Felipe Contreras, Jun 18, 2021
  24. 22/45 completion: zsh: always set compsetFelipe Contreras, Jun 18, 2021
  25. 21/45 completion: zsh: add excluded optionsFelipe Contreras, Jun 18, 2021
  26. 23/45 completion: factor out check in __gitcompFelipe Contreras, Jun 18, 2021
  27. 24/45 completion: simplify equal suffix checkFelipe Contreras, Jun 18, 2021
  28. 25/45 completion: refactor __gitcompFelipe Contreras, Jun 18, 2021
  29. 26/45 completion: simplify __gitcompFelipe Contreras, Jun 18, 2021
  30. 27/45 completion: bash: change suffix check in __gitcompFelipe Contreras, Jun 18, 2021
  31. 28/45 completion: improve __gitcomp suffix codeFelipe Contreras, Jun 18, 2021
  32. 29/45 completion: bash: simplify config_variable_nameFelipe Contreras, Jun 18, 2021
  33. 30/45 test: completion: add missing testFelipe Contreras, Jun 18, 2021
  34. 31/45 completion: bash: improve __gitcomp descriptionFelipe Contreras, Jun 18, 2021
  35. 32/45 completion: add __gitcomp_optsFelipe Contreras, Jun 18, 2021
  36. 33/45 completion: bash: cleanup __gitcomp* invocationsFelipe Contreras, Jun 18, 2021
  37. 34/45 completion: bash: shuffle __gitcomp functionsFelipe Contreras, Jun 18, 2021
  38. 35/45 completion: zsh: simplify __gitcomp_directFelipe Contreras, Jun 18, 2021
  39. 36/45 completion: zsh: shuffle __gitcomp* functionsFelipe Contreras, Jun 18, 2021
  40. 37/45 completion: zsh: fix direct quotingFelipe Contreras, Jun 18, 2021
  41. 38/45 completion: zsh: add elements individually in __gitcomp_optsFelipe Contreras, Jun 18, 2021
  42. 39/45 completion: zsh: add __gitcompadd helperFelipe Contreras, Jun 18, 2021
  43. 40/45 completion: zsh: add correct removable suffixFelipe Contreras, Jun 18, 2021
  44. 42/45 completion: bash: refactor _get_comp_words_by_ref()Felipe Contreras, Jun 18, 2021
  45. 41/45 completion: bash: simplify _get_comp_words_by_ref()Felipe Contreras, Jun 18, 2021
  46. 43/45 completion: bash: cleanup _get_comp_words_by_ref()Felipe Contreras, Jun 18, 2021
  47. 44/45 completion: bash: trivial cleanupFelipe Contreras, Jun 18, 2021
  48. 45/45 completion: bash: rename _get_comp_words_by_ref()Felipe Contreras, Jun 18, 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.