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

[PATCH] tcsh-completion re-using git-completion.bash

From
Marc Khouzam <marc.khouzam@gmail.com>
Date
Nov 15, 2012, 11:51 UTC
Message-ID
<1352980269-15569-1-git-send-email-marc.khouzam@gmail.com>
In-Reply-To
<CAFj1UpHgPvdDeKZ-Ap7-aVx6p_pxT4a2F01ajmNa00txPyS=Qw@mail.gmail.com>

The current tcsh-completion support for Git, as can be found on the Internet, takes the approach of defining the possible completions explicitly. This has the obvious draw-back to require constant updating as the Git code base evolves.

The approach taken by this commit is to to re-use the advanced bash completion script and use its result for tcsh completion. This is achieved by executing (versus sourcing) the bash script and outputting the completion result for tcsh consumption.

Three solutions were looked at to implement this approach with (A) being retained:

  A) Modifications:
          git-completion.bash and new git-completion.tcsh
     Modify the existing git-completion.bash script to support
     being sourced using bash (as now), but also executed using bash.
     When being executed, the script will output the result of the
     computed completion to be re-used elsewhere (e.g., in tcsh).
     The modification to git-completion.bash is made not to be
     tcsh-specific, but to allow future users to also re-use its
     output.  Therefore, to be general, git-completion.bash accepts a
     second optional parameter, which is not used by tcsh, but could
     prove useful for other users.
     Pros:
       1- allows the git-completion.bash script to easily be re-used
       2- tcsh support is mostly isolated in git-completion.tcsh
     Cons (for tcsh users only):
       1- requires the user to copy both git-completion.tcsh and
          git-completion.bash to ${HOME}
       2- requires bash script to have a fixed name and location:
          ${HOME}/.git-completion.bash
  B) Modifications:
          git-completion.bash
     Modify the existing git-completion.bash script to support
     being sourced using bash (as now), but also executed using bash,
     and sourced using tcsh.
     Pros:
       1- only requires the user to deal with a single file
       2- maintenance more obvious for tcsh since it is entirely part
          of the same git-completion.bash script.
     Cons:
       1- tcsh support could affect bash support as they share the
          same script
       2- small tcsh section must use syntax suitable for both tcsh
          and bash and must be at the beginning of the script
       3- requires script to have a fixed name and location:
          ${HOME}/.git-completion.sh (for tcsh users only)
  C) Modifications:
          New git-completion.tcsh
     Provide a short tcsh script that converts git-completion.bash
     into an executable script suitable to be used by tcsh.
     Pros:
       1- tcsh support is entirely isolated in git-completion.tcsh
       2- new tcsh script can be as complex as needed
     Cons (for tcsh users only):
       1- requires the user to copy both git-completion.tcsh and
          git-completion.bash to ${HOME}
       2- requires bash script to have a fixed name and location:
          ${HOME}/.git-completion.bash
       3- sourcing the new script will generate a third script

Approach (A) was selected to keep the tcsh completion support well isolated without introducing excessive complexity.

Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
---

Here is the updated version of the patch. I got git send-email to work, so I hope the formatting will be correct.

Thanks in advance.
Marc
 contrib/completion/git-completion.bash |   47 ++++++++++++++++++++++++++++++++
 contrib/completion/git-completion.tcsh |   29 +++++++++++++++++++
 2 files changed, 76 insertions(+), 0 deletions(-)
 create mode 100644 contrib/completion/git-completion.tcsh
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index be800e0..d71a016 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2481,3 +2481,50 @@ __git_complete gitk __gitk_main
 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
 __git_complete git.exe __git_main
 fi
