Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git-completer tab complete? #1786

Closed
dam5h opened this issue Sep 30, 2016 · 28 comments
Closed

Git-completer tab complete? #1786

dam5h opened this issue Sep 30, 2016 · 28 comments
Labels

Comments

@dam5h
Copy link

dam5h commented Sep 30, 2016

In trying xonsh for the first time for the past 24hrs I am missing the git completion that I had in zsh. I read up on the completers section in the docs and also googled around a bit but I don't see any reference to git competetion for xonsh. There is a bash script for this. Is it possible to leverage this from xonsh, or not so much?

Thanks!

@scopatz
Copy link
Member

scopatz commented Sep 30, 2016

Hi @dam5h - Yes git - and all bash completions - should work in xonsh. Please ensure that the $BASH_COMPLETIONS envvar points to your actual bash completion files. http://xon.sh/envvars.html#bash-completions This works on windows too, if you are there.

@ghost ghost added the question label Sep 30, 2016
@OJFord
Copy link
Contributor

OJFord commented Sep 30, 2016

I hadn't missed this, but I just noticed I have the same issue.

Without having changed it:

$ $BASH_COMPLETIONS
['/usr/local/share/bash-completion/bash_completion', '/usr/local/etc/bash_completion']

However, it seems the git completions are actually at /usr/local/etc/bash_completion.d/git-completion.bash - neither of the aforementioned directories exist on my (macOS) system.

$ $BASH_COMPLETIONS.append('/usr/local/etc/bash_completion.d/')

resolves the issue, but I see no reason to leave the others if they don't exist for you either, @dam5h.

Are these built-in xonsh defaults @scopatz, or bash defaults, or something else? I'm pretty sure I never set them.

@gforsyth
Copy link
Collaborator

They're built-in xonsh defaults. Their location can vary, especially on OSX, depending on how bash completion was installed

@dam5h
Copy link
Author

dam5h commented Sep 30, 2016

Sweet! Its working now. I am on arch linux and needed to install bash-completion and it ended up in the default location. My ZSH must have been using something other than bash-completion to do the same thing. Thanks!

@gforsyth
Copy link
Collaborator

gforsyth commented Oct 1, 2016

Great news, @dam5h ! I'm going to go ahead and close this, then, but reopen if any issues persist.

@Glutexo
Copy link

Glutexo commented May 17, 2019

