Last active
December 21, 2015 02:29
-
-
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.
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
| 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' | |
| for REMOTE in `git remote`; do | |
| if [[ $BRANCH == "$REMOTE/"* ]]; then | |
| 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 | |
| if [[ $SHORT_BRANCH == 'master' ]]; then | |
| # todo `git br -a | grep HEAD | ....` | |
| 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 | |
| IFS="$IFS_STORE" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment