diff options
author | Dominik Ritter <dritter03@googlemail.com> | 2018-11-18 11:26:26 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-18 11:26:26 +0300 |
commit | d1e4c4c988de048092181c4403a263a6aa1bbdad (patch) | |
tree | c82de51b4e3b5431a63c7ba91a161e3c5db44704 | |
parent | 724781e89681d2f34bd3760ef710a58a093b5595 (diff) | |
parent | 6df2ba0ae02e585c557b2ff2ccb9f6f207a7c6b8 (diff) |
Merge pull request #1080 from macserv/port/vcs-segment-untracked-file-check
[Bugfix] Port #1071 to `master` (Fix fatal errors emitted by untracked file check in vcs.zsh)
-rwxr-xr-x | functions/vcs.zsh | 44 | ||||
-rwxr-xr-x | test/segments/vcs-git.spec | 29 |
2 files changed, 46 insertions, 27 deletions
diff --git a/functions/vcs.zsh b/functions/vcs.zsh index 6c9bd055..cb53dd3e 100755 --- a/functions/vcs.zsh +++ b/functions/vcs.zsh @@ -8,33 +8,23 @@ set_default POWERLEVEL9K_VCS_SHOW_SUBMODULE_DIRTY false function +vi-git-untracked() { - [[ -z "${vcs_comm[gitdir]}" || "${vcs_comm[gitdir]}" == "." ]] && return - # If we are in a .git folder, do not check for untracked files. - [[ "${PWD:A}" =~ "\.git/" ]] && return - - # If we are in a repos root folder, vcs_comm[gitdir] yields ".git". - # Inside the .git dir itself (and not a subdir of it) the variable - # yields ".". In any other case (either a subdirectory of .git or - # the repo itself), the value of vcs_comm[gitdir] is the absolute - # path to the .git directory. - # Therefore we can step up a directory, if we are inside the .git - # folder. And in any other case, use the parent directory of the - # gitdir. - local repoDir="." - # Getting the parent dir of the current dir "." is still ".", so - # is is safe to do this always. - [[ "${vcs_comm[gitdir]}" != ".git" ]] && repoDir="${vcs_comm[gitdir]:A:h}" - [[ "${vcs_comm[gitdir]}" == "." ]] && repoDir="${PWD:A:h}" - - if [[ "$POWERLEVEL9K_VCS_SHOW_SUBMODULE_DIRTY" == "true" && "$(command git submodule foreach --quiet --recursive 'command git ls-files --others --exclude-standard')" != "" ]]; then - hook_com[unstaged]+=" $(print_icon 'VCS_UNTRACKED_ICON')" - VCS_WORKDIR_HALF_DIRTY=true - elif [[ "$(command git ls-files --others --exclude-standard "${repoDir}")" != "" ]]; then - hook_com[unstaged]+=" $(print_icon 'VCS_UNTRACKED_ICON')" - VCS_WORKDIR_HALF_DIRTY=true - else - VCS_WORKDIR_HALF_DIRTY=false - fi + [[ -z "${vcs_comm[gitdir]}" || "${vcs_comm[gitdir]}" == "." ]] && return + + # get the root for the current repo or submodule + local repoTopLevel="$(command git rev-parse --show-toplevel 2> /dev/null)" + # dump out if we're outside a git repository (which includes being in the .git folder) + [[ $? != 0 || -z $repoTopLevel ]] && return + + local untrackedFiles=$(command git ls-files --others --exclude-standard "${repoTopLevel}") + + if [[ -z $untrackedFiles && "$POWERLEVEL9K_VCS_SHOW_SUBMODULE_DIRTY" == "true" ]]; then + untrackedFiles+=$(command git submodule foreach --quiet --recursive 'command git ls-files --others --exclude-standard') + fi + + [[ -z $untrackedFiles ]] && return + + hook_com[unstaged]+=" $(print_icon 'VCS_UNTRACKED_ICON')" + VCS_WORKDIR_HALF_DIRTY=true } function +vi-git-aheadbehind() { diff --git a/test/segments/vcs-git.spec b/test/segments/vcs-git.spec index ab2962c8..da28b955 100755 --- a/test/segments/vcs-git.spec +++ b/test/segments/vcs-git.spec @@ -503,4 +503,33 @@ function testBranchNameScriptingVulnerability() { assertEquals '%K{002} %F{000} $(./evil_script.sh) %k%F{002}%f ' "$(build_left_prompt)" } +function testGitSubmoduleWorks() { + local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(vcs) + local POWERLEVEL9K_VCS_SHOW_SUBMODULE_DIRTY="true" + unset POWERLEVEL9K_VCS_UNTRACKED_BACKGROUND + + mkdir ../submodule + cd ../submodule + git init 1>/dev/null + touch "i-am-tracked.txt" + git add . 1>/dev/null && git commit -m "Initial Commit" 1>/dev/null + + local submodulePath="${PWD}" + + cd - + git submodule add "${submodulePath}" 2>/dev/null + git commit -m "Add submodule" 1>/dev/null + + cd submodule + + source "${P9K_HOME}/powerlevel9k.zsh-theme" + + local result="$(build_left_prompt)" + [[ "$result" =~ ".*(is outside repository)+" ]] && return 1 + + assertEquals "%K{002} %F{000} master %k%F{002}%f " "$result" +} + + source shunit2/shunit2 |