Created
May 28, 2009 14:26
-
-
Save ap/119340 to your computer and use it in GitHub Desktop.
shell func for cd'ing to top of git working copy
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
# these implementations both take special care to keep `cd -` working correctly | |
# good way | |
gcd() { | |
local TOP="`git rev-parse --show-cdup 2> /dev/null`" || return 1 | |
[ "$TOP" ] && cd "$TOP" | |
} | |
# old, dumb, bad way | |
gcd() { | |
local OLDOLDPWD=$OLDPWD | |
pushd . > /dev/null | |
while : ; do | |
if [ -d .git ] ; then | |
OLDPWD="`dirs -l +1`" | |
popd -n > /dev/null | |
return | |
fi | |
[ "$PWD" = / ] && break | |
cd .. | |
done | |
popd > /dev/null | |
OLDPWD=$OLDOLDPWD | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment