Last active
June 1, 2017 17:50
-
-
Save kfox/420ceb0b2c6bd665b5e5 to your computer and use it in GitHub Desktop.
Bash function to open a GitHub repo from the command line
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
# this should be added to or loaded by your .bashrc | |
# usage: gh | |
# gh <repo_name> | |
# gh <user>/<repo_name> | |
function gh { | |
repo="$1" | |
if [ -z "$repo" ]; then | |
# if no argument provided, then get the | |
# URL of the repo in your current working directory | |
repo=$(git ls-remote --get-url) | |
if [[ $repo == *"[email protected]"* ]]; then | |
repo=${repo/[email protected]:/https:\/\/github.com/} | |
repo=${repo/.git//} | |
fi | |
elif [[ $repo != *\/* ]]; then | |
# arg format: vim-vinegar | |
repo=$(curl -s -X GET "https://api.github.com/search/repositories?q=$repo&fork=false&in=html_url&order=desc" | python -c 'import sys,json;data=json.loads(sys.stdin.read()); print data["items"][0]["html_url"]') | |
else | |
# arg format: tpope/vim-vinegar | |
repo="https://github.com/$repo" | |
fi | |
# open the URL in your default browser (Ubuntu) | |
# xdg-open $repo (or possibly gnome-open $repo) | |
# open the URL in your default browser (OS X) | |
open $repo | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment