Created
September 20, 2013 12:32
-
-
Save dair-targ/6636773 to your computer and use it in GitHub Desktop.
autocd for git
This file contains 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
function err_handle() | |
{ | |
status=$? | |
lastcmd=$(history | tail -1 | sed 's/^ *[0-9]* *//') | |
# Perform autocd | |
if [ -d "$lastcmd" ] | |
then | |
# Comment the following line out if autocd is enabled | |
cd $lastcmd | |
return | |
fi | |
# If the command is not a directory, it may be a branch name | |
ref_exists=$(git show-ref "refs/heads/$lastcmd") | |
if [[ "$ref_exists" ]] | |
then | |
git checkout "$lastcmd" | |
return | |
fi | |
# If the command is neither directory nor branch - try execute it as git command | |
if [[ "$lastcmd" =~ ^(add|commit|checkout|pull|push) ]] | |
then | |
git $lastcmd | |
return | |
fi | |
} | |
trap err_handle ERR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment