Last active
November 28, 2016 04:22
-
-
Save mblarsen/550cc55c215bb2299197ad02075e7704 to your computer and use it in GitHub Desktop.
A small shell command like `cd` that cd-s you back to project root.
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 | |
# | |
# A small script that works like `cd` except that | |
# it keeps cd-ing until it reachs the project root | |
# project is defined by default as the directory | |
# that contains a .git sub-directory. | |
# | |
# Change this behavior like this: | |
# CDR_ROOT_ID=composer.json | |
# | |
# Now the root dir will be the directory containing | |
# a composer.json file. | |
# | |
# To install stick this in your shell config file. | |
# | |
function cdr() | |
{ | |
set -e | |
CDR_ROOT_ID=${CDR_ROOT_ID:-.git} | |
ORG_WD=$(pwd) | |
CURRENT_DIR=$(pwd) | |
CURRENT_GIT_DIR=$CURRENT_DIR"/"$CDR_ROOT_ID | |
echo "Looking for: $CURRENT_GIT_DIR" | |
while [ ! -e "$CURRENT_GIT_DIR" ] && [ "$CURRENT_DIR" != "/" ]; do | |
CURRENT_DIR=$(dirname "$CURRENT_DIR") | |
CURRENT_GIT_DIR=$CURRENT_DIR"/"$CDR_ROOT_ID | |
done | |
if [ -e "$CURRENT_GIT_DIR" ]; then | |
cd "$CURRENT_DIR" | |
if [ ! -z "$1" ]; then | |
cd "$1" | |
fi | |
else | |
echo "No project root found" | |
cd "$ORG_WD" | |
fi | |
} | |
#vim :set ft=sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment