threads / patch / 32092

patchFwd: [PATCH] Add tcsh-completion support to contrib by using git-completion.bash

Subject: Fwd: [PATCH] Add tcsh-completion support to contrib by using git-completion.bash

## tl;dr

39 messages between Nov 12, 2012 and Nov 20, 2012. Diffs are folded; open one to read it.

replies: 38people: 4as markdown or json

Marc Khouzam· Nov 12, 2012, 20:07 UTC · lore
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.

Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
==
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
Show changes to 2 files +86 −2

contrib/completion/git-completion.bash, contrib/completion/git-completion.tcsh

diff --git a/contrib/completion/git-completion.bash
b/contrib/completion/git-completion.bash
index be800e0..6d4b57a 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -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
diff --git a/contrib/completion/git-completion.tcsh
b/contrib/completion/git-completion.tcsh
new file mode 100755
index 0000000..7b7baea
--- /dev/null
+++ b/contrib/completion/git-completion.tcsh
@@ -0,0 +1,34 @@
+#!tcsh
+#
+# tcsh completion support for core Git.
+#
+# Copyright (C) 2012 Marc Khouzam <marc.khouzam@gmail.com>
+# Distributed under the GNU General Public License, version 2.0.
+#
+# This script makes use of the git-completion.bash script to
+# determine the proper completion for git commands under tcsh.
+#
+# To use this completion script:
+#
+#    1) Copy both this file and the bash completion script to your
${HOME} directory
+#       using the names ${HOME}/.git-completion.tcsh and
${HOME}/.git-completion.bash.
+#    2) Add the following line to your .tcshrc/.cshrc:
+#        source ${HOME}/.git-completion.tcsh
+
+# One can change the below line to use a different location
+set __git_tcsh_completion_script = ${HOME}/.git-completion.bash
+
+# Check that the user put the script in the right place
+if ( ! -e ${__git_tcsh_completion_script} ) then
+       echo "ERROR in git-completion.tcsh script.  Cannot find:
${__git_tcsh_completion_script}.  Git completion will not work."
+       exit
+endif
+
+# 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
SZEDER Gábor· Nov 13, 2012, 11:14 UTC · re: Marc Khouzam · lore

Re: Fwd: [PATCH] Add tcsh-completion support to contrib by using git-completion.bash

Hi,
On Mon, Nov 12, 2012 at 03:07:46PM -0500, Marc Khouzam wrote:
> Hi,
[...]
> Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
[...]
Show 9 quoted 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.

Show 10 quoted lines
> diff --git a/contrib/completion/git-completion.bash
> b/contrib/completion/git-completion.bash
> index be800e0..6d4b57a 100644
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -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.
Show 20 quoted lines
>  #
>  # 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)

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.

Show 29 quoted lines
> +
> +       # 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}
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.

> +       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[*]}"
Show 6 quoted lines
> +}
> +
> +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?

Show 41 quoted lines
> +fi
> diff --git a/contrib/completion/git-completion.tcsh
> b/contrib/completion/git-completion.tcsh
> new file mode 100755
> index 0000000..7b7baea
> --- /dev/null
> +++ b/contrib/completion/git-completion.tcsh
> @@ -0,0 +1,34 @@
> +#!tcsh
> +#
> +# tcsh completion support for core Git.
> +#
> +# Copyright (C) 2012 Marc Khouzam <marc.khouzam@gmail.com>
> +# Distributed under the GNU General Public License, version 2.0.
> +#
> +# This script makes use of the git-completion.bash script to
> +# determine the proper completion for git commands under tcsh.
> +#
> +# To use this completion script:
> +#
> +#    1) Copy both this file and the bash completion script to your
> ${HOME} directory
> +#       using the names ${HOME}/.git-completion.tcsh and
> ${HOME}/.git-completion.bash.
> +#    2) Add the following line to your .tcshrc/.cshrc:
> +#        source ${HOME}/.git-completion.tcsh
> +
> +# One can change the below line to use a different location
> +set __git_tcsh_completion_script = ${HOME}/.git-completion.bash
> +
> +# Check that the user put the script in the right place
> +if ( ! -e ${__git_tcsh_completion_script} ) then
> +       echo "ERROR in git-completion.tcsh script.  Cannot find:
> ${__git_tcsh_completion_script}.  Git completion will not work."
> +       exit
> +endif
> +
> +# 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.

