Last active
April 29, 2017 10:46
-
-
Save chee/492291caaf54e70caf37680bb05f7641 to your computer and use it in GitHub Desktop.
print project root directory (takes an optional relative or absolute path, defaults to pwd)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| if [ "${1:0:1}" == '/' ]; then | |
| dir="$(dirname $1)" | |
| else | |
| dir="$(pwd)/$1" | |
| fi | |
| project_root_files=".git .hg .project" | |
| while [ "$dir" != '/' ] && [ -z "$project_root" ]; do | |
| for file in $project_root_files; do | |
| [ -e "$dir/$file" ] && project_root="$dir" | |
| done | |
| dir=$(dirname "$dir") | |
| done | |
| if [ -z "$project_root" ]; then | |
| pwd | |
| exit 1 | |
| else | |
| echo "$project_root" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment