0

I'm getting an issue where the __git_ps1 script returns the wrong branch.

First, I'll check my branches:

ssalisbury@DOTWeb ssalisbury (master)$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

Then I'll create and checkout a new branch, and the __git_ps1 script will still show that I'm on my original branch:

ssalisbury@DOTWeb ssalisbury (master)$ git checkout -b newBranch
Switched to a new branch 'newBranch'
ssalisbury@DOTWeb ssalisbury (master)$ git branch -a
  master
* newBranch
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
ssalisbury@DOTWeb ssalisbury (master)$

I've been able to determine that it's showing what branch another repository on the same machine has checked out, although I can't figure out why. How can I make sure that it's displaying the information for the repository I'm in?

My PS1 is the following:

\[\e[1;32m\]\u\[\e[0;33m\]@\h \[\e[1;36m\]\W\[\e[1;33m\]$(__git_ps1 " (%s)")\[\e[1;32m\]\$ \[\e[0m\]

The PS1 string is created by a login script. Here are the relevant lines from that script:

BGreen='\e[1;32m'       # Green
Yellow='\e[0;33m'       # Yellow
BCyan='\e[1;36m'        # Cyan
BYellow='\e[1;33m'      # Yellow
BGreen='\e[1;32m'       # Green
Color_Off='\e[0m'       # Text Reset

export PS1='\['$BGreen'\]\u''\['$Yellow'\]@\h ''\['$BCyan'\]\W''\['$BYellow'\]$(__git_ps1 " (%s)")''\['$BGreen'\]\$ ''\['$Color_Off'\]'
smsalisbury
  • 185
  • 2
  • 14
  • Show the line where you assign the value to `PS1`. I'm going to guess that you are running `__git_ps1` at that time and not adding `$(__git_ps1 " (%s)"` itself to the prompt string. (Alternatively what does `declare -p PS1` output?) – Etan Reisner Jun 04 '15 at 16:27
  • @etan I just added the PS1 declaration to the question text. `declare -p PS1` outputs `declare -x PS1="\\[\\e[1;32m\\]\\u\\[\\e[0;33m\\]@\\h \\[\\e[1;36m\\]\\W\\[\\e[1;33m\\]\$(__git_ps1 \" (%s)\")\\[\\e[1;32m\\]\\\$ \\[\\e[0m\\]"`. – smsalisbury Jun 04 '15 at 16:36
  • Looks like I was wrong about that then. If you manually run `__git_ps1 %s` at the prompt do you get the correct or incorrect output at that point? What does `set -x; __git_ps1 %s; set +x` output (it will be a lot)? – Etan Reisner Jun 04 '15 at 16:39

1 Answers1

1

The issue was I had aliased test to change directories to the other repository. I had forgotten that test is a command-line utility and __git_ps1 uses it. When __git_ps1 was trying to use test it was instead changing directories to the other repository.

The problem was fixed by removing that alias.

smsalisbury
  • 185
  • 2
  • 14