Skip to contentSkip to conversation

Add tcsh-completion support to contrib by using git-completion.bash

PATCH In review Marc Khouzam 4 participants 39 messages Jun 15, 2016 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CAFj1UpE6OtJEojaED1_DZJD0kU=nVsFE_w8xa0oJE-6auCU2rw@mail.gmail.com

#1 lore raw

PATCH 1/1 Add tcsh-completion support to contrib by using git-completion.bash

Hi,

this patch allows tcsh-users to get the benefits of the awesome git-completion.bash script. It could also help other shells do the same.

==

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). 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 the user to explicitly make the script executable
when using tcsh (for tcsh users only)
4- 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.

==

With the changes applied, tcsh users should:

# 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

The code can be found on GitHub.
Option (A):
https://github.com/marckhouzam/git/commit/86d3a8e740ae85b4b4462c997a0fd969b1b2d24c

Option (B):
https://github.com/marckhouzam/git/commit/e64606541682eaf66c0a56aceff279ca6e1d06cd

Option (C):
https://github.com/marckhouzam/git/commit/59792455f1e6a98d3ffeb828f4cff1ded0e4ed37

Thanks

Marc

---
 contrib/completion/git-completion.bash |   53 +++++++++++++++++++++++++++++++-
 contrib/completion/git-completion.tcsh |   34 ++++++++++++++++++++
 2 files changed, 86 insertions(+), 1 deletions(-)
 create mode 100755 contrib/completion/git-completion.tcsh
(quoted diff) +0 -0

b/contrib/completion/git-completion.bash
index be800e0..6d4b57a 100644

contrib/completion/git-completion.bash +27 -1
@@ -1,4 +1,6 @@
-#!bash
+#!/bin/bash
+# The above line is important as this script can be executed when used
+# with another shell such as tcsh
 #
 # bash/zsh completion support for core Git.
 #
@@ -2481,3 +2483,52 @@ __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 to the command-line as bash would.
+       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]}

+
+ # Print the result that is stored in the bash variable ${COMPREPLY}
+ for i in ${COMPREPLY[@]}; do
+ echo "$i"
+ done
+}
+
+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

(quoted diff) +0 -0

b/contrib/completion/git-completion.tcsh
new file mode 100755
index 0000000..7b7baea

contrib/completion/git-completion.tcsh new file +13 -0
@@ -0,0 +1,34 @@
+#!tcsh
+#
+# tcsh completion support for core Git.
+#
+# Copyright (C) 2012 Marc Khouzam <[email protected]>
+# 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
+
+# Make the script executable if it is not
+if ( ! -x ${__git_tcsh_completion_script} ) then
+ chmod u+x ${__git_tcsh_completion_script}
+endif
+
+complete git 'p/*/${__git_tcsh_completion_script} "${COMMAND_LINE}" | sort | uniq/'
+complete gitk 'p/*/${__git_tcsh_completion_script} "${COMMAND_LINE}" | sort | uniq/'
+
--
1.7.0.4

#2 lore raw

Hi,

Quoted 1 line · Marc Khouzam

Hi,

[...]

Quoted 1 line

[...]

Quoted 9 lines

Thanks

Marc

---
 contrib/completion/git-completion.bash |   53 +++++++++++++++++++++++++++++++-
 contrib/completion/git-completion.tcsh |   34 ++++++++++++++++++++
 2 files changed, 86 insertions(+), 1 deletions(-)
 create mode 100755 contrib/completion/git-completion.tcsh

Please have a look at Documentation/SubmittingPatches to see how to properly format the commit message, i.e. no greeting and sign-off in the commit message part, and the S-o-b line should be the last before the '---'.

Your patch seems to be severely line-wrapped. That document also contains a few MUA-specific tips to help avoid that.

Other than that, it's a good description of the changes and considerations. I agree that this approach seems to be the best from the three.

contrib/completion/git-completion.bash line 3

Quoted 10 lines
contrib/completion/git-completion.bash +3 -1
index be800e0..6d4b57a 100644
@@ -1,4 +1,6 @@
-#!bash
+#!/bin/bash
+# The above line is important as this script can be executed when used
+# with another shell such as tcsh

See comment near the end.

Quoted 10 lines
contrib/completion/git-completion.bash +3 -0
 #
 # bash/zsh completion support for core Git.
 #
@@ -2481,3 +2483,52 @@ __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

contrib/completion/git-completion.bash __git_complete gitk __gitk_main · line 2498

Quoted 10 lines
contrib/completion/git-completion.bash +10 -0
+# 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 to the command-line as bash would.
+       COMP_WORDS=($1)

That comment is only true for older Bash versions. Since v4 Bash splits the command line at characters that the readline library treats as word separators when performing word completion. But the completion script has functions to deal with both, so this shouldn't be a problem.

Quoted 19 lines
contrib/completion/git-completion.bash +11 -0
+
+       # 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

contrib/completion/git-completion.bash +4 -0
+                       # 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

contrib/completion/git-completion.bash

Quoted 10 lines
contrib/completion/git-completion.bash +10 -0
+                       # 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]}
+
+       # Print the result that is stored in the bash variable ${COMPREPLY}

Really? ;)

I like the above comments about setting COMP_CWORD, because they explain why you do what you do, which would be otherwise difficult to figure out. But telling that an echo in a for loop over an array prints that array is, well, probably not necessary.

contrib/completion/git-completion.bash

Quoted 3 lines
contrib/completion/git-completion.bash +3 -0
+       for i in ${COMPREPLY[@]}; do
+               echo "$i"
+       done

There is no need for the loop here to print the array one element per line:

        local IFS=$'\n'
        echo "${COMPREPLY[*]}"

contrib/completion/git-completion.bash

Quoted 6 lines
contrib/completion/git-completion.bash +6 -0
+}
+
+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"

Where does the second argument come from? Below you run this script as '${__git_tcsh_completion_script} "${COMMAND_LINE}"', i.e. $2 is never set. Am I missing something?

Quoted 34 lines
contrib/completion/git-completion.bash +1 -0
+fi
contrib/completion/git-completion.tcsh new file +13 -0
new file mode 100755
index 0000000..7b7baea
@@ -0,0 +1,34 @@
+#!tcsh
+#
+# tcsh completion support for core Git.
+#
+# Copyright (C) 2012 Marc Khouzam <[email protected]>
+# 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.

contrib/completion/git-completion.tcsh new file +9 -0
+#    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."

contrib/completion/git-completion.tcsh

Quoted 7 lines
contrib/completion/git-completion.tcsh new file +7 -0
+       exit
+endif
+
+# Make the script executable if it is not
+if ( ! -x ${__git_tcsh_completion_script} ) then
+       chmod u+x ${__git_tcsh_completion_script}
+endif

Not sure about this. If I source a script to provide completion for a command, then I definitely don't expect it to change file permissions.

However, even if the git completion script is not executable, you can still run it with 'bash ${__git_tcsh_completion_script}'. This way neither the user would need to set permissions, not the script would need to set it behind the users back. Furthermore, this would also make changing the shebang line unnecessary.

Quoted 5 lines
contrib/completion/git-completion.tcsh new file +2 -0
+
+complete git  'p/*/`${__git_tcsh_completion_script} "${COMMAND_LINE}"

