如何为git-flow别名启用自动完成?

时间:2012-01-17 10:25:18

标签: git bash autocomplete alias git-flow

这是打破

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

它最终起作用(在2个'标签'之后),但在每个“标签”按下时都会抛出错误。

-bash: [: 1: unary operator expected

有什么想法吗?

4 个答案:

答案 0 :(得分:2)

当我这样做时,它对我有用:

  1. wget http://www.triquanta.nl/sites/default/files/git-flow.bash
  2. source git-flow.bash
  3. alias f ='git flow feature'
  4. 完成-F __git_flow_feature f
  5. f 标签 标签
  6. 无论如何,“[:1:一元运算符期望”错误的最常见原因是你在shell脚本代码中有:

    if [ 1 = $MYVAL ] 
    

    并且您的MYVAL未设置。检查完成功能。您可以添加set -x进行调试。

    通常最简单的解决方案是引用变量,以便运算符获取空参数,但是会有正确数量的参数:

    if [ 1 = "$MYVAL" ] 
    

答案 1 :(得分:2)

我也有这个问题,每次谷歌搜索都会把我带回到这篇文章。

我发布了使用Michal的回答和Daenyth的评论发现的解决方案......

我的git-flow.bash完全相同,但我认为我们的git完成文件可能会有所不同。

要解决此问题,我必须修改位于/etc/bash_completion.d/git

的git完成文件

旧:

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

新:

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

注意我必须添加到新代码的双括号。这是我做的唯一改变。

答案 2 :(得分:2)

为什么不只使用git-flow-completion bash 的说明是:

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

还有zshfish的说明。

答案 3 :(得分:1)

我有这个别名:

alias gn="git-number"
alias gb="gn -c git blame"
alias ge="gn -c $EDITOR"
alias ga="gn add"
alias gr="gn -c git reset"
alias gap="EDITOR='$EDITOR -w' gn add -p"
alias gd="gn -c git diff -b -w --ignore-blank-lines"
alias gds="gd --staged"
alias gc="gn -c git checkout"
alias gcf="git flow feature checkout"
alias gl="gn -c git log -w -b -p --ignore-blank-lines"
alias gls="git log --stat"
alias cm="EDITOR='$EDITOR -w' git commit"
alias grb="git stash save 'REBASE' && EDITOR='$EDITOR -w' git rebase -i"
alias grbc="EDITOR='$EDITOR -w' git rebase --continue"

gcd() {
    test -n "$1" && cd $(dirname $(git list $1))
}

source ~/.git-completion.bash
__git_complete gn  _git
__git_complete ga  _git_add
__git_complete gap _git_add
__git_complete gd  _git_diff
__git_complete gds _git_diff
__git_complete gc  _git_checkout
__git_complete gcf _git_checkout
__git_complete gl  _git_log
__git_complete gls _git_log
__git_complete cm  _git_commit

source ~/.git-flow-completion.bash

并安装completion脚本:

wget -O ~/.git-completion.bash https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
wget -O ~/.git-flow-completion.bash https://raw.githubusercontent.com/petervanderdoes/git-flow-completion/develop/git-flow-completion.bash    

此处提到的Git号是:https://github.com/holygeek/git-number 只需将repo中的二进制文件复制到~/bin

即可