Skip to content

Instantly share code, notes, and snippets.

@davemasse
Forked from tag-dmasse/.bashrc
Created May 31, 2013 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davemasse/5682615 to your computer and use it in GitHub Desktop.
Save davemasse/5682615 to your computer and use it in GitHub Desktop.
Change directory upwards to the first directory containing a .git directory. Helpful in quickly getting to the root of a git project.
cdg () {
CD="`~/bin/cdg.sh`"
cd $CD
}
#!/bin/bash
PATH="./"
LASTPATH="$PATH"
while [ 1 -eq 1 ]
do
if [ `/usr/bin/find "$PATH" -maxdepth 1 -type d -name ".git" | /usr/bin/wc -l` = "1" ]
then
echo "$PATH"
exit 0
else
LASTPATH="$PATH"
PATH="$PATH../"
# Exit if this is the highest directory available -- no .git found
if [ `/bin/readlink -f "$PATH"` = `/bin/readlink -f "$LASTPATH"` ]
then
echo "./"
exit 1
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment