xta / HalloweenBash

Custom Bash Profile Generator
http://xta.github.io/HalloweenBash/
333 stars 32 forks source link

git-branch problem #10

Closed atupal closed 11 years ago

atupal commented 11 years ago

You use a shell fucntion in PS1 But when you "cd" to a git repo dir, you need source ~/.bashrc to go the git branch display in the command prompt. and then you cd to other dir(not a git repo), and the branch is staill in the bash prompt...

Is any error in my environ (arch-linux , bash) ? I then use PROMPT_COMMAND solve this:

export PROMPT_COMMAND='RET=$?;\
  BRANCH="";\
  ERRMSG="";\
  if [[ $RET != 0 ]]; then\
    ERRMSG=" $RET";\
  fi;\
  if git branch 2>/dev/null 1>/dev/null; then\
    BRANCH=$(git branch 2>/dev/null | grep \* |  cut -d " " -f 2);\
  fi;
  user="\[\e[0;31m\]\u"
  host="\[\e[0;32m\]\h"
  fdir="\[\e[1;34m\]\w"
  prompt="\[\e[0;31m\]\$"
  data="\[\e[0;36m\]date:$BOLD\D{%c}"
  git_branch="\[\e[0;33m\]$BRANCH\[\e[m\]"
  ter_color="\[\e[0;32m\]"
  PS1="$user@$host $fdir $prompt $data $git_branch $ter_color\n>"
  #PS1="$GREEN\u@\h $BLUE\W $CYAN$BRANCH$RED$ERRMSG \$ $LIGHT_GRAY";'

thx

ptw: awesome project !

atupal commented 11 years ago

there are the code HalloweenBash generate:

function parse_git_branch { 
   git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' 
} 
export PS1="\h:\W \u\$(parse_git_branch)$ "
xta commented 11 years ago

Hi @atupal,

I apologize, but I don't understand your question. It's possible your environment has different dependencies which you may need to include.

As I understand my code, it shows you the current git branch if your current directory uses git. You get no git branch information if your current directory does not use git.

Let me know if you have any questions.

Thanks, -Rex

atupal commented 11 years ago

I find the problem in my code: Because I use the follow code:

function parse_git_branch { 
   git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' 
} 
git_branch=" \[\e[0;33m\]$(parse_git_branch)\[\e[m\] "
export PS1="\h:\W \u\$git_branch$ "

so after I source .bashrc the $git_branch become a fixed string but not raw string after initial. (because i use the "); And it work after I change the " to' ;

function parse_git_branch { 
   git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' 
} 
git_branch=' \[\e[0;33m\]$(parse_git_branch)\[\e[m\] '
export PS1="\h:\W \u\ $git_branch$ "

thx! I am sorry for my careless ...

xta commented 11 years ago

@atupal Great, glad it works!