La variable env de PS1 no funciona en Mac

I have a script(not written by myself) which shows the git branch/svn branch in my command prompt. Does anyone know why this would not work on mac? It works perfectly in linux.

Desde 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\]\u@\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='\u@\h:\w$(__repo)\$ '
fi
unset color_prompt

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

preguntado el 03 de mayo de 12 a las 17:05

@Alain, I'm going to go out on a limb and suggest it's "How do I get it working on Mac?" -

@blahdiblah Hmm, the editor suggests that it's "Why doesn't this work on Mac?" -

@Alain Damn! I was so close! -

4 Respuestas

Mac OS X installations of Git don't have __git_ps1 incluido.

Uso:

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

as a substitution.

contestado el 03 de mayo de 12 a las 17:05

\(*.*)/ hooray for grep \(*.*)/ - Alain

Not a beautiful solution, but I had the same problem a while ago and this is what I found and put in my .profile. Probably I wasn't able to get a better one. I'd be happy to see it. - Rafael Rawicki

You can get the "full" definition of __git_ps1 aquí. github.com/git/git/blob/master/contrib/completion/… It's a bit heavy, but gives you nice information about rebasing, merging, etc. - Chepner

De hecho, la git-osx-instalador viene con git-completion.bash and installs it to /usr/local/git/contrib/completion/. Just source that file from your .bash_profile para obtener __git_ps1. - schuberth

@sschuberth Thanks for the hint! Looks like the prompt script has now been split out into git-prompt.sh found in the same directory as git-completion.bash (git version 1.8.3.4). - Johann

The script you provided fails to detect git repos if the command __git_ps1 fails. Add this to ~/.bash_profile:

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

Assuming you stored the script file as ~/.ps1, agregue también:

source ~/.ps1

  • This solution also enables tab completion for git.
  • Mac OS X installations of git do have __git_ps1 included, thanks sschuberth and cheapener for mentioning git-completion.bash.

Respondido el 10 de enero de 13 a las 22:01

My file was located in /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash. Mountain Lion with Xcode CLI installed. - Jonatan Little

On a new Yosemite mac using built in git, I used this:

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;\u@\h: \w\a\]\[\e[32;1m\]\u@\h:\w \[\e[33;1m\]$(__git_ps1 "[%s] ")\[\e[32;1m\]\$ \[\e[0m\]'

Note: on El Capitan, I had to change the path of the git scripts to /Applications/Xcode.app/Contents/Developer/usr/share/git-core and I guess you have to have XCode installed for this to work.

Respondido 06 Abr '16, 08:04

If you installed git through macports (git-core), you should add the following to ~/.bash_profile:

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

The location of the git-prompt.sh seemed to have changed a few times.

Respondido 02 Abr '13, 14:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.