+
+# Method that will output the result of the completion done by
+# the bash completion script, so that it can be re-used in another
+# context than the bash complete command.
+# It accepts 1 to 2 arguments:
+# 1: The command-line to complete
+# 2: The index of the word within argument #1 in which the cursor is
+#    located (optional). If parameter 2 is not provided, it will be
+#    determined as best possible using parameter 1.
+__git_complete_with_output ()
+{
+	# Set COMP_WORDS in a way that can be handled by the bash script.
+	COMP_WORDS=($1)
+
+	# Set COMP_CWORD to the cursor location as bash would.
+	if [ -n "${2-}" ]; then
+		COMP_CWORD=$2
+	else
+		# Assume the cursor is at the end of parameter #1.
+		# We must check for a space as the last character which will
+		# tell us that the previous word is complete and the cursor
+		# is on the next word.
+		if [ "${1: -1}" == " " ]; then
+			# The last character is a space, so our location is at the end
+			# of the command-line array
+			COMP_CWORD=${#COMP_WORDS[@]}
+		else
+			# The last character is not a space, so our location is on the
+			# last word of the command-line array, so we must decrement the
+			# count by 1
+			COMP_CWORD=$((${#COMP_WORDS[@]}-1))
+		fi
+	fi
+
+	# Call _git() or _gitk() of the bash script, based on the first
+	# element of the command-line
+	_${COMP_WORDS[0]}
+
+	local IFS=$'\n'
+	echo "${COMPREPLY[*]}"
+}
+
+if [ -n "${1-}" ] ; then
+  # If there is an argument, we know the script is being executed
+  # so go ahead and run the _git_complete_with_output function
+  __git_complete_with_output "${1-}" "${2-}"
+fi
diff --git a/contrib/completion/git-completion.tcsh b/contrib/completion/git-completion.tcsh
new file mode 100644
index 0000000..6096ea8
--- /dev/null
+++ b/contrib/completion/git-completion.tcsh
@@ -0,0 +1,29 @@
+#!tcsh
+#
+# tcsh completion support for core Git.
+#
+# Copyright (C) 2012 Marc Khouzam <marc.khouzam@gmail.com>
+# Distributed under the GNU General Public License, version 2.0.
+#
+# This script makes use of the git-completion.bash script to
+# determine the proper completion for git commands under tcsh.
+#
+# To use this completion script:
+#
+#    1) Copy both this file and the bash completion script to your ${HOME} directory
+#       using the names ${HOME}/.git-completion.tcsh and ${HOME}/.git-completion.bash.
+#    2) Add the following line to your .tcshrc/.cshrc:
+#        source ${HOME}/.git-completion.tcsh
+
+# One can change the below line to use a different location
+set __git_tcsh_completion_script = ${HOME}/.git-completion.bash
+
+# Check that the user put the script in the right place
+if ( ! -e ${__git_tcsh_completion_script} ) then
+	echo "ERROR in git-completion.tcsh script.  Cannot find: ${__git_tcsh_completion_script}.  Git completion will not work."
+	exit
+endif
+
+complete git  'p/*/`bash ${__git_tcsh_completion_script} "${COMMAND_LINE}" | sort | uniq`/'
+complete gitk 'p/*/`bash ${__git_tcsh_completion_script} "${COMMAND_LINE}" | sort | uniq`/'
+
-- 
1.7.0.4
Previous: Marc KhouzamNext: Felipe Contreras
Message 7 of 39 in “Fwd: [PATCH] Add tcsh-completion support to contrib by using git-completion.bash”
  1. Marc KhouzamNov 12, 2012
  2. SZEDER GáborNov 13, 2012
  3. Marc KhouzamNov 13, 2012
  4. SZEDER GáborNov 13, 2012
  5. completion: remove 'help' duplicate from porcelain commandsSZEDER Gábor, Nov 14, 2012
  6. Marc KhouzamNov 14, 2012
  7. tcsh-completion re-using git-completion.bashMarc Khouzam, Nov 15, 2012
  8. Felipe ContrerasNov 16, 2012
  9. Marc KhouzamNov 16, 2012
  10. Felipe ContrerasNov 16, 2012
  11. Marc KhouzamNov 16, 2012
  12. tcsh-completion re-using git-completion.bashMarc Khouzam, Nov 16, 2012
  13. Felipe ContrerasNov 16, 2012
  14. tcsh-completion re-using git-completion.bashMarc Khouzam, Nov 16, 2012
  15. Junio C HamanoNov 16, 2012
  16. Felipe ContrerasNov 16, 2012
  17. Felipe ContrerasNov 16, 2012
  18. Marc KhouzamNov 16, 2012
  19. Felipe ContrerasNov 16, 2012
  20. SZEDER GáborNov 16, 2012
  21. Felipe ContrerasNov 16, 2012
  22. SZEDER GáborNov 16, 2012
  23. Felipe ContrerasNov 16, 2012
  24. SZEDER GáborNov 17, 2012
  25. Felipe ContrerasNov 17, 2012
  26. SZEDER GáborNov 17, 2012
  27. Junio C HamanoNov 16, 2012
  28. Felipe ContrerasNov 16, 2012
  29. Marc KhouzamNov 17, 2012
  30. Felipe ContrerasNov 17, 2012
  31. Marc KhouzamNov 20, 2012
  32. Felipe ContrerasNov 20, 2012
  33. Marc KhouzamNov 20, 2012
  34. Junio C HamanoNov 20, 2012
  35. Felipe ContrerasNov 13, 2012
  36. SZEDER GáborNov 14, 2012
  37. Felipe ContrerasNov 15, 2012
  38. Marc KhouzamNov 14, 2012
  39. SZEDER GáborNov 14, 2012

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.