Hm… not working for me on macOS even after adding '/usr/local/etc/bash_completion.d' to $BASH_COMPLETIONS. :(

@scopatz
Copy link
Member

scopatz commented May 17, 2019

@Glutexo - can you provide a little more information about your system? Where do the bash completions actually live?

@Glutexo
Copy link

Glutexo commented Jun 18, 2019

Thanks for response, @scopatz! My bash completions are really in /usr/local/etc/bash_completion.d. git-completion.bash is among them. $BASH_COMPLETIONS contains:

['/usr/local/etc/bash_completion.d', '/usr/local/share/bash-completion/bash_completion', '/usr/local/etc/bash_completion']

System is Darwin Kernel Version 18.6.0: Thu Apr 25 23:16:27 PDT 2019; root:xnu-4903.261.4~2/RELEASE_X86_64.

When I type git into the terminal and press , I get a list of files instead of bash completions.

@scopatz
Copy link
Member

scopatz commented Jun 18, 2019

You might need to find the git completion file (called /home/scopatz/miniconda/share/bash-completion/completions/git for me) and add that to $BASH_COMPLETIONS.

@Glutexo
Copy link

Glutexo commented Jun 19, 2019

It looks like I don’t have anything that would contain completions/git. locate says so. Do I need some other bash completion script than that git-completion.bash? 🤔 If so, where do I get it?

@scopatz
Copy link
Member

scopatz commented Jun 19, 2019

@Glutexo - what does your xonfig command say? Getting git bash completions is somewhat a matter of which system you are on

@Glutexo
Copy link

Glutexo commented Jun 24, 2019

Here is the output:

+------------------+-----------------+
| xonsh            | 0.9.6           |
| Python           | 3.7.3           |
| PLY              | 3.11            |
| have readline    | True            |
| prompt toolkit   | 2.0.9           |
| shell type       | prompt_toolkit2 |
| pygments         | 2.4.2           |
| on posix         | True            |
| on linux         | False           |
| on darwin        | True            |
| on windows       | False           |
| on cygwin        | False           |
| on msys2         | False           |
| is superuser     | False           |
| default encoding | utf-8           |
| xonsh encoding   | utf-8           |
| encoding errors  | surrogateescape |
+------------------+-----------------+

@scopatz
Copy link
Member

scopatz commented Jun 24, 2019

Have you followed the instructions here: https://xon.sh/osx.html#tab-completion?

@Glutexo
Copy link

Glutexo commented Jun 25, 2019

I did. I have the bash-completion2 package installed. The file /usr/local/share/bash-completion/bash_completion exists, is readable and is listed in $BASH_COMPLETIONS. Yet the completions still don’t work. :(

@unikey-mdupree
Copy link

I'm having a similar issue

@DBerke
Copy link

DBerke commented Oct 28, 2019

I'm trying xonsh for the first time, and running into a similar problem. Pressing Tab while entering Git commands currently suggests filenames. I'm on macOS 10.12.6.

  1. I installed bash-completion2 using brew install bash-completion2. It appears to have installed successfully.
  2. The script /usr/local/share/bash-completion/bash_completion exists, and is readable.
  3. $BASH_COMPLETIONS shows:
    EnvPath( ['/usr/local/share/bash-completion/bash_completion', '/usr/local/etc/bash_completion'] )
  4. I don't have a "git" entry in ~/anaconda3/share/bash-completion/completions/
  5. Running xonfig returns
+------------------+-----------------+
| xonsh            | 0.9.13          |
| Python           | 3.7.3           |
| PLY              | 3.11            |
| have readline    | True            |
| prompt toolkit   | 2.0.10          |
| shell type       | prompt_toolkit2 |
| pygments         | 2.4.2           |
| on posix         | True            |
| on linux         | False           |
| on darwin        | True            |
| on windows       | False           |
| on cygwin        | False           |
| on msys2         | False           |
| is superuser     | False           |
| default encoding | utf-8           |
| xonsh encoding   | utf-8           |
| encoding errors  | surrogateescape |
+------------------+-----------------+

Hope that helps suggest a solution! I'd really like to have my Git completions back. :)

Edit: I don't suppose it has anything to with this error that pops up everytime I start xonsh in a terminal?
XonshError: xonsh: subprocess mode: permission denied: .

@gforsyth
Copy link
Collaborator

@DBerke -- are you using the version of git that comes with OSX? Or did you also install it using brew? If there's no git-completions.bash file (or similar) in those completion directories then the completions won't show up.

And the contents of the anaconda3 bash completions folder won't really do anything unless that directory is listed in $BASH_COMPLETIONS

@DBerke
Copy link

DBerke commented Oct 29, 2019

@gforsyth - Thanks. I don't remember having to install git so I'm assuming it's the version that comes with OSX, but it's been almost two years since I got the computer so I may have forgotten. Is there a way to check?

I moved the bash-completions script to /usr/local/share/bash-completion/bash-completion/git-completions.bash, started xonsh, but still no luck with git tab completions. I then tried running source-bash on the file, and got the following error:

~/code dev $ source-bash /usr/local/share/bash-completion/bash-completion/git-completions.bash      
xonsh: error: Source failed: 'source "/usr/local/share/bash-completion/bash-completion/git-completions.bash"'
xonsh: error: Possible reasons: File not found or syntax error

Investigating, I found that the file was actually a symlink pointing to /usr/local/Cellar/bash-completion@2/2.8/share/bash-completion/bash_completion, so I tried copying it over directly with cp (ls shows it be really located there, alongside the symlink which I renamed). I can nano into the file and verify it's there and test confirms it exists, but it still seems to be unreadable by xonsh for some reason:

>>> git_file = Path('/usr/local/share/bash-completion/bash-completion/git-completions.bash')
>>> git_file.exists()
False

However, if I open up the Python REPL (v3.7.3) and run the same code it returns True. I wonder if it's a permissions problem, maybe? Perhaps xonsh doesn't have permission to see that directory somehow?

@gforsyth
Copy link
Collaborator

Caveat: I'm using bash-completions from brew (not version 2) on OSX for this

That said, I only need to have the bash_completion script path in $BASH_COMPLETIONS for completions to work (that is, I don't need to include the bash_completion.d/ directory in that env-var list). xonsh should be running under your username, so as long as you have permissions to read those files, then I don't think it's a permissions problem. However, it's probably worth checking on the owner/group of the individual completion files in bash_completion.d/

The git install question was prompted because, for me, I don't have the git-completions.bash file unless I brew install git, but I see from your comments that the file is clearly present.

@DBerke
Copy link

DBerke commented Nov 4, 2019

How does xonsh use the git-completions.bash script, exactly? I tried running source-bash on a copy of it in my home directory, and it ran without error or output, but git completions still didn't work. If I could get it to work "manually", maybe it'd help pin down where it's failing automatically.

@jaraco
Copy link
Collaborator

jaraco commented Jan 18, 2020

Caveat: I'm using bash-completions from brew (not version 2) on OSX for this

I think you mean bash-completion. There's nothing called bash-completions.

Sometime around fall 2019, bash completions for Git stopped working in my environment, possibly due to the Catalina upgrade, or updates to XCode, updates to xonsh, or similar. The issue I'm experiencing is very similar to that reported by DBerke.

I'd like to get git completions to work according to the recommended procedure.

Here's my xonfig:

+------------------+----------------------+
| xonsh            | 0.9.13.dev1          |
| Git SHA          | 9f7ccc65             |
| Commit Date      | Oct 15 17:14:50 2019 |
| Python           | 3.8.1                |
| PLY              | 3.11                 |
| have readline    | True                 |
| prompt toolkit   | 2.0.9                |
| shell type       | prompt_toolkit2      |
| pygments         | None                 |
| on posix         | True                 |
| on linux         | False                |
| on darwin        | True                 |
| on windows       | False                |
| on cygwin        | False                |
| on msys2         | False                |
| is superuser     | False                |
| default encoding | utf-8                |
| xonsh encoding   | utf-8                |
| encoding errors  | surrogateescape      |
+------------------+----------------------+

I've installed bash-completion2, which appears to be an alias to bash-completion@2:

importlib_resources master $ brew install bash-completion2                                                                                                                                                   
==> Downloading https://homebrew.bintray.com/bottles/bash-completion@2-2.10.catalina.bottle.tar.gz
Already downloaded: /Users/jaraco/Library/Caches/Homebrew/downloads/e6cbaeeeeee724017e28fd254acea1d8629d7d6b97ca446a91e1176c17abc0a2--bash-completion@2-2.10.catalina.bottle.tar.gz
==> Pouring bash-completion@2-2.10.catalina.bottle.tar.gz
==> Caveats
Add the following to your ~/.bash_profile:
  [[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"

If you'd like to use existing homebrew v1 completions, add the following before the previous line:
  export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d"
==> Summary
🍺  /usr/local/Cellar/bash-completion@2/2.10: 741 files, 1015KB

The docs say:

This will install the bash_completion file in /usr/local/share/bash-completion/bash_completion which is in the current xonsh code and so should just work.

And indeed that file now exists:

~ $ ls -la /usr/local/share/bash-completion/bash_completion                                                                                                                                                  
lrwxr-xr-x  1 jaraco  wheel  73 Jan 18 08:19 /usr/local/share/bash-completion/bash_completion -> ../../Cellar/bash-completion@2/2.10/share/bash-completion/bash_completion

Yet still git completions are not working.

If there's no git-completions.bash file (or similar) in those completion directories then the completions won't show up.

@gforsyth For me, "those completion directories" seems to refer to the entries specified in $BASH_COMPLETIONS, but the entries in that env var aren't directories. /usr/local/share/bash-completion/bash_completion is a file and /usr/local/etc/bash_completion does not exist. I searched the xonsh codebase for git-completions.bash, and found nothing. So this advice feels mis-guided.

I can source /usr/local/share/bash-completion/bash_completion, but it has no effect (that I've noticed).

To debug, I put a breakpoint where the completion script is run.

It seems the bash completion is running successfully, but returning a list of files and not git completions.

skeleton master $ git branch > /Users/jaraco/.local/pipx/venvs/xonsh/lib/python3.8/site-packages/xonsh/completers/__amalgam__.py(430)bash_completions()
-> try:
(Pdb) print(script)

source "/usr/local/share/bash-completion/bash_completion"

# Override some functions in bash-completion, do not quote for readline
quote_readline()
{
    echo "$1"
}

_quote_readline_by_ref()
{
    if [[ $1 == \'* || $1 == \"* ]]; then
        # Leave out first character
        printf -v $2 %s "${1:1}"
    else
        printf -v $2 %s "$1"
    fi

    [[ ${!2} == \$* ]] && eval $2=${!2}
}


function _get_complete_statement {
    complete -p git 2> /dev/null || echo "-F _minimal"
}

function getarg {
    find=$1
    shift 1
    prev=""
    for i in $* ; do
        if [ "$prev" = "$find" ] ; then
            echo $i
        fi
        prev=$i
    done
}

_complete_stmt=$(_get_complete_statement)
if echo "$_complete_stmt" | grep --quiet -e "_minimal"
then
    declare -f _completion_loader > /dev/null && _completion_loader git
    _complete_stmt=$(_get_complete_statement)
fi

# Is -C (subshell) or -F (function) completion used?
if [[ $_complete_stmt =~ "-C" ]] ; then
    _func=$(eval getarg "-C" $_complete_stmt)
else                                                                                                                                                                                                         
    _func=$(eval getarg "-F" $_complete_stmt)
    declare -f "$_func" > /dev/null || exit 1
fi

echo "$_complete_stmt"
export COMP_WORDS=(git branch)
export COMP_LINE='git branch '
export COMP_POINT=${#COMP_LINE}
export COMP_COUNT=12
export COMP_CWORD=2
$_func git "" branch

# print out completions, right-stripped if they contain no internal spaces
shopt -s extglob
for ((i=0;i<${#COMPREPLY[*]};i++))
do
    no_spaces="${COMPREPLY[i]//[[:space:]]}"
    no_trailing_spaces="${COMPREPLY[i]%%+([[:space:]])}"
    if [[ "$no_spaces" == "$no_trailing_spaces" ]]; then
        echo "$no_trailing_spaces"
    else
        echo "${COMPREPLY[i]}"
    fi
done

(Pdb) n
> /Users/jaraco/.local/pipx/venvs/xonsh/lib/python3.8/site-packages/xonsh/completers/__amalgam__.py(431)bash_completions()
-> out = subprocess.check_output(
(Pdb) n
> /Users/jaraco/.local/pipx/venvs/xonsh/lib/python3.8/site-packages/xonsh/completers/__amalgam__.py(432)bash_completions()
-> [command, "-c", script],
(Pdb) n
> /Users/jaraco/.local/pipx/venvs/xonsh/lib/python3.8/site-packages/xonsh/completers/__amalgam__.py(433)bash_completions()
-> universal_newlines=True,
(Pdb) 
> /Users/jaraco/.local/pipx/venvs/xonsh/lib/python3.8/site-packages/xonsh/completers/__amalgam__.py(434)bash_completions()
-> stderr=subprocess.PIPE,
(Pdb) 
> /Users/jaraco/.local/pipx/venvs/xonsh/lib/python3.8/site-packages/xonsh/completers/__amalgam__.py(435)bash_completions()
-> env=env,
(Pdb) 
> /Users/jaraco/.local/pipx/venvs/xonsh/lib/python3.8/site-packages/xonsh/completers/__amalgam__.py(431)bash_completions()
-> out = subprocess.check_output(
(Pdb) 
> /Users/jaraco/.local/pipx/venvs/xonsh/lib/python3.8/site-packages/xonsh/completers/__amalgam__.py(437)bash_completions()
-> if not out:
(Pdb) print(out)
complete -F _minimal git
.flake8
.flake8.orig
pytest.ini
LICENSE
.pre-commit-config.yaml
setup.py.orig
pyproject.toml
.coveragerc
docs
.readthedocs.yml
azure-pipelines.yml
appveyor.yml
setup.py
tox.ini
setup.cfg
README.rst
.git
pyproject.toml.orig
CHANGES.rst
.travis.yml
skeleton.md

I suspect what's happened is that with the move from bash to zsh on Catalina, macOS no longer includes git completions for bash.

@jaraco
Copy link
Collaborator

jaraco commented Jan 18, 2020

Indeed, when I run bash -l and then try to complete a git command, it similarly fails to provide git-contexualized completions. So the real question is - how to restore git completions for bash on macOS?

@jaraco
Copy link
Collaborator

jaraco commented Jan 18, 2020

I see there are a number of bash completions present:

skeleton master $ ls /usr/local/etc/bash_completion.d/                                                                                                                                                       
az                       gapplication             git-completion.bash      gresource                hg-completion.bash       ninja-completion.sh
bash                     gdbus                    git-flow-completion.bash gsettings                hub.bash_completion.sh   npm
brew                     gio                      git-prompt.sh            helm                     kubectl                  pandoc

@jaraco
Copy link
Collaborator

jaraco commented Jan 18, 2020

In /usr/local/share/bash-completion/bash_completion, I see this line:

compat_dir=${BASH_COMPLETION_COMPAT_DIR:-/usr/local/Cellar/bash-completion@2/2.10/etc/bash_completion.d}

And /usr/local/Cellar/bash-completion@2/2.10/etc/bash_completion.d doesn't exist.

And I find when I set $BASH_COMPLETION_COMPAT_DIR='/usr/local/etc/bash_completion.d', git completions start working.

I think there's a bug in Homebrew where it's installing the completions to one directory but referencing them in another.

@jaraco
Copy link
Collaborator

jaraco commented Jan 18, 2020

I've reported the issue to Homebrew.

@brechtm
Copy link

brechtm commented Mar 27, 2021

@jaraco Is this still working for you today? Did you need to perform any custom configuration since the Homebrew fix?

I can't get completions working in xonsh for git. Completion does work for other tools that have a completions file in /usr/local/etc/bash_completion.d/ (e.g. brew, tig), however.

I should note that git completion doesn't work under bash (v5) either (completions for other tools do work), unless I source /usr/local/etc/bash_completion.d/git-completion.bash manually.

@jaraco
Copy link
Collaborator

jaraco commented Mar 27, 2021

No. Git completion recently became broken again for me, on xonsh and on bash, because I have hub installed and it's a known problem.

@brechtm
Copy link

brechtm commented Mar 28, 2021

Thanks @jaraco. I forgot I even had hub installed since I don't use it. Uninstalling it fixes git completion for me!

Note that gh replaces hub as the official GitHub CLI tool. gh doesn't break git completion. Here's a well-hidden feature comparison.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants