2012-05-03 37 views
10

我有一個腳本(不是我自己寫的),它顯示了我的命令提示符中的git branch/svn分支。有誰知道爲什麼這不適用於Mac?它在linux中完美工作。PS1的env變量不能在mac上工作

https://github.com/xumingming/dotfiles/blob/master/.ps1

# Display ps1 with colorful pwd and git status 
# Acording to Jimmyxu .bashrc 
# Modified by Ranmocy 
# -- 

if type -P tput &>/dev/null && tput setaf 1 &>/dev/null; then 
    color_prompt=yes 
else 
    color_prompt= 
fi 

__repo() { 
    branch=$(type __git_ps1 &>/dev/null && __git_ps1 | sed -e "s/^ (//" -e "s/)$//") 
    if [ "$branch" != "" ]; then 
     vcs=git 
    else 
     branch=$(type -P hg &>/dev/null && hg branch 2>/dev/null) 
     if [ "$branch" != "" ]; then 
      vcs=hg 
     elif [ -e .bzr ]; then 
      vcs=bzr 
     elif [ -e .svn ]; then 
      vcs=svn 
     else 
      vcs= 
     fi 
    fi 
    if [ "$vcs" != "" ]; then 
     if [ "$branch" != "" ]; then 
      repo=$vcs:$branch 
     else 
      repo=$vcs 
     fi 
     echo -n "($repo)" 
    fi 
    return 0 
} 

if [ "$color_prompt" = yes ]; then 
# PS1='\[\e[01;32m\]\[email protected]\h\[\e[00m\]:\[\e[01;34m\]\w\[\e[33;40m\]$(__repo)\[\e[00m\]\$ ' 
    PS1='\[\e[01;32m\]\u\[\e[00m\]:\[\e[01;34m\]\W\[\e[33m\]$(__repo)\[\e[00m\]\$ ' 
else 
    PS1='\[email protected]\h:\w$(__repo)\$ ' 
fi 
unset color_prompt 

case "$TERM" in 
xterm*|rxvt*) 
    PS1="\[\e]0;\W\a\]$PS1" 
    ;; 
*) 
    ;; 
esac 
+0

@Alain,我要出去走走,並建議它是「我怎樣才能在Mac上工作?」 – blahdiblah

+0

@blahdiblah嗯,編輯建議它是「爲什麼這不適用於Mac?」 – Alain

+0

@阿蘭媽的!我太親近了! – blahdiblah

回答

21

混帳的Mac OS X的安裝沒有__git_ps1包括在內。

用途:

alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\1)/'" 

作爲替代。

+4

'\(*。*)/'hooray for grep'\(*。*)/' – Alain

+0

不是一個漂亮的解決方案,但我前一段時間遇到了同樣的問題,這就是我在'.profile '。可能我無法獲得更好的一個。我很高興看到它。 –

+0

非常感謝,它的作品! –

14

如果命令__git_ps1失敗,則您提供的腳本無法檢測到git倉庫。這種加入~/.bash_profile

source /usr/local/git/contrib/completion/git-completion.bash 
source /usr/local/git/contrib/completion/git-prompt.sh 

假設你存儲腳本文件作爲~/.ps1,還加:

source ~/.ps1 

  • 該解決方案還使標籤完成的飯桶。
  • Mac OS X installations of git do包含__git_ps1,感謝sschuberth和cheapener提到的git-completion.bash。
+2

我的文件位於/Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash。安裝了Xcode CLI的Mountain Lion。 –

1

如果您安裝了通過使用MacPorts(GIT核心)的git,您應該添加以下~/.bash_profile

source /opt/local/etc/profile.d/bash_completion.sh 
source /opt/local/share/git-core/git-prompt.sh 

的git-prompt.sh的位置似乎已經改變了幾次。

10

在新的約塞米蒂MAC使用內置的混帳,我用這個:

source /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash 
source /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh 
export PS1='\[\e]0;\[email protected]\h: \w\a\]\[\e[32;1m\]\[email protected]\h:\w \[\e[33;1m\]$(__git_ps1 "[%s] ")\[\e[32;1m\]\$ \[\e[0m\]' 

注:酋長,我只好到GIT腳本的路徑更改爲/Applications/Xcode.app/Contents/Developer/usr/share/git-core,我想你必須有爲此安裝了XCode。

+0

的確,這適用於OS X 10.10 Yosemite。 –

+0

YES SIR,工作完美。 – oldergod