Skip to content

Commit 9673b8c

Browse files
marckhouzamgitster
authored andcommitted
tcsh-completion re-using git-completion.bash
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 sourcing the bash script and outputting the completion result for tcsh consumption. Three solutions were looked at to implement this approach with (C) 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 generates another script which extends git-completion.bash. This new script can be used by tcsh to perform completion. 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 (C) was selected avoid any modification to git-completion.bash. Signed-off-by: Marc Khouzam <[email protected]> Reviewed-by: Felipe Contreras <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8c7a786 commit 9673b8c

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!tcsh
2+
#
3+
# tcsh completion support for core Git.
4+
#
5+
# Copyright (C) 2012 Marc Khouzam <[email protected]>
6+
# Distributed under the GNU General Public License, version 2.0.
7+
#
8+
# When sourced, this script will generate a new script that uses
9+
# the git-completion.bash script provided by core Git. This new
10+
# script can be used by tcsh to perform git completion.
11+
# The current script also issues the necessary tcsh 'complete'
12+
# commands.
13+
#
14+
# To use this completion script:
15+
#
16+
# 1) Copy both this file and the bash completion script to ${HOME}.
17+
# You _must_ use the name ${HOME}/.git-completion.bash for the
18+
# bash script.
19+
# (e.g. ~/.git-completion.tcsh and ~/.git-completion.bash).
20+
# 2) Add the following line to your .tcshrc/.cshrc:
21+
# source ~/.git-completion.tcsh
22+
23+
set __git_tcsh_completion_original_script = ${HOME}/.git-completion.bash
24+
set __git_tcsh_completion_script = ${HOME}/.git-completion.tcsh.bash
25+
26+
cat << EOF > ${__git_tcsh_completion_script}
27+
#!bash
28+
#
29+
# This script is GENERATED and will be overwritten automatically.
30+
# Do not modify it directly. Instead, modify the git-completion.tcsh
31+
# script provided by Git core.
32+
#
33+
34+
source ${__git_tcsh_completion_original_script}
35+
36+
# Set COMP_WORDS in a way that can be handled by the bash script.
37+
COMP_WORDS=(\$1)
38+
39+
# The cursor is at the end of parameter #1.
40+
# We must check for a space as the last character which will
41+
# tell us that the previous word is complete and the cursor
42+
# is on the next word.
43+
if [ "\${1: -1}" == " " ]; then
44+
# The last character is a space, so our location is at the end
45+
# of the command-line array
46+
COMP_CWORD=\${#COMP_WORDS[@]}
47+
else
48+
# The last character is not a space, so our location is on the
49+
# last word of the command-line array, so we must decrement the
50+
# count by 1
51+
COMP_CWORD=\$((\${#COMP_WORDS[@]}-1))
52+
fi
53+
54+
# Call _git() or _gitk() of the bash script, based on the first
55+
# element of the command-line
56+
_\${COMP_WORDS[0]}
57+
58+
IFS=\$'\n'
59+
echo "\${COMPREPLY[*]}" | sort | uniq
60+
EOF
61+
62+
complete git 'p/*/`bash ${__git_tcsh_completion_script} "${COMMAND_LINE}"`/'
63+
complete gitk 'p/*/`bash ${__git_tcsh_completion_script} "${COMMAND_LINE}"`/'

0 commit comments

Comments
 (0)