Disable auto-completion of remote branches in Git Bash?

后端 未结 9 1485
广开言路
广开言路 2020-11-30 01:59

I\'m working on a fairly large git repo with a couple of thousand (remote) branches. I am used to using auto-completion (using [TAB]) in the console (Git Bash in that case),

相关标签:
9条回答
  • 2020-11-30 02:10

    You can hack /etc/bash_completion.d/git

    You'll need to edit __git_refs ()

    Note that the change in behaviour will apply every where (so even with git push/pull where you might not want it to). You could of course, make a copy of the function or pass an extra parameter, but I leave that to you

    0 讨论(0)
  • 2020-11-30 02:12

    FWW here is a hack to __git_complete_refs that does the trick

    __git_complete_refs ()
    

    { local remote track pfx cur_="$cur" sfx=" "

    while test $# != 0; do
        case "$1" in
        --remote=*) remote="${1##--remote=}" ;;
        --track)    track="yes" ;;
        --pfx=*)    pfx="${1##--pfx=}" ;;
        --cur=*)    cur_="${1##--cur=}" ;;
        --sfx=*)    sfx="${1##--sfx=}" ;;
        *)      return 1 ;;
        esac
        shift
    done
    echo cur_ $cur_ > a
     if [[  $GIT_COMPLETION_CHECKOUT_NO_GUESS != 1 || $cur_ == "origin"* ]]; then
        __gitcomp_direct "$(__git_refs "$remote" "$track" "$pfx" "$cur_" "$sfx")"
      else
        __gitcomp_direct "$(__git_heads  "" "$cur_")"
      fi
    

    }

    0 讨论(0)
  • 2020-11-30 02:14

    I'm not using Git Bash myself, but if this is the same as mentioned in http://tekrat.com/2008/04/30/bash-autocompletion-git-super-lazy-goodness/, you should be able to replace git branch -a with a plain git branch in

    _complete_git() {
      if [ -d .git ]; then
        branches=`git branch -a | cut -c 3-`
        tags=`git tag`
        cur="${COMP_WORDS[COMP_CWORD]}"
        COMPREPLY=( $(compgen -W "${branches} ${tags}" -- ${cur}) )
      fi
    }
    complete -F _complete_git git checkout
    

    (in your .profile or similar) and get what you want.

    0 讨论(0)
  • 2020-11-30 02:17

    Carey Metcalfe wrote a blog post containing a solution that also edits the auto-completion function, but with slightly newer code than other answers. He also defines an alias checkoutr that keeps the old auto-complete behavior in case it’s ever needed.

    In short, first create the checkoutr alias with this command:

    git config --global alias.checkoutr checkout
    

    Then find git-completion.bash, copy the _git_checkout function into your shell’s RC file so that it gets redefined, and inside that function, replace this line:

    __git_complete_refs $track_opt
    

    with the following lines:

    if [ "$command" = "checkoutr" ]; then
        __git_complete_refs $track_opt
    else
        __gitcomp_direct "$(__git_heads "" "$cur" " ")"
    fi
    

    See the blog post for more details and potential updates to the code.

    0 讨论(0)
  • 2020-11-30 02:20

    If you installed git-completion via homebrew, it's located here: /usr/local/etc/bash_completion.d/git-completion.bash

    Following erik.weathers' answer above, I made the following change so autocompletion can work for both local and remote based on the current prefix. By default, it'll only search local, but if I specify origin/… it'll know I want to search remote branches too.

    In the _git_checkout () method, change

        __gitcomp_nl "$(__git_refs '' $track)"
    

    to:

        # only search local branches instead of remote branches if origin isn't specified
        if [[ $cur == "origin/"* ]]; then
            __gitcomp_nl "$(__git_refs '' $track)"
        else
            __gitcomp_nl "$(__git_heads '' $track)"
        fi
    

    Of course, you can change origin to something else or you can have it search through through a list of remote prefixes if you have more than 1.

    0 讨论(0)
  • 2020-11-30 02:27

    Modifying $(brew --prefix)/etc/bash_completion.d/git-completion.bash is not a good idea because it will be overwritten every time you update Git through Homebrew.

    Combining all the answers I overwrite only _git_checkout function from the completion file in my .bash_profile after sourcing the completion file:

    _git_checkout ()
    {
      __git_has_doubledash && return
    
      case "$cur" in
        --conflict=*)
          __gitcomp "diff3 merge" "" "${cur##--conflict=}"
          ;;
        --*)
          __gitcomp "
          --quiet --ours --theirs --track --no-track --merge
          --conflict= --orphan --patch
          "
          ;;
        *)
          # check if --track, --no-track, or --no-guess was specified
          # if so, disable DWIM mode
          local flags="--track --no-track --no-guess" track=1
          if [ -n "$(__git_find_on_cmdline "$flags")" ]; then
            track=''
          fi
          # only search local branches instead of remote branches if origin isn't
          # specified
          if [[ $cur == "origin/"* ]]; then
            __gitcomp_nl "$(__git_refs '' $track)"
          else
            __gitcomp_nl "$(__git_heads '' $track)"
          fi
          ;;
      esac
    }
    
    0 讨论(0)
提交回复
热议问题