| sort | uniq`/'
+complete gitk 'p/*/${__git_tcsh_completion_script} "${COMMAND_LINE}" | sort | uniq/'

Is the 'sort | uniq' really necessary? After the completion function returns Bash automatically sorts the elements in COMPREPLY and removes any duplicates. Doesn't tcsh do the same? I have no idea about tcsh completion.

Does the git completion script returns any duplicates at all? Ambigious refs come to mind, but I just checked that refs completion, or rather 'git for-each-ref' (the command driving refs completion), is kind enough to make any ambigious ref names unique (i.e. a branch and a tag with the same name is listed as 'heads/name' and 'tags/name').

Thanks,
Gábor

#3 lore raw

Thanks for the review.

Quoted 21 lines · SZEDER Gábor

Hi,

Quoted 1 line · Marc Khouzam

Hi,

[...]

Quoted 1 line

[...]

Quoted 9 lines

Thanks

Marc

---
 contrib/completion/git-completion.bash |   53 +++++++++++++++++++++++++++++++-
 contrib/completion/git-completion.tcsh |   34 ++++++++++++++++++++
 2 files changed, 86 insertions(+), 1 deletions(-)
 create mode 100755 contrib/completion/git-completion.tcsh
Quoted 4 lines

Please have a look at Documentation/SubmittingPatches to see how to properly format the commit message, i.e. no greeting and sign-off in the commit message part, and the S-o-b line should be the last before the '---'.

Sorry about that, since I should have noticed it in the doc. I will do my best to address all submission issues mentioned when I post the next version of the patch.

Quoted 7 lines

Your patch seems to be severely line-wrapped. That document also contains a few MUA-specific tips to help avoid that.

Other than that, it's a good description of the changes and considerations. I agree that this approach seems to be the best from the three.

Quoted 12 lines

contrib/completion/git-completion.bash line 3

Quoted 10 lines
contrib/completion/git-completion.bash +3 -1
index be800e0..6d4b57a 100644
@@ -1,4 +1,6 @@
-#!bash
+#!/bin/bash
+# The above line is important as this script can be executed when used
+# with another shell such as tcsh

See comment near the end.

Great suggestion below. I removed the above change.

Quoted 8 lines

contrib/completion/git-completion.bash __git_complete gitk __gitk_main · line 2498

Quoted 2 lines
contrib/completion/git-completion.bash +2 -0
+       # Set COMP_WORDS to the command-line as bash would.
+       COMP_WORDS=($1)

That comment is only true for older Bash versions. Since v4 Bash splits the command line at characters that the readline library treats as word separators when performing word completion. But the completion script has functions to deal with both, so this shouldn't be a problem.

I've updated the comment to be more general and left the code the same since it is supported by the script.

Quoted 4 lines

contrib/completion/git-completion.bash line 6

Quoted 2 lines
contrib/completion/git-completion.bash +1 -0
+       # Print the result that is stored in the bash variable ${COMPREPLY}

Really? ;)

Removed :)

Quoted 9 lines

contrib/completion/git-completion.bash

Quoted 3 lines
contrib/completion/git-completion.bash +3 -0
+       for i in ${COMPREPLY[@]}; do
+               echo "$i"
+       done

There is no need for the loop here to print the array one element per line:

        local IFS=$'\n'
        echo "${COMPREPLY[*]}"

Better. Thanks.

Quoted 8 lines

contrib/completion/git-completion.bash

Quoted 4 lines
contrib/completion/git-completion.bash +4 -0
+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"

Where does the second argument come from? Below you run this script as '${__git_tcsh_completion_script} "${COMMAND_LINE}"', i.e. $2 is never set. Am I missing something?

This second argument is optional and, if present, will be put in $COMP_CWORD. If not present, $COMP_CWORD must be computed
from $1. Also see comment above _git_complete_with_output (). tcsh does not provide me with this information, so I cannot make use of it. However, I thought it would be more future-proof to allow it for other shells which may have that information.

It is not necessary for tcsh, so I can remove if you prefer?

Quoted 8 lines

contrib/completion/git-completion.bash

Quoted 4 lines
contrib/completion/git-completion.bash +4 -0
+# Make the script executable if it is not
+if ( ! -x ${__git_tcsh_completion_script} ) then
+       chmod u+x ${__git_tcsh_completion_script}
+endif

Not sure about this. If I source a script to provide completion for a command, then I definitely don't expect it to change file permissions.

Quoted 5 lines

However, even if the git completion script is not executable, you can still run it with 'bash ${__git_tcsh_completion_script}'. This way neither the user would need to set permissions, not the script would need to set it behind the users back. Furthermore, this would also make changing the shebang line unnecessary.

Very nice! Done.

Quoted 9 lines
Quoted 4 lines

+complete git 'p/*/${__git_tcsh_completion_script} "${COMMAND_LINE}" | sort | uniq/'
+complete gitk 'p/*/${__git_tcsh_completion_script} "${COMMAND_LINE}" | sort | uniq/'

Is the 'sort | uniq' really necessary? After the completion function returns Bash automatically sorts the elements in COMPREPLY and removes any duplicates. Doesn't tcsh do the same? I have no idea about tcsh completion.

On my machine, tcsh does not remove duplicates. It does sort the results but that is done after I've run 'uniq', which is too late. I'm not happy about this
either, but the other option is to improve git-completion.bash to avoid duplicates,
which seemed less justified.

Quoted 1 line

Does the git completion script returns any duplicates at all?

It does. 'help' is returned twice for example.
Also, when completing 'git checkout ' in the git repo, I can see multiple 'todo' branches, as well as 'master', 'pu', 'next', etc.

You can actually try it without tcsh by running my proposed version of git-completion.bash like this:

cd git/contrib/completion
bash git-completion.bash "git checkout " | sort | uniq --repeated

Quoted 4 lines

Ambigious refs come to mind, but I just checked that refs completion, or rather 'git for-each-ref' (the command driving refs completion), is kind enough to make any ambigious ref names unique (i.e. a branch and a tag with the same name is listed as 'heads/name' and 'tags/name').

I will post a new version of the patch after looking at Felipe's patch for zsh, which I was not aware of.

Thanks!

Marc

#4 lore raw

Hi,

Quoted 9 lines · Marc Khouzam
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+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"

Where does the second argument come from? Below you run this script as '${__git_tcsh_completion_script} "${COMMAND_LINE}"', i.e. $2 is never set. Am I missing something?

Quoted 8 lines

This second argument is optional and, if present, will be put in $COMP_CWORD. If not present, $COMP_CWORD must be computed
from $1. Also see comment above _git_complete_with_output (). tcsh does not provide me with this information, so I cannot make use of it. However, I thought it would be more future-proof to allow it for other shells which may have that information.

It is not necessary for tcsh, so I can remove if you prefer?

I see. I read those comments and understood what it is about. I was just surprised that the code is there to make use of it, yet it's not specified when invoking that function.

Since it's a trivial piece of code, I would say let's keep it. Could you please add a sentence about it (that it's for possible future users and it's not used at the moment) to the commit message for future reference?

Quoted 10 lines
Quoted 9 lines
Quoted 4 lines

+complete git 'p/*/${__git_tcsh_completion_script} "${COMMAND_LINE}" | sort | uniq/'
+complete gitk 'p/*/${__git_tcsh_completion_script} "${COMMAND_LINE}" | sort | uniq/'

Is the 'sort | uniq' really necessary? After the completion function returns Bash automatically sorts the elements in COMPREPLY and removes any duplicates. Doesn't tcsh do the same? I have no idea about tcsh completion.

Quoted 6 lines

On my machine, tcsh does not remove duplicates. It does sort the results but that is done after I've run 'uniq', which is too late. I'm not happy about this
either, but the other option is to improve git-completion.bash to avoid duplicates,
which seemed less justified.

Ok. Then keep it for the time being, and we'll see what we can do to avoid those duplicates.

Quoted 3 lines
Quoted 1 line

Does the git completion script returns any duplicates at all?

It does. 'help' is returned twice for example.

Right. Now that you mentioned it, I remember I noticed it a while ago, too. I even wrote a patch to fix it, but not sure what became of it. Will try to dig it up.

Quoted 8 lines

Also, when completing 'git checkout ' in the git repo, I can see multiple 'todo' branches, as well as 'master', 'pu', 'next', etc.

You can actually try it without tcsh by running my proposed version of git-completion.bash like this:

cd git/contrib/completion
bash git-completion.bash "git checkout " | sort | uniq --repeated

Interesting, I can't reproduce. Are the duplicates also there, if you start a bash, source git-completion.bash, and run __git_refs ?

#5 lore raw

PATCH 1/1 completion: remove 'help' duplicate from porcelain commands

The list of all git commands is computed from the output of 'git help -a', which already includes 'help', so there is no need to explicitly add it once more when computing the list of porcelain commands.

Note that 'help' wasn't actually offered twice because of this, because Bash filters duplicates from possible completion words.

---

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Does the git completion script returns any duplicates at all?

It does. 'help' is returned twice for example.

Right. Now that you mentioned it, I remember I noticed it a while ago, too. I even wrote a patch to fix it, but not sure what became of it. Will try to dig it up.

Here it is. It turns out I wrote it in May this year, but according to gmane and my mailbox never sent it out.

 contrib/completion/git-completion.bash | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
contrib/completion/git-completion.bash +1 -1
index bc0657a2..b7b1a834 100644
@@ -585,7 +585,7 @@ __git_list_porcelain_commands ()
 {
 	local i IFS=" "$'\n'
 	__git_compute_all_commands
-	for i in "help" $__git_all_commands
+	for i in $__git_all_commands
 	do
 		case $i in
 		*--*)             : helper pattern;;
Signature · 1 line
1.8.0.128.g441b4b3
#6 lore raw
In reply to SZEDER Gábor
Quoted 21 lines · SZEDER Gábor

Hi,

Quoted 9 lines · Marc Khouzam
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+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"

Where does the second argument come from? Below you run this script as '${__git_tcsh_completion_script} "${COMMAND_LINE}"', i.e. $2 is never set. Am I missing something?

Quoted 8 lines

This second argument is optional and, if present, will be put in $COMP_CWORD. If not present, $COMP_CWORD must be computed
from $1. Also see comment above _git_complete_with_output (). tcsh does not provide me with this information, so I cannot make use of it. However, I thought it would be more future-proof to allow it for other shells which may have that information.

It is not necessary for tcsh, so I can remove if you prefer?

Quoted 8 lines

I see. I read those comments and understood what it is about. I was just surprised that the code is there to make use of it, yet it's not specified when invoking that function.

Since it's a trivial piece of code, I would say let's keep it. Could you please add a sentence about it (that it's for possible future users and it's not used at the moment) to the commit message for future reference?

Will do.

Quoted 17 lines
Quoted 10 lines
Quoted 9 lines
Quoted 4 lines

+complete git 'p/*/${__git_tcsh_completion_script} "${COMMAND_LINE}" | sort | uniq/'
+complete gitk 'p/*/${__git_tcsh_completion_script} "${COMMAND_LINE}" | sort | uniq/'

Is the 'sort | uniq' really necessary? After the completion function returns Bash automatically sorts the elements in COMPREPLY and removes any duplicates. Doesn't tcsh do the same? I have no idea about tcsh completion.

Quoted 6 lines

On my machine, tcsh does not remove duplicates. It does sort the results but that is done after I've run 'uniq', which is too late. I'm not happy about this
either, but the other option is to improve git-completion.bash to avoid duplicates,
which seemed less justified.

Quoted 2 lines

Ok. Then keep it for the time being, and we'll see what we can do to avoid those duplicates.

Thanks.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Does the git completion script returns any duplicates at all?

It does. 'help' is returned twice for example.

Right. Now that you mentioned it, I remember I noticed it a while ago, too. I even wrote a patch to fix it, but not sure what became of it. Will try to dig it up.

Thanks for already posting the patch.

Quoted 11 lines
Quoted 8 lines

Also, when completing 'git checkout ' in the git repo, I can see multiple 'todo' branches, as well as 'master', 'pu', 'next', etc.

You can actually try it without tcsh by running my proposed version of git-completion.bash like this:

cd git/contrib/completion
bash git-completion.bash "git checkout " | sort | uniq --repeated

Interesting, I can't reproduce. Are the duplicates also there, if you start a bash, source git-completion.bash, and run __git_refs ?

Running __git_refs does not show the duplicates, but running __git refs '' 1
does show them.
That second parameter causes __git_refs to
"use the guess heuristic employed by checkout for tracking branches"

I don't quite understand this, but what I can see is that my remote branches GitHub/master and origin/master each cause another 'master' to be listed:

$ __git_refs '' 1|grep master
master
GitHub/master
origin/master
master
master

All fixes are done and I'll post a second version of the patch as soon as I can figure out the formatting properly.

Thanks again

Marc

#7 lore raw

PATCH 1/1 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 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.

---

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
contrib/completion/git-completion.bash +47 -0
index be800e0..d71a016 100644
@@ -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
contrib/completion/git-completion.tcsh new file +29 -0
new file mode 100644
index 0000000..6096ea8
@@ -0,0 +1,29 @@
+#!tcsh
+#
+# tcsh completion support for core Git.
+#
+# Copyright (C) 2012 Marc Khouzam <[email protected]>
+# 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`/'
+
Signature · 1 line
1.7.0.4
#8 lore raw

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

