Skip to content

Instantly share code, notes, and snippets.

@ex-nerd
Last active December 21, 2015 02:29
Show Gist options
  • Save ex-nerd/6235705 to your computer and use it in GitHub Desktop.
Save ex-nerd/6235705 to your computer and use it in GitHub Desktop.
Bash function to view the current repository/branch on github. Should work fine with multiple remotes.
# Opens the current branch on github.com, if possible.
# Would call this "github" but it conflicts with github's own CLI app to open their desktop app.
git_hub() {
BRANCH=`git rev-parse --symbolic-full-name --abbrev-ref @{u} 2>/dev/null`
if [[ $? != 0 ]]; then
echo "Not currently in a git repository"
return
fi
if [[ -z $BRANCH ]]; then
echo "Can't determine current git branch name"
return
fi
IFS_STORE="$IFS"
IFS=$'\n'
FOUND=0
for REMOTE in `git remote`; do
if [[ $BRANCH == "$REMOTE/"* ]]; then
FOUND=1
SHORT_BRANCH=${BRANCH#$REMOTE/}
GITHUB_REPO=`git remote show "$REMOTE" | grep 'Fetch URL' | awk -F: '{ print $3 }' | sed -e 's,.git$,,'`
if [[ $? != 0 ]]; then
echo "Error loading github project info"
break
fi
if [[ -z $BRANCH ]]; then
echo "Couldn't determine github project info"
break
fi
# Check for 'master' Disabled because I need support odd repositories that don't use "master" as the default
# TODO DEF_BRANCH=`git br -a | grep HEAD | ....`
#if [[ $SHORT_BRANCH == 'master' ]]; then
# URL="https://github.com/$GITHUB_REPO/"
#else
URL="https://github.com/$GITHUB_REPO/tree/$SHORT_BRANCH"
#fi
echo "$URL"
if [[ `uname` == 'Darwin' ]]; then
open "$URL"
elif `which xdg-open >/dev/null`; then
xdg-open "$URL"
else
echo "Couldn't find URL opener"
fi
break
fi
done
if [[ $FOUND == 0 ]]; then
echo "This branch does not appear to be tracking a remote."
fi
IFS="$IFS_STORE"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment