Reputation: 79
I have oh my zsh installed on mac OS catalina and using iTerm2 as terminal. I am using robbyrussell.zsh-theme
theme and have modified it to print git email in the prompt (More info here). I have modified robbyrussell.zsh-theme
to this:
PROMPT="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
PROMPT+=' %{$fg[cyan]%}%~%{$reset_color%} $(git_prompt_info)'
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg[blue]%}$(git_current_user_email)["
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}] %{$fg[yellow]%}✗"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%}] %{$fg[green]%}✔"
When I move to a git directory it is not picking up user email from local .git/config
or from global ~/.git/config
.
I went through multiple stackoverflow and other articles and tried bunch of stuff but with no success.
The approach that worked for me was creating aliases to switch between two email addresses:
home='git config user.email "<homeemail>" && source ~/.zshrc'
work='git config user.email "<workemail>" && source ~/.zshrc'
But I have to execute these commands all the time to make prompt pick up the email even if it is set in local git config.
Can someone help regarding what needs to be done to zsh prompt to read git user email directly when we cd to a repo?
Upvotes: 3
Views: 1128
Reputation: 79
As per solution suggested by @mihi with a small tweak, this worked fine for me:
PROMPT="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
PROMPT+=' %{$fg[cyan]%}%~%{$reset_color%} %{$fg[blue]%}$(git_current_user_email)$(git_prompt_info)'
ZSH_THEME_GIT_PROMPT_PREFIX="["
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}] %{$fg[yellow]%}✗"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%}] %{$fg[green]%}✔"
Upvotes: 0
Reputation: 3846
This happens because ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg[blue]%}$(git_current_user_email)["
uses double quotes, which causes $(git_current_user_email)
to be evaluated only once when robbyrussell.zsh-theme
gets sourced, not for every prompt.
You can confirm this by running echo "$ZSH_THEME_GIT_PROMPT_PREFIX"
, which should now contain the email address, instead of a literal $(git_current_user_email)
.
Unfortunately you can't use single quotes (which do not evaluate substitutions) either here. As then you'll see a literal $(git_current_user_email)
in your prompt, as the git_prompt_info
function (which uses ZSH_THEME_GIT_PROMPT_PREFIX
internally) does not evaluate it.
What you can do however is to put $(git_current_user_email)
directly into PROMPT
, which does get evaluated on each new prompt.
But we'll need to disable it ourselves when not in a git repository and can't rely on the git plugin.
Something like this:
PROMPT="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
PROMPT+=' %{$fg[cyan]%}%~%{$reset_color%} $(my_git_prompt_prefix)$(git_prompt_info)'
ZSH_THEME_GIT_PROMPT_PREFIX=""
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}] %{$fg[yellow]%}✗"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%}] %{$fg[green]%}✔"
function my_git_prompt_prefix() {
# Based on: https://github.com/ohmyzsh/ohmyzsh/blob/d646884add277d134235a9b18ab755388d6e0d8d/lib/git.zsh#L15-L23
# If we are on a folder not tracked by git, get out.
# Otherwise, check for hide-info at global and local repository level
if ! __git_prompt_git rev-parse --git-dir &> /dev/null \
|| [[ "$(__git_prompt_git config --get oh-my-zsh.hide-info 2>/dev/null)" == 1 ]]; then
return 0
fi
local ref
ref=$(__git_prompt_git symbolic-ref --short HEAD 2> /dev/null) \
|| ref=$(__git_prompt_git rev-parse --short HEAD 2> /dev/null) \
|| return 0
# The actual git prompt prefix
echo "%{$fg[blue]%}$(git_current_user_email)["
}
(Also notice how PROMPT
has single quotes instead of double quotes).
More details on the different quoting styles and substitutions can be found here: https://mywiki.wooledge.org/Quotes
Upvotes: 2