]> git.ipfire.org Git - thirdparty/git.git/blame - contrib/completion/git-completion.tcsh
Merge branch 'js/complete-checkout-t'
[thirdparty/git.git] / contrib / completion / git-completion.tcsh
CommitLineData
9673b8c3
MK
1# tcsh completion support for core Git.
2#
3# Copyright (C) 2012 Marc Khouzam <marc.khouzam@gmail.com>
4# Distributed under the GNU General Public License, version 2.0.
5#
6# When sourced, this script will generate a new script that uses
7# the git-completion.bash script provided by core Git. This new
8# script can be used by tcsh to perform git completion.
9# The current script also issues the necessary tcsh 'complete'
10# commands.
11#
12# To use this completion script:
13#
92f1c042 14# 0) You need tcsh 6.16.00 or newer.
9673b8c3
MK
15# 1) Copy both this file and the bash completion script to ${HOME}.
16# You _must_ use the name ${HOME}/.git-completion.bash for the
17# bash script.
18# (e.g. ~/.git-completion.tcsh and ~/.git-completion.bash).
19# 2) Add the following line to your .tcshrc/.cshrc:
20# source ~/.git-completion.tcsh
75ed918b
MK
21# 3) For completion similar to bash, it is recommended to also
22# add the following line to your .tcshrc/.cshrc:
23# set autolist=ambiguous
24# It will tell tcsh to list the possible completion choices.
9673b8c3 25
92f1c042
MK
26set __git_tcsh_completion_version = `\echo ${tcsh} | \sed 's/\./ /g'`
27if ( ${__git_tcsh_completion_version[1]} < 6 || \
28 ( ${__git_tcsh_completion_version[1]} == 6 && \
29 ${__git_tcsh_completion_version[2]} < 16 ) ) then
30 echo "git-completion.tcsh: Your version of tcsh is too old, you need version 6.16.00 or newer. Git completion will not work."
31 exit
32endif
33unset __git_tcsh_completion_version
34
9673b8c3
MK
35set __git_tcsh_completion_original_script = ${HOME}/.git-completion.bash
36set __git_tcsh_completion_script = ${HOME}/.git-completion.tcsh.bash
37
ce45ea6a
MK
38# Check that the user put the script in the right place
39if ( ! -e ${__git_tcsh_completion_original_script} ) then
75ed918b
MK
40 echo "git-completion.tcsh: Cannot find: ${__git_tcsh_completion_original_script}. Git completion will not work."
41 exit
ce45ea6a
MK
42endif
43
0b1f688b 44cat << EOF >! ${__git_tcsh_completion_script}
9673b8c3
MK
45#!bash
46#
47# This script is GENERATED and will be overwritten automatically.
75ed918b
MK
48# Do not modify it directly. Instead, modify git-completion.tcsh
49# and source it again.
9673b8c3
MK
50
51source ${__git_tcsh_completion_original_script}
52
c6929ff2
MK
53# Remove the colon as a completion separator because tcsh cannot handle it
54COMP_WORDBREAKS=\${COMP_WORDBREAKS//:}
55
56# For file completion, tcsh needs the '/' to be appended to directories.
57# By default, the bash script does not do that.
58# We can achieve this by using the below compatibility
59# method of the git-completion.bash script.
60__git_index_file_list_filter ()
61{
62 __git_index_file_list_filter_compat
63}
64
9673b8c3 65# Set COMP_WORDS in a way that can be handled by the bash script.
ce45ea6a 66COMP_WORDS=(\$2)
9673b8c3
MK
67
68# The cursor is at the end of parameter #1.
69# We must check for a space as the last character which will
70# tell us that the previous word is complete and the cursor
71# is on the next word.
ce45ea6a 72if [ "\${2: -1}" == " " ]; then
75ed918b
MK
73 # The last character is a space, so our location is at the end
74 # of the command-line array
75 COMP_CWORD=\${#COMP_WORDS[@]}
9673b8c3 76else
75ed918b
MK
77 # The last character is not a space, so our location is on the
78 # last word of the command-line array, so we must decrement the
79 # count by 1
80 COMP_CWORD=\$((\${#COMP_WORDS[@]}-1))
9673b8c3
MK
81fi
82
ff7b83f5
TI
83# Call __git_wrap__git_main() or __git_wrap__gitk_main() of the bash script,
84# based on the first argument
85__git_wrap__\${1}_main
9673b8c3
MK
86
87IFS=\$'\n'
92f1c042 88if [ \${#COMPREPLY[*]} -eq 0 ]; then
75ed918b
MK
89 # No completions suggested. In this case, we want tcsh to perform
90 # standard file completion. However, there does not seem to be way
91 # to tell tcsh to do that. To help the user, we try to simulate
92 # file completion directly in this script.
93 #
94 # Known issues:
95 # - Possible completions are shown with their directory prefix.
96 # - Completions containing shell variables are not handled.
97 # - Completions with ~ as the first character are not handled.
98
99 # No file completion should be done unless we are completing beyond
100 # the git sub-command. An improvement on the bash completion :)
101 if [ \${COMP_CWORD} -gt 1 ]; then
102 TO_COMPLETE="\${COMP_WORDS[\${COMP_CWORD}]}"
103
104 # We don't support ~ expansion: too tricky.
105 if [ "\${TO_COMPLETE:0:1}" != "~" ]; then
106 # Use ls so as to add the '/' at the end of directories.
92f1c042 107 COMPREPLY=(\`ls -dp \${TO_COMPLETE}* 2> /dev/null\`)
75ed918b
MK
108 fi
109 fi
110fi
111
92f1c042
MK
112# tcsh does not automatically remove duplicates, so we do it ourselves
113echo "\${COMPREPLY[*]}" | sort | uniq
114
115# If there is a single completion and it is a directory, we output it
116# a second time to trick tcsh into not adding a space after it.
117if [ \${#COMPREPLY[*]} -eq 1 ] && [ "\${COMPREPLY[0]: -1}" == "/" ]; then
118 echo "\${COMPREPLY[*]}"
119fi
120
9673b8c3
MK
121EOF
122
75ed918b
MK
123# Don't need this variable anymore, so don't pollute the users environment
124unset __git_tcsh_completion_original_script
125
126complete git 'p,*,`bash ${__git_tcsh_completion_script} git "${COMMAND_LINE}"`,'
127complete gitk 'p,*,`bash ${__git_tcsh_completion_script} gitk "${COMMAND_LINE}"`,'