Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

Commit

Permalink
Merge branch 'next' into add_vcs_vulnerability_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dritter authored Nov 16, 2018
2 parents 4344eca + b430a21 commit b8b0bb4
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 50 deletions.
34 changes: 14 additions & 20 deletions segments/dir.p9k
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ prompt_dir() {
local current_path=${PWD} # WAS: local current_path="$(print -P '%~')"
local pathPrefix
[[ ${current_path} == "${HOME}"* ]] && pathPrefix="~/" || pathPrefix="/"
# check if the user wants to use absolute paths or "~" paths
[[ ${(L)P9K_DIR_PATH_ABSOLUTE} != "true" ]] && current_path=${current_path//$HOME/"~"}
# Always rewrite current path to be prefixed with ~ if in home folder
current_path=${current_path//$HOME/"~"}
# declare all local variables
local directory test_dir test_dir_length trunc_path threshold
# if we are not in "~" or "/", split the paths into an array and exclude "~"
Expand Down Expand Up @@ -112,31 +112,22 @@ prompt_dir() {
local markedFolder="${markedFolders[1]}"
# We want to remove up to the matched parent folder
markedFolder="${markedFolder:h}"
# TODO: Do not duplicate code from above, but this is necessary to truncate the path right.
# Also, this has to take P9K_DIR_PATH_ABSOLUTE into account..
# Remove $HOME from it. This has to be done, as the truncated
# path may be prefixed by a tilde, and we cannot resolve the
# current path softly without symlinks.
markedFolder=${markedFolder//$HOME/"~"}
local shortenedFolder="${current_path#${markedFolder}*}"
current_path="${pathPrefix}${P9K_DIR_SHORTEN_DELIMITER}${current_path#${markedFolder}*}"
fi
fi
;;
truncate_with_package_name)
local name repo_path package_path current_dir zero

package_path=${PWD:A}
# Get the path of the Git repo, which should have the package.json file
if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == "true" ]]; then
# Get path from the root of the git repository to the current dir
local gitPath=$(git rev-parse --show-prefix)
# Remove trailing slash from git path, so that we can
# remove that git path from the pwd.
gitPath=${gitPath%/}
package_path=${PWD%%$gitPath}
# Remove trailing slash
package_path=${package_path%/}
elif [[ $(git rev-parse --is-inside-git-dir 2> /dev/null) == "true" ]]; then
package_path=${PWD%%/.git*}
fi
# Search for the folder marker in the parent directories and
# buildup a pattern that is removed from the current path
# later on.
local -a markedFolders
markedFolders=( $(__p9k_upsearch "(${(j:|:)P9K_DIR_PACKAGE_FILES})") )
local package_path="${markedFolders[1]}"

# Replace the shortest possible match of the marked folder from
# the current path. Remove the amount of characters up to the
Expand Down Expand Up @@ -173,6 +164,9 @@ prompt_dir() {
esac
fi

# check if the user wants to use absolute paths or "~" paths
[[ ${(L)P9K_DIR_PATH_ABSOLUTE} == "true" ]] && current_path=${current_path//"~"/$HOME}

# save state of path for highlighting and bold options
local path_opt=$current_path

Expand Down
43 changes: 14 additions & 29 deletions segments/vcs.p9k
Original file line number Diff line number Diff line change
Expand Up @@ -122,41 +122,26 @@ p9k::set_default P9K_VCS_HIDE_TAGS false
p9k::set_default P9K_VCS_INTERNAL_HASH_LENGTH "8" # Default: Just display the first 8 characters of our changeset-ID.
p9k::set_default P9K_VCS_DIR_SHORTEN_DELIMITER $'\U2026'
p9k::set_default P9K_VCS_SHOW_SUBMODULE_DIRTY true

################################################################
# Register segment helper default values
p9k::set_default P9K_VCS_SHOW_SUBMODULE_DIRTY false
p9k::set_default P9K_VCS_GIT_ALWAYS_SHOW_REMOTE_BRANCH 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 [[ "$P9K_VCS_SHOW_SUBMODULE_DIRTY" == "true" && "$(command git submodule foreach --quiet --recursive 'command git ls-files --others --exclude-standard')" != "" ]]; then
hook_com[unstaged]+=" ${__P9K_ICONS[VCS_UNTRACKED]}"
VCS_WORKDIR_HALF_DIRTY=true
elif [[ "$(command git ls-files --others --exclude-standard "${repoDir}")" != "" ]]; then
hook_com[unstaged]+=" ${__P9K_ICONS[VCS_UNTRACKED]}"
VCS_WORKDIR_HALF_DIRTY=true
else
VCS_WORKDIR_HALF_DIRTY=false
# 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 && "$P9K_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]+=" ${__P9K_ICONS[VCS_UNTRACKED]}"
VCS_WORKDIR_HALF_DIRTY=true
}

function +vi-git-aheadbehind() {
Expand Down
17 changes: 16 additions & 1 deletion test/segments/dir.spec
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,23 @@ function testDirPathAbsoluteWorks() {
local P9K_DIR_PATH_ABSOLUTE=true

cd ~
assertEquals "%K{004} %F{000}${PWD} %k%F{004}%f " "$(__p9k_build_left_prompt)"
local absoluteDir="${PWD}"
assertEquals "%K{004} %F{000}${absoluteDir} %k%F{004}%f " "$(__p9k_build_left_prompt)"

local P9K_DIR_PATH_ABSOLUTE=false
assertEquals "%K{004} %F{000}~ %k%F{004}%f " "$(__p9k_build_left_prompt)"

typeset -a _strategies
_strategies=( truncate_from_left truncate_from_right truncate_middle truncate_to_last truncate_to_first_and_last truncate_absolute truncate_to_unique truncate_with_folder_marker truncate_with_package_name )

for strategy in ${_strategies}; do
local P9K_DIR_PATH_ABSOLUTE=true
P9K_DIR_SHORTEN_STRATEGY=${strategy}
assertEquals "${strategy} failed rendering absolute dir" "%K{004} %F{000}${absoluteDir} %k%F{004}%f " "$(__p9k_build_left_prompt)"

local P9K_DIR_PATH_ABSOLUTE=false
assertEquals "${strategy} failed rendering relative dir" "%K{004} %F{000}~ %k%F{004}%f " "$(__p9k_build_left_prompt)"
done
cd -
}

Expand Down
28 changes: 28 additions & 0 deletions test/segments/vcs_git.spec
Original file line number Diff line number Diff line change
Expand Up @@ -626,4 +626,32 @@ function testBranchNameScriptingVulnerability() {
assertEquals '%K{002} %F{000} $(./evil_script.sh) %k%F{002}%f ' "$(__p9k_build_left_prompt)"
}

function testGitSubmoduleWorks() {
local -a P9K_LEFT_PROMPT_ELEMENTS
P9K_LEFT_PROMPT_ELEMENTS=(vcs)
local P9K_VCS_SHOW_SUBMODULE_DIRTY="true"
unset P9K_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="$(__p9k_build_left_prompt 2>&1)"
[[ "$result" =~ ".*(is outside repository)+" ]] && return 1

assertEquals "%K{002} %F{000} master %k%F{002}%f " "$result"
}

source shunit2/shunit2

0 comments on commit b8b0bb4

Please sign in to comment.