ZSH vcs_info:如何指示Git中是否存在未跟踪的文件

Flu*_*lux 6 git zsh

在我的 zsh 配置中,我有以下设置vcs_info

zstyle ':vcs_info:*' enable git
zstyle ':vcs_info:*' check-for-changes true
zstyle ':vcs_info:*' unstagedstr '!'
zstyle ':vcs_info:*' stagedstr '+'
zstyle ':vcs_info:*' formats "%u%c"
Run Code Online (Sandbox Code Playgroud)

使用这些设置,当我位于包含未暂存更改的 git 存储库中时,!提示中将显示 a 。当有阶段性变更时,+会在提示中显示 。

这一切都很好,但是我如何让 zsh 指示,例如?,当存储库中存在未跟踪的文件时?

我在 zsh 手册中找不到这方面的内置设置。有没有办法获取 Git 存储库中是否存在未跟踪文件的指示?

ymo*_*nad 8

在 zsh 的源代码中,有一个很好的示例,T当存在未跟踪的文件时显示标记以进行提示:Misc/vcs_info_examples

### Display the existence of files not yet known to VCS

### git: Show marker (T) if there are untracked files in repository
# Make sure you have added staged to your 'formats':  %c
zstyle ':vcs_info:git*+set-message:*' hooks git-untracked

+vi-git-untracked(){
    if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == 'true' ]] && \
        git status --porcelain | grep '??' &> /dev/null ; then
        # This will show the marker if there are any untracked files in repo.
        # If instead you want to show the marker only if there are untracked
        # files in $PWD, use:
        #[[ -n $(git ls-files --others --exclude-standard) ]] ; then
        hook_com[staged]+='T'
    fi
}
Run Code Online (Sandbox Code Playgroud)

您可以从这里复制和粘贴,但我编写了一个稍微修改的版本,它使用杂项而不是向staged.

zstyle ':vcs_info:*' formats "%u%c%m"
zstyle ':vcs_info:git*+set-message:*' hooks git-untracked

+vi-git-untracked() {
  if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == 'true' ]] && \
     git status --porcelain | grep -m 1 '^??' &>/dev/null
  then
    hook_com[misc]='?'
  fi
}
Run Code Online (Sandbox Code Playgroud)

%m格式中对应于 中的字符hook_com[misc]。另外,只需 grep 的整个输出git status --porcelain,相反,使用它可能会更快grep -m 1(取决于标准输出的缓冲方式或git status --porcelain实现方式)。