Skip to content

Instantly share code, notes, and snippets.

@chee
Last active April 29, 2017 10:46
Show Gist options
  • Select an option

  • Save chee/492291caaf54e70caf37680bb05f7641 to your computer and use it in GitHub Desktop.

Select an option

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)
#!/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