Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable auto-complete for a git-flow alias?

This is breaking

alias f='git flow feature'
complete -F __git_flow_feature f

It works eventually (after 2 'tabs') but throws an error on each 'tab' press.

-bash: [: 1: unary operator expected

Any ideas?

like image 854
Alex Avatar asked Jan 17 '12 10:01

Alex


3 Answers

It works for me, when I do:

  1. wget http://www.triquanta.nl/sites/default/files/git-flow.bash
  2. source git-flow.bash
  3. alias f='git flow feature'
  4. complete -F __git_flow_feature f
  5. f tabtab

Anyhow, the most common reason for the "[: 1: unary operator expected" error is that you have in the shell script code like:

if [ 1 = $MYVAL ] 

and your MYVAL is not set. Inspect your completion functions. You can add set -x to debug it.

Usually the easiest solution is to quote the variable so the operator will get the empty argument, but will have correct number of arguments:

if [ 1 = "$MYVAL" ] 
like image 135
Michał Šrajer Avatar answered Sep 25 '22 18:09

Michał Šrajer


I had this problem too and every Google search lead me back to this post.

I am posting the solution I found using Michal's answer and Daenyth's comment...

My git-flow.bash was identical, but I think our git completion files might be varying.

To fix this I had to modify my git completion file located at /etc/bash_completion.d/git

Old:

# __git_find_on_cmdline requires 1 argument
__git_find_on_cmdline ()
{
local word subcommand c=1
while [ $c -lt $cword ]; do
    word="${words[c]}"
    for subcommand in $1; do
        if [ "$subcommand" = "$word" ]; then
            echo "$subcommand"
            return
        fi
    done
    c=$((++c))
done
}

New:

# __git_find_on_cmdline requires 1 argument
__git_find_on_cmdline ()
{
local word subcommand c=1
while [[ $c -lt $cword ]]; do
    word="${words[c]}"
    for subcommand in $1; do
        if [ "$subcommand" = "$word" ]; then
            echo "$subcommand"
            return
        fi
    done
    c=$((++c))
done
}

Notice the double bracket I had to add to the new code. That was the only change I made.

like image 23
Kyle Johnson Avatar answered Sep 23 '22 18:09

Kyle Johnson


Why don't just use git-flow-completion? The instructions for bash are:

$ cd /etc/bash_completion.d
$ sudo wget https://raw.githubusercontent.com/bobthecow/git-flow-completion/master/git-flow-completion.bash
$ exec $SHELL

there are also instructions for zsh or fish.

like image 20
mmoya Avatar answered Sep 25 '22 18:09

mmoya