Use git's show-toplevel to get top level directory

pull/22/head
Andrew Scott 9 years ago
parent c3019d11ef
commit d720b725e8

@ -467,30 +467,18 @@ prompt_dir() {
local name repo_path package_path current_dir zero local name repo_path package_path current_dir zero
# Get the path of the Git repo, which should have the package.json file # 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 [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == "true" ]]; then
if [[ "$repo_path" == ".git" ]]; then package_path=$(git rev-parse --show-toplevel)
# If the current path is the root of the project, then the package path is elif [[ $(git rev-parse --is-inside-git-dir 2> /dev/null) == "true" ]]; then
# the current directory and we don't want to append anything to represent package_path=${$(pwd)%%/.git*}
# 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 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]|){*})' zero='%([BSUbfksu]|([FB]|){*})'
current_dir=$(pwd) current_dir=$(pwd)
# Then, find the length of the package_path string, and save the # 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 # subdirectory path as a substring of the current directory's path from 0
# to the length of the package path's string # to the length of the package path's string
subdirectory_path=$(truncatePathFromRight "/${current_dir:${#${(S%%)package_path//$~zero/}}}") subdirectory_path=$(truncatePathFromRight "${current_dir:${#${(S%%)package_path//$~zero/}}}")
fi
fi
# Parse the 'name' from the package.json; if there are any problems, just # Parse the 'name' from the package.json; if there are any problems, just
# print the file path # print the file path
if name=$( cat "$package_path/package.json" 2> /dev/null | grep "\"name\""); then if name=$( cat "$package_path/package.json" 2> /dev/null | grep "\"name\""); then

Loading…
Cancel
Save