Show 5 quoted 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.

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

Marc Khouzam· Nov 13, 2012, 20:12 UTC · re: SZEDER Gábor · lore

Re: Fwd: [PATCH] Add tcsh-completion support to contrib by using git-completion.bash

Thanks for the review.
On Tue, Nov 13, 2012 at 6:14 AM, SZEDER Gábor <szeder@ira.uka.de> wrote:
Show 25 quoted lines
> Hi,
>
> On Mon, Nov 12, 2012 at 03:07:46PM -0500, Marc Khouzam wrote:
>> Hi,
>
> [...]
>
>> Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
>
> [...]
>
>> 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 '---'.

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.

Show 19 quoted 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.
>
>> diff --git a/contrib/completion/git-completion.bash
>> b/contrib/completion/git-completion.bash
>> index be800e0..6d4b57a 100644
>> --- a/contrib/completion/git-completion.bash
>> +++ b/contrib/completion/git-completion.bash
>> @@ -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.
Show 8 quoted lines
>> +       # 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.

>
>> +       # Print the result that is stored in the bash variable ${COMPREPLY}
>
> Really? ;)
Removed :)
Show 9 quoted lines
>> +       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.
Show 8 quoted lines
>> +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?
Show 13 quoted lines
>> +# 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.
Very nice!  Done.
Show 9 quoted 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.

> 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

> 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
SZEDER Gábor· Nov 13, 2012, 23:46 UTC · re: Marc Khouzam · lore

Re: Fwd: [PATCH] Add tcsh-completion support to contrib by using git-completion.bash

Hi,
On Tue, Nov 13, 2012 at 03:12:44PM -0500, Marc Khouzam wrote:
Show 17 quoted lines
> >> +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?

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?

Show 16 quoted 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.

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

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

Show 8 quoted 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 ?

SZEDER Gábor· Nov 14, 2012, 00:49 UTC · re: SZEDER Gábor · lore

[PATCH] 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.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
Show 7 quoted lines
> > > 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(-)
Show changes to contrib/completion/git-completion.bash +1 −1
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index bc0657a2..b7b1a834 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -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;;
-- 
1.8.0.128.g441b4b3
Marc Khouzam· Nov 14, 2012, 04:26 UTC · re: SZEDER Gábor · lore

Re: Fwd: [PATCH] Add tcsh-completion support to contrib by using git-completion.bash

On Tue, Nov 13, 2012 at 6:46 PM, SZEDER Gábor <szeder@ira.uka.de> wrote:
Show 29 quoted lines
> Hi,
>
> On Tue, Nov 13, 2012 at 03:12:44PM -0500, Marc Khouzam wrote:
>> >> +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?
>
> 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.
Show 19 quoted 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.
>
> Ok.  Then keep it for the time being, and we'll see what we can do to
> avoid those duplicates.
Thanks.
Show 7 quoted lines
>> > 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.
Show 11 quoted 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
Marc Khouzam· Nov 15, 2012, 11:51 UTC · re: Marc Khouzam · lore

[PATCH] 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.

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

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

Thanks in advance.
Marc
 contrib/completion/git-completion.bash |   47 ++++++++++++++++++++++++++++++++
 contrib/completion/git-completion.tcsh |   29 +++++++++++++++++++
 2 files changed, 76 insertions(+), 0 deletions(-)
 create mode 100644 contrib/completion/git-completion.tcsh
Show changes to 2 files +76 −0

contrib/completion/git-completion.bash, contrib/completion/git-completion.tcsh

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index be800e0..d71a016 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2481,3 +2481,50 @@ __git_complete gitk __gitk_main
 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
 __git_complete git.exe __git_main
 fi
