Zsh git 提示中的电子邮件未刷新
Zsh git email in prompt not refreshing
问题
我在 mac OS catalina 上安装了 oh my zsh 并使用 iTerm2 作为终端。我正在使用 robbyrussell.zsh-theme
主题并将其修改为在提示中打印 git 电子邮件(更多信息 here)。我已将 robbyrussell.zsh-theme
修改为:
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]%}✔"
当我移动到 git 目录时,它没有从本地 .git/config
或全局 ~/.git/config
.
获取用户电子邮件
完成的研究:
我浏览了多篇 Whosebug 和其他文章并尝试了很多东西但没有成功。
对我有用的方法是创建别名以在两个电子邮件地址之间切换:
home='git config user.email "<homeemail>" && source ~/.zshrc'
work='git config user.email "<workemail>" && source ~/.zshrc'
但我必须一直执行这些命令才能提示接收电子邮件,即使它是在本地 git 配置中设置的。
当我们 cd 到 repo 时,有人可以帮助 zsh 提示直接读取 git 用户电子邮件需要做什么吗?
发生这种情况是因为 ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg[blue]%}$(git_current_user_email)["
使用双引号,这导致 $(git_current_user_email)
在 robbyrussell.zsh-theme
获取来源时仅计算一次,而不是针对每个提示。
您可以通过 运行 echo "$ZSH_THEME_GIT_PROMPT_PREFIX"
确认这一点,现在应该包含电子邮件地址,而不是文字 $(git_current_user_email)
。
遗憾的是,您也不能在此处使用单引号(不评估替换)。然后你会在提示中看到文字 $(git_current_user_email)
,因为 git_prompt_info
函数(在内部使用 ZSH_THEME_GIT_PROMPT_PREFIX
)不会计算它。
但是您可以做的 是将 $(git_current_user_email)
直接放入 PROMPT
,它会在每个新提示上进行评估。
但是当不在 git 存储库中并且不能依赖 git 插件时,我们需要自己禁用它。
像这样:
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)["
}
(还要注意 PROMPT
如何使用单引号而不是双引号)。
可以在此处找到有关不同引用样式和替换的更多详细信息:https://mywiki.wooledge.org/Quotes
根据@mihi 建议的解决方案稍作调整,这对我来说效果很好:
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]%}✔"
问题
我在 mac OS catalina 上安装了 oh my zsh 并使用 iTerm2 作为终端。我正在使用 robbyrussell.zsh-theme
主题并将其修改为在提示中打印 git 电子邮件(更多信息 here)。我已将 robbyrussell.zsh-theme
修改为:
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]%}✔"
当我移动到 git 目录时,它没有从本地 .git/config
或全局 ~/.git/config
.
完成的研究:
我浏览了多篇 Whosebug 和其他文章并尝试了很多东西但没有成功。
对我有用的方法是创建别名以在两个电子邮件地址之间切换:
home='git config user.email "<homeemail>" && source ~/.zshrc'
work='git config user.email "<workemail>" && source ~/.zshrc'
但我必须一直执行这些命令才能提示接收电子邮件,即使它是在本地 git 配置中设置的。
当我们 cd 到 repo 时,有人可以帮助 zsh 提示直接读取 git 用户电子邮件需要做什么吗?
发生这种情况是因为 ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg[blue]%}$(git_current_user_email)["
使用双引号,这导致 $(git_current_user_email)
在 robbyrussell.zsh-theme
获取来源时仅计算一次,而不是针对每个提示。
您可以通过 运行 echo "$ZSH_THEME_GIT_PROMPT_PREFIX"
确认这一点,现在应该包含电子邮件地址,而不是文字 $(git_current_user_email)
。
遗憾的是,您也不能在此处使用单引号(不评估替换)。然后你会在提示中看到文字 $(git_current_user_email)
,因为 git_prompt_info
函数(在内部使用 ZSH_THEME_GIT_PROMPT_PREFIX
)不会计算它。
但是您可以做的 是将 $(git_current_user_email)
直接放入 PROMPT
,它会在每个新提示上进行评估。
但是当不在 git 存储库中并且不能依赖 git 插件时,我们需要自己禁用它。
像这样:
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)["
}
(还要注意 PROMPT
如何使用单引号而不是双引号)。
可以在此处找到有关不同引用样式和替换的更多详细信息:https://mywiki.wooledge.org/Quotes
根据@mihi 建议的解决方案稍作调整,这对我来说效果很好:
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]%}✔"