Full non-working code:
change_git=''
function cd() {
change_git=''
builtin cd "$@"
}
function git_info() {
if [ -z $change_git]; then
local st=$(bash -c "git symbolic-ref HEAD" 2> /dev/null)
local bn=${st#refs/heads/}
if [[ ! -z "${bn// }" ]]; then
change_git="branch is: ${st#refs/heads/}"
fi
fi
echo $change_git
}
PS1=$'Dir git status: $(git_info)'
This is what I am trying to achieve:
The git branch should be cached in the change_git
variable until the directory is changed. However, that doesn't happen and the variable is calculated every time.
Since I would like to print this to prompt, this causes the prompt to be slow (hence my reason for caching). And yes, prompt is faster once I remove this code, and is hence how I know that it is executed everytime.
Also, this is in zsh. And yes, I am also using oh-my-zsh.
Finally, please do not ask me to replace bash -c "git symbolic-ref HEAD"
with just git symbolic-ref HEAD
. I am using WSL and sharing git from Ubuntu to my Windows; and I am using MSYS2 as main terminal.