Quoted 10 lines · Marc Khouzam

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.

Quoted 5 lines

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

A) Modifications:
git-completion.bash and new git-completion.tcsh

As I said, I don't think this is needed. It can be done in a single stand-alone script without modifications to git-completion.bash.

This works:

set called = ($_)
set script = "${called[2]}.tmp"

cat <<\EOF > $script
source "$HOME/.git-completion.sh"

# 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]}

IFS=$'\n'
echo "${COMPREPLY[*]}"
\EOF

complete git  'p/*/`bash ${script} "${COMMAND_LINE}" | sort | uniq`/'
complete gitk 'p/*/`bash ${script} "${COMMAND_LINE}" | sort | uniq`/'
Signature · 1 line
Felipe Contreras
#9 lore raw

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

Quoted 14 lines · Felipe Contreras
Quoted 13 lines · Marc Khouzam

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:

Quoted 7 lines
Quoted 2 lines

A) Modifications:
git-completion.bash and new git-completion.tcsh

As I said, I don't think this is needed. It can be done in a single stand-alone script without modifications to git-completion.bash.

This works:

Thank you for taking the time to try things out.

What you suggest below is an improvement on solution (C). I had chosen (A) instead because (C) creates a third script which gets generated each time a new shell is started. It should be safe, but it felt a little wrong.
But I have to admit I was on the fence between the two solutions.

If you guys don't think it is bad to generate a third script (that the user may notice in his ${HOME}),
I'll post a new patch (and try once more to get gmail not to replace the tabs with spaces), using your improved solution (C).

Quoted 1 line

set called = ($_)

I fought with this a lot before posting to the list. It seems that $_ is not set when a double sourcing happens. Testing the solution as an actual user showed me that when I start a new shell it
sources ~/.tcshrc, which then sources ~/.git-completion.tcsh and then $_ is empty for some reason.

I couldn't find another way to figure out where the script is located, which is why I had to force the user to use ${HOME} for everything.

Quoted 4 lines

set script = "${called[2]}.tmp"

cat <<\EOF > $script
source "$HOME/.git-completion.sh"

This is nice. Shame on me not to have thought about it. In my version I actually 'cat' the entire bash script into $script instead of simply sourcing it.

Quoted 7 lines

# 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

Since this code will be part of a tcsh-only script, I don't think we need to prepare for a possible $2. tcsh won't provide it. So, I'll remove that logic, which will simplify things slightly.

Quoted 20 lines

# 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]}
Quoted 6 lines

IFS=$'\n'
echo "${COMPREPLY[*]}"
\EOF

complete git  'p/*/`bash ${script} "${COMMAND_LINE}" | sort | uniq`/'
complete gitk 'p/*/`bash ${script} "${COMMAND_LINE}" | sort | uniq`/'

I am worried about 'sort' and 'uniq' being aliased by the user, so I was thinking of using '\sort | \uniq'

I'll work on the new version of the solution.

Thanks again

Marc

#10 lore raw

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

Quoted 22 lines · Marc Khouzam
Quoted 20 lines · Felipe Contreras
Quoted 10 lines · Marc Khouzam

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.

Quoted 5 lines

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

A) Modifications:
git-completion.bash and new git-completion.tcsh

As I said, I don't think this is needed. It can be done in a single stand-alone script without modifications to git-completion.bash.

Quoted 7 lines
Quoted 1 line

This works:

Thank you for taking the time to try things out.

What you suggest below is an improvement on solution (C). I had chosen (A) instead because (C) creates a third script which gets generated each time a new shell is started.

We could generate the script only when it's not already present. The disadvantage is that if this script is updated, the helper one would not. One way to solve the problem would be to append the current version of git, and figure a way to query it out. Another would be to checksum it. But then again, maybe it's more expensive to check the version or checksum than just write the file again.

Is it possible to just check if this is a login shell?

Quoted 9 lines
Quoted 1 line

set called = ($_)

I fought with this a lot before posting to the list. It seems that $_ is not set when a double sourcing happens. Testing the solution as an actual user showed me that when I start a new shell it
sources ~/.tcshrc, which then sources ~/.git-completion.tcsh and then $_ is empty for some reason.

Quoted 3 lines

I couldn't find another way to figure out where the script is located, which is why I had to force the user to use ${HOME} for everything.

Ah :(

Signature · 1 line
Felipe Contreras
#11 lore raw

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

Quoted 27 lines · Felipe Contreras
Quoted 26 lines · Marc Khouzam
Quoted 14 lines · Felipe Contreras
Quoted 13 lines · Marc Khouzam

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:

Quoted 7 lines
Quoted 2 lines

A) Modifications:
git-completion.bash and new git-completion.tcsh

As I said, I don't think this is needed. It can be done in a single stand-alone script without modifications to git-completion.bash.

This works:

Thank you for taking the time to try things out.

Quoted 7 lines
Quoted 3 lines

What you suggest below is an improvement on solution (C). I had chosen (A) instead because (C) creates a third script which gets generated each time a new shell is started.

We could generate the script only when it's not already present. The disadvantage is that if this script is updated, the helper one would not.

I didn't like that too much either.

Quoted 4 lines