+
+# Method that will output the result of the completion done by
+# the bash completion script, so that it can be re-used in another
+# context than the bash complete command.
+# It accepts 1 to 2 arguments:
+# 1: The command-line to complete
+# 2: The index of the word within argument #1 in which the cursor is
+#    located (optional). If parameter 2 is not provided, it will be
+#    determined as best possible using parameter 1.
+__git_complete_with_output ()
+{
+	# Set COMP_WORDS in a way that can be handled by the bash script.
+	COMP_WORDS=($1)
+
+	# Set COMP_CWORD to the cursor location as bash would.
+	if [ -n "${2-}" ]; then
+		COMP_CWORD=$2
+	else
+		# Assume the cursor is at the end of parameter #1.
+		# We must check for a space as the last character which will
+		# tell us that the previous word is complete and the cursor
+		# is on the next word.
+		if [ "${1: -1}" == " " ]; then
+			# The last character is a space, so our location is at the end
+			# of the command-line array
+			COMP_CWORD=${#COMP_WORDS[@]}
+		else
+			# The last character is not a space, so our location is on the
+			# last word of the command-line array, so we must decrement the
+			# count by 1
+			COMP_CWORD=$((${#COMP_WORDS[@]}-1))
+		fi
+	fi
+
+	# Call _git() or _gitk() of the bash script, based on the first
+	# element of the command-line
+	_${COMP_WORDS[0]}
+
+	local IFS=$'\n'
+	echo "${COMPREPLY[*]}"
+}
+
+if [ -n "${1-}" ] ; then
+  # If there is an argument, we know the script is being executed
+  # so go ahead and run the _git_complete_with_output function
+  __git_complete_with_output "${1-}" "${2-}"
+fi
diff --git a/contrib/completion/git-completion.tcsh b/contrib/completion/git-completion.tcsh
new file mode 100644
index 0000000..6096ea8
--- /dev/null
+++ b/contrib/completion/git-completion.tcsh
@@ -0,0 +1,29 @@
+#!tcsh
+#
+# tcsh completion support for core Git.
+#
+# Copyright (C) 2012 Marc Khouzam <marc.khouzam@gmail.com>
+# Distributed under the GNU General Public License, version 2.0.
+#
+# This script makes use of the git-completion.bash script to
+# determine the proper completion for git commands under tcsh.
+#
+# To use this completion script:
+#
+#    1) Copy both this file and the bash completion script to your ${HOME} directory
+#       using the names ${HOME}/.git-completion.tcsh and ${HOME}/.git-completion.bash.
+#    2) Add the following line to your .tcshrc/.cshrc:
+#        source ${HOME}/.git-completion.tcsh
+
+# One can change the below line to use a different location
+set __git_tcsh_completion_script = ${HOME}/.git-completion.bash
+
+# Check that the user put the script in the right place
+if ( ! -e ${__git_tcsh_completion_script} ) then
+	echo "ERROR in git-completion.tcsh script.  Cannot find: ${__git_tcsh_completion_script}.  Git completion will not work."
+	exit
+endif
+
+complete git  'p/*/`bash ${__git_tcsh_completion_script} "${COMMAND_LINE}" | sort | uniq`/'
+complete gitk 'p/*/`bash ${__git_tcsh_completion_script} "${COMMAND_LINE}" | sort | uniq`/'
+
-- 
1.7.0.4
Felipe Contreras· Nov 16, 2012, 01:41 UTC · re: Marc Khouzam · lore

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

On Thu, Nov 15, 2012 at 12:51 PM, Marc Khouzam <marc.khouzam@gmail.com> wrote:
Show 15 quoted lines
> 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

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`/'

-- 
Felipe Contreras
Marc Khouzam· Nov 16, 2012, 14:39 UTC · re: Felipe Contreras · lore

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

On Thu, Nov 15, 2012 at 8:41 PM, Felipe Contreras <felipe.contreras@gmail.com> wrote:

Show 21 quoted lines
> On Thu, Nov 15, 2012 at 12:51 PM, Marc Khouzam <marc.khouzam@gmail.com> wrote:
>> 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
>
> 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).

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

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

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

Show 26 quoted 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]}
>
> 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
Felipe Contreras· Nov 16, 2012, 15:33 UTC · re: Marc Khouzam · lore

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

On Fri, Nov 16, 2012 at 3:39 PM, Marc Khouzam <marc.khouzam@gmail.com> wrote:
Show 29 quoted lines
> On Thu, Nov 15, 2012 at 8:41 PM, Felipe Contreras
> <felipe.contreras@gmail.com> wrote:
>> On Thu, Nov 15, 2012 at 12:51 PM, Marc Khouzam <marc.khouzam@gmail.com> wrote:
>>> 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
>>
>> 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.

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?
Show 12 quoted lines
>> 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.
Ah :(
-- 
Felipe Contreras
Marc Khouzam· Nov 16, 2012, 15:48 UTC · re: Felipe Contreras · lore

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

On Fri, Nov 16, 2012 at 10:33 AM, Felipe Contreras <felipe.contreras@gmail.com> wrote:

Show 34 quoted lines
> On Fri, Nov 16, 2012 at 3:39 PM, Marc Khouzam <marc.khouzam@gmail.com> wrote:
>> On Thu, Nov 15, 2012 at 8:41 PM, Felipe Contreras
>> <felipe.contreras@gmail.com> wrote:
>>> On Thu, Nov 15, 2012 at 12:51 PM, Marc Khouzam <marc.khouzam@gmail.com> wrote:
>>>> 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
>>>
>>> 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.
>
> 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.
> 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.

> 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 :)

Show 17 quoted lines
>>> 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.
>
> Ah :(
>
> --
> Felipe Contreras
Marc Khouzam· Nov 16, 2012, 16:12 UTC · re: Marc Khouzam · lore

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

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

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

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

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

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
Show changes to contrib/completion/git-completion.tcsh +63 −1
diff --git a/contrib/completion/git-completion.tcsh
b/contrib/completion/git-completion.tcsh
new file mode 100644
index 0000000..76395f9
--- /dev/null
+++ b/contrib/completion/git-completion.tcsh
@@ -0,0 +1,63 @@
+#!tcsh
+#
+# tcsh completion support for core Git.
+#
+# Copyright (C) 2012 Marc Khouzam <marc.khouzam@gmail.com>
+# Distributed under the GNU General Public License, version 2.0.
+#
+# 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}
"${COMMAND_LINE}" | \sort | \uniq`/'
+complete gitk 'p/*/`bash ${__git_tcsh_completion_script}
"${COMMAND_LINE}" | \sort | \uniq`/'
--
1.8.0.1.g9fe2839
Felipe Contreras· Nov 16, 2012, 17:21 UTC · re: Marc Khouzam · lore

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

On Fri, Nov 16, 2012 at 5:12 PM, Marc Khouzam <marc.khouzam@gmail.com> wrote:
Show 72 quoted lines
> diff --git a/contrib/completion/git-completion.tcsh
> b/contrib/completion/git-completion.tcsh
> new file mode 100644
> index 0000000..76395f9
> --- /dev/null
> +++ b/contrib/completion/git-completion.tcsh
> @@ -0,0 +1,63 @@
> +#!tcsh
> +#
> +# tcsh completion support for core Git.
> +#
> +# Copyright (C) 2012 Marc Khouzam <marc.khouzam@gmail.com>
> +# Distributed under the GNU General Public License, version 2.0.
> +#
> +# 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}
> "${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.

-- 
Felipe Contreras
Marc Khouzam· Nov 16, 2012, 18:43 UTC · re: Felipe Contreras · lore

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

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

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

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

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

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
Show changes to contrib/completion/git-completion.tcsh +63 −1
diff --git a/contrib/completion/git-completion.tcsh
b/contrib/completion/git-completion.tcsh
new file mode 100644
index 0000000..f0327fc
--- /dev/null
+++ b/contrib/completion/git-completion.tcsh
@@ -0,0 +1,63 @@
+#!tcsh
+#
+# tcsh completion support for core Git.
+#
+# Copyright (C) 2012 Marc Khouzam <marc.khouzam@gmail.com>
+# Distributed under the GNU General Public License, version 2.0.
+#
+# 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[*]}" | sort | uniq
+EOF
+
+complete git  'p/*/`bash ${__git_tcsh_completion_script} "${COMMAND_LINE}"`/'
+complete gitk 'p/*/`bash ${__git_tcsh_completion_script} "${COMMAND_LINE}"`/'
--
1.8.0.1.g9fe2839
Junio C Hamano· Nov 16, 2012, 19:59 UTC · re: Marc Khouzam · lore

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

Marc Khouzam <marc.khouzam@gmail.com> writes:
Show 30 quoted lines
> 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
> 
> Approach (C) was selected avoid any modification to git-completion.bash.
>
> Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
> ---
>
> 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.
Felipe Contreras· Nov 16, 2012, 20:01 UTC · re: Junio C Hamano · lore

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

On Fri, Nov 16, 2012 at 8:59 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 38 quoted lines
> Marc Khouzam <marc.khouzam@gmail.com> writes:
>
>> 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
>>
>> Approach (C) was selected avoid any modification to git-completion.bash.
>>
>> Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
>> ---
>>
>> 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.
That's fine by me.
Cheers.
-- 
Felipe Contreras
Felipe Contreras· Nov 16, 2012, 17:18 UTC · re: Marc Khouzam · lore

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

On Fri, Nov 16, 2012 at 4:48 PM, Marc Khouzam <marc.khouzam@gmail.com> wrote:
> On Fri, Nov 16, 2012 at 10:33 AM, Felipe Contreras
> <felipe.contreras@gmail.com> wrote:
Show 5 quoted lines
>> 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.

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

-- 
Felipe Contreras
Marc Khouzam· Nov 16, 2012, 18:20 UTC · re: Felipe Contreras · lore

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

On Fri, Nov 16, 2012 at 12:18 PM, Felipe Contreras <felipe.contreras@gmail.com> wrote:

Show 16 quoted lines
> On Fri, Nov 16, 2012 at 4:48 PM, Marc Khouzam <marc.khouzam@gmail.com> wrote:
>> On Fri, Nov 16, 2012 at 10:33 AM, Felipe Contreras
>> <felipe.contreras@gmail.com> wrote:
>
>>> 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.
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 :)

Show 6 quoted 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.

> 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
>
> --
> Felipe Contreras
Felipe Contreras· Nov 16, 2012, 20:04 UTC · re: Marc Khouzam · lore

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

On Fri, Nov 16, 2012 at 7:20 PM, Marc Khouzam <marc.khouzam@gmail.com> wrote:
Show 27 quoted lines
> On Fri, Nov 16, 2012 at 12:18 PM, Felipe Contreras
> <felipe.contreras@gmail.com> wrote:
>> On Fri, Nov 16, 2012 at 4:48 PM, Marc Khouzam <marc.khouzam@gmail.com> wrote:
>>> On Fri, Nov 16, 2012 at 10:33 AM, Felipe Contreras
>>> <felipe.contreras@gmail.com> wrote:
>>
>>>> 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.
>
> 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.
Show 16 quoted 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.

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.

Show 5 quoted lines
>> 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.
-- 
Felipe Contreras
SZEDER Gábor· Nov 16, 2012, 20:40 UTC · re: Felipe Contreras · lore

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

On Fri, Nov 16, 2012 at 09:04:06PM +0100, Felipe Contreras wrote:
Show 9 quoted 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.
> 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.

Felipe Contreras· Nov 16, 2012, 21:03 UTC · re: SZEDER Gábor · lore

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

On Fri, Nov 16, 2012 at 9:40 PM, SZEDER Gábor <szeder@ira.uka.de> wrote:
Show 12 quoted lines
> On Fri, Nov 16, 2012 at 09:04:06PM +0100, Felipe Contreras wrote:
>> > 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.
For what?
>> 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.
Show 6 quoted 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.
-- 
Felipe Contreras
SZEDER Gábor· Nov 16, 2012, 21:22 UTC · re: Felipe Contreras · lore

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

On Fri, Nov 16, 2012 at 10:03:41PM +0100, Felipe Contreras wrote:
Show 15 quoted lines
> On Fri, Nov 16, 2012 at 9:40 PM, SZEDER Gábor <szeder@ira.uka.de> wrote:
> > On Fri, Nov 16, 2012 at 09:04:06PM +0100, Felipe Contreras wrote:
> >> > 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.
> 
> For what?
For zsh.
Show 6 quoted 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.
Show 11 quoted 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.

Show 5 quoted 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.

Felipe Contreras· Nov 16, 2012, 21:46 UTC · re: SZEDER Gábor · lore

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

On Fri, Nov 16, 2012 at 10:22 PM, SZEDER Gábor <szeder@ira.uka.de> wrote:
> On Fri, Nov 16, 2012 at 10:03:41PM +0100, Felipe Contreras wrote:
Show 14 quoted 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.
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.
> 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
Show 8 quoted 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.
-- 
Felipe Contreras
SZEDER Gábor· Nov 17, 2012, 10:56 UTC · re: Felipe Contreras · lore

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

On Fri, Nov 16, 2012 at 10:46:16PM +0100, Felipe Contreras wrote:
Show 21 quoted lines
> On Fri, Nov 16, 2012 at 10:22 PM, SZEDER Gábor <szeder@ira.uka.de> wrote:
> > On Fri, Nov 16, 2012 at 10:03:41PM +0100, Felipe Contreras wrote:
> 
> >> > 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.
> 
> No, it does not. Previous incarnations didn't have this dependency:
> 
> http://article.gmane.org/gmane.comp.version-control.git/196720
Good.
Show 14 quoted 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

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.

Show 10 quoted 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.

Felipe Contreras· Nov 17, 2012, 11:46 UTC · re: SZEDER Gábor · lore

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

On Sat, Nov 17, 2012 at 11:56 AM, SZEDER Gábor <szeder@ira.uka.de> wrote:
> On Fri, Nov 16, 2012 at 10:46:16PM +0100, Felipe Contreras wrote:
Show 10 quoted 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.

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

> 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.
Show 14 quoted 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.
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.

-- 
Felipe Contreras
SZEDER Gábor· Nov 17, 2012, 14:17 UTC · re: Felipe Contreras · lore

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

On Sat, Nov 17, 2012 at 12:46:27PM +0100, Felipe Contreras wrote:
Show 16 quoted lines
> On Sat, Nov 17, 2012 at 11:56 AM, SZEDER Gábor <szeder@ira.uka.de> wrote:
> > On Fri, Nov 16, 2012 at 10:46:16PM +0100, Felipe Contreras wrote:
> 
> >> 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.
> 
> 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?

Show 22 quoted 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.
> 
> >> >> 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.
> 
> Do we have to pay attention?
Unless you don't mind possible breakages of zsh completion, yes.
> 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. ;)

Junio C Hamano· Nov 16, 2012, 21:20 UTC · re: SZEDER Gábor · lore

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

SZEDER Gábor <szeder@ira.uka.de> writes:
> 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.

Felipe Contreras· Nov 16, 2012, 21:56 UTC · re: Junio C Hamano · lore

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

On Fri, Nov 16, 2012 at 10:20 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 11 quoted lines
> SZEDER Gábor <szeder@ira.uka.de> writes:
>
>> 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.

> 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.
-- 
Felipe Contreras
Marc Khouzam· Nov 17, 2012, 17:15 UTC · re: Felipe Contreras · lore

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

On Fri, Nov 16, 2012 at 4:56 PM, Felipe Contreras <felipe.contreras@gmail.com> wrote:

Show 8 quoted lines
> On Fri, Nov 16, 2012 at 10:20 PM, Junio C Hamano <gitster@pobox.com> wrote:
>
>> 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
Felipe Contreras· Nov 17, 2012, 18:01 UTC · re: Marc Khouzam · lore

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

On Sat, Nov 17, 2012 at 6:15 PM, Marc Khouzam <marc.khouzam@gmail.com> wrote:
Show 19 quoted lines
> On Fri, Nov 16, 2012 at 4:56 PM, Felipe Contreras
> <felipe.contreras@gmail.com> wrote:
>> On Fri, Nov 16, 2012 at 10:20 PM, Junio C Hamano <gitster@pobox.com> wrote:
>>
>>> 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.
I don't see any concerns.
> 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:

Show changes to contrib/completion/git-completion.bash +26 −4
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -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
-- 
Felipe Contreras
Marc Khouzam· Nov 20, 2012, 14:58 UTC · re: Felipe Contreras · lore

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

On Sat, Nov 17, 2012 at 1:01 PM, Felipe Contreras <felipe.contreras@gmail.com> wrote:

Show 15 quoted lines
>
>> I gather that using a wrapper for zsh causes concerns about
>> backwards-compatibility.
>
> I don't see any concerns.
>
>> 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.
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/git@vger.kernel.org/msg12192.html

For reference, the more general solution was proposed here: [2] http://www.mail-archive.com/git@vger.kernel.org/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
Felipe Contreras· Nov 20, 2012, 15:15 UTC · re: Marc Khouzam · lore

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

On Tue, Nov 20, 2012 at 3:58 PM, Marc Khouzam <marc.khouzam@gmail.com> wrote:
Show 10 quoted 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/git@vger.kernel.org/msg12192.html
This one is already merged to 'next'.
Cheers.
-- 
Felipe Contreras
Marc Khouzam· Nov 20, 2012, 18:20 UTC · re: Felipe Contreras · lore

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

On Tue, Nov 20, 2012 at 10:15 AM, Felipe Contreras <felipe.contreras@gmail.com> wrote:

Show 14 quoted lines
> On Tue, Nov 20, 2012 at 3:58 PM, Marc Khouzam <marc.khouzam@gmail.com> wrote:
>
>> 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/git@vger.kernel.org/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
Junio C Hamano· Nov 20, 2012, 21:07 UTC · re: Marc Khouzam · lore

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

Marc Khouzam <marc.khouzam@gmail.com> writes:
Show 6 quoted lines
>> 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.
Felipe Contreras· Nov 13, 2012, 18:31 UTC · re: Marc Khouzam · lore

Re: [PATCH] Add tcsh-completion support to contrib by using git-completion.bash

On Mon, Nov 12, 2012 at 9:07 PM, Marc Khouzam <marc.khouzam@gmail.com> wrote:
> 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

Show 63 quoted 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
>
> diff --git a/contrib/completion/git-completion.bash
> b/contrib/completion/git-completion.bash
> index be800e0..6d4b57a 100644
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -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]}
You might want to use __${COMP_WORDS[0]}_main instead.
Show 12 quoted lines
> +
> +       # 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.

Show 40 quoted lines
> diff --git a/contrib/completion/git-completion.tcsh
> b/contrib/completion/git-completion.tcsh
> new file mode 100755
> index 0000000..7b7baea
> --- /dev/null
> +++ b/contrib/completion/git-completion.tcsh
> @@ -0,0 +1,34 @@
> +#!tcsh
> +#
> +# tcsh completion support for core Git.
> +#
> +# Copyright (C) 2012 Marc Khouzam <marc.khouzam@gmail.com>
> +# Distributed under the GNU General Public License, version 2.0.
> +#
> +# This script makes use of the git-completion.bash script to
> +# determine the proper completion for git commands under tcsh.
> +#
> +# To use this completion script:
> +#
> +#    1) Copy both this file and the bash completion script to your
> ${HOME} directory
> +#       using the names ${HOME}/.git-completion.tcsh and
> ${HOME}/.git-completion.bash.
> +#    2) Add the following line to your .tcshrc/.cshrc:
> +#        source ${HOME}/.git-completion.tcsh
> +
> +# One can change the below line to use a different location
> +set __git_tcsh_completion_script = ${HOME}/.git-completion.bash
> +
> +# Check that the user put the script in the right place
> +if ( ! -e ${__git_tcsh_completion_script} ) then
> +       echo "ERROR in git-completion.tcsh script.  Cannot find:
> ${__git_tcsh_completion_script}.  Git completion will not work."
> +       exit
> +endif
> +
> +# 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?
> +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.
-- 
Felipe Contreras
SZEDER Gábor· Nov 14, 2012, 00:11 UTC · re: Felipe Contreras · lore

Re: [PATCH] Add tcsh-completion support to contrib by using git-completion.bash

On Tue, Nov 13, 2012 at 07:31:45PM +0100, Felipe Contreras wrote:
Show 6 quoted lines
> On Mon, Nov 12, 2012 at 9:07 PM, Marc Khouzam <marc.khouzam@gmail.com> wrote:
> > +       # 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.

Show 6 quoted lines
> > +# 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.

Felipe Contreras· Nov 15, 2012, 02:40 UTC · re: SZEDER Gábor · lore

Re: [PATCH] Add tcsh-completion support to contrib by using git-completion.bash

On Wed, Nov 14, 2012 at 1:11 AM, SZEDER Gábor <szeder@ira.uka.de> wrote:
Show 10 quoted lines
> On Tue, Nov 13, 2012 at 07:31:45PM +0100, Felipe Contreras wrote:
>> On Mon, Nov 12, 2012 at 9:07 PM, Marc Khouzam <marc.khouzam@gmail.com> wrote:
>> > +       # 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.

Show 11 quoted lines
>> > +# 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.
-- 
Felipe Contreras
Marc Khouzam· Nov 14, 2012, 03:36 UTC · re: Felipe Contreras · lore

Re: [PATCH] Add tcsh-completion support to contrib by using git-completion.bash

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

On Tue, Nov 13, 2012 at 1:31 PM, Felipe Contreras <felipe.contreras@gmail.com> wrote:

Show 7 quoted lines
> On Mon, Nov 12, 2012 at 9:07 PM, Marc Khouzam <marc.khouzam@gmail.com> wrote:
>
>> 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.

Show 81 quoted 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
>>
>> diff --git a/contrib/completion/git-completion.bash
>> b/contrib/completion/git-completion.bash
>> index be800e0..6d4b57a 100644
>> --- a/contrib/completion/git-completion.bash
>> +++ b/contrib/completion/git-completion.bash
>> @@ -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]}
>
> You might want to use __${COMP_WORDS[0]}_main instead.
>
>> +
>> +       # 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.

Show 52 quoted lines
>> diff --git a/contrib/completion/git-completion.tcsh
>> b/contrib/completion/git-completion.tcsh
>> new file mode 100755
>> index 0000000..7b7baea
>> --- /dev/null
>> +++ b/contrib/completion/git-completion.tcsh
>> @@ -0,0 +1,34 @@
>> +#!tcsh
>> +#
>> +# tcsh completion support for core Git.
>> +#
>> +# Copyright (C) 2012 Marc Khouzam <marc.khouzam@gmail.com>
>> +# Distributed under the GNU General Public License, version 2.0.
>> +#
>> +# This script makes use of the git-completion.bash script to
>> +# determine the proper completion for git commands under tcsh.
>> +#
>> +# To use this completion script:
>> +#
>> +#    1) Copy both this file and the bash completion script to your
>> ${HOME} directory
>> +#       using the names ${HOME}/.git-completion.tcsh and
>> ${HOME}/.git-completion.bash.
>> +#    2) Add the following line to your .tcshrc/.cshrc:
>> +#        source ${HOME}/.git-completion.tcsh
>> +
>> +# One can change the below line to use a different location
>> +set __git_tcsh_completion_script = ${HOME}/.git-completion.bash
>> +
>> +# Check that the user put the script in the right place
>> +if ( ! -e ${__git_tcsh_completion_script} ) then
>> +       echo "ERROR in git-completion.tcsh script.  Cannot find:
>> ${__git_tcsh_completion_script}.  Git completion will not work."
>> +       exit
>> +endif
>> +
>> +# 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?
>
>> +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.

Show 9 quoted 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
SZEDER Gábor· Nov 14, 2012, 00:09 UTC · re: Marc Khouzam · lore

Re: Fwd: [PATCH] Add tcsh-completion support to contrib by using git-completion.bash

Hi,
I've got two more comments.
On Mon, Nov 12, 2012 at 03:07:46PM -0500, Marc Khouzam wrote:
Show 14 quoted lines
> @@ -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 ()

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.

Show 6 quoted lines
> +{
> +       # 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).

> +if [ -n "$1" ] ; then
Same here.
> +  # 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

← back to recent threads