Search code examples
gitsh

How to show branch name in prompt only in git repository?


I'm using git bash on Windows 11, and I customized the prompt by modifying the git-prompt.sh file to suit my taste and to display the name of the virtual environment. I changed the prompt to display as follows:

user in folder on git(branch-name) using (venv-name)

both "on git(branch-name)" and "using (venv-name)" should only be displayed when it makes sense, that is, when a virtual environment is active and/or when it is in a git repository.

The display of "using (venv-name)" is working perfectly, but "on git(branch-name)" is not, "on git" is being displayed all the time in any folder.

prompt image

The git-prompt.sh file looks like this:

if test -f /etc/profile.d/git-sdk.sh
then
    TITLEPREFIX=SDK-${MSYSTEM#MINGW}
else
    TITLEPREFIX=$MSYSTEM
fi

if test -f ~/.config/git/git-prompt.sh
then
    . ~/.config/git/git-prompt.sh
else
    PS1='\[\033]0;$Git Bash: $PWD\007\]' # set window title
    PS1="$PS1"'\n'                 # new line
    PS1="$PS1"'\[\033[95m\]'       # change to light magenta
    PS1="$PS1"'\u '                # user <space>
    PS1="$PS1"'\[\033[97m\]'       # change to white
    PS1="$PS1"'in '                # in <space>
    PS1="$PS1"'\[\033[96m\]'       # change to light cyan
    PS1="$PS1"'\W'                 # current working directory
    if test -z "$WINELOADERNOEXEC"
    then
        GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
        COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
        COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
        COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
        if test -f "$COMPLETION_PATH/git-prompt.sh"
        then
            . "$COMPLETION_PATH/git-completion.bash"
            . "$COMPLETION_PATH/git-prompt.sh"
            if __git_ps1 > /dev/null 2>&1;   # here the problem
            then
                PS1="$PS1"'\[\033[97m\]'  # change color to white
                PS1="$PS1"' on '          # <space> on <space>
                PS1="$PS1"'\[\033[93m\]'  # change color to light yellow
                PS1="$PS1"'git`__git_ps1`'   # current git branch
            fi
        fi
    fi
    if test -n "$VIRTUAL_ENV"
    then
        PS1="$PS1"'\[\033[97m\]'  # change color to white
        PS1="$PS1"' using '           # <space> using <space>
        PS1="$PS1"'\[\033[92m\]'   # change to light green
        PS1="$PS1"'(`basename "$VIRTUAL_ENV"`) ' # virtual env name
    fi
    PS1="$PS1"'\[\033[0m\]'        # change color
    PS1="$PS1"'\n'                 # new line
    PS1="$PS1"'$ '                 # prompt: always $
fi

MSYS2_PS1="$PS1"               # for detection by MSYS2 SDK's bash.basrc

# Evaluate all user-specific Bash completion scripts (if any)
if test -z "$WINELOADERNOEXEC"
then
    for c in "$HOME"/bash_completion.d/*.bash
    do
        # Handle absence of any scripts (or the folder) gracefully
        test ! -f "$c" ||
        . "$c"
    done
fi

I already tried:

git_branch=$(__git_ps1)
if test -n "$git_branch"; then

and:

if __git_ps1 > /dev/null 2>&1; then

and:

if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then

and none of these work!

Does anyone have any ideas on how to make this work?


Solution

  • I had to use PROMPT_COMMAND, setting PROMPT_COMMAND to a function that changes PS1. And I had to use the condition git rev-parse --is-inside-work-tree > /dev/null 2>&1 to validate whether it is a git repository or not.