One way to solve the problem would be to append the current version of git, and figure a way to query it out. Another would be to checksum it. But then again, maybe it's more expensive to check the version or checksum than just write the file again.

Yeah, I'm also thinking that re-generating the script is not bad enough to introduce this complexity.

Quoted 1 line

Is it possible to just check if this is a login shell?

I think it would be nice to allow the user to manually source git-completion.tcsh, in case they want to make manual modifications to it.

I think the most user-friendly option is to actually re-generate the script each time. It feels wrong, but it works well :)

Quoted 17 lines
Quoted 9 lines
Quoted 1 line

set called = ($_)

I fought with this a lot before posting to the list. It seems that $_ is not set when a double sourcing happens. Testing the solution as an actual user showed me that when I start a new shell it
sources ~/.tcshrc, which then sources ~/.git-completion.tcsh and then $_ is empty for some reason.

Quoted 3 lines

I couldn't find another way to figure out where the script is located, which is why I had to force the user to use ${HOME} for everything.

Ah :(

--
Felipe Contreras

#12 lore raw

PATCH v3 1/1 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.

---

Here's another version which adds contrib/completion/git-completion.tcsh but does not modify contrib/completion/git-completion.bash at all.

(Sorry, but I still can't get gmail to stop changing the tabs for spaces)

Thanks in advance.

Marc

 contrib/completion/git-completion.tcsh | 63 ++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
 create mode 100644 contrib/completion/git-completion.tcsh
(quoted diff) +0 -0

b/contrib/completion/git-completion.tcsh
new file mode 100644
index 0000000..76395f9

contrib/completion/git-completion.tcsh new file +62 -0

62 lines not on this page · raw message

"${COMMAND_LINE}" | \sort | \uniq`/'
+complete gitk 'p/*/bash ${__git_tcsh_completion_script} "${COMMAND_LINE}" | \sort | \uniq/'
--
1.8.0.1.g9fe2839

#13 lore raw

Re: [PATCH v3] tcsh-completion re-using git-completion.bash

Quoted 69 lines · Marc Khouzam
contrib/completion/git-completion.tcsh new file +62 -0
new file mode 100644
index 0000000..76395f9
@@ -0,0 +1,63 @@
+#!tcsh
+#
+# tcsh completion support for core Git.
+#
+# Copyright (C) 2012 Marc Khouzam <[email protected]>
+# Distributed under the GNU General Public License, version 2.0.
+#
+# When sourced, this script will generate a new script that uses
+# the git-completion.bash script provided by core Git.  This new
+# script can be used by tcsh to perform git completion.
+# The current script also issues the necessary tcsh 'complete'
+# commands.
+#
+# To use this completion script:
+#
+#    1) Copy both this file and the bash completion script to ${HOME}.
+#       You _must_ use the name ${HOME}/.git-completion.bash for the
+#       bash script.
+#       (e.g. ~/.git-completion.tcsh and ~/.git-completion.bash).
+#    2) Add the following line to your .tcshrc/.cshrc:
+#        source ~/.git-completion.tcsh
+
+set __git_tcsh_completion_original_script = ${HOME}/.git-completion.bash
+set __git_tcsh_completion_script = ${HOME}/.git-completion.tcsh.bash
+
+cat << EOF > ${__git_tcsh_completion_script}
+#!bash
+#
+# This script is GENERATED and will be overwritten automatically.
+# Do not modify it directly.  Instead, modify the git-completion.tcsh
+# script provided by Git core.
+#
+
+source ${__git_tcsh_completion_original_script}
+
+# Set COMP_WORDS in a way that can be handled by the bash script.
+COMP_WORDS=(\$1)
+
+# 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
+
+# Call _git() or _gitk() of the bash script, based on the first
+# element of the command-line
+_\${COMP_WORDS[0]}
+
+IFS=\$'\n'
+echo "\${COMPREPLY[*]}"
+EOF
+
+complete git  'p/*/`bash ${__git_tcsh_completion_script}
Quoted 3 lines

"${COMMAND_LINE}" | \sort | \uniq`/'
+complete gitk 'p/*/bash ${__git_tcsh_completion_script} "${COMMAND_LINE}" | \sort | \uniq/'

This looks good to me. Except that maybe the sort and uniq can be moved to inside the script.

Signature · 1 line
Felipe Contreras
#14 lore raw

PATCH v4 1/1 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.

---

As suggested, I put the 'sort | uniq' inside the script.
In that case, I don't need to worry about aliases since 'sort |uniq' will be executed in bash, for which the tcsh user surely doesn't have aliases setup.

Thanks

Marc

 contrib/completion/git-completion.tcsh | 63 ++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
 create mode 100644 contrib/completion/git-completion.tcsh
(quoted diff) +0 -0

b/contrib/completion/git-completion.tcsh
new file mode 100644
index 0000000..f0327fc

contrib/completion/git-completion.tcsh new file +63 -0

63 lines not on this page · raw message

--
1.8.0.1.g9fe2839

#15 lore raw

Re: [PATCH v4] tcsh-completion re-using git-completion.bash

Quoted 22 lines · Marc Khouzam

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.
...
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

Quoted 8 lines

Approach (C) was selected avoid any modification to git-completion.bash.

---

As suggested, I put the 'sort | uniq' inside the script.
In that case, I don't need to worry about aliases since 'sort |uniq' will be executed in bash, for which the tcsh user surely doesn't have aliases setup.

OK, so does this look OK to everybody (it does, looking at the difference between v3 and this one, to me)?

The patch may deserve a Reviewed-by: by Felipe, by the way. I can add one while applying.

Thanks.

#16 lore raw

Re: [PATCH v4] tcsh-completion re-using git-completion.bash

Quoted 33 lines · Junio C Hamano
Quoted 22 lines · Marc Khouzam

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.
...
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

Quoted 8 lines

Approach (C) was selected avoid any modification to git-completion.bash.

---

As suggested, I put the 'sort | uniq' inside the script.
In that case, I don't need to worry about aliases since 'sort |uniq' will be executed in bash, for which the tcsh user surely doesn't have aliases setup.

Quoted 5 lines

OK, so does this look OK to everybody (it does, looking at the difference between v3 and this one, to me)?

The patch may deserve a Reviewed-by: by Felipe, by the way. I can add one while applying.

That's fine by me.

Cheers.

Signature · 1 line
Felipe Contreras
#17 lore raw

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

In reply to Marc Khouzam
Quoted 8 lines · Marc Khouzam
Quoted 1 line · Felipe Contreras

Is it possible to just check if this is a login shell?

I think it would be nice to allow the user to manually source git-completion.tcsh, in case they want to make manual modifications to it.

Yeah, they could still do that... because they would be running in a login shell.

What I meant is that if the user does: tcsh my_script_that_has_nothing_to_do_with_completion.sh, they would not be executing this whole script.

Quoted 2 lines

I think the most user-friendly option is to actually re-generate the script each time. It feels wrong, but it works well :)

I'm not too strongly opposed to add that function to the bash completion, but to do it only for tcsh doesn't sound right, specially when there are other alternatives. Correct me if I'm wrong, but very few people use tcsh.

Signature · 1 line
Felipe Contreras
#18 lore raw

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

Quoted 10 lines · Felipe Contreras
Quoted 8 lines · Marc Khouzam
Quoted 1 line · Felipe Contreras

Is it possible to just check if this is a login shell?

I think it would be nice to allow the user to manually source git-completion.tcsh, in case they want to make manual modifications to it.

Quoted 6 lines

Yeah, they could still do that... because they would be running in a login shell.

What I meant is that if the user does: tcsh my_script_that_has_nothing_to_do_with_completion.sh, they would not be executing this whole script.

Oh, I see now.

I can put a check in the script for the existence of the $prompt variable. This will indicate if it is a login shell or not.
However, a good .cshrc file should already have such a check to avoid sourcing a bunch of useless things. So, I personally think that we should not add it to the git-completion.tcsh script but let the tcsh user decide to do it herself. But I don't mind being overruled :)

Quoted 6 lines
Quoted 2 lines

I think the most user-friendly option is to actually re-generate the script each time. It feels wrong, but it works well :)

I'm not too strongly opposed to add that function to the bash completion, but to do it only for tcsh doesn't sound right, specially when there are other alternatives.

I agree, and this is why I made the proposed __git_complete_with_output () generic. That way it could be used by other shells or programs. But at this time, only tcsh would make use of it.

If you think having __git_complete_with_output () could be useful for others, I think we should go with solution (A). If you don't think so, or if it is better to wait until a need arises first, then solution (C) will work fine.

Quoted 1 line

Correct me if I'm wrong, but very few people use tcsh.

Less than I originally thought, when I started working on this patch :-\ But I'm still hoping that the those people will be a little happier with their git completion.

Marc

Quoted 3 lines

--
Felipe Contreras

#19 lore raw

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

Quoted 19 lines · Marc Khouzam
Quoted 10 lines · Felipe Contreras
Quoted 8 lines · Marc Khouzam
Quoted 1 line · Felipe Contreras

Is it possible to just check if this is a login shell?

I think it would be nice to allow the user to manually source git-completion.tcsh, in case they want to make manual modifications to it.

Quoted 6 lines

Yeah, they could still do that... because they would be running in a login shell.

What I meant is that if the user does: tcsh my_script_that_has_nothing_to_do_with_completion.sh, they would not be executing this whole script.

Quoted 8 lines

Oh, I see now.

I can put a check in the script for the existence of the $prompt variable. This will indicate if it is a login shell or not.
However, a good .cshrc file should already have such a check to avoid sourcing a bunch of useless things. So, I personally think that we should not add it to the git-completion.tcsh script but let the tcsh user decide to do it herself. But I don't mind being overruled :)

Sounds sensible to me.

Quoted 12 lines
Quoted 6 lines
Quoted 2 lines

I think the most user-friendly option is to actually re-generate the script each time. It feels wrong, but it works well :)

I'm not too strongly opposed to add that function to the bash completion, but to do it only for tcsh doesn't sound right, specially when there are other alternatives.

I agree, and this is why I made the proposed __git_complete_with_output () generic. That way it could be used by other shells or programs. But at this time, only tcsh would make use of it.

Quoted 4 lines

If you think having __git_complete_with_output () could be useful for others, I think we should go with solution (A). If you don't think so, or if it is better to wait until a need arises first, then solution (C) will work fine.

I don't see how it could be useful to others, and if we find out that it could, we can always move the code.

Quoted 5 lines
Quoted 1 line

Correct me if I'm wrong, but very few people use tcsh.

Less than I originally thought, when I started working on this patch :-\ But I'm still hoping that the those people will be a little happier with their git completion.

I think they would :) But we don't need to modify bash's script for that (for now).

Cheers.

Signature · 1 line
Felipe Contreras
#20 lore raw

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

Quoted 9 lines · Felipe Contreras
Quoted 9 lines

I agree, and this is why I made the proposed __git_complete_with_output () generic. That way it could be used by other shells or programs. But at this time, only tcsh would make use of it.

If you think having __git_complete_with_output () could be useful for others, I think we should go with solution (A). If you don't think so, or if it is better to wait until a need arises first, then solution (C) will work fine.

I think it would be useful.

Quoted 2 lines

I don't see how it could be useful to others, and if we find out that it could, we can always move the code.

For zsh, perhaps?

As I understand the main issues with using the completion script with zsh are the various little incompatibilities between the two shells and bugs in zsh's emulation of Bash's completion-related builtins. Running the completion script under Bash and using its results in zsh would solve these issues at the root. And would allow as to remove some if [[ -n ${ZSH_VERSION-} ]] code.

#21 lore raw

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

Quoted 1 line · SZEDER Gábor

For zsh, perhaps?

Yeah, I was wondering about that.

If we make zsh completion read output from a little stub in bash completion, just like Felipe steered this series for tcsh, we do not have to worry about "zsh does not split words unless emulating a shell and here is a way to tell zsh to do so" kind of stuff in bash completion. The point is not about the quality of zsh's emulation of (k)sh when it is run under that mode, but is about not having to have that logic in bash-only part in the first place.

#22 lore raw

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

Quoted 11 lines · Junio C Hamano
Quoted 1 line · SZEDER Gábor

For zsh, perhaps?

Yeah, I was wondering about that.

If we make zsh completion read output from a little stub in bash completion, just like Felipe steered this series for tcsh, we do not have to worry about "zsh does not split words unless emulating a shell and here is a way to tell zsh to do so" kind of stuff in bash completion.

Do we worry about that now? If we do, the only reason is because we hadn't had a proper wrapper, like the one I'm proposing to merge. So, we had to put things inside if [[ -n ${ZSH_VERSION-} ]]. Those things would move to my wrapper.

The only exception where we had to change code outside of that chunk that I'm aware of is '8d58c90 completion: Use parse-options raw output for simple long options', which is probably fixed in later versions of zsh, and if not, we could always replace those functions inside my wrapper.

Quoted 3 lines

The point is not about the quality of zsh's emulation
of (k)sh when it is run under that mode, but is about not having to have that logic in bash-only part in the first place.

As I said, that logic can be moved away _if_ my wrapper is merged. But then again, that would cause regressions to existing users.

Maybe we should warn them right now that they should be using my wrapper, and that this method of zsh support would be obsoleted. But my wrapper probably hasn't received enough testing, so do we really want to do that right now?

Either way, I'm confident that whatever code we need can be consolidated in git-completion.zsh, even without having to run bash.

Cheers.

Signature · 1 line
Felipe Contreras
#23 lore raw

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

Quoted 8 lines · Felipe Contreras
Quoted 3 lines · Junio C Hamano

The point is not about the quality of zsh's emulation
of (k)sh when it is run under that mode, but is about not having to have that logic in bash-only part in the first place.

As I said, that logic can be moved away _if_ my wrapper is merged. But then again, that would cause regressions to existing users.

Please forgive me as I don't know the background of the efforts for zsh git-completion or
the syntax for zsh completion, but I thought I'd mention another approach I tried for tcsh
which may work for zsh.

I gather that using a wrapper for zsh causes concerns about backwards-compatibility.
So, what could be done is have the bash script do both jobs: setup the zsh completion
commands, and output the git completion using bash itself. At the top of git-completion.bash
(or it could be even pushed at the bottom using if/else) we could use:

if [[ -n ${ZSH_VERSION-} ]]; then

  # replace below by zsh completion commands calling `bash
${HOME}/.git-completion.bash`
  complete git   'p/*/`bash ${HOME}/.git-completion.bash ${COMMAND_LINE}`/'
  complete gitk 'p/*/`bash ${HOME}/.git-completion.bash ${COMMAND_LINE}`/'
  exit
fi

That way the zsh user would still simply do 'source ~/.git-completion.bash' which would
only execute the two zsh completion setup commands. Then, when completion is triggered, it calls bash ${HOME}/.git-completion.bash ${COMMAND_LINE} and processes the output like tcsh does. This limits the zsh-specific code to 2 lines for
the entire script.

I got this to work for tcsh (solution (B)) adding the following a the top of git-completion.bash:

test "$tcsh" != "" && \
complete git 'p,*,`${HOME}/.git-completion.sh
"${COMMAND_LINE}"|\sort|\uniq`,' && \
complete gitk 'p,*,`${HOME}/.git-completion.sh
"${COMMAND_LINE}"|\sort|\uniq`,' && \
exit

but I didn't think people would go for that since those lines have to work in both bash
and tcsh syntax. I thought this made the script a bit brittle.

Just a thought.

Marc

#24 lore raw

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

Quoted 11 lines · Marc Khouzam
Quoted 8 lines · Felipe Contreras
Quoted 3 lines · Junio C Hamano

The point is not about the quality of zsh's emulation
of (k)sh when it is run under that mode, but is about not having to have that logic in bash-only part in the first place.

As I said, that logic can be moved away _if_ my wrapper is merged. But then again, that would cause regressions to existing users.

Quoted 8 lines

Please forgive me as I don't know the background of the efforts for zsh git-completion or
the syntax for zsh completion, but I thought I'd mention another approach I tried for tcsh
which may work for zsh.

I gather that using a wrapper for zsh causes concerns about backwards-compatibility.

I don't see any concerns.

Quoted 6 lines

if [[ -n ${ZSH_VERSION-} ]]; then

  # replace below by zsh completion commands calling `bash
${HOME}/.git-completion.bash`
  complete git   'p/*/`bash ${HOME}/.git-completion.bash ${COMMAND_LINE}`/'
  complete gitk 'p/*/`bash ${HOME}/.git-completion.bash ${COMMAND_LINE}`/'

That doesn't work in zsh. It might be possible to do something similar, but it would probably require many more lines.

And we can achieve the same by essentially moving the relevant code of my wrapper:

contrib/completion/git-completion.bash +6 -4
@@ -23,10 +23,6 @@
 #    3) Consider changing your PS1 to also show the current branch,
 #       see git-prompt.sh for details.
 
-if [[ -n ${ZSH_VERSION-} ]]; then
-       autoload -U +X bashcompinit && bashcompinit
-fi
-
 case "$COMP_WORDBREAKS" in
 *:*) : great ;;
 *)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
@@ -2404,6 +2400,32 @@ __gitk_main ()
        __git_complete_revlist
 }
 
+if [[ -n ${ZSH_VERSION-} ]]; then
+       emulate -L zsh
+
+       __gitcompadd ()
+       {
+               compadd -Q -S "$4" -P "${(M)cur#*[=:]}" -p "$2" --

${=1} && _ret=0
+ }
+
+ _git ()
+ {
+ local _ret=1
+ () {
+ emulate -L ksh

+                       local cur cword prev
+                       cur=${words[CURRENT-1]}
+                       prev=${words[CURRENT-2]}
+                       let cword=CURRENT-1
+                       __${service}_main
+               }
+               let _ret && _default -S '' && _ret=0
+               return _ret
+       }
+       compdef _git git gitk
+       return
+fi
+
 __git_func_wrap ()
 {
        if [[ -n ${ZSH_VERSION-} ]]; then
Signature · 1 line
Felipe Contreras
#25 lore raw

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

Quoted 10 lines · Felipe Contreras
Quoted 3 lines

I gather that using a wrapper for zsh causes concerns about backwards-compatibility.

I don't see any concerns.

Quoted 3 lines

if [[ -n ${ZSH_VERSION-} ]]; then

  # replace below by zsh completion commands calling `bash
${HOME}/.git-completion.bash`
Quoted 5 lines
Quoted 2 lines
  complete git   'p/*/`bash ${HOME}/.git-completion.bash ${COMMAND_LINE}`/'
  complete gitk 'p/*/`bash ${HOME}/.git-completion.bash ${COMMAND_LINE}`/'

That doesn't work in zsh. It might be possible to do something similar, but it would probably require many more lines.

Hi,

since there doesn't seem to be an agreement that the approach to achieve tcsh git-completion would be useful for zsh (the other possible shell that could use it is ksh, but I haven't looked into that), maybe the simplest thing is to keep the
tcsh solution contained in a tcsh-only script. This is the latest solution as proposed here:

[1] http://www.mail-archive.com/[email protected]/msg12192.html

For reference, the more general solution was proposed here:
[2] http://www.mail-archive.com/[email protected]/msg12122.html

If there is interest in merging [1], please let me know and I'll post another version which adds a check to make sure that the user properly copied git-completion.bash to be used by the new git-completion.tcsh.

Thanks for your input.

Marc

#26 lore raw

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

Quoted 10 lines · Marc Khouzam

Hi,

since there doesn't seem to be an agreement that the approach to achieve tcsh git-completion would be useful for zsh (the other possible shell that could use it is ksh, but I haven't looked into that), maybe the simplest thing is to keep the
tcsh solution contained in a tcsh-only script. This is the latest solution as proposed here:

[1] http://www.mail-archive.com/[email protected]/msg12192.html

This one is already merged to 'next'.

Cheers.

Signature · 1 line
Felipe Contreras
#27 lore raw

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

Quoted 11 lines · Felipe Contreras
Quoted 9 lines · Marc Khouzam

Hi,

since there doesn't seem to be an agreement that the approach to achieve tcsh git-completion would be useful for zsh (the other possible shell that could use it is ksh, but I haven't looked into that), maybe the simplest thing is to keep the
tcsh solution contained in a tcsh-only script. This is the latest solution as proposed here:

Quoted 3 lines
Quoted 1 line

[1] http://www.mail-archive.com/[email protected]/msg12192.html

This one is already merged to 'next'.

Awesome! I didn't notice.

If I want to suggest an improvement (like checking if the bash script is available),
do I just post a patch here?

Thanks a lot for moving forward with this so quickly!

Marc

#28 lore raw

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

Quoted 6 lines · Marc Khouzam
Quoted 1 line

This one is already merged to 'next'.

Awesome! I didn't notice.

If I want to suggest an improvement (like checking if the bash script is available), do I just post a patch here?

Yes, as a follow-up patch (or two).

Thanks.

#29 lore raw

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

In reply to SZEDER Gábor
Quoted 6 lines · SZEDER Gábor
Quoted 5 lines · Felipe Contreras
Quoted 5 lines

I agree, and this is why I made the proposed __git_complete_with_output () generic. That way it could be used by other shells or programs. But at this time, only tcsh would make use of it.

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

If you think having __git_complete_with_output () could be useful for others, I think we should go with solution (A). If you don't think so, or if it is better to wait until a need arises first, then solution (C) will work fine.

I think it would be useful.

For what?

Quoted 4 lines
Quoted 2 lines

I don't see how it could be useful to others, and if we find out that it could, we can always move the code.

For zsh, perhaps?

Nope.

Quoted 6 lines

As I understand the main issues with using the completion script with zsh are the various little incompatibilities between the two shells and bugs in zsh's emulation of Bash's completion-related builtins. Running the completion script under Bash and using its results in zsh would solve these issues at the root. And would allow as to remove some if [[ -n ${ZSH_VERSION-} ]] code.

We can remove that code already, because we now have code that is superior than zsh's bash completion emulation:

http://article.gmane.org/gmane.comp.version-control.git/208173

This is the equivalent of what Marc is doing, except that zsh has no problems running bash's code. Note there's a difference with zsh's emulation bash (or rather bourne shell, or k shell), and zsh's emulation of bash's _completion_. The former is fine, the later is not.

Of course, people might not be aware of this new script, and would expect sourcing the bash one to work right away. Maybe at some point we might throw a warning to suggest them to use my new script. But I think we should wait a few releases just to make sure that people test it and nothing is broken.

Cheers.

Signature · 1 line
Felipe Contreras
#30 lore raw

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

Quoted 7 lines · Felipe Contreras
Quoted 6 lines · SZEDER Gábor
Quoted 5 lines · Felipe Contreras
Quoted 5 lines

I agree, and this is why I made the proposed __git_complete_with_output () generic. That way it could be used by other shells or programs. But at this time, only tcsh would make use of it.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

If you think having __git_complete_with_output () could be useful for others, I think we should go with solution (A). If you don't think so, or if it is better to wait until a need arises first, then solution (C) will work fine.

I think it would be useful.

For what?

For zsh.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

I don't see how it could be useful to others, and if we find out that it could, we can always move the code.

For zsh, perhaps?

Nope.

Sure.

Quoted 11 lines
Quoted 6 lines

As I understand the main issues with using the completion script with zsh are the various little incompatibilities between the two shells and bugs in zsh's emulation of Bash's completion-related builtins. Running the completion script under Bash and using its results in zsh would solve these issues at the root. And would allow as to remove some if [[ -n ${ZSH_VERSION-} ]] code.

We can remove that code already, because we now have code that is superior than zsh's bash completion emulation:

http://article.gmane.org/gmane.comp.version-control.git/208173

Which depends on the completion script having a wrapper function around compgen filling COMPREPLY. However, COMPREPLY will be soon filled by hand-rolled code to prevent expansion issues with compgen, and there will be no such wrapper.

Quoted 5 lines

This is the equivalent of what Marc is doing, except that zsh has no problems running bash's code. Note there's a difference with zsh's emulation bash (or rather bourne shell, or k shell), and zsh's emulation of bash's _completion_. The former is fine, the later is not.

There are a couple of constructs supported by Bash but not by zsh, which we usually try to avoid.

#31 lore raw

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

Quoted 9 lines · SZEDER Gábor
Quoted 7 lines · Felipe Contreras
Quoted 6 lines

As I understand the main issues with using the completion script with zsh are the various little incompatibilities between the two shells and bugs in zsh's emulation of Bash's completion-related builtins. Running the completion script under Bash and using its results in zsh would solve these issues at the root. And would allow as to remove some if [[ -n ${ZSH_VERSION-} ]] code.

Quoted 7 lines
Quoted 4 lines

We can remove that code already, because we now have code that is superior than zsh's bash completion emulation:

http://article.gmane.org/gmane.comp.version-control.git/208173

Which depends on the completion script having a wrapper function around compgen filling COMPREPLY.

No, it does not. Previous incarnations didn't have this dependency:

http://article.gmane.org/gmane.comp.version-control.git/196720

I just thought it was neater this way.

Quoted 3 lines

However, COMPREPLY will be soon
filled by hand-rolled code to prevent expansion issues with compgen, and there will be no such wrapper.

I'm still waiting to see a resemblance of that code, but my bet would be that there will be a way to fill both COMPREPLY, and call zsh's compadd. But it's hard to figure that out without any code. Which is why I'm thinking on doing it myself.

But even in that case, if push comes to shoves, this zsh wrapper can ultimately read COMPREPLY and figure things backwards, as even more previous versions did:

http://article.gmane.org/gmane.comp.version-control.git/189310

Quoted 8 lines
Quoted 5 lines

This is the equivalent of what Marc is doing, except that zsh has no problems running bash's code. Note there's a difference with zsh's emulation bash (or rather bourne shell, or k shell), and zsh's emulation of bash's _completion_. The former is fine, the later is not.

There are a couple of constructs supported by Bash but not by zsh, which we usually try to avoid.

Yes, and is that a big deal?

Cheers.

Signature · 1 line
Felipe Contreras
#32 lore raw

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

Quoted 13 lines · Felipe Contreras
Quoted 12 lines · SZEDER Gábor
Quoted 10 lines · Felipe Contreras
Quoted 6 lines

As I understand the main issues with using the completion script with zsh are the various little incompatibilities between the two shells and bugs in zsh's emulation of Bash's completion-related builtins. Running the completion script under Bash and using its results in zsh would solve these issues at the root. And would allow as to remove some if [[ -n ${ZSH_VERSION-} ]] code.

We can remove that code already, because we now have code that is superior than zsh's bash completion emulation:

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

http://article.gmane.org/gmane.comp.version-control.git/208173

Which depends on the completion script having a wrapper function around compgen filling COMPREPLY.

No, it does not. Previous incarnations didn't have this dependency:

http://article.gmane.org/gmane.comp.version-control.git/196720

Good.

Quoted 9 lines
Quoted 3 lines

However, COMPREPLY will be soon
filled by hand-rolled code to prevent expansion issues with compgen, and there will be no such wrapper.

I'm still waiting to see a resemblance of that code, but my bet would be that there will be a way to fill both COMPREPLY, and call zsh's compadd. But it's hard to figure that out without any code. Which is why I'm thinking on doing it myself.

Quoted 5 lines

But even in that case, if push comes to shoves, this zsh wrapper can ultimately read COMPREPLY and figure things backwards, as even more previous versions did:

http://article.gmane.org/gmane.comp.version-control.git/189310

Even better. I was just going to propose that zsh's completion could just read the contents of COMPREPLY at the end of _git() and _gitk(), because this way no zsh-induced helper functions and changes would be needed to the completion script at all.

However, running the completion script with Bash would also prevent possible issues caused by incompatibilities between the two shells mentioned below.

Quoted 10 lines
Quoted 8 lines
Quoted 5 lines

This is the equivalent of what Marc is doing, except that zsh has no problems running bash's code. Note there's a difference with zsh's emulation bash (or rather bourne shell, or k shell), and zsh's emulation of bash's _completion_. The former is fine, the later is not.

There are a couple of constructs supported by Bash but not by zsh, which we usually try to avoid.

Yes, and is that a big deal?

Not that big, but I wanted to point out that it's not "fine" either. Just a slight maintenance burden, because we have to pay attention not to use such constructs.

#33 lore raw

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

Quoted 6 lines · SZEDER Gábor
Quoted 4 lines · Felipe Contreras

But even in that case, if push comes to shoves, this zsh wrapper can ultimately read COMPREPLY and figure things backwards, as even more previous versions did:

Quoted 6 lines
Quoted 1 line

http://article.gmane.org/gmane.comp.version-control.git/189310

Even better. I was just going to propose that zsh's completion could just read the contents of COMPREPLY at the end of _git() and _gitk(), because this way no zsh-induced helper functions and changes would be needed to the completion script at all.

I would rather modify the __gitcomp function. Parsing COMPREPLY is too cumbersome.

Quoted 3 lines

However, running the completion script with Bash would also prevent possible issues caused by incompatibilities between the two shells mentioned below.

It could, but it doesn't now.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

This is the equivalent of what Marc is doing, except that zsh has no problems running bash's code. Note there's a difference with zsh's emulation bash (or rather bourne shell, or k shell), and zsh's emulation of bash's _completion_. The former is fine, the later is not.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

There are a couple of constructs supported by Bash but not by zsh, which we usually try to avoid.

Yes, and is that a big deal?

Not that big, but I wanted to point out that it's not "fine" either. Just a slight maintenance burden, because we have to pay attention not to use such constructs.

Do we have to pay attention?

I say when we encounter one of such maintenance burden issues _then_ we think about it. In the meantime for all we know sourcing bash's script from zsh is fine.

Signature · 1 line
Felipe Contreras
#34 lore raw

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

Quoted 9 lines · Felipe Contreras
Quoted 8 lines · SZEDER Gábor
Quoted 5 lines · Felipe Contreras

But even in that case, if push comes to shoves, this zsh wrapper can ultimately read COMPREPLY and figure things backwards, as even more previous versions did:

http://article.gmane.org/gmane.comp.version-control.git/189310

Quoted 7 lines
Quoted 4 lines

Even better. I was just going to propose that zsh's completion could just read the contents of COMPREPLY at the end of _git() and _gitk(), because this way no zsh-induced helper functions and changes would be needed to the completion script at all.

I would rather modify the __gitcomp function. Parsing COMPREPLY is too cumbersome.

Each element of COMPREPLY contains a possible completion word. What parsing is needed to use that, that is so cumbersome?

Quoted 15 lines
Quoted 3 lines

However, running the completion script with Bash would also prevent possible issues caused by incompatibilities between the two shells mentioned below.

It could, but it doesn't now.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines

This is the equivalent of what Marc is doing, except that zsh has no problems running bash's code. Note there's a difference with zsh's emulation bash (or rather bourne shell, or k shell), and zsh's emulation of bash's _completion_. The former is fine, the later is not.

There are a couple of constructs supported by Bash but not by zsh, which we usually try to avoid.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Yes, and is that a big deal?

Not that big, but I wanted to point out that it's not "fine" either. Just a slight maintenance burden, because we have to pay attention not to use such constructs.

Do we have to pay attention?

Unless you don't mind possible breakages of zsh completion, yes.

Quoted 3 lines

I say when we encounter one of such maintenance burden issues _then_ we think about it. In the meantime for all we know sourcing bash's script from zsh is fine.

That's a cool argument, will remember it when it again comes to refactoring the __gitcomp() tests. For now those tests work just fine. When we encounter maintenance burden issues, like fixing a bug requiring the same modification to all of those tests, then we'll think about it. ;)

#35 lore raw
In reply to Marc Khouzam
Quoted 2 lines · Marc Khouzam

this patch allows tcsh-users to get the benefits of the awesome git-completion.bash script. It could also help other shells do the same.

Maybe you can try to take a look at the same for zsh:
http://article.gmane.org/gmane.comp.version-control.git/208173

Quoted 55 lines
---
 contrib/completion/git-completion.bash |   53 +++++++++++++++++++++++++++++++-
 contrib/completion/git-completion.tcsh |   34 ++++++++++++++++++++
 2 files changed, 86 insertions(+), 1 deletions(-)
 create mode 100755 contrib/completion/git-completion.tcsh
contrib/completion/git-completion.bash +27 -1
index be800e0..6d4b57a 100644
@@ -1,4 +1,6 @@
-#!bash
+#!/bin/bash
+# The above line is important as this script can be executed when used
+# with another shell such as tcsh
 #
 # bash/zsh completion support for core Git.
 #
@@ -2481,3 +2483,52 @@ __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 to the command-line as bash would.
+       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

contrib/completion/git-completion.bash +4 -0
+                       # 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

contrib/completion/git-completion.bash

Quoted 8 lines
contrib/completion/git-completion.bash +8 -0
+                       # 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]}

You might want to use __${COMP_WORDS[0]}_main instead.

contrib/completion/git-completion.bash

Quoted 12 lines
contrib/completion/git-completion.bash +12 -0
+
+       # Print the result that is stored in the bash variable ${COMPREPLY}
+       for i in ${COMPREPLY[@]}; do
+               echo "$i"
+       done
+}
+
+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

Why do you need this function in this file? You can very easily add this function to git-completion.tcsh.

Quoted 33 lines
contrib/completion/git-completion.tcsh new file +13 -0
new file mode 100755
index 0000000..7b7baea
@@ -0,0 +1,34 @@
+#!tcsh
+#
+# tcsh completion support for core Git.
+#
+# Copyright (C) 2012 Marc Khouzam <[email protected]>
+# 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.

contrib/completion/git-completion.tcsh new file +9 -0
+#    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."

contrib/completion/git-completion.tcsh

Quoted 7 lines
contrib/completion/git-completion.tcsh new file +7 -0
+       exit
+endif
+
+# Make the script executable if it is not
+if ( ! -x ${__git_tcsh_completion_script} ) then
+       chmod u+x ${__git_tcsh_completion_script}
+endif

Why not just source it?

Quoted 4 lines

+complete git 'p/*/${__git_tcsh_completion_script} "${COMMAND_LINE}" | sort | uniq/'
+complete gitk 'p/*/${__git_tcsh_completion_script} "${COMMAND_LINE}" | sort | uniq/'

This seems to be very different from bash's 'complete'. I wonder if the 'complete' commands in the original script cause any problems. Maybe only if you source it, but then again, I would expect a warning or something when you run it.

But you can use the trick I did with zsh so you can source it:

complete ()
{

	# do nothing
	return 0
}

. "$script"

It looks like in your case you would need to save the old complete() function though, or somehow restore it. If you go for this method, you use the __*_main functions though.

Cheers.

Signature · 1 line
Felipe Contreras
#36 lore raw
Quoted 6 lines · Felipe Contreras

Review

Quoted 3 lines · Marc Khouzam
(quoted diff) +3 -0
+       # Call _git() or _gitk() of the bash script, based on the first
+       # element of the command-line
+       _${COMP_WORDS[0]}

You might want to use __${COMP_WORDS[0]}_main instead.

That wouldn't work. __git_main() doesn't set up the command-line-specific variables, but the wrapper around it does.

Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+# Make the script executable if it is not
+if ( ! -x ${__git_tcsh_completion_script} ) then
+       chmod u+x ${__git_tcsh_completion_script}
+endif

Why not just source it?

The goal is to re-use a Bash script to do completion in tcsh. They are two different breeds, tcsh doesn't grok bash. So sourcing the completion script is not an option, but we can still run it via Bash and use it's results.

#37 lore raw
Quoted 10 lines · SZEDER Gábor
Quoted 6 lines · Felipe Contreras

Review

Quoted 3 lines · Marc Khouzam
(quoted diff) +3 -0
+       # Call _git() or _gitk() of the bash script, based on the first
+       # element of the command-line
+       _${COMP_WORDS[0]}

You might want to use __${COMP_WORDS[0]}_main instead.

That wouldn't work. __git_main() doesn't set up the command-line-specific variables, but the wrapper around it does.

Yeah, but you can set those command-line-specific variables manually, like the zsh completion wrapper does.

The problem with the _git wrapper is that it will call the bash-specific complete command.

Quoted 11 lines
Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+# Make the script executable if it is not
+if ( ! -x ${__git_tcsh_completion_script} ) then
+       chmod u+x ${__git_tcsh_completion_script}
+endif

Why not just source it?

The goal is to re-use a Bash script to do completion in tcsh. They are two different breeds, tcsh doesn't grok bash. So sourcing the completion script is not an option, but we can still run it via Bash and use it's results.

I see, but the tcsh script can do something like this:

bash <<\EOF
echo $BASH
\EOF

Cheers.

Signature · 1 line
Felipe Contreras
#38 lore raw
In reply to Felipe Contreras

Thanks for the review. I wasn't aware that you were doing a similar effort for zsh.

Quoted 7 lines · Felipe Contreras
Quoted 2 lines · Marc Khouzam

this patch allows tcsh-users to get the benefits of the awesome git-completion.bash script. It could also help other shells do the same.

Maybe you can try to take a look at the same for zsh:
http://article.gmane.org/gmane.comp.version-control.git/208173

Cool. The major difference is that (as Gábor mentioned) zsh understands bash syntax but tcsh does not. tcsh doesn't even allow to define functions. So we have
to take a different approach to get the bash completion script to be used by tcsh.

Quoted 66 lines
Quoted 55 lines
---
 contrib/completion/git-completion.bash |   53 +++++++++++++++++++++++++++++++-
 contrib/completion/git-completion.tcsh |   34 ++++++++++++++++++++
 2 files changed, 86 insertions(+), 1 deletions(-)
 create mode 100755 contrib/completion/git-completion.tcsh
contrib/completion/git-completion.bash +27 -1
index be800e0..6d4b57a 100644
@@ -1,4 +1,6 @@
-#!bash
+#!/bin/bash
+# The above line is important as this script can be executed when used
+# with another shell such as tcsh
 #
 # bash/zsh completion support for core Git.
 #
@@ -2481,3 +2483,52 @@ __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 to the command-line as bash would.
+       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

contrib/completion/git-completion.bash +4 -0
+                       # 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

contrib/completion/git-completion.bash

Quoted 8 lines
contrib/completion/git-completion.bash +8 -0
+                       # 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]}

You might want to use __${COMP_WORDS[0]}_main instead.

Quoted 15 lines

contrib/completion/git-completion.bash

Quoted 12 lines
contrib/completion/git-completion.bash +12 -0
+
+       # Print the result that is stored in the bash variable ${COMPREPLY}
+       for i in ${COMPREPLY[@]}; do
+               echo "$i"
+       done
+}
+
+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

Why do you need this function in this file? You can very easily add this function to git-completion.tcsh.

tcsh does not allow to define functions, so it is not aware of any of the git-completion.bash functions. So, git-completion.tcsh cannot call anything from git-completion.bash.

Quoted 43 lines
Quoted 33 lines
contrib/completion/git-completion.tcsh new file +13 -0
new file mode 100755
index 0000000..7b7baea
@@ -0,0 +1,34 @@
+#!tcsh
+#
+# tcsh completion support for core Git.
+#
+# Copyright (C) 2012 Marc Khouzam <[email protected]>
+# 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.

contrib/completion/git-completion.tcsh new file +9 -0
+#    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."

contrib/completion/git-completion.tcsh

Quoted 7 lines
contrib/completion/git-completion.tcsh new file +7 -0
+       exit
+endif
+
+# Make the script executable if it is not
+if ( ! -x ${__git_tcsh_completion_script} ) then
+       chmod u+x ${__git_tcsh_completion_script}
+endif

Why not just source it?

Quoted 9 lines
Quoted 4 lines

+complete git 'p/*/${__git_tcsh_completion_script} "${COMMAND_LINE}" | sort | uniq/'
+complete gitk 'p/*/${__git_tcsh_completion_script} "${COMMAND_LINE}" | sort | uniq/'

This seems to be very different from bash's 'complete'. I wonder if the 'complete' commands in the original script cause any problems. Maybe only if you source it, but then again, I would expect a warning or something when you run it.

If you source the script under tcsh it will fail miserably because the bash syntax is very different. But when you run it, it runs under the bash shell so everything will work fine. The 'complete' command in the bash script will run, but will only affect the temporary bash shell that was started to run the script. Useless but harmless.

Quoted 9 lines

But you can use the trick I did with zsh so you can source it:

complete ()
{

        # do nothing
        return 0
}

. "$script"

This is pretty cool. Function overriding in shell scripts! tcsh doesn't even have functions, so that trick is out of its league :)

Thanks

Marc

#39 lore raw
In reply to Marc Khouzam

Hi,

I've got two more comments.

Quoted 4 lines · Marc Khouzam
contrib/completion/git-completion.bash +0 -0
@@ -2481,3 +2483,52 @@ __git_complete gitk __gitk_main
 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
 __git_complete git.exe __git_main
 fi

contrib/completion/git-completion.bash __git_complete gitk __gitk_main · line 2495

Quoted 10 lines
contrib/completion/git-completion.bash +10 -0
+
+# 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 ()

We differentiate between _git_whatever() and __git_whatever() functions. The former performs completion for the 'whatever' git command/alias, the latter is a completion helper function. This is a helper function, so it should begin with double underscores.

contrib/completion/git-completion.bash __git_complete gitk __gitk_main · line 2501

Quoted 6 lines
contrib/completion/git-completion.bash +6 -0
+{
+       # Set COMP_WORDS to the command-line as bash would.
+       COMP_WORDS=($1)
+
+       # Set COMP_CWORD to the cursor location as bash would.
+       if [ -n "$2" ]; then

A while ago the completion script was made 'set -u'-clean. (If 'set -u' is enabled, then it's an error to access undefined variables). I'm not sure how many people are out there who'd use this script for tcsh while having 'set -u' in their profile... probably not that many. Still, I think it would be great to keep it up.

Here $2 would be undefined, so accessingit it would cause an error under those semantincs. Please use ${2-} instead (use empty string when undefined).

Review __git_complete gitk __gitk_main · line 2502

Quoted 1 line
(quoted diff) +1 -0
+if [ -n "$1" ] ; then

Same here.

Review __git_complete gitk __gitk_main · line 2505

Quoted 3 lines
(quoted diff) +3 -0
+  # 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"

And here.

Thanks
Gábor