bash-completion2だと$BASH_COMPLETION_DIRというシェル変数が無い件

MacPorts$ sudo port install bash-completionしたら、bash-completionのバージョンが2系になってました。1系から変わった部分も有るらしく、補完はちゃんと動くけどGitとプロンプト変数PS1とbash_completionと - kanonjiの日記でセットしたGItのブランチ名をプロンプトに表示するやつが動かなかったので、調べてみました。

まとめ

$BASH_COMPLETION_DIRが無くなった

$BASH_COMPLETION_DIRというシェル変数がなくなっていて、.bash_profileに書いたif [ -f $BASH_COMPLETION_DIR/git ]; thenというif文が常にfalseになってました。

/opt/local/share/bash-completion/completions/

$BASH_COMPLETION_DIRに代入されていたファイルパスも、基本的には無くなっています。代わりに/opt/local/share/bash-completion/completions/に、各コマンドのcompletionsファイル*1が配置されています。

$ set | grep /opt/local/share/bash-completion/completions/

このファイルパスが代入されたシェル変数は、どうやら無いみたい。

$BASH_COMPLETION_COMPAT_DIR
$ set | grep BASH_COMPLETION_COMPAT_DIR
BASH_COMPLETION_COMPAT_DIR=/opt/local/etc/bash_completion.d

$BASH_COMPLETION_DIRに代入されていたファイルパスは、互換用のシェル変数っぽい名前の$BASH_COMPLETION_COMPAT_DIRに代入されています。今回の環境では1系のbash-completionが元々入って無かったので/opt/local/etc/bash_completion.dは存在しませんでした。

__git_ps1等が別ファイルになった

# 3) Consider changing your PS1 to also show the current branch,
# see git-prompt.sh for details.

https://github.com/git/git/blob/v1.8.0/contrib/completion/git-completion.bash

https://github.com/git/git/blob/v1.8.0/contrib/completion/git-prompt.sh

1.8.0からなのか、もう少し前からなのか分からないけど、PS1環境変数を使ってプロンプトにgitのブランチ名を出すのに必要なコードは、別ファイルに切り出されている様子。

$ vim .profile #.bash_profile や .bashrcでも環境に合わせて
# bash_completion.d/git
if [ -f $BASH_COMPLETION_DIR/git ]; then
    export PS1='\[\033[01;32m\]\u@\h\[\033[01;33m\] \w$(__git_ps1) \n\[\033[01;34m\]\$\[\033[00m\] '
else
    export PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$ '
fi
Gitとプロンプト変数PS1とbash_completionと - kanonjiの日記
$ vim .profile #.bash_profile や .bashrcでも環境に合わせて
# bash-prompt
if [ -f /opt/local/share/git-core/git-prompt.sh ]; then
    . /opt/local/share/git-core/git-prompt.sh
    export PS1='\[\033[01;32m\]\u@\h\[\033[01;33m\] \w$(__git_ps1) \n\[\033[01;34m\]\$\[\033[00m\] '
else
    export PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$ '
fi

こんな感じで取り急ぎ動く様にしました。

補足と余談

MacPortsにあるbash-completionのセットアップ手順

howto/bash-completion – MacPorts

MacPortsWikiにセットアップ手順が解説されています。Mac標準のbash 3.2ではbash-completion 2系が動かないのでbash 4.1以上を設定したり、.bash_profileに書く設定が載ってます。

MacPortsでパッケージをインストールする際、常に+bash_completion variantsを指定する
$ sudo vi /opt/local/etc/macports/variants.conf

+bash_completion

このファイルに、常に使いたいvariantsを設定出来るみたいです。

.bash_profileから読み込むファイル
$ ls -al /opt/local/etc/bash_completion 
lrwxr-xr-x  1 root  admin  43  7  6 07:17 /opt/local/etc/bash_completion -> /opt/local/etc/profile.d/bash_completion.sh

この様にシンボリックリンクが貼ってあります。

/opt/local/etc/bash_completion内から/opt/local/share/bash-completion/bash_completionが呼ばれています。この中で、前述の$BASH_COMPLETION_COMPAT_DIRが定義されてたりします。

環境

Mac Mac OS X 10.7.4(Lion)
git-core @1.8.0_0+bash_completion+credential_osxkeychain+doc+pcre+python27
bash-completion @2.0_1

書いた日

2012-11-21
ちょっとほっといたけどそんなに日は経ってない。

*1:と呼んでいいのか分からないけど