I'm trying to set my bash prompt to with the following script. Everything works, but the part where I print out the branch name in a Git repository and the status of the branch in color. The colors are somewhat arbitrary, but needless to say it would be red if any files are uncommitted, or yellow if files are unstaged, and green for anything else. It's printing out the part i want in white thought. When I run the part of the script where, towards the end, I define $branchStyle individually, it works, but its not within here. What am I doing wrong?
prompt_git() {
local s=""
local branchName=""
# check if the current directory is in a git repository
if [ $(git rev-parse --is-inside-work-tree &>/dev/null; printf "%s" $?) == 0 ]; then
# check if the current directory is in .git before running git checks
if [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == "false" ]; then
# ensure index is up to date
git update-index --really-refresh -q &>/dev/null
# check for uncommitted changes in the index
if ! $(git diff --quiet --ignore-submodules --cached); then
s="$s+";
fi
# check for unstaged changes
if ! $(git diff-files --quiet --ignore-submodules --); then
s="$s!";
fi
# check for untracked files
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
s="$s?";
fi
# check for stashed files
if $(git rev-parse --verify refs/stash &>/dev/null); then
s="$s$";
fi
fi
# get the short symbolic ref
# if HEAD isn't a symbolic ref, get the short SHA
# otherwise, just give up
branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
git rev-parse --short HEAD 2> /dev/null || \
printf "(unknown)")"
[ -n "$s" ] && s=" [$s]"
printf "%s" "$1$branchName$s"
else
return
fi
}
set_prompts() {
local bold=$(tput bold)
local reset=$(tput sgr0)
local base05=$(tput setaf 188) # light grey
local base08=$(tput setaf 210) # red
local base0A=$(tput setaf 221) # yellow
local base0B=$(tput setaf 114) # green
if git rev-parse --git-dir >/dev/null 2>&1; then
# check for uncommitted changes in the index
if ! git diff-index --quiet --cached HEAD --ignore-submodules -- >&2; then
branchStyle=$base08
# check for unstaged changes
elif ! git diff-files --quiet --ignore-submodules -- >&2; then
branchStyle=$base0A
else
branchStyle=$base0B
fi
fi
PS1+="\$(prompt_git \"$bold$base05 on $branchStyle\")" # git repository details
export PS1
}
set_prompts
unset set_prompts
prompt_gitfunction is designed to find changes in the repo and add a string so you can see this in the prompt, and I have a different check in theset_promptsfunction that determines if the branch has uncommitted changes or unstaged changes and sets the color accordingly. I'm fine with this all being done in 1 or many functions, whatever is the best result.echo "$(tput setaf 210)Why is this text white instead of pink"