【问题标题】:Zsh git email in prompt not refreshing提示中的 Zsh git 电子邮件不刷新
【发布时间】:2021-07-30 11:27:09
【问题描述】:

问题

我在 mac OS catalina 上安装了我的 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 接收用户电子邮件。

研究完成:

我浏览了多篇 * 和其他文章并尝试了很多东西但没有成功。

对我有用的方法是创建别名以在两个电子邮件地址之间切换:

home='git config user.email "<homeemail>" && source ~/.zshrc'
work='git config user.email "<workemail>" && source ~/.zshrc'

但我必须一直执行这些命令才能让提示接收电子邮件,即使它是在本地 git config 中设置的。

当我们 cd 到 repo 时,对于 zsh 提示直接读取 git 用户电子邮件需要做什么,有人可以帮忙吗?

【问题讨论】:

  • 我刚刚意识到,当不在 git 存储库中时,我的答案并没有真正起作用。我已经更新了。
  • 即使不在 git repo 中,您的回答对我也很有效。但只需要进行小的格式更改即可将[ 保留在ZSH_THEME_GIT_PROMPT_PREFIX 中,而不是将其带到PROMPT

标签: zsh oh-my-zsh quoting


【解决方案1】:

根据@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]%}✔"

【讨论】:

    【解决方案2】:

    发生这种情况是因为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

    【讨论】:

    • 您之前的解决方案运行良好,但需要对不在 git repo 中的情况进行小幅调整。我在下面发布的解决方案对我有用。
    最近更新 更多