3

I'm currently using git-prompt.sh to customize my bash prompt (PS1) to show a status of my git repo in my bash prompt.

This stackoverflow answer was very helpful but not exactly what I'm looking for.

My current .bashrc appears like this:

[aj@computer-name my_current_working_directory (git-branch-name)]$

My .gitconfig uses the following:

[color "status"]
  added = green
  changed = yellow
  untracked = red

The questions is. How do I achieve the following?

I would like my bash prompt to continue to appear the way it does above but change the color of the (git-branch-name) based on the status colors I have set in my .gitconfig

Thanks a million!

Community
  • 1
  • 1
AJ.
  • 1,248
  • 10
  • 27
  • 1
    You'll probably need to write scripts and functions that you can call in your prompt config in order to do that. –  Apr 01 '14 at 17:19
  • I ended up customizing the the git-prompt.sh file. Here's what I ended up with. https://github.com/AJ-Acevedo/dotfiles/commit/7ce6466aaf5beda52e42af566a7252924dddde99 – AJ. Apr 04 '14 at 03:26

2 Answers2

4

I wanted a similar feature today and I achieved it by modifying git-prompt.sh as follows:

__git_ps1_colorize_gitstring ()
{
    if [[ -n ${ZSH_VERSION-} ]]; then
        local c_red='%F{red}'
        local c_green='%F{green}'
        local c_clear='%f'
    else
        local c_red='\[\e[31m\]'
        local c_green='\[\e[32m\]'
        local c_clear='\[\e[0m\]'
    fi

    local branch_color=""
    if [ "$w" = "*" ]; then  # modified
        branch_color="$c_red"
    elif  [ -n "$u" ]; then  # untracked
        branch_color="$c_red"
    elif [ -n "$i" ]; then
        branch_color="$c_green"
    else
        branch_color="$c_clear"
    fi

    c="$branch_color$c"
    z="$c_clear$z"
    w=""
    i=""
    s=""
    u=""
    r="$c_clear$r" 
}

And adding the following to my .bashrc:

GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWUNTRACKEDFILES=1
GIT_PS1_SHOWCOLORHINTS=1

This changes the color of my branch name without adding *, %, or = strings to the prompt.

Ryne Everett
  • 6,427
  • 3
  • 37
  • 49
2

I found this snippet from an article about customizing your prompt. This isn't exactly what you're looking for but just needs some small modifications. There are some colors which are not defined in this snippet but can be found here

export PS1=$IBlack$Time12h$Color_Off'$(git branch &>/dev/null;\
if [ $? -eq 0 ]; then \
  echo "$(echo `git status` | grep "nothing to commit" > /dev/null 2>&1; \
if [ "$?" -eq "0" ]; then \
  # @4 - Clean repository - nothing to commit
  echo "'$Green'"$(__git_ps1 " (%s)"); \
else \
  # @5 - Changes to working tree
  echo "'$IRed'"$(__git_ps1 " {%s}"); \
fi) '$BYellow$PathShort$Color_Off'\$ "; \
else \
  # @2 - Prompt when not in GIT repo
echo " '$Yellow$PathShort$Color_Off'\$ "; \
fi)'

also here is a link with a list of customizations for the prompt

http://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html

mbarnard
  • 151
  • 7
  • 1
    I'll except this as the answer as it totally gave be the resources to achieve what I was looking for. I also stumbled across https://github.com/magicmonty/bash-git-prompt which I'm going to check out. Thanks! – AJ. Apr 03 '14 at 03:28
  • The url with your colors snippet no longer has it. – Faust Mar 16 '20 at 01:45