Skip to content

Instantly share code, notes, and snippets.

@nick-f
Created October 9, 2017 22:17
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 nick-f/43064054846d1a68de5e702685b588ec to your computer and use it in GitHub Desktop.
Save nick-f/43064054846d1a68de5e702685b588ec to your computer and use it in GitHub Desktop.
Shell function to open the current repo's GitHub page
alias gho=githubOpen
function githubOpen() {
if [ ! -d .git ] ;
then echo "ERROR: This isnt a git directory" && return false;
fi
git_url=`git config --get remote.origin.url`
if [[ "${git_url}" == git@github* ]]; then
if [[ "${git_url}" == *.git ]]; then
repo_owner=`echo "${git_url}" | sed -Ene 's#git@github.com:([^/]*)/(.*).git#\1#p'`
repo_name=`echo "${git_url}" | sed -Ene 's#git@github.com:([^/]*)/(.*).git#\2#p'`
else
repo_owner=`echo "${git_url}" | sed -Ene 's#git@github.com:([^/]*)/(.*)#\1#p'`
repo_name=`echo "${git_url}" | sed -Ene 's#git@github.com:([^/]*)/(.*)#\2#p'`
fi
url="https://github.com/${repo_owner}/${repo_name}"
elif [[ "${git_url}" == https://github* ]]; then
url=${git_url%.git}
else
echo "ERROR: Remote origin is invalid" && return false;
fi
open "${url}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment