获取在 Zsh 中指示 Git-branch 的提示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1128496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:44:33  来源:igfitidea点击:

To get a prompt which indicates Git-branch in Zsh

gitzshprompt

提问by Léo Léopold Hertz ??

I run the following codes separately as my prompt unsuccessfully in .zshrc. This suggests me that apparently I do not have a program called __git_ps1. It is not in MacPorts.

我在 .zshrc 中单独运行以下代码作为我的提示失败。这表明我显然没有名为 __git_ps1 的程序。它不在 MacPorts 中。

#1

#1

PROMPT="$(__git_ps1 " \[3[1;32m\] (%s)\[3[0m\]")$"$

#2

#2

PROMPT="$(__git_ps1 " (%s)")$"$

#3

#3

# Get the name of the branch we are on
git_prompt_info() {
  branch_prompt=$(__git_ps1)
  if [ -n "$branch_prompt" ]; then
    status_icon=$(git_status)
    echo $branch_prompt $status_icon
  fi
}

# Show character if changes are pending
git_status() {
  if current_git_status=$(git status | grep 'added to commit' 2> /dev/null); then
    echo "?"
  fi
}
autoload -U colors
colors
setopt prompt_subst
PROMPT='
%~%{$fg_bold[black]%}$(git_prompt_info)
→ %{$reset_color%}'

How can you get a prompt which shows the name of a Git-branch?

如何获得显示 Git 分支名称的提示?

回答by ko-dos

__git_ps1is from git-completion.bash. In zsh you probably have to provide your own function to determine the current directories git branch. There are quite a few blog postsabout a git promptfor zsh.

__git_ps1来自 git-completion.bash。在 zsh 中,您可能必须提供自己的函数来确定当前目录 git branch。有很多关于zsh的git 提示的博客文章

You just need:

您只需要:

  • a function to provide the branch name
  • enable prompt (command) substitution
  • add the function to your prompt
  • 提供分支名称的函数
  • 启用提示(命令)替换
  • 将该功能添加到您的提示中

For example

例如

git_prompt() {
 ref=$(git symbolic-ref HEAD | cut -d'/' -f3)
 echo $ref
}
setopt prompt_subst
PS1=$(git_prompt)%#
autoload -U promptinit
promptinit

Update: use the zsh vcs_info module instead of git_prompt()

更新:使用 zsh vcs_info 模块而不是 git_prompt()

setopt prompt_subst
autoload -Uz vcs_info
zstyle ':vcs_info:*' actionformats \
    '%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%F{3}|%F{1}%a%F{5}]%f '
zstyle ':vcs_info:*' formats       \
    '%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%F{5}]%f '
zstyle ':vcs_info:(sv[nk]|bzr):*' branchformat '%b%F{1}:%F{3}%r'

zstyle ':vcs_info:*' enable git cvs svn

# or use pre_cmd, see man zshcontrib
vcs_info_wrapper() {
  vcs_info
  if [ -n "$vcs_info_msg_0_" ]; then
    echo "%{$fg[grey]%}${vcs_info_msg_0_}%{$reset_color%}$del"
  fi
}
RPROMPT=$'$(vcs_info_wrapper)'

回答by Olivier Verdier

Here is an extended git prompt for zsh: zsh-git-prompt.

这是 zsh 的扩展 git 提示:zsh-git-prompt

alt text

替代文字

回答by Christopher

ko-dos's answer is great, but I prefer a slightly more git aware prompt than the one he uses. Specifically, I like staged, unstaged, and untracked file notifications in the prompt itself. Using his answer and the zsh vcs_info examples,I came up with the following:

ko-dos 的回答很好,但我更喜欢比他使用的提示稍微多一点的 git 感知提示。具体来说,我喜欢提示本身中的暂存、未暂存和未跟踪文件通知。使用他的回答和zsh vcs_info 示例,我想出了以下内容:

setopt prompt_subst
autoload -Uz vcs_info
zstyle ':vcs_info:*' stagedstr 'M' 
zstyle ':vcs_info:*' unstagedstr 'M' 
zstyle ':vcs_info:*' check-for-changes true
zstyle ':vcs_info:*' actionformats '%F{5}[%F{2}%b%F{3}|%F{1}%a%F{5}]%f '
zstyle ':vcs_info:*' formats \
  '%F{5}[%F{2}%b%F{5}] %F{2}%c%F{3}%u%f'
zstyle ':vcs_info:git*+set-message:*' hooks git-untracked
zstyle ':vcs_info:*' enable git 
+vi-git-untracked() {
  if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == 'true' ]] && \
  [[ $(git ls-files --other --directory --exclude-standard | sed q | wc -l | tr -d ' ') == 1 ]] ; then
  hook_com[unstaged]+='%F{1}??%f'
fi
}


precmd () { vcs_info }
PROMPT='%F{5}[%F{2}%n%F{5}] %F{3}%3~ ${vcs_info_msg_0_} %f%# '

This creates a prompt that mimics the colorized output of git status -s(which can be configured in your .gitconfigfile). A picture is perhaps most helpful here:

这将创建一个模仿git status -s(可以在您的.gitconfig文件中配置)的彩色输出的提示。一张图片可能在这里最有帮助:

prompt

迅速的

Compared with git status -s:

git status -s

enter image description here

在此处输入图片说明

If you don't like colorized output, or would prefer some other character or prompt construction, just change the stagedstr, unstagedstr, and hook_com[unstaged]values in the above code.

如果你不喜欢彩色输出,或宁愿其它字符或提示建设,只是改变了stagedstrunstagedstrhook_com[unstaged]在上面的代码值。

回答by Phil

A lot of these solutions seemed slow for me when mashing the return key, so here's an option that is basic and speedy:

在混合返回键时,很多这些解决方案对我来说似乎很慢,所以这里有一个基本且快速的选项:

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ ()/'
}

setopt PROMPT_SUBST
PROMPT='%9c%{%F{green}%}$(parse_git_branch)%{%F{none}%} $ '

You'll get a prompt that looks like this: ~/dev/project (feature-branch) $

您将收到如下所示的提示: ~/dev/project (feature-branch) $

回答by Dan Rosenstark

I just redid mine since we have long branch names at work. This one will truncate with an ellipsis if it's more than 35 characters.

我只是重做了我的,因为我们有很长的分支名称在工作。如果它超过 35 个字符,这个将被省略号截断。

parse_git_branch() {
    git_status="$(git status 2> /dev/null)"
    pattern="On branch ([^[:space:]]*)"
    if [[ ! ${git_status} =~ "(working (tree|directory) clean)" ]]; then
        state="*"
    fi
    if [[ ${git_status} =~ ${pattern} ]]; then
      branch=${match[1]}
      branch_cut=${branch:0:35}
      if (( ${#branch} > ${#branch_cut} )); then
          echo "(${branch_cut}…${state})"
      else
          echo "(${branch}${state})"
      fi
    fi
}

setopt PROMPT_SUBST
PROMPT='%{%F{blue}%}%9c%{%F{none}%}$(parse_git_branch)$'

(I'm embarrassed at how proud I am of this.)

(我为我为此感到多么自豪而感到尴尬。)

回答by Léo Léopold Hertz ??

Thank you for the links!

谢谢你的链接!

I made the following prompt based on them

我根据他们做了以下提示

     # get the name of the branch we are on
     git_prompt_info() { 
         git branch | awk '/^\*/ { print  }'
     }
     get_git_dirty() { 
       git diff --quiet || echo '*'
     }

     autoload -U colors
     colors     
     setopt prompt_subst

     PROMPT='%{$fg[blue]%}%c %{$fg_bold[red]%}$(git_prompt_info)$(get_git_dirty)%{$fg[blue]%} $ %{$reset_color%}'                                           

     RPROMPT='%{$fg[green]%}%1(j.%j.)'        

Please, suggest any improvements.

请提出任何改进建议。