From c3019d11ef2a8e1b550b4d05e22473ef6e3e8596 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Thu, 2 Jun 2016 01:49:00 +0100 Subject: [PATCH 1/2] Handle shorting path when in the .git directory `git rev-parse --git-dir` returns just `.` when in the top level of a`.git` directory. This would cause a problem when extracting the package_path. Closes #271 --- powerlevel9k.zsh-theme | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 4206bf51..b3873429 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -475,6 +475,10 @@ prompt_dir() { package_path="." subdirectory_path="" else + # Handle the edge case where $repo_path is '.' due to the current directory being the .git directory. + if [[ "$repo_path" == "." ]]; then + repo_path=$(pwd) + fi # If the current path is something else, get the path to the package.json # file by finding the repo path and removing the '.git` from the path package_path=${repo_path:0:-4} From d720b725e8644072a240be924eec09d1f0d21edb Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 14 Jun 2016 11:58:33 +0100 Subject: [PATCH 2/2] Use git's show-toplevel to get top level directory --- powerlevel9k.zsh-theme | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index b3873429..0bbeb7c5 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -467,30 +467,18 @@ prompt_dir() { local name repo_path package_path current_dir zero # Get the path of the Git repo, which should have the package.json file - if repo_path=$(git rev-parse --git-dir 2>/dev/null); then - if [[ "$repo_path" == ".git" ]]; then - # If the current path is the root of the project, then the package path is - # the current directory and we don't want to append anything to represent - # the path to a subdirectory - package_path="." - subdirectory_path="" - else - # Handle the edge case where $repo_path is '.' due to the current directory being the .git directory. - if [[ "$repo_path" == "." ]]; then - repo_path=$(pwd) - fi - # If the current path is something else, get the path to the package.json - # file by finding the repo path and removing the '.git` from the path - package_path=${repo_path:0:-4} - zero='%([BSUbfksu]|([FB]|){*})' - current_dir=$(pwd) - # Then, find the length of the package_path string, and save the - # subdirectory path as a substring of the current directory's path from 0 - # to the length of the package path's string - subdirectory_path=$(truncatePathFromRight "/${current_dir:${#${(S%%)package_path//$~zero/}}}") - fi + if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == "true" ]]; then + package_path=$(git rev-parse --show-toplevel) + elif [[ $(git rev-parse --is-inside-git-dir 2> /dev/null) == "true" ]]; then + package_path=${$(pwd)%%/.git*} fi + zero='%([BSUbfksu]|([FB]|){*})' + current_dir=$(pwd) + # Then, find the length of the package_path string, and save the + # subdirectory path as a substring of the current directory's path from 0 + # to the length of the package path's string + subdirectory_path=$(truncatePathFromRight "${current_dir:${#${(S%%)package_path//$~zero/}}}") # Parse the 'name' from the package.json; if there are any problems, just # print the file path if name=$( cat "$package_path/package.json" 2> /dev/null | grep "\"name\""); then