2017-04-19 98 views
1

:vcs_info:似乎知道,如果你在一個git或HG目錄裏,如果你打開它:如何通過`:vcs_info:`確定我是否在hg或git目錄中?

zstyle ':vcs_info:*' enable git hg 

即,您可以修改您提示這種方式。

如何獲取該數據以便我可以編寫if條件?例如

if [[ $some_magic_zstyle_vcs_info_variable ]]; then 
    echo "I'm in a git dir!" 
fi 
+0

如果你只關心如何使用zsh zstyle內部做到這一點,這個問題不應該被標記'git'和'mercurial'(IMO無論如何)。要在沒有任何zsh內部的情況下做到這一點,遍歷文件系統尋找'.git'或'.hg'目錄:無論你首先擊中哪一個可能是正確的。 – torek

+0

@torek足夠公平,刪除標籤。我希望這可以像計算機一樣快 - 它會進入提示。 – mpen

+1

實際的代碼(原始版本)似乎在https://www.zsh.org/mla/users/2008/msg00842.html - 它不是很明顯要看什麼,但我看到'VCS_INFO_git_detect'實際上使用' git rev-parse --is-inside-git-dir',而不是走文件系統(儘管這只是將作業推送到'git rev-parse'),而'VCS_INFO_hg_detect'執行目錄(緩存) 。我不知道當前的代碼是否相同。 – torek

回答

1

mafredi提供了一個better solution

zstyle ':vcs_info:*' enable git hg 
zstyle ':vcs_info:*' max-exports 3 
zstyle ':vcs_info:(git|hg):*' formats ' %b' 'x%R' '%s' 
zstyle ':vcs_info:(git|hg):*' actionformats ' %b|%a' 'x%R' '%s' 

... 

if [[ "$vcs_info_msg_2_" == "git" ]]; then 
    # git 
fi 

if [[ "$vcs_info_msg_2_" == "hg" ]]; then 
    # hg 
fi 

%s將把VCS名稱爲$vcs_info_msg_2